为什么我的图不是二分图的,以为我使用相应的networkx函数将它们创建为二分图? [英] Why my graphs are not bipartite, thought I create them as bipartite using corresponding networkx function?

查看:53
本文介绍了为什么我的图不是二分图的,以为我使用相应的networkx函数将它们创建为二分图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个函数:

B = nx.Graph()
B.add_nodes_from([1, 2, 3, 4], bipartite=0)
B.add_nodes_from(["a", "b", "c"], bipartite=1)
B.add_edges_from([(1, "a"), (1, "b"), (2, "b"), (2, "c"), (3, "c"), (4, "a"), (1,2)])
layout = nx.bipartite_layout(B, [1, 2, 3, 4])
nx.draw_networkx(B, pos=layout)

如您所见,该图必须是二部图,但是我知道了:

As you see, this graph must be bipartite, however I get this:

1和2已连接.因此,如果我可以使用此 B.add.edges_from([[(1,a),(1,b),(2,b),(2,; c''),(3,"c"),(4,"a"),(1,2)]),拧干此代码的意义是: B.add_nodes_from([1,2,3,4],bipartite = 0)并指定其为bipartite?

1 and 2 are connected. so, if i can add edges between them using this B.add_edges_from([(1, "a"), (1, "b"), (2, "b"), (2, "c"), (3, "c"), (4, "a"), (1,2)]), what is a point of wring this: B.add_nodes_from([1, 2, 3, 4], bipartite=0) and specifying that its bipartite?

推荐答案

您已为节点分配了属性 bipartite = 0 1 .但是,这只是节点的属性.您可以使用任何名称(颜色,重量,种类名称等)为它们分配所需的任何属性-不会影响存在的边缘.特别是,没有特别注意属性的名称.您已经将属性命名为'bipartite',但是就networkx命令而言,您可以轻松地将该属性命名为'fubar'.没关系.

You've assigned the nodes an attribute bipartite=0 or 1. However, that's just an attribute of the nodes. You can assign them any attribute you want, with any name (a color, a weight, a species name, etc) - it has no impact on what edges exist. In particular, there's no special attention paid to the name of the attribute. You've named an attribute 'bipartite', but as far as the networkx commands are concerned you could have just as easily named that attribute 'fubar'. It doesn't care.

您在节点1和2之间添加了一条边.Networkx不会查看图的属性,而是选择拒绝允许您创建它的边.

You added an edge between nodes 1 and 2. Networkx won't look at the attributes of the graph and make a choice to refuse to allow you the edge you asked it to create.

对于您而言,我认为您的问题的答案是设置名为'bipartite'的属性似乎没有意义.

In your case, I think the answer to your question is that there doesn't seem to be a point in setting an attribute named 'bipartite'.

这篇关于为什么我的图不是二分图的,以为我使用相应的networkx函数将它们创建为二分图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆