【问题标题】:How to convert a bipartite graph into no bipartite如何将二分图转换为无二分图
【发布时间】:2019-09-17 23:09:24
【问题描述】:

我已经尝试了很长时间没有成功,所以我最好问问你。

首先,我正在研究 python 3networkx

我有一个二分图作为图像A,其中根据'group'属性有两种类型的节点(group='R'和group ='X')。此外,有些关系是可逆的,如 R4,而有些则不是(所以我想在这些情况下我们必须展开节点)。

我需要的是只留下R组的节点并消除X个,但保持它们之间的关系。也就是说,将绿色节点转换为边,并保留仅包含蓝色节点的图形

哦,求求你了!!,有人可以帮帮我吗?

非常欢迎任何帮助。

提前衷心感谢您!

在此处绘制图像:

https://media.springernature.com/full/springer-static/image/art%3A10.1038%2Fs41540-018-0067-y/MediaObjects/41540_2018_67_Fig1_HTML.png

【问题讨论】:

  • 您能否以不同的方式重述这个问题?您有一个现有的二分图。您想创建一个新图表,它仅包含这些组中的一个。新图中的边是什么意思? [注意,看看 networkx 中的 project 命令,因为这听起来很可能是你所追求的]

标签: python-3.x graph networkx bipartite


【解决方案1】:

遍历图的节点,如果节点是绿色的,则在其所有邻居之间添加一条边(只会是蓝色的)。最后删除所有绿色节点。

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)

【讨论】:

    猜你喜欢
    • 2021-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-26
    • 2021-07-25
    • 2020-01-14
    • 1970-01-01
    • 2020-07-15
    相关资源
    最近更新 更多