如何使用 networkx 构建二分体 (python)

How to build a bipartite using networkx (python)

为什么结果显示为 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))

因为你的图没有连接。绘制图形时:

你看到你有 3 个未连接的子图(即没有连接它们的边)。但是你的图表是 bipartite:

print(nx.is_bipartite(B))

输出:

True

连通二分图如下所示:

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))

输出:

True
True