I have a networkx.Graph
object representing a graph whose nodes represent English words, and whose edges between two wnodes imply that the two words that those nodes represent have at least one shared cognitive synonym between their synsets (i.e. a non-empty intersection). I hope that is interesting or useful background to someone, but my problem is a more widely applicable one relating to graphs, networkx
, and Python.
Many induced subgraphs (edge-induced, or vertex-induced) of this graph are both edge disjoint and vertex disjoint, and I'd like to separate these subgraphs into their own networkx.Graph
objects such that they're connected and mutually disjoint. It is possible that I'm just using the wrong search terms for the networkx
documentation, but I didn't see anything promising related to "disjoint". Here are some examples from a tiny portion of the graph.
I looked through the search results for [networkx] disjoint
on Stack Overflow and didn't see what I was looking for. For example, one result talked about getting the induced subgraph when there's already have an edge set to induce from. Or another post talked about trying to draw two disjoint graphs, but that's assuming you already have them. Related to the graph theory aspect of my question, but not the networkx
aspect, is that apparently there's such a thing as a flood fill algorithm that might address the part of my question.
Now, for a minimum working example, let's create a small random graph but ensure that it is disconnected.
import networkx as nx
g = nx.fast_gnp_random_graph(10, 0.3)
while nx.is_connected(g):
g = nx.fast_gnp_random_graph(10, 0.3)
At this point, we have a graph g
. What I'm thinking of is somethine like below, where I occupy a list of the graphs that are to be mutually disjoint. I need to not only add more graphs as I loop over the nodes, but also update the graphs as I go. I thought maybe unions of induced graphs might work, but the nx.disjoint_union_all
and nx.union
are either going to force the graphs to be disjoint by relabelling (I don't want this) or expect the graphs to already be disjoint.
graphs = []
for node in g.nodes(): # same 'g' we made above
if graphs:
pass
else:
graphs.append(g.subgraph([i for i in g.neighbors(node)] +\
[node]).copy())
How can I separate an unconnected networkx graph into multiple mutually disjoint graphs that are connected?