我在设计一个玩具二部网络时遇到了困难。
from igraph import *
def create_bipartite(n1, n2, directed=False):
g = Graph(n1+n2, directed=True)
g.vs["type"] = 0
g.vs[n2:]["type"] = 1
return g
gb=create_bipartite(4,3)
gb.add_edges([(0,1),(0,2),(3,4),(0,4),(0,5),(3,6)])
> In [358]: gb.is_bipartite()
Out[358]: True
In [359]: gb.bipartite_projection()
---------------------------------------------------------------------------
InternalError Traceback (most recent call last)
<ipython-input-359-a7b9927dc7bb> in <module>()
----> 1 gb.bipartite_projection()
/usr/lib/python2.7/dist-packages/igraph/__init__.pyc in bipartite_projection(self, types, multiplicity, *args, **kwds)
2530 superclass_meth = super(Graph, self).bipartite_projection
2531 if multiplicity:
-> 2532 g1, g2, w1, w2 = superclass_meth(types, True, *args, **kwds)
2533 g1.es["weight"] = w1
2534 g2.es["weight"] = w2
InternalError: Error at structure_generators.c:84: Invalid (negative) vertex id, Invalid vertex id
In [360]: 我得到的错误是试图投影到任一节点:这是什么意思?负顶点?有什么办法解决这个问题吗?
发布于 2015-01-05 07:26:36
更新版本的igraph给出了一个更具信息性的错误消息(实际上,我不确定这个更改是否已经发布了--我生活在前沿):
InternalError: Error at ../../src/bipartite.c:198: Non-bipartite edge found in
bipartite projection, Invalid value(您可能会对g.is_bipartite()返回True的原因感到惊讶-原因是g.is_bipartite()只检查图形是否具有名为type的顶点属性)。
问题是你的类型向量看起来像这样:
>>> gb.vs["type"]
[0, 0, 0, 1, 1, 1, 1]因为在顶点3和4之间有一条边,这两个顶点都属于类型1,所以这个图不是二部图。我强烈怀疑实际的bug存在于create_bipartite中,您希望在其中编写以下代码:
g.vs[n1:]["type"] = 1而不是这样:
g.vs[n2:]["type"] = 1https://stackoverflow.com/questions/27760729
复制相似问题