Finding cycle of 3 nodes ( or triangles) in a graph

后端 未结 11 2184
南旧
南旧 2020-12-31 15:42

I am working with complex networks. I want to find group of nodes which forms a cycle of 3 nodes (or triangles) in a given graph. As my graph contains about million edges, u

11条回答
  •  醉话见心
    2020-12-31 16:33

    Surprised to see no mention of the Networkx triangles function. I know it doesn't necessarily return the groups of nodes that form a triangle, but should be pretty relevant to many who find themselves on this page.

    nx.triangles(G) # list of how many triangles each node is part of
    sum(nx.triangles(G).values())/3 # total number of triangles
    

    An alternative way to return clumps of nodes would be something like...

    for u,v,d in G.edges(data=True):
        u_array = adj_m.getrow(u).nonzero()[1] # get lists of all adjacent nodes
        v_array = adj_m.getrow(v).nonzero()[1]
        # find the intersection of the two sets - these are the third node of the triangle
        np.intersect1d(v_array,u_array)
    

提交回复
热议问题