/
dfs.py
47 lines (32 loc) · 1.08 KB
/
dfs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import networkx as nx
def dfs(node, graph):
print('hello')
root = [n for n, d in graph.in_degree().items() if d == 0][0]
return __dfs(node, graph, root)
def __dfs(node, graph, current_node):
print('check if ' + current_node + ' is ' + node)
if current_node == node:
return node
graph.node[current_node]['visited'] = True
for child in graph.successors(current_node):
if graph.node[child]['visited'] == False:
result = __dfs(node, graph, child)
if result is not None:
return result
def create_graph(tree):
graph = nx.DiGraph()
for node in tree.keys():
for value in tree[node]:
graph.add_edge(node, value)
for n in graph.nodes():
graph.node[n]['visited'] = False
return graph
def test(tree):
graph = create_graph(tree)
print(dfs('ggc2', graph))
family_tree = {'root': ['child1', 'child2'],
'child1': ['gc1', 'gc2'],
'child2': ['gc3'],
'gc3': ['ggc1', 'ggc2', 'ggc3']}
if __name__ == '__main__':
test(family_tree)