-
Notifications
You must be signed in to change notification settings - Fork 70
/
bfs_2D.py
52 lines (38 loc) · 1.48 KB
/
bfs_2D.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
# Program to do bfs in 2-d matrix assuming you are provided wioth the start node and the
# only directions you can travel is up down left and right
from collections import deque
print("Enter size of the matrix:")
n, m = map(int, input().split())
visited = [[0 for i in range(m)] for i in range(n)]
# keeping track of every node from the root node
dist = [[0 for i in range(m)] for i in range(n)]
def isValid(node_x, node_y):
if 0 <= node_x <= n-1 and 0 <= node_y <= m-1:
if visited[node_x][node_y] == 0:
return True
else:
return False
# level order traversal with respect to the root node
def bfs(root_x, root_y):
visited[root_x][root_y] = 1
q = deque()
q.append([root_x, root_y])
# up,right,down,left
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
while len(q) > 0:
node = q.popleft()
for i in range(len(dx)):
new_x = node[0] + dx[i]
new_y = node[1] + dy[i]
if isValid(new_x, new_y):
visited[new_x][new_y] = 1
# distance of child node = distance of parent node + 1
dist[new_x][new_y] = dist[node[0]][node[1]] + 1
# append the new nodes to the queue
q.append([new_x, new_y])
print("Enter which node you want to act as the root node")
node, y = map(int, input().split())
bfs(node, y)
for i in range(0, n):
print(" ".join(str(j) for j in dist[i][0:]))