Calculating the number of open triangles in a networkx graph

Viewed 230

I have a networkx graph G, say for example

import networkx as nx
G=nx.erdos_renyi_graph(100,.1)

I would like to count in an efficient way to the number of open triples, that is, the frequency of the pattern: A is connected to B, B is connected to C but A and B are not connected.

Is there a clever way to exploit a built in function for this?

1 Answers

If you can compute the distance matrix (1 if i->j, else 0) of G, you can square it and for each non-diagonal entry, check if a[i,j] is different from a[j,i].

Related