How to find subgraphs in a directed graph without converting to undirected graph?

前端 未结 2 978
臣服心动
臣服心动 2021-01-06 16:53

I have a graph that has many subgraphs. I have some edges that connect two nodes in both directions, that is, A-->B and B-->A. The bidirectionality is important, as it repre

2条回答
  •  逝去的感伤
    2021-01-06 17:22

    Maybe you are looking for weakly connected components?

    That algorithm treats the edges as if they were undirected and returns the connected components in that graph.

    In [1]: import networkx as nx
    
    In [2]: G = nx.DiGraph([(1,2),(2,1),(3,4)])
    
    In [3]: for w in nx.weakly_connected_component_subgraphs(G):
       ...:     print(w.edges())
       ...:     
    [(1, 2), (2, 1)]
    [(3, 4)]
    

提交回复
热议问题