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_triangles", abbreviation = "_notri")
019    public class NumOfTriangles implements GraphReportExtension {
020        public Object calculate(GraphData gd) {
021            GraphModel graph = gd.getGraph();
022            return getNumOfTriangles(graph);
023        }
024    
025        /**
026         * @return the number of triangles in the given graph
027         */
028        public static int getNumOfTriangles(GraphModel graph) {
029            int cc = 0;
030            for (VertexModel i : graph) {
031                for (VertexModel j : AlgorithmUtils.getNeighbors(graph, i))
032                    for (VertexModel k : AlgorithmUtils.getNeighbors(graph, j)) {
033                        if (k.getId() != i.getId() && graph.isEdge(k, i))
034                            cc++;
035                    }
036    
037            }
038            return cc / 6;
039        }
040    
041        public String getName() {
042            return "Number Of Triangles";
043        }
044    
045        public String getDescription() {
046            return "Number Of Triangles";
047        }
048    }