-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfs.py
More file actions
240 lines (191 loc) · 6.39 KB
/
dfs.py
File metadata and controls
240 lines (191 loc) · 6.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
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
"""
Depth-First Search (DFS) Algorithm
==================================
Used for: Tree traversal, connected components, cycle detection, pathfinding
Time Complexity: O(V + E) where V = vertices, E = edges
Space Complexity: O(V) for recursion stack or explicit stack
"""
from collections import defaultdict, deque
from typing import List, Set, Dict, Optional
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 dfs_brute_force(graph: Dict[int, List[int]], start: int) -> List[int]:
"""
Brute Force DFS: Uses simple recursion without optimization
Time Complexity: O(V + E) - visits each vertex and edge once
Space Complexity: O(V) - recursion stack depth can be V in worst case
Problems:
- Can cause stack overflow for deep graphs
- No cycle detection
- Inefficient for repeated searches
"""
visited = []
def dfs_recursive(node):
visited.append(node)
for neighbor in graph.get(node, []):
if neighbor not in visited:
dfs_recursive(neighbor)
dfs_recursive(start)
return visited
# ==================== OPTIMIZED APPROACH ====================
def dfs_iterative_optimized(graph: Dict[int, List[int]], start: int) -> List[int]:
"""
Optimized DFS: Uses explicit stack to avoid recursion limits
Time Complexity: O(V + E) - visits each vertex and edge once
Space Complexity: O(V) - explicit stack and visited set
Advantages:
- No stack overflow issues
- Can handle very large graphs
- Memory efficient with set lookup
"""
if start not in graph:
return [start] if start is not None else []
visited = set()
result = []
stack = [start]
while stack:
node = stack.pop()
if node not in visited:
visited.add(node)
result.append(node)
# Add neighbors in reverse order to maintain left-to-right traversal
for neighbor in reversed(graph[node]):
if neighbor not in visited:
stack.append(neighbor)
return result
def dfs_with_path_tracking(graph: Dict[int, List[int]], start: int, target: int) -> Optional[List[int]]:
"""
DFS with path tracking - finds path from start to target
Time Complexity: O(V + E)
Space Complexity: O(V)
"""
visited = set()
path = []
def dfs_path(node):
if node in visited:
return False
visited.add(node)
path.append(node)
if node == target:
return True
for neighbor in graph.get(node, []):
if dfs_path(neighbor):
return True
path.pop() # Backtrack
return False
if dfs_path(start):
return path
return None
def detect_cycle_directed(graph: Dict[int, List[int]]) -> bool:
"""
Detect cycle in directed graph using DFS
Time Complexity: O(V + E)
Space Complexity: O(V)
"""
WHITE, GRAY, BLACK = 0, 1, 2
color = defaultdict(int)
def has_cycle(node):
if color[node] == GRAY: # Back edge found
return True
if color[node] == BLACK: # Already processed
return False
color[node] = GRAY
for neighbor in graph.get(node, []):
if has_cycle(neighbor):
return True
color[node] = BLACK
return False
for vertex in graph:
if color[vertex] == WHITE:
if has_cycle(vertex):
return True
return False
def connected_components(graph: Dict[int, List[int]]) -> List[List[int]]:
"""
Find all connected components using DFS
Time Complexity: O(V + E)
Space Complexity: O(V)
"""
visited = set()
components = []
def dfs_component(node, component):
visited.add(node)
component.append(node)
for neighbor in graph.get(node, []):
if neighbor not in visited:
dfs_component(neighbor, component)
# 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 = []
dfs_component(vertex, component)
components.append(component)
return components
# ==================== 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--- DFS Traversals ---")
print("Brute Force DFS:", dfs_brute_force(graph, 1))
print("Optimized DFS:", dfs_iterative_optimized(graph, 1))
print("\n--- Path Finding ---")
path = dfs_with_path_tracking(graph, 1, 7)
print(f"Path from 1 to 7: {path}")
print("\n--- Cycle Detection ---")
# Cycle graph
cycle_graph = {1: [2], 2: [3], 3: [1]}
print(f"Has cycle: {detect_cycle_directed(cycle_graph)}")
print("\n--- Connected Components ---")
# Disconnected graph
disconnected = {1: [2], 2: [1], 3: [4], 4: [3], 5: []}
components = connected_components(disconnected)
print(f"Connected components: {components}")
"""
COMMON DFS PATTERNS:
1. Tree Traversal:
- Preorder: Process node, then children
- Inorder: Left child, node, right child (binary trees)
- Postorder: Children first, then node
2. Graph Problems:
- Connected components
- Cycle detection
- Topological sorting (with modifications)
- Path finding
3. Backtracking:
- N-Queens
- Sudoku
- Maze solving
- Permutations/Combinations
WHEN TO USE DFS:
- Tree/graph traversal needed
- Path finding with backtracking
- Detecting cycles
- Connected components
- When you need to explore as far as possible before backtracking
WHEN NOT TO USE DFS:
- Finding shortest path (use BFS instead)
- Level-order traversal needed
- Very deep graphs (stack overflow risk with recursive)
"""