我将从我的代码开始,因为对于那些对语言有更好理解的人来说,这可能是一个明显的问题:
g = graphs.CompleteGraph(60).complement()
for i in range(1,180):
a = randint(0,59)
b = randint(0,59)
h = copy(g)
h.add_edge(a,b)
if h.is_circular_planar():
g.add_edge(a,b)
strong = copy(strong_resolve(g))
S = strong.vertex_cover()
d = {'#00FF00': [], '#FF0000': []}
for v in G.vertices():
if v in S:
d['#FF0000'].append(v)
else:
d['#00FF00'].append(v)
g.plot(layout="spring", vertex_colors=d).show()
strong.plot(vertex_colors=d).show()
new_strong = copy(strong)
for w in new_strong.vertices():
if len(new_strong.neighbors(w)) == 0: #trying to remove
new_strong.delete_vertex(w) #disconnected vertices
new_strong.plot(vertex_colors=d).show()注意: strong_resolve是一个函数,它接收一个图并输出另一个图。前两个代码块可以正常工作。
我的问题是,一旦我添加第三个块,事情就不再工作了。在兜圈子中,我得到了这段代码的变体,当添加这些代码时,会导致错误,删除后,错误会以某种方式保留下来。现在发生的情况是,for循环似乎一直持续到它的结束,只有这样,它才会出现以下错误:
Traceback (most recent call last): if h.is_circular_planar():
File "", line 1, in <module>
File "/tmp/tmprzreop/___code___.py", line 30, in <module>
exec compile(u'new_strong.plot(vertex_colors=d).show()
File "", line 1, in <module>
File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/misc/decorators.py", line 550, in wrapper
return func(*args, **options)
File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/graphs/generic_graph.py", line 15706, in plot
return self.graphplot(**options).plot()
File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/graphs/generic_graph.py", line 15407, in graphplot
return GraphPlot(graph=self, options=options)
File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/graphs/graph_plot.py", line 247, in __init__
self.set_vertices()
File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/graphs/graph_plot.py", line 399, in set_vertices
pos += [self._pos[j] for j in vertex_colors[i]]
KeyError: 0这可能是因为KeyError: 0有时是1或2,这取决于某些未知因素。
我事先为我的可怕代码道歉,并承认我真的不知道我在做什么,但如果有人能帮我解决这个问题,我会非常感激的。
发布于 2015-08-10 22:50:36
我想出来了!事实证明,错误来自d有在new_strong中没有意义的条目,即那些已经被删除的顶点。这导致了绘图()试图根据d对顶点进行着色时的关键错误。
https://stackoverflow.com/questions/31928451
复制相似问题