/
kosarajus_two_pass_algorithm.py
59 lines (47 loc) · 1.48 KB
/
kosarajus_two_pass_algorithm.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from graph_search import Graph
'''
Implementation of Kosaraju's two-pass algorithm, which finds strongly connected components within a directed graph.
'''
t = 0
s = None
class GraphWithTS(Graph):
def __init__(self, l, is_directed=False):
Graph.__init__(self, l, is_directed)
self.t = 0
self.s = None
def find_strongly_connected_components(l):
def depth_first_search(graph, start_node):
def search(graph, node):
global t, s
node.visited = True
node.s = s
for neighbour in node.neighbours:
if not neighbour.visited:
search(graph, neighbour)
t += 1
node.t = t
return search(graph, start_node)
r_l = map(lambda i: [i[1], i[0]], l)
graph = GraphWithTS(l, True)
r_graph = GraphWithTS(r_l, True)
def dfs_loop(graph):
global s
nodes = graph.get_nodes()
nodes.sort(key=lambda n: n.label, reverse=True)
for node in nodes:
if not node.visited:
s = node
depth_first_search(graph, node)
for n in r_graph.get_nodes():
n.label = n.id
dfs_loop(r_graph)
for n in r_graph.get_nodes():
graph.get_node(n.id).label = n.t
dfs_loop(graph)
result = {}
for n in graph.get_nodes():
leader = n.s.id
if not result.has_key(leader):
result[leader] = []
result[leader].append(n.id)
return result.values()