-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopological_sort.py
More file actions
445 lines (341 loc) · 12.8 KB
/
topological_sort.py
File metadata and controls
445 lines (341 loc) · 12.8 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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
"""
Topological sorting for Directed Acyclic Graphs (DAGs).
Produces a linear ordering of vertices such that for every directed edge (u, v),
vertex u comes before v in the ordering. Uses both DFS-based and Kahn's algorithm
(BFS-based) approaches for different use cases.
Time complexity: O(V + E) for both algorithms, where V is vertices and E is edges.
Space complexity: O(V + E) for the graph representation and auxiliary data structures.
"""
from __future__ import annotations
from collections import deque
# Don't use annotations during contest
from typing import Generic, TypeVar
NodeT = TypeVar("NodeT")
class TopologicalSort(Generic[NodeT]):
def __init__(self) -> None:
self.graph: dict[NodeT, list[NodeT]] = {}
self.in_degree: dict[NodeT, int] = {}
def add_edge(self, u: NodeT, v: NodeT) -> None:
"""Add directed edge from u to v."""
if u not in self.graph:
self.graph[u] = []
self.in_degree[u] = 0
if v not in self.in_degree:
self.in_degree[v] = 0
self.graph[v] = []
self.graph[u].append(v)
self.in_degree[v] += 1
def kahn_sort(self) -> list[NodeT] | None:
"""
Topological sort using Kahn's algorithm (BFS-based).
Returns the topological ordering, or None if the graph has a cycle.
"""
in_deg = self.in_degree.copy()
queue = deque([node for node, deg in in_deg.items() if deg == 0])
result = []
while queue:
node = queue.popleft()
result.append(node)
for neighbor in self.graph[node]:
in_deg[neighbor] -= 1
if in_deg[neighbor] == 0:
queue.append(neighbor)
# Check if all nodes are processed (no cycle)
if len(result) != len(self.in_degree):
return None
return result
def dfs_sort(self) -> list[NodeT] | None:
"""
Topological sort using DFS.
Returns the topological ordering, or None if the graph has a cycle.
"""
WHITE, GRAY, BLACK = 0, 1, 2
color = dict.fromkeys(self.in_degree, WHITE)
result = []
def dfs(node: NodeT) -> bool:
if color[node] == GRAY: # Back edge (cycle)
return False
if color[node] == BLACK: # Already processed
return True
color[node] = GRAY
for neighbor in self.graph[node]:
if not dfs(neighbor):
return False
color[node] = BLACK
result.append(node)
return True
for node in self.in_degree:
if color[node] == WHITE and not dfs(node):
return None
return result[::-1]
def has_cycle(self) -> bool:
"""Check if the graph contains a cycle."""
return self.kahn_sort() is None
def longest_path(self) -> dict[NodeT, int]:
"""
Find longest path from each node in the DAG.
Returns a dictionary mapping each node to its longest path length.
"""
topo_order = self.kahn_sort()
if topo_order is None:
msg = "Graph contains a cycle"
raise ValueError(msg)
dist = dict.fromkeys(self.in_degree, 0)
for node in topo_order:
for neighbor in self.graph[node]:
dist[neighbor] = max(dist[neighbor], dist[node] + 1)
return dist
def test_main() -> None:
ts: TopologicalSort[int] = TopologicalSort()
edges = [(5, 2), (5, 0), (4, 0), (4, 1), (2, 3), (3, 1)]
for u, v in edges:
ts.add_edge(u, v)
kahn_result = ts.kahn_sort()
dfs_result = ts.dfs_sort()
assert kahn_result is not None
assert dfs_result is not None
assert not ts.has_cycle()
# Test with cycle
ts_cycle: TopologicalSort[int] = TopologicalSort()
ts_cycle.add_edge(1, 2)
ts_cycle.add_edge(2, 3)
ts_cycle.add_edge(3, 1)
assert ts_cycle.has_cycle()
# Don't write tests below during competition.
def test_empty_graph() -> None:
ts: TopologicalSort[int] = TopologicalSort()
# Empty graph should return empty list
assert ts.kahn_sort() == []
assert ts.dfs_sort() == []
assert not ts.has_cycle()
def test_single_node() -> None:
ts: TopologicalSort[str] = TopologicalSort()
ts.add_edge("A", "A") # This creates a self-loop (cycle)
assert ts.has_cycle()
assert ts.kahn_sort() is None
assert ts.dfs_sort() is None
# Single node without self-loop
ts2: TopologicalSort[str] = TopologicalSort()
ts2.in_degree["A"] = 0
ts2.graph["A"] = []
result = ts2.kahn_sort()
assert result == ["A"]
assert not ts2.has_cycle()
def test_simple_chain() -> None:
# Linear chain: A -> B -> C -> D
ts: TopologicalSort[str] = TopologicalSort()
edges = [("A", "B"), ("B", "C"), ("C", "D")]
for u, v in edges:
ts.add_edge(u, v)
kahn_result = ts.kahn_sort()
dfs_result = ts.dfs_sort()
assert kahn_result == ["A", "B", "C", "D"]
assert dfs_result == ["A", "B", "C", "D"]
assert not ts.has_cycle()
def test_simple_cycle() -> None:
# Simple 3-cycle: A -> B -> C -> A
ts: TopologicalSort[str] = TopologicalSort()
edges = [("A", "B"), ("B", "C"), ("C", "A")]
for u, v in edges:
ts.add_edge(u, v)
assert ts.has_cycle()
assert ts.kahn_sort() is None
assert ts.dfs_sort() is None
def test_disconnected_components() -> None:
# Two disconnected chains: A->B and C->D
ts: TopologicalSort[str] = TopologicalSort()
edges = [("A", "B"), ("C", "D")]
for u, v in edges:
ts.add_edge(u, v)
kahn_result = ts.kahn_sort()
dfs_result = ts.dfs_sort()
# Both should work, order might vary for disconnected components
assert kahn_result is not None
assert dfs_result is not None
assert len(kahn_result) == 4
assert len(dfs_result) == 4
assert not ts.has_cycle()
# Check that ordering constraints are satisfied
assert kahn_result.index("A") < kahn_result.index("B")
assert kahn_result.index("C") < kahn_result.index("D")
def test_diamond_dag() -> None:
# Diamond: A -> B,C B,C -> D
ts: TopologicalSort[str] = TopologicalSort()
edges = [("A", "B"), ("A", "C"), ("B", "D"), ("C", "D")]
for u, v in edges:
ts.add_edge(u, v)
kahn_result = ts.kahn_sort()
dfs_result = ts.dfs_sort()
assert kahn_result is not None
assert dfs_result is not None
assert not ts.has_cycle()
# Check ordering constraints
for result in [kahn_result, dfs_result]:
assert result.index("A") < result.index("B")
assert result.index("A") < result.index("C")
assert result.index("B") < result.index("D")
assert result.index("C") < result.index("D")
def test_complex_dag() -> None:
# More complex DAG with multiple valid orderings
ts: TopologicalSort[int] = TopologicalSort()
edges = [(1, 2), (1, 3), (2, 4), (3, 4), (3, 5), (4, 6), (5, 6)]
for u, v in edges:
ts.add_edge(u, v)
kahn_result = ts.kahn_sort()
dfs_result = ts.dfs_sort()
assert kahn_result is not None
assert dfs_result is not None
assert not ts.has_cycle()
# Verify all ordering constraints
constraints = [(1, 2), (1, 3), (2, 4), (3, 4), (3, 5), (4, 6), (5, 6)]
for result in [kahn_result, dfs_result]:
for u, v in constraints:
assert result.index(u) < result.index(v)
def test_large_cycle() -> None:
# Large cycle: 0 -> 1 -> 2 -> ... -> 99 -> 0
ts: TopologicalSort[int] = TopologicalSort()
n = 100
for i in range(n):
ts.add_edge(i, (i + 1) % n)
assert ts.has_cycle()
assert ts.kahn_sort() is None
assert ts.dfs_sort() is None
def test_tree_structure() -> None:
# Binary tree structure (DAG)
ts: TopologicalSort[int] = TopologicalSort()
edges = [(1, 2), (1, 3), (2, 4), (2, 5), (3, 6), (3, 7)]
for u, v in edges:
ts.add_edge(u, v)
kahn_result = ts.kahn_sort()
dfs_result = ts.dfs_sort()
assert kahn_result is not None
assert dfs_result is not None
assert not ts.has_cycle()
# Check tree constraints
for result in [kahn_result, dfs_result]:
assert result.index(1) < result.index(2)
assert result.index(1) < result.index(3)
assert result.index(2) < result.index(4)
assert result.index(2) < result.index(5)
assert result.index(3) < result.index(6)
assert result.index(3) < result.index(7)
def test_longest_path() -> None:
# Test longest path functionality
ts: TopologicalSort[str] = TopologicalSort()
edges = [("A", "B"), ("A", "C"), ("B", "D"), ("C", "D"), ("D", "E")]
for u, v in edges:
ts.add_edge(u, v)
longest_paths = ts.longest_path()
# Expected longest paths from each node
assert longest_paths["A"] == 0
assert longest_paths["B"] == 1
assert longest_paths["C"] == 1
assert longest_paths["D"] == 2
assert longest_paths["E"] == 3
def test_longest_path_with_cycle() -> None:
# Should raise error for graphs with cycles
ts: TopologicalSort[int] = TopologicalSort()
edges = [(1, 2), (2, 3), (3, 1)] # Cycle
for u, v in edges:
ts.add_edge(u, v)
try:
ts.longest_path()
assert False, "Should raise ValueError for cyclic graph"
except ValueError:
pass
def test_multiple_sources() -> None:
# Graph with multiple sources (nodes with in-degree 0)
ts: TopologicalSort[int] = TopologicalSort()
edges = [(1, 3), (2, 3), (3, 4), (5, 6)]
for u, v in edges:
ts.add_edge(u, v)
kahn_result = ts.kahn_sort()
dfs_result = ts.dfs_sort()
assert kahn_result is not None
assert dfs_result is not None
assert not ts.has_cycle()
# Sources (1, 2, 5) should come before their dependents
for result in [kahn_result, dfs_result]:
assert result.index(1) < result.index(3)
assert result.index(2) < result.index(3)
assert result.index(3) < result.index(4)
assert result.index(5) < result.index(6)
def test_course_prerequisites() -> None:
# Real-world example: course prerequisites
ts: TopologicalSort[str] = TopologicalSort()
# Math1 -> Math2 -> Math3, Physics1 -> Physics2, Math2 -> Physics2
edges = [
("Math1", "Math2"), ("Math2", "Math3"),
("Physics1", "Physics2"), ("Math2", "Physics2")
]
for u, v in edges:
ts.add_edge(u, v)
kahn_result = ts.kahn_sort()
assert kahn_result is not None
assert not ts.has_cycle()
# Verify prerequisites
assert kahn_result.index("Math1") < kahn_result.index("Math2")
assert kahn_result.index("Math2") < kahn_result.index("Math3")
assert kahn_result.index("Physics1") < kahn_result.index("Physics2")
assert kahn_result.index("Math2") < kahn_result.index("Physics2")
def test_complex_dependencies() -> None:
# Complex dependency graph
ts: TopologicalSort[str] = TopologicalSort()
edges = [
("underwear", "pants"), ("underwear", "shoes"),
("pants", "belt"), ("pants", "shoes"),
("shirt", "belt"), ("shirt", "tie"),
("tie", "jacket"), ("belt", "jacket"),
("socks", "shoes")
]
for u, v in edges:
ts.add_edge(u, v)
kahn_result = ts.kahn_sort()
dfs_result = ts.dfs_sort()
assert kahn_result is not None
assert dfs_result is not None
assert not ts.has_cycle()
# Check some key dependencies
for result in [kahn_result, dfs_result]:
assert result.index("underwear") < result.index("pants")
assert result.index("pants") < result.index("shoes")
assert result.index("socks") < result.index("shoes")
assert result.index("shirt") < result.index("tie")
assert result.index("tie") < result.index("jacket")
def test_stress_large_dag() -> None:
# Large DAG: layered graph
ts: TopologicalSort[int] = TopologicalSort()
layers = 10
nodes_per_layer = 10
# Connect each node in layer i to all nodes in layer i+1
for layer in range(layers - 1):
for i in range(nodes_per_layer):
for j in range(nodes_per_layer):
u = layer * nodes_per_layer + i
v = (layer + 1) * nodes_per_layer + j
ts.add_edge(u, v)
kahn_result = ts.kahn_sort()
dfs_result = ts.dfs_sort()
assert kahn_result is not None
assert dfs_result is not None
assert not ts.has_cycle()
assert len(kahn_result) == layers * nodes_per_layer
def main() -> None:
test_main()
test_empty_graph()
test_single_node()
test_simple_chain()
test_simple_cycle()
test_disconnected_components()
test_diamond_dag()
test_complex_dag()
test_large_cycle()
test_tree_structure()
test_longest_path()
test_longest_path_with_cycle()
test_multiple_sources()
test_course_prerequisites()
test_complex_dependencies()
test_stress_large_dag()
if __name__ == "__main__":
main()