-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfid.py
87 lines (67 loc) · 1.7 KB
/
dfid.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from collections import defaultdict
# list representation
class Graph:
def __init__(self, vertices):
self.V = vertices
self.graph = defaultdict(list)
def addEdge(self, u, v):
self.graph[u].append(v)
def DLS(self, src, target, maxDepth, open, close, path):
open.append(src)
if src == target:
return True
if maxDepth <= 0:
return False
print("Open List : ", open)
print("Close List : ", close)
print()
open.pop()
close.append(src)
for i in self.graph[src]:
result = self.DLS(i, target, maxDepth-1, open, close, path)
if (result):
path.append(i)
return True
return False
def IDDFS(self, src, target, maxDepth):
print('Graph : ', dict(self.graph))
print()
for i in range(maxDepth):
open = []
close = []
path = []
print("Max Depth : ", i)
res = self.DLS(src, target, i, open, close, path)
print("Open List : ", open)
print("Close List : ", close)
print()
if (res):
path.append(src)
path.reverse()
print(path)
return True
return False
# Create a graph given in the above diagram
n = int(input("Enter no. of edges : "))
g = Graph(n)
for i in range(n):
print("\nEnter edge ", i+1, " -")
u = int(input("Enter u : "))
v = int(input("Enter v : "))
g.addEdge(u, v)
# g.addEdge(0, 1)
# g.addEdge(0, 2)
# g.addEdge(1, 3)
# g.addEdge(1, 4)
# g.addEdge(2, 5)
# g.addEdge(2, 6)
src = int(input("\nEnter source node : "))
target = int(input("Enter target node : "))
maxDepth = int(input("Enter maximum depth : "))
print()
if g.IDDFS(src, target, maxDepth) == True:
print("Target is reachable from source " +
"within max depth")
else:
print("Target is NOT reachable from source " +
"within max depth")