-
Notifications
You must be signed in to change notification settings - Fork 1
/
depth_limited_search.py
49 lines (44 loc) · 1009 Bytes
/
depth_limited_search.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
graph = {
'A':['B','C'],
'B':['D','E'],
'C':['F','G'],
'D':['H','I'],
'E':['J','K'],
'F':['L','M'],
'G':['N','O'],
'H':[],
'I':[],
'J':[],
'K':[],
'L':[],
'M':[],
'N':[],
'O':[]
}
def DLS(start,goal,path,level,maxD):
print('\nCurrent level-->',level)
print('Goal node testing for',start)
path.append(start)
if start == goal:
print("Goal test successful")
return path
print('Goal node testing failed')
if level==maxD:
return False
print('\nExpanding the current node',start)
for child in graph[start]:
if DLS(child,goal,path,level+1,maxD):
return path
path.pop()
return False
start = 'A'
goal = input('Enter the goal node:-')
maxD = int(input("Enter the maximum depth limit:-"))
print()
path = list()
res = DLS(start,goal,path,0,maxD)
if(res):
print("Path to goal node available")
print("Path",path)
else:
print("No path available for the goal node in given depth limit")