001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    
005    package graphlab.plugins.reports.basicreports;
006    
007    import graphlab.graph.graph.GraphModel;
008    import graphlab.platform.lang.CommandAttitude;
009    import graphlab.plugins.main.GraphData;
010    import graphlab.plugins.main.core.AlgorithmUtils;
011    import graphlab.plugins.reports.extension.GraphReportExtension;
012    
013    import java.util.ArrayList;
014    import java.util.List;
015    
016    /**
017     * @author Mohammad Ali Rostami
018     */
019    
020    @CommandAttitude(name = "girth_size", abbreviation = "_gs")
021    public class GirthSize implements GraphReportExtension {
022    
023        private static int bfs(int start, double mat[][], int cc, int girth) {
024            int baba[] = new int[cc];
025            int dist[] = new int[cc];
026            for (int i = 0; i < cc; i++) {
027                baba[i] = AlgorithmUtils.Max_Int;
028                dist[i] = AlgorithmUtils.Max_Int;
029            }
030    
031            dist[start] = 0;
032            baba[start] = -1;
033            List<Integer> ll = new ArrayList<Integer>();
034            ll.add(start);
035            while (ll.size() > 0) {
036                int currentNode = ll.remove(0);
037                for (int j = 0; j < cc; j++)
038                    if (mat[currentNode][j] == 1)
039                        if (dist[j] == AlgorithmUtils.Max_Int) {
040                            dist[j] = dist[currentNode] + 1;
041                            baba[j] = currentNode;
042                            if (2 * dist[j] < girth - 1)
043                                ll.add(j);
044                        } else if (dist[j] + dist[currentNode] < girth - 1 && baba[currentNode] != j)
045                            girth = dist[j] + dist[currentNode] + 1;
046    
047            }
048            return girth;
049        }
050    
051    
052        public Object calculate(GraphData gd) {
053            GraphModel graph = gd.getGraph();
054            return getgirthSize(graph);
055    
056        }
057    
058        /**
059         * @return the girth size of the given graph
060         */
061        public static int getgirthSize(GraphModel graph) {
062            int size = graph.getVertexArray().length;
063            double mat[][] = graph.getAdjacencyMatrix().getArray();
064            int girth = AlgorithmUtils.Max_Int;
065            for (int i = 0; i < size; i++) {
066                int sizeofsmallestcycle = bfs(i, mat, size, girth);
067                if (sizeofsmallestcycle != AlgorithmUtils.Max_Int && girth > sizeofsmallestcycle)
068                    girth = sizeofsmallestcycle;
069            }
070            if (girth == AlgorithmUtils.Max_Int) return 0;
071            return girth;
072        }
073    
074        public String getName() {
075            return "Graph Girth Size";
076        }
077    
078        public String getDescription() {
079            return "Graph Girth Size";
080        }
081    }