-
Notifications
You must be signed in to change notification settings - Fork 131
/
Breadth_First_Search.py
52 lines (43 loc) · 1.58 KB
/
Breadth_First_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
50
51
52
'''graph algorithm which is used to traverse a graph to find a particular node to ensure that we have visited all the nodes by crossing a layer at each step.
In the process, it first visits all the neighbouring vertices of s, then it visits the vertices that have a distance of two of s, then a distance of three and so on.'''
class Queue():
def __init__(self):
self.size = 0
self.list = []
def enqueue(self, data):
self.list.append(data)
self.size += 1
def dequeue(self):
try:
self.size -= 1
return self.list.pop(0)
except Exception as error:
print(f'{error} is not possible')
def xprint(self, index):
print(self.list[index])
def breadth_first(graph, root):
queue = Queue()
visited_nodes = list()
queue.enqueue(root)
visited_nodes.append(root)
current_node = root
while queue.size > 0:
current_node = queue.dequeue()
adj_nodes = graph[current_node]
remaining_elements = sorted(set(adj_nodes) - set(visited_nodes))
if len(remaining_elements) > 0:
for element in remaining_elements:
visited_nodes.append(element)
queue.enqueue(element)
return visited_nodes
if __name__ == '__main__':
graph = dict()
graph['A'] = ['B', 'G', 'D']
graph['B'] = ['A', 'F', 'E']
graph['C'] = ['F', 'H']
graph['D'] = ['F', 'A']
graph['E'] = ['B', 'G']
graph['F'] = ['B', 'D', 'C']
graph['G'] = ['A', 'E']
graph['H'] = ['C']
print(breadth_first(graph, 'A'))