Python 图论圣人,关键错误

Python 图论圣人,关键错误,python,sage,Python,Sage,我将从我的代码开始,因为对于那些更好地理解该语言的人来说,这可能是一个明显的问题: 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

我将从我的代码开始,因为对于那些更好地理解该语言的人来说,这可能是一个明显的问题:

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
Traceback(最近一次调用last):如果h.是圆形的平面()
文件“”,第1行,在
文件“/tmp/tmprzreop/\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
exec编译(u'new\u strong.plot(顶点颜色=d).show()
文件“”,第1行,在
包装器中的文件“/usr/lib/sagemath/local/lib/python2.7/site packages/sage/misc/decorators.py”,第550行
返回函数(*参数,**选项)
文件“/usr/lib/sagemath/local/lib/python2.7/site packages/sage/graphs/generic_graph.py”,第15706行,在绘图中
返回self.graphplot(**选项).plot()
graphplot中的文件“/usr/lib/sagemath/local/lib/python2.7/site packages/sage/graphs/generic_graph.py”,第15407行
返回GraphPlot(图=自身,选项=选项)
文件“/usr/lib/sagemath/local/lib/python2.7/site packages/sage/graphs/graph_plot.py”,第247行,在u_init中__
self.set_顶点()
文件“/usr/lib/sagemath/local/lib/python2.7/site packages/sage/graphs/graph\u plot.py”,第399行,位于集合顶点中
pos+=[self.\u pos[j]表示j的顶点颜色[i]]
关键错误:0
这可能因KeyError而有所不同:0有时是1或2,具体取决于某些未知因素


我为我糟糕的代码提前道歉,并承认我真的不知道我在做什么,但如果有人能在这里帮助我,我将不胜感激。

我找到了答案!原来错误来自于d的条目在new_strong中毫无意义,即那些已经删除的顶点的条目。这导致了键er当plot()尝试根据d为顶点着色时的ror