无法添加边,IGraph中的无效顶点ID [英] Cannot add edges, Invalid vertex ID in IGraph

查看:365
本文介绍了无法添加边,IGraph中的无效顶点ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用igraph在python中编写代码,当我尝试使用while循环添加边时出现此错误

I was trying to write the code in python using igraph and when i tried to add edges using a while loop this error came up

while(i<k)
  g.add_vertices(theInts[i])
  i=i+1
  g.add_edges([(theInts[i-1],theInts[i])])

我认为索引编制可能是一个问题,所以我也包含了一个if语句,但这似乎不是问题.

I thought that indexing might be a problem so i also included a if statement but that doesnt seems to be the problem.

请帮助!!!

推荐答案

我认为这一切都取决于g的顶点.如果以空的g开始,则只有顶点0,因此,如果您尝试使用两个不同的顶点调用add_edges,那将是行不通的.您必须添加更多顶点.当然,这全部取决于循环之前图形的外观以及i是什么.

I think this all depends on what g has for vertices. If you start off with an empty g, you only have the vertex 0, so if you're trying to call add_edges with two different vertices, it's just not going to work. You have to add some more vertices. Of course this all depends on what your graph looks like before the loop, and what i is.

您可以使用print显示一些有关图形的简短信息.例如,

You can display some brief information about your graph with print. For example,

>>> import igraph
>>> graph = igraph.Graph()
>>> print graph
Undirected graph (|V| = 1, |E| = 0)

如果i从0开始,那么您将不会在循环中第一次添加任何顶点.因此,当您尝试添加边线时,您将尝试添加到不存在的顶点.

If i starts at 0, then you will not add any vertices with your loop the first time around. So when you try to add edges, you're trying to add to vertices that do not exist.

>>> graph.add_vertices(0)
<igraph.Graph object at 0xcea850>
>>> print graph
Undirected graph (|V| = 1, |E| = 0)
>>> graph.add_edges([(0, 1)])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
igraph.core.InternalError: Error at type_indexededgelist.c:245: cannot add edges, Invalid vertex id

如果这不是问题,请尝试打印边缘,看看它们是否与您想要的相符.

If that is not the issue, try printing the edges and see if they match up with what you want.

>>> graph.add_vertices(5)
<igraph.Graph object at 0xcea850>
>>> print graph
Undirected graph (|V| = 6, |E| = 3)
>>> graph.add_edges([(1, 1), (2, 3), (3, 5)])
<igraph.Graph object at 0xcea850>
>>> graph.get_edgelist()
[(1, 1), (2, 3), (3, 5)]

此外,拥有完整的TraceBack可能会有所帮助.

Also, it might be a bit more helpful to have the complete TraceBack.

根据您的评论

所以您说的是这样的结构:

So you're saying you have a structure like this:

>>> graph = igraph.Graph()
>>> print graph
Undirected graph (|V| = 1, |E| = 0)

您只想添加顶点2?我不确定您是否可以使用igraph做到这一点.它似乎必须按顺序排列每个顶点.您可以检查一下是否有顶点,然后根据需要添加它们,记住这些图形是基于0的.像这样的东西.

And you want to add just vertex 2? I am not sure that you can do that with igraph. It seems to have to have each vertex in order. You can check to see if you have the vertex and then add them if necessary, remembering that these graphs are 0-based. Something like this.

>>> vertices = 1, 2, 13, 4, 21, 5
>>> map_graph = igraph.Graph()
>>> print map_graph
Undirected graph (|V| = 1, |E| = 0)
>>> map_graph.add_vertices(max(vertices))
<igraph.Graph object at 0xceaa50>
>>> print map_graph
Undirected graph (|V| = 22, |E| = 0)
>>> map(map_graph.add_edges, zip(vertices, vertices[1:]))
[<igraph.Graph object at 0xceaa50>, <igraph.Graph object at 0xceaa50>, <igraph.Graph object at 0xceaa50>, <igraph.Graph object at 0xceaa50>, <igraph.Graph object at 0xceaa50>]
>>> print map_graph
Undirected graph (|V| = 22, |E| = 5)
>>> map_graph.get_edgelist()
[(1, 2), (2, 13), (4, 13), (4, 21), (5, 21)]

或者,如果您不喜欢地图,可以将其循环播放.

Or if you don't like maps, you can loop it up.

>>> vertices = 1, 2, 13, 4, 21, 5
>>> loop_graph = igraph.Graph()
>>> print loop_graph
Undirected graph (|V| = 1, |E| = 0)
>>> loop_graph.add_vertices(max(vertices))
<igraph.Graph object at 0xcea950>
>>> print loop_graph
Undirected graph (|V| = 22, |E| = 0)
>>> for pair in zip(vertices, vertices[1:]):
...     loop_graph.add_edges(pair)
... 
<igraph.Graph object at 0xcea950>
<igraph.Graph object at 0xcea950>
<igraph.Graph object at 0xcea950>
<igraph.Graph object at 0xcea950>
<igraph.Graph object at 0xcea950>
>>> print loop_graph
Undirected graph (|V| = 22, |E| = 5)
>>> loop_graph.get_edgelist()
[(1, 2), (2, 13), (4, 13), (4, 21), (5, 21)]

不过,也许有更好的方法可以做到这一点.如果这不是您想要的内容,请更详细地编辑原始问题以及一些实际代码.

There is probably a better way to do this though. If this isn't what you're looking for, please edit your original question with more detail, and some actual code.

这篇关于无法添加边,IGraph中的无效顶点ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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