Finding all disconnected subgraphs in a graph

后端 未结 7 2233
长发绾君心
长发绾君心 2020-12-07 22:57

I have a graph which contains an unknown number of disconnected subgraphs. What\'s a good algorithm (or Java library) to find them all?

7条回答
  •  没有蜡笔的小新
    2020-12-07 23:08

    Not a Java implementation but perhaps it will be useful for someone, here is how to do it in Python:

    import networkx as nx
    g = nx.Graph()
    # add nodes/edges to graph
    d = list(nx.connected_component_subgraphs(g))
    # d contains disconnected subgraphs
    # d[0] contains the biggest subgraph
    

    More information here.

提交回复
热议问题