Skip to content

Instantly share code, notes, and snippets.

@spasiu
Created October 24, 2014 13:32
Show Gist options
  • Save spasiu/899275142b5edf2d0af9 to your computer and use it in GitHub Desktop.
Save spasiu/899275142b5edf2d0af9 to your computer and use it in GitHub Desktop.
unconditional depth search
SAMPLE_GRAPH = {1:[2,3,4],
2:[5,6,7,8],
3:[9],
4:[10],
5:[9],
6:[3,4],
7:[2,11],
8:[9],
9:[1,2,3,4],
10:[6],
11:[]} #sample value
START = 1 #sample value
#globals
graph = SAMPLE_GRAPH
start = START
visited = []
def visit(node):
visited.append(node)
return graph[node]
def check_visited(node):
return node not in visited
def generate_list(node):
return filter(check_visited, visit(node))
def iterator(node):
connections = generate_list(node)
for connection in connections:
iterator(connection)
iterator(start)
print visited
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment