-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs.py
More file actions
93 lines (77 loc) · 2.39 KB
/
bfs.py
File metadata and controls
93 lines (77 loc) · 2.39 KB
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
88
89
90
91
92
93
# Part 1: bfs on a binary tree
import collections
# creating the node class for the binary tree nodes
class Node:
def __init__(self, key):
self.val = key
self.left = None
self.right = None
res = []
def bfs(root):
q = collections.deque()
q.append(root)
while q:
qlen = len(q)
level = []
for i in range(qlen):
x = q.popleft()
if x:
q.append(x.left)
q.append(x.right)
level.append(x.val)
# because we don't want to add empty levels
if level:
res.append(level)
# building the actual tree with key values of the nodes
root = Node(2)
root.left = Node(4)
root.right = Node(6)
root.left.left = Node(34)
root.right.right = Node(45)
root.left.right = Node(23)
bfs(root)
print(res)
# Part 2: bfs on a graph
res = []
def bfs(graph, root):
q = collections.deque()
q.append(root)
# for tracking visited nodes, you could either use
# a set (visit) or a bool list (visited).
# That's why both are used to show
visited = [False] * (max(graph) + 10)
visited[root] = True
visit = set()
visit.add(root)
while q:
vertex = q.popleft()
res.append(vertex)
if vertex in graph:
for neighbours in graph[vertex]:
if visited[neighbours] == False:
visited[neighbours] = True
visit.add(neighbours)
q.append(neighbours)
graph = {0: [1, 2], 1: [2], 2: [3], 3: [1, 2]} # directed graph
source = 1
bfs(graph, source)
print(res)
# bfs on a grid/matrix
def bfs(matrix):
ROW, COL = len(matrix), len(matrix[0])
visit = set()
q = collections.deque()
# if source is given
q.append(sr, sc)
# if source not given, then build the q from the matrix
# look at the "Rotten Orange" leetcode problem
while q:
dir = [[0, 1], [1, 0], [-1, 0], [0, -1]]
row, col = q.popleft()
for dr, dc in dir:
r, c = row + dr, col + dc
# checking if the rows or cols are out of bounds
if r >= ROW or r < 0 or c >= COL or c < 0 or (r, c) in visit or #some other condition as needed:
continue
# do something based on the problem
visit.add((r, c))