Eulerian Circuit Recursion语句重置我的列表

时间:2017-02-27 06:47:57

标签: python python-3.x

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>

1 个答案:

答案 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')])