-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs.py
More file actions
354 lines (275 loc) · 9.89 KB
/
bfs.py
File metadata and controls
354 lines (275 loc) · 9.89 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
"""
Breadth-First Search (BFS) Algorithm
====================================
Used for: Shortest path (unweighted), level-order traversal, minimum spanning tree
Time Complexity: O(V + E) where V = vertices, E = edges
Space Complexity: O(V) for queue and visited set
"""
from collections import deque, defaultdict
from typing import List, Set, Dict, Optional, Tuple
class Graph:
def __init__(self):
self.graph = defaultdict(list)
self.vertices = set()
def add_edge(self, u, v, directed=True):
"""Add edge to graph"""
self.graph[u].append(v)
self.vertices.add(u)
self.vertices.add(v)
if not directed:
self.graph[v].append(u)
# ==================== BRUTE FORCE APPROACH ====================
def bfs_brute_force(graph: Dict[int, List[int]], start: int) -> List[int]:
"""
Brute Force BFS: Simple implementation without optimizations
Time Complexity: O(V + E) - visits each vertex and edge once
Space Complexity: O(V) - queue can hold all vertices in worst case
Problems:
- Uses list as queue (O(n) for pop(0))
- No distance tracking
- Inefficient for large graphs
"""
if start not in graph and not any(start in neighbors for neighbors in graph.values()):
return [start]
visited = []
queue = [start] # Using list as queue (inefficient)
while queue:
node = queue.pop(0) # O(n) operation!
if node not in visited:
visited.append(node)
for neighbor in graph.get(node, []):
if neighbor not in visited and neighbor not in queue:
queue.append(neighbor)
return visited
# ==================== OPTIMIZED APPROACH ====================
def bfs_optimized(graph: Dict[int, List[int]], start: int) -> List[int]:
"""
Optimized BFS: Uses deque for O(1) queue operations
Time Complexity: O(V + E) - visits each vertex and edge once
Space Complexity: O(V) - queue and visited set
Advantages:
- O(1) queue operations with deque
- Set lookup for visited (O(1))
- Memory efficient
"""
if start not in graph:
# Check if start exists as a neighbor
all_nodes = set(graph.keys())
for neighbors in graph.values():
all_nodes.update(neighbors)
if start not in all_nodes:
return [start]
visited = set()
result = []
queue = deque([start])
visited.add(start)
while queue:
node = queue.popleft()
result.append(node)
for neighbor in graph.get(node, []):
if neighbor not in visited:
visited.add(neighbor)
queue.append(neighbor)
return result
def bfs_shortest_path(graph: Dict[int, List[int]], start: int, target: int) -> Optional[List[int]]:
"""
Find shortest path using BFS
Time Complexity: O(V + E)
Space Complexity: O(V)
"""
if start == target:
return [start]
visited = set([start])
queue = deque([(start, [start])])
while queue:
node, path = queue.popleft()
for neighbor in graph.get(node, []):
if neighbor == target:
return path + [neighbor]
if neighbor not in visited:
visited.add(neighbor)
queue.append((neighbor, path + [neighbor]))
return None
def bfs_with_distances(graph: Dict[int, List[int]], start: int) -> Dict[int, int]:
"""
BFS with distance tracking from start node
Time Complexity: O(V + E)
Space Complexity: O(V)
"""
distances = {start: 0}
queue = deque([start])
while queue:
node = queue.popleft()
current_dist = distances[node]
for neighbor in graph.get(node, []):
if neighbor not in distances:
distances[neighbor] = current_dist + 1
queue.append(neighbor)
return distances
def bfs_level_order_traversal(graph: Dict[int, List[int]], start: int) -> List[List[int]]:
"""
BFS level-order traversal - returns nodes grouped by level
Time Complexity: O(V + E)
Space Complexity: O(V)
"""
if start not in graph:
return [[start]]
result = []
queue = deque([start])
visited = set([start])
while queue:
level_size = len(queue)
current_level = []
for _ in range(level_size):
node = queue.popleft()
current_level.append(node)
for neighbor in graph.get(node, []):
if neighbor not in visited:
visited.add(neighbor)
queue.append(neighbor)
result.append(current_level)
return result
def bfs_connected_components(graph: Dict[int, List[int]]) -> List[List[int]]:
"""
Find all connected components using BFS
Time Complexity: O(V + E)
Space Complexity: O(V)
"""
visited = set()
components = []
# Get all vertices
all_vertices = set(graph.keys())
for neighbors in graph.values():
all_vertices.update(neighbors)
for vertex in all_vertices:
if vertex not in visited:
component = []
queue = deque([vertex])
visited.add(vertex)
while queue:
node = queue.popleft()
component.append(node)
for neighbor in graph.get(node, []):
if neighbor not in visited:
visited.add(neighbor)
queue.append(neighbor)
components.append(component)
return components
def is_bipartite(graph: Dict[int, List[int]]) -> bool:
"""
Check if graph is bipartite using BFS two-coloring
Time Complexity: O(V + E)
Space Complexity: O(V)
"""
color = {}
# Get all vertices
all_vertices = set(graph.keys())
for neighbors in graph.values():
all_vertices.update(neighbors)
for start in all_vertices:
if start not in color:
queue = deque([start])
color[start] = 0
while queue:
node = queue.popleft()
for neighbor in graph.get(node, []):
if neighbor not in color:
color[neighbor] = 1 - color[node]
queue.append(neighbor)
elif color[neighbor] == color[node]:
return False
return True
# ==================== GRID BFS ====================
def bfs_grid_shortest_path(grid: List[List[int]], start: Tuple[int, int], end: Tuple[int, int]) -> int:
"""
BFS on 2D grid to find shortest path
0 = walkable, 1 = obstacle
Time Complexity: O(rows * cols)
Space Complexity: O(rows * cols)
"""
if not grid or not grid[0]:
return -1
rows, cols = len(grid), len(grid[0])
if grid[start[0]][start[1]] == 1 or grid[end[0]][end[1]] == 1:
return -1
if start == end:
return 0
queue = deque([(start[0], start[1], 0)])
visited = set([start])
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
while queue:
row, col, dist = queue.popleft()
for dr, dc in directions:
new_row, new_col = row + dr, col + dc
if (0 <= new_row < rows and 0 <= new_col < cols and
grid[new_row][new_col] == 0 and (new_row, new_col) not in visited):
if (new_row, new_col) == end:
return dist + 1
visited.add((new_row, new_col))
queue.append((new_row, new_col, dist + 1))
return -1
# ==================== EXAMPLE USAGE ====================
if __name__ == "__main__":
# Create sample graph
graph = {
1: [2, 3],
2: [4, 5],
3: [6],
4: [],
5: [7],
6: [],
7: []
}
print("Graph:", graph)
print("\n--- BFS Traversals ---")
print("Brute Force BFS:", bfs_brute_force(graph, 1))
print("Optimized BFS:", bfs_optimized(graph, 1))
print("\n--- Shortest Path ---")
path = bfs_shortest_path(graph, 1, 7)
print(f"Shortest path from 1 to 7: {path}")
print("\n--- Distances ---")
distances = bfs_with_distances(graph, 1)
print(f"Distances from node 1: {distances}")
print("\n--- Level Order ---")
levels = bfs_level_order_traversal(graph, 1)
print(f"Level order traversal: {levels}")
print("\n--- Bipartite Check ---")
bipartite_graph = {1: [2, 4], 2: [1, 3], 3: [2, 4], 4: [1, 3]}
print(f"Is bipartite: {is_bipartite(bipartite_graph)}")
print("\n--- Grid BFS ---")
grid = [
[0, 0, 1, 0],
[1, 0, 0, 0],
[0, 0, 0, 1],
[0, 1, 0, 0]
]
shortest = bfs_grid_shortest_path(grid, (0, 0), (3, 3))
print(f"Shortest path in grid: {shortest}")
"""
COMMON BFS PATTERNS:
1. Shortest Path (Unweighted):
- Level-by-level exploration guarantees shortest path
- Use when all edges have equal weight
2. Level Order Traversal:
- Process nodes level by level
- Tree level order, graph layers
3. Connected Components:
- Find all reachable nodes from a starting point
- Count number of islands/components
4. Bipartite Check:
- Two-coloring using BFS
- Detect odd cycles
WHEN TO USE BFS:
- Finding shortest path in unweighted graphs
- Level-order traversal needed
- Finding minimum number of steps
- Exploring neighbors before going deeper
WHEN NOT TO USE BFS:
- Weighted graphs (use Dijkstra instead)
- Memory is very limited (DFS uses less memory)
- Need to explore all paths (use DFS)
- Very wide graphs (BFS queue can become very large)
BFS vs DFS:
- BFS: Breadth-first, uses queue, finds shortest path
- DFS: Depth-first, uses stack/recursion, uses less memory
"""