-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTarjan.py
More file actions
79 lines (64 loc) · 1.5 KB
/
Tarjan.py
File metadata and controls
79 lines (64 loc) · 1.5 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
class Stack:
def __init__(self):
self.items = []
def is_empty(self):
return len(self.items) == 0
def push(self, item):
self.items.append(item)
def pop(self):
if not self.is_empty():
return self.items.pop()
else:
return "pop from an empty stack"
def peek(self):
if not self.is_empty():
return self.items[-1]
else:
return "peek from an empty stack"
def size(self):
return len(self.items)
UNVISITED = -1
n = 8
g ={
0 : [1],
1 : [2],
2 : [0],
3 : [4, 7],
4 : [5],
5 : [0, 6],
6 : [0, 2, 4],
7 : [3, 5]
}
id = 0
sccCount = 0
ids = [-1] * n
low = [0] * n
onStack = [False] * n
stack = Stack()
def findSccs():
for i in range(n):
if(ids[i] == UNVISITED):
dfs(i)
return low
def dfs(at):
global id
stack.push(at)
onStack[at] = True
ids[at] = low[at] = id+1
id += 1
for to in g[at]:
if ids[to] == UNVISITED:
dfs(to)
if onStack[to]:
low[at] = min(low[at], low[to])
if ids[at] == low[at]:
while True:
node = stack.pop()
onStack[node] = False
low[node] = ids[at]
if node == at:
break
global sccCount
sccCount += 1
findSccs()
print("Number of strongly connected components:", sccCount)