【发布时间】:2022-01-05 02:49:55
【问题描述】:
我想使用 networkx 制作一个二分图。我关注documentation 和this previous answer
df = pd.DataFrame({'Name': ['John','John','Aron','Aron','Jeny','Jeny'],
'Movie':['A','B','C','A','Y','Z']})
G = nx.Graph()
G.add_nodes_from(df.Name, bipartite=0)
G.add_nodes_from(df.Movie, bipartite=1)
G.add_edges_from(df.values)
因为我的图表是断开的,即
nx.is_connected(G)
>False
top = nx.bipartite.sets(G)[0]
>AmbiguousSolution
我遵循以下文档:
top_nodes = {n for n, d in G.nodes(data=True) if d["bipartite"] == 0}
Z = nx.bipartite.projected_graph(G, top_nodes)
nx.draw(Z)
我明白了:
我预计:
【问题讨论】:
标签: python graph networkx graph-theory