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

后端 未结 11 2140
南旧
南旧 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:34

    Pretty easy and clear way to do is to use Networkx:

    With Networkx you can get the loops of an undirected graph by nx.cycle_basis(G) and then select the ones with 3 nodes

    cycls_3 = [c for c in nx.cycle_basis(G) if len(c)==3]
    

    or you can find all the cliques by find_cliques(G) and then select the ones you want (with 3 nodes). cliques are sections of the graph where all the nodes are connected to each other which happens in cycles/loops with 3 nodes.

提交回复
热议问题