Last active
June 5, 2020 08:25
-
-
Save wilderfield/e0b453f52e0fb4dbe3c8157cc6d2dcc3 to your computer and use it in GitHub Desktop.
DFS Iterative
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def dfsIterative(node,visited=set()): | |
stack = [node] | |
while stack: | |
node = stack.pop() | |
if node not in visited: | |
visited.add(node) | |
# Process this node here | |
for neighbor in reversed(getNeighbors(node)): # Reversal not mandatory | |
if neighbor not in visited: | |
stack.append(neighbor) | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment