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.graph.graph.VertexModel;
009    import graphlab.platform.lang.CommandAttitude;
010    import graphlab.plugins.main.GraphData;
011    import graphlab.plugins.main.core.AlgorithmUtils;
012    import graphlab.plugins.reports.extension.GraphReportExtension;
013    
014    /**
015     * @author Mohammad Ali Rostami
016     */
017    
018    @CommandAttitude(name = "num_of_quadrangle", abbreviation = "_noqa")
019    public class NumOfQuadrangle implements GraphReportExtension {
020        public String getName() {
021            return "number of quadrangle";
022        }
023    
024        public String getDescription() {
025            return "number of quadrangle";
026        }
027    
028        public Object calculate(GraphData gd) {
029            GraphModel graph = gd.getGraph();
030            return getNumOfQuadrangles(graph);
031        }
032    
033        /**
034         * @return number of quadrangles in the given graph
035         */
036        public static int getNumOfQuadrangles(GraphModel graph) {
037            int quadrangles = 0;
038            for (VertexModel i : graph)
039                for (VertexModel j : AlgorithmUtils.getNeighbors(graph, i))
040                    for (VertexModel k : AlgorithmUtils.getNeighbors(graph, j))
041                        for (VertexModel l : AlgorithmUtils.getNeighbors(graph, k))
042                            if (l != j &&
043                                    l != i &&
044                                    k != i &&
045                                    graph.isEdge(i, l)) quadrangles++;
046    
047            return quadrangles / 8;
048        }
049    }