私はfinding all cycles in graph implementationについての別のディスカッションスレッドを読むと、この質問に出くわしました。誰もこの例でこのキーワードのペアの使用を説明できますか?ありがとう。Pythonでは "continue"と "yield"というキーワードのペアは何をしていますか?
01 def dfs(graph, start, end):
02 fringe = [(start, [])]
03 while fringe:
04 state, path = fringe.pop()
05 if path and state == end:
06 yield path
07 continue
08 for next_state in graph[state]:
09 if next_state in path:
10 continue
11 fringe.append((next_state, path+[next_state]))
>>> graph = { 1: [2, 3, 5], 2: [1], 3: [1], 4: [2], 5: [2] }
>>> cycles = [[node]+path for node in graph for path in dfs(graph, node, node)]
>>> len(cycles)
7
>>> cycles
[[1, 5, 2, 1], [1, 3, 1], [1, 2, 1], [2, 1, 5, 2], [2, 1, 2], [3, 1, 3], [5, 2, 1, 5]]