-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathdfs.py
61 lines (40 loc) · 1.45 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
graph = {'Arad': ['Zerind', 'Sibiu', 'Timisoara'],
'Bucharest': ['Urziceni', 'Pitesti', 'Giurgiu', 'Fagaras'],
'Craiova': ['Dobreta', 'Bimnicu Vilcea', 'Pitesti'],
'Dobreta': ['Mehadia'],
'Eforie': ['Hirsoya'],
'Tasai': ['Vaslui', 'Neamt'],
'Lugoj': ['Timisoara', 'Mehadia'],
'Oradea': ['Zerind', 'Sibiu'],
'Pitesti': ['Bimnicu Vilcea'],
'Urziceni': ['Vaslui'],
'Zerind': ['Oradea', 'Arad'],
'Sibiu': ['Oradea', 'Arad', 'Bimnicu Vilcea', 'Fagaras'],
'Timisoara': ['Arad', 'Lugoj'],
'Mehadia': ['Lugoj', 'Dobreta'],
'Bimnicu Vilcea': ['Sibiu', 'Pitesti', 'Craiova'],
'Fagaras': ['Sibiu', 'Bucharest'],
'Giurgiu': ['Bucharest'],
'Vaslui': ['Urziceni','Iasai'],
'Neaput': ['Tagai']
}
def IDDES(root, goal):
depth = 0
while True:
print("Looping at depth %i"%(depth))
result = DLS(root, goal, depth)
print ("Result: %s, Goal: %s" % (result, goal))
if result == goal:
return result
depth = depth +1
def DLS(node, goal, depth):
print ("node: %s, goal %s, depth: %i" % (node, goal, depth))
if depth == 0 and node == goal:
print("---Found goal, returning ---")
return node
elif depth > 0:
print("Looping through children %s" %(graph.get(node, [])))
for child in graph.get(node, []):
if goal == DLS(child, goal, depth-1):
return goal
IDDES('Arad', 'Bucharest')