0

I'm trying to create a bipartite graph that contains an isolated node, however nx.bipartite_layout connects it to other nodes, nevertheless. How can I prevent this from happening?

I tried to restructure the graph in many ways but it only works well when all the nodes have at least one edge.

You can use this code to reproduce the problem:

G = nx.dense_gnm_random_graph(6, 9, 5)
G.remove_edges_from([(3,1), (3,4), (3,5), (4,5)])
print(G.edges())
# prints [(0, 2), (0, 4), (2, 3), (2, 4), (2, 5)]
nx.draw(G, pos=nx.bipartite_layout(G, list(G.nodes())[:len(G.nodes())//2]), node_color='gray',with_labels=True)
# produces a graph with node 1 tied to 0 and 2 (even though it is an isolate)

The actual output is supposed to be a graph in which 1 is still isolated.

Arn
  • 1,898
  • 12
  • 26
  • Which version of `networkx` are you using? I can't reproduce the issue as my version (latest pull from github) does not seem to have that function. – Paul Brodersen Mar 29 '19 at 11:49
  • @Paul Brodersen Version 2.2. Which function are you talking about? [Here](https://networkx.github.io/documentation/latest/reference/generated/networkx.drawing.layout.bipartite_layout.html) is the bipartite layout documentation. – Arn Mar 29 '19 at 12:01
  • I was talking about `bipartite_layout` but you can safely disregard my previous comment. Somehow the paths in my testing virtual environment are messed up so that I was actually using networkx 1.1 instead of the version I pulled from github. – Paul Brodersen Mar 29 '19 at 12:05
  • Regarding you layout problem: pretty much all layout algorithms assume a fully connected graph. There is no rational place to put an unconnected node w.r.t. all other nodes (and by extension to unconnected subgraphs), so the placement is arbitrary. As a corollary, most algorithms fail in one way or another when you hand them an unconnected graph. I have discussed some approaches to deal with the problem [here](https://stackoverflow.com/a/53156709/2912349). – Paul Brodersen Mar 29 '19 at 12:12
  • All that being said, your example `[(0, 2), (0, 4), (2, 3), (2, 4), (2, 5)]` is not a bipartite graph, so I am not sure that the layout is actually your problem. – Paul Brodersen Mar 29 '19 at 12:13
  • Okay, you're right, my example was not the most carefully crafted. Thanks for the suggestion, I'll look into the layout strategies mentioned in your other post. – Arn Mar 29 '19 at 12:18

0 Answers0