-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbfs copy.py
71 lines (60 loc) · 1.8 KB
/
bfs copy.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
from collections import defaultdict
class Graph:
def __init__(self):
self.graph = defaultdict(list)
def add_edge(self, u, v):
self.graph[u].append(v)
def BFS(self, source):
visited = set()
queue = []
queue.append(source)
visited.add(source)
while queue:
first = queue.pop(0)
print(first, end=" ")
for neighbour in self.graph[first]:
if neighbour not in visited:
visited.add(neighbour)
queue.append(neighbour)
def DFS(self, source):
visited = set()
self.dfs(visited, self.graph, source)
def dfs(self, visited, graph, source):
if source not in visited:
visited.add(source)
print(source, end=" ")
for neighbour in graph[source]:
self.dfs(visited, graph, neighbour)
def IDDFS(self, source, target, maxdepth):
for i in range(maxdepth):
if self.DLS(source, target, i):
return True
return False
def DLS(self, source, target, maxdepth):
if source == target:
return True
if maxdepth <=0:
return False
for i in self.graph[source]:
if self.DLS(i, target, maxdepth-1):
return True
return False
g = Graph()
g.add_edge(0, 1)
g.add_edge(0, 2)
g.add_edge(1, 3)
g.add_edge(1, 4)
g.add_edge(2, 5)
g.add_edge(2, 6)
print("Breadth First Traversal (starting from vertex 2)")
g.BFS(2)
print("\nDFS: ")
g.DFS(2)
print("\nID:")
target = 6; maxDepth = 3; src = 0
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")