Sep-25-2019, 01:43 PM
Here's the more efficient version I was thinking of:
def DFS(graph, root): visited = set([root]) current = root possibles = (node for node in graph[current] if node not in visited) search_stack = [] edges = [] while len(visited) < len(graph): try: possible = next(possibles) edges.append((current, possible)) visited.add(possible) search_stack.append((current, possibles)) current = possible possibles = (node for node in graph[current] if node not in visited) except StopIteration: current, possibles = search_stack.pop() return edges
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures