Created
July 15, 2015 20:43
-
-
Save dalejung/98017a6eb641cc9c6d7c to your computer and use it in GitHub Desktop.
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
import ast | |
graph = {'A': set(['B', 'C']), | |
'B': set(['A', 'D', 'E']), | |
'C': set(['A', 'F']), | |
'D': set(['B']), | |
'E': set(['B', 'F']), | |
'F': set(['C', 'E'])} | |
def dfs(graph, start): | |
visited, stack = set(), [start] | |
while stack: | |
vertex = stack.pop() | |
if vertex not in visited: | |
visited.add(vertex) | |
stack.extend(graph[vertex] - visited) | |
return visited | |
dfs(graph, 'A') # {'E', 'D', 'F', 'A', 'C', 'B'} | |
mod = ast.parse("dale + 1") | |
stack = [mod] | |
jobs = [] | |
while stack: | |
node = stack.pop() | |
jobs.append(node) | |
for child_node in ast.iter_child_nodes(node): | |
stack.append(child_node) | |
jobs = list(reversed(jobs)) | |
done = {} | |
for node in jobs: | |
ndict = {} | |
done[node] = ndict | |
for child_node in ast.iter_child_nodes(node): | |
cdict = done[child_node] | |
ndict[child_node] = cdict |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment