Created
January 14, 2016 20:18
-
-
Save KerryJones/fa6dc959d6dfa0b291fc to your computer and use it in GitHub Desktop.
Python Depth-First-Search
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
class Node: | |
visited = None | |
data = None | |
adjacent = None | |
def __init__(self, value): | |
self.data = value | |
self.adjacent = [] | |
self.visited = False | |
def dfs(node, data): | |
if node.visited: | |
return False | |
if node.data == data: | |
return node | |
node.visited = True | |
for adjacent_node in node.adjacent: | |
result_node = dfs(adjacent_node, data) | |
if result_node: | |
return result_node | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment