Comment on Iterative DFS with stack-based graph traversal (2024)Comments−quibono1ySo... am I misunderstanding or is it enough to swap the iteration over the neighbours of a node and the visited check? for nbr in graph[node]: if not visited[nbr]: into if node in visited: continue visited.add(node) for nbr in graph[node]: stack.append(nbr)−DannyBee1yIt should be enough :)
Comments
So... am I misunderstanding or is it enough to swap the iteration over the neighbours of a node and the visited check?
intoIt should be enough :)