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 |
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 dfsRecursive(node,visited=set()): | |
visited.add(node) | |
# Process this node here | |
for neighbor in getNeighbors(node): | |
if neighbor not in visited: | |
dfsRecursive(neighbor,visited) | |
return |
NewerOlder