Skip to content

Instantly share code, notes, and snippets.

@KerryJones
Created January 14, 2016 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KerryJones/fa6dc959d6dfa0b291fc to your computer and use it in GitHub Desktop.
Save KerryJones/fa6dc959d6dfa0b291fc to your computer and use it in GitHub Desktop.
Python Depth-First-Search
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