I have a bipartite network and I would like to count the number of squares in the network, for instance:
import networkx as nx
B=nx.Graph()
B=nx.bipartite.random_graph(100, 100, .05)
I know that networkx has a built in method to calculate square clustering coefficient (the share of closed 4-paths vs open 3-paths), but I would like to simply count the number of closed 4 paths. Can someone think of a nice trick using built in functions?
I think you can simply adjust the code nx.square_clustering
, as follows. If needed wrap it into a function
v = 0 # or any other node
clustering = 0
potential = 0
for u, w in combinations(B[v], 2):
squares = len((set(B[u]) & set(B[w])) - {v})
clustering += squares