Created
February 21, 2016 17:39
-
-
Save durcana/d6a4c539d68005c4da36 to your computer and use it in GitHub Desktop.
Breadth first search code written before interview with David for Recurse Center. Will also change this as I change dfs
This file contains hidden or 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 networkx as nx | |
def bfs(node, graph): | |
roots = [n for n, d in graph.in_degree().items() if d == 0] | |
for root in roots: | |
if node in graph.successors(root): | |
return node | |
for child in graph.successors(root): | |
graph.remove_edge(root, child) | |
return bfs(node, graph) | |
""" | |
The rest of this code is for testing. | |
When running the test function, I am expecting either the node in the | |
first parameter of dfs to return if the node is in the graph created in | |
create_graph, or to return a statement that the node is not in the tree | |
if it is not in the graph. | |
Family tree is a non-binary tree. | |
""" | |
def create_graph(tree): | |
graph = nx.DiGraph() | |
for node in tree.keys(): | |
for value in tree[node]: | |
graph.add_edge(node, value) | |
return graph | |
def test(): | |
family_tree = {'roots': ['child1', 'child2'], | |
'child1': ['gc1', 'gc2'], | |
'child2': ['gc3'], | |
'gc3': ['ggc1', 'ggc2', 'ggc3']} | |
graph = create_graph(family_tree) | |
print(bfs('ggc2', graph)) | |
if __name__ == '__main__': | |
test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment