-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKosaraju's SCC Algo.py
54 lines (42 loc) · 1.44 KB
/
Kosaraju's SCC Algo.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
from collections import defaultdict
import sys
sys.setrecursionlimit(10**5)
class Graph:
def __init__(self):
self.neighbours=defaultdict(list)
self.finished=[] #stack nodes by finish time
self.scc=defaultdict(set)
def addEdge(self,u,v):
self.neighbours[u].append(v)
def DFSVisit(self,s,visited):
visited[s]=True
for v in self.neighbours[s]:
if(not visited[v]):
self.DFSVisit(v,visited)
self.finished.append(s)
def DFS(self,n):
visited=defaultdict(lambda:False)
for i in range(1,n+1):
if(not visited[i]):
self.DFSVisit(i,visited)
def DFSVisitKosaraju(self,s,visited,startNode):
visited[s]=True
for v in self.neighbours[s]:
if(not visited[v]):
self.DFSVisitKosaraju(v,visited,startNode)
self.scc[startNode].add(v)
def DFSKosaraju(self,n,finishedStack):
visited=defaultdict(lambda:False)
while finishedStack:
u=finishedStack.pop()
if(not visited[u]):
self.DFSVisitKosaraju(u,visited,u)
n,m=[int(x) for x in input().split()]
g1=Graph()
g2=Graph() #reversed edges
for i in range(m):
u,v=[int(x) for x in input().split()]
g1.addEdge(u,v)
g2.addEdge(v,u)
g1.DFS(n)
g2.DFSKosaraju(n,g1.finished)