-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfs_algorithm.py
53 lines (38 loc) · 990 Bytes
/
bfs_algorithm.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
def main():
# 0 1 2 3
# 0 0 1 1 0
# 1 0 0 1 0
# 2 1 0 0 1
# 3 0 0 0 1
# The graph's adjacency matrix
matrix = [[0,1,1,0],
[0,0,1,0],
[1,0,0,1],
[0,0,0,1]];
# The visited array
visited = [0,0,0,0]
# Add the start node to the queue
# Node 0 in this case
queue = [0]
# Set the visited value of node 0 to visited
visited[0] = 1
# Dequeue node 0
node = queue.pop(0);
print (node)
while True:
for x in range (0, len(visited)):
# Check is route exists and that node isn't visited
if matrix[node][x] == 1 and visited[x] == 0:
# Visit node
visited[x] = 1;
# Enqueue element
queue.append(x)
# When queue is empty, break
if len(queue) == 0:
break;
else:
# Dequeue element from queue
node = queue.pop(0)
print ("Deque",node)
if __name__ == "__main__":
main()