-
Notifications
You must be signed in to change notification settings - Fork 2
/
kosaraju.py
executable file
·39 lines (33 loc) · 938 Bytes
/
kosaraju.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
#!/usr/bin/env python3
def dfs(graph, node, visited, exit_order):
visited.add(node)
for neighbor in graph[node]:
if neighbor not in visited:
dfs(graph, neighbor, visited, exit_order)
exit_order.append(node)
def topological_sort(graph):
visited = set()
order = []
for node in range(len(graph)):
if node in visited: continue
dfs(graph, node, visited, order)
return order[::-1]
def kosaraju(graph):
transposed = [[] for _ in graph]
for node, neighbors in enumerate(graph):
for neighbor in neighbors:
transposed[neighbor].append(node)
visited = set()
components = []
for node in topological_sort(graph):
if node in visited: continue
components.append([])
dfs(transposed, node, visited, components[-1])
return components
graph = [
[1, 2, 3],
[2, 3],
[3, 1],
[]
]
print(kosaraju(graph))