問題描述
如何將二分圖轉換為無二分圖 (How to convert a bipartite graph into no bipartite)
我已經嘗試了很長時間沒有成功,所以我最好問問你。
首先,我正在研究 python 3 和 networkx。
我有一個二部圖作為圖像A,根據他們的'group'屬性(group=' R'和組='X')。此外,有些關係是可逆的,如 R4,而有些則不是(所以我想在這些情況下我們必須展開節點)。
我需要的是只留下R組的節點並消除X的節點,但保持它們之間的關係。也就是說,
參考解法
方法 1:
Go over nodes of the graph, if the node is green add an edge between all its neighbours (which will only be blue). At the end remove all the green nodes.
to_remove = []
for node in G.nodes(data = True):
if node[1]["type"] == "Green": ## check if the node is green
to_remove.append(node[0])
## go over all neighbours of the node and add an edge between them
neighbours = list(G.neighbors(node[0]))
for i in range(0, len(neighbours)‑1):
for j in range(i+1, len(neighbours)):
G.add_edge(neighbours[i],neighbours[j])
## remove the green nodes
G.remove_nodes_from(to_remove)