0

Why the result shows the False?

import networkx as nx
P1 = [1, 2, 3]
P2 = ["a", "b", "c"]
PPI = list(zip(P1, P2))
B = nx.Graph()
B.add_nodes_from(P1, bipartite=0)
B.add_nodes_from(P2, bipartite=1)
B.add_edges_from(PPI)
print(nx.is_connected(B))
cronoik
  • 15,434
  • 3
  • 40
  • 78
Ricky
  • 3
  • 2

1 Answers1

0

Because your graph is not connected. When you plot your graph: bipartiteGraph

you see that you have 3 subgraphs that are not connected (i.e. there is no edge that connects them). But your graph is bipartite:

print(nx.is_bipartite(B))

Output:

True

A graph that is connected and bipartite is shown below:

import networkx as nx
P1 = [1, 2, 3]
P2 = ["a", "b", "c"]
PPI = [(1, 'a'), (2, 'b'), (3, 'c'), (1, 'b'), (2, 'c')]
B = nx.Graph()
B.add_nodes_from(P1, bipartite=0)
B.add_nodes_from(P2, bipartite=1)
B.add_edges_from(PPI)
print(nx.is_connected(B))
print(nx.is_bipartite(B))

Output:

True
True
cronoik
  • 15,434
  • 3
  • 40
  • 78
  • Thanks for your answer. Very helpful for understanding! If I want to plot the unconnected bipartite graph given the provided example, how to do it and let two sets of nodes having different colors and positions? Thanks – Ricky Oct 25 '20 at 21:31
  • @Ricky Please ask a separate question for that. But keep in mind that a good question at stackoverflow shows what you have tried by yourself and you should therefore have checked this [answer](https://stackoverflow.com/questions/27084004/bipartite-graph-in-networkx) before. – cronoik Oct 26 '20 at 04:30