I saw in this video that computing the clustering coefficient of central node of a star graph using the following algorithm is theta(n^2) and for a clique it is theta(n^3). is that correct?
def clustering_coefficient(G,v):
neighbors = G[v].keys()
if len(neighbors) == 1: return 0.0
links = 0.0
for w in neighbors:
for u in neighbors:
if u in G[w]: links += 0.5
return 2.0*links/(len(neighbors)*(len(neighbors)-1))
The complexity depends on the density of your graph, and the efficiency of the
inpredicate.A naive implementation on a complete graph obviously is
O(n^3): two nested loops and oneinpredicate, each running naively in linear time. If you keep the links in a hashmap (and not in a dense matrix representation!) then the runtime is onlyO(n^2)– for a single node. But usually, such an algorithm is applied for each node, adding another factor ofnto it.If your graph is not complete (and you use a more efficient
inpredicate), things get a lot faster. Assuming that every node hassqrt(n)neighbors, the complexity of the algorithm will beO(sqrt(n)^2)*n(for all nodes, that is), which is probably theirO(n^2)result.Assuming that every node has exactly two neighbors. Then the complexity can easily be brought down to
O(1) * n. Oh, and if every node has 0 neighbors, it’s even simpler.