def eulerian_circuit(graph):
trail = []
for edge in graph:
graph.sort()
a,b = edge
if not trail:
trail.append(a)
trail.append(b)
graph.remove((a,b))
print(graph)
if a == trail[-1]:
trail.append(b)
graph.remove((a,b))
print(graph)
if not graph:
return trail
eulerian_circuit(graph)
return trail,graph
eulerian_circuit([('a','b'), ('b','c'), ('a','f'), ('b','d'), ('d','f'), ('b','e'), ('e','g'), ('c','g'), ('f','g'), ('f','h'), ('g','h')])
我的问题是,当我调用递归eulerian_circuit(图)
时我的路径在顶部使用trail = []重置。我该如何解决这个问题?
(代码未完成电路应答,仍然必须实现当剩下的一条边连接回起始顶点...并且必须找出其他错误我肯定会出现。新的python)< / p>
答案 0 :(得分:0)
您可以将trail
作为参数传递给eulerian_circuit
的子调用。
为None
提供该功能的默认值trail
,以便您知道它是否是最高级别的通话:
def eulerian_circuit(graph, trail=None):
if trail is None:
trail = []
...
eulerian_circuit(graph, trail):
用法:
eulerian_circuit([('a','b'), ('b','c'), ('a','f')])