我正在尝试解决Udacity上的一个问题,描述如下:
# Find Eulerian Tour
#
# Write a function that takes in a graph
# represented as a list of tuples
# and return a list of nodes that
# you would follow on an Eulerian Tour
#
# For example, if the input graph was
# [(1, 2), (2, 3), (3, 1)]
# A possible Eulerian tour would be [1, 2, 3, 1]
Run Code Online (Sandbox Code Playgroud)
我提出了以下解决方案,虽然不像某些递归算法那样优雅,但似乎在我的测试用例中起作用.
def find_eulerian_tour(graph):
tour = []
start_vertex = graph[0][0]
tour.append(start_vertex)
while len(graph) > 0:
current_vertex …Run Code Online (Sandbox Code Playgroud)