I want to compute the clustering coefficient in a graph with more than 1.5M vertices.
I have a Dictionary
with vertex ID as key and the value is a List
with all the vertices which are connected to the vertex ID.
clustering coefficient = 3 * triangles / number of connected triplets.
The problem is: it takes me more than 4 hours to compute the number of triangles in the graph.
My code:
List<string> list_t = new List<string>();
Dictionary<string, List<string>> copy = new Dictionary<string, List<string>>(Global.dict_edge_undirected);
int triangles = 0; // (number of triangles in graph)*3
foreach (KeyValuePair<string, List<string>> pair in copy)
{
if (pair.Value.Count > 1)
{
foreach (string neigh1 in pair.Value)
{
list_t = copy[neigh1];
foreach (string neigh2 in pair.Value)
{
if (neigh1 != neigh2 && list_t.Contains(neigh2))
{
triangles++;
}
}
}
}
}
How can I reduce the run time?
C++ igraph library calculates the clustering coefficient of this graph in less than 3 minutes.
What am I missing?