What is an efficient algorithm for counting the number of triangles in an undirected graph )(where a graph is a set of vertices and edges)? I've been searching Google and reading through my shelf of textbooks for a few hours each day for three days in a row.
This is for a homework assignment where I need such an algorithm, but developing it doesn't count for anything on the assignment. It is expected that we can simply find such an algorithm from outside resources, but I'm at the end of my rope.
For clarification, a triangle in a graph is a a cycle of length three. The trick is that it needs to work on vertex sets with at most 10,000 nodes.
I'm currently working in C#, but care more about the general approach towards solving this problem than code to copy and paste.
At the high level, my attempts thus far included:
The algorithm itself is part of calculating the clustering coefficient.
Given an Undirected simple graph, We need to find how many triangles it can have. For example below graph have 2 triangles in it. Let A[][] be the adjacency matrix representation of the graph. If we calculate A3, then the number of triangles in Undirected Graph is equal to trace(A3) / 6.
The Triangle Count algorithm counts the number of triangles for each node in the graph. A triangle is a set of three nodes where each node has a relationship to the other two. In graph theory terminology, this is sometimes referred to as a 3-clique.
Some Common Graph AlgorithmsBreadth First Search (BFS) Depth First Search (DFS) Dijkstra. Floyd-Warshall Algorithm.
The number of triangles is 1, 8, 35, 110, 287, 632, 1302, 2400, 4257, 6956 for polygons with 3 through 12 sides. If we connect all vertices of a regular N-sided polygon we obtain a figure with = N (N - 1) / 2 lines. For N=8, the figure is: Careful counting shows that there are 632 triangles in this eight sided figure.
You will need depth first search. The algorithm will be:
1) For the current node ask all unvisited adjacent nodes
2) for each of those nodes run depth two check to see if a node at depth 2 is your current node from step one
3) mark current node as visited
4) on make each unvisited adjacent node your current node (1 by 1) and run the same algorithm
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With