-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFS_SCC.py
102 lines (89 loc) · 2.67 KB
/
DFS_SCC.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
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
from collections import defaultdict
class Graph:
def __init__(self, vertices):
self.V= vertices #No. of vertices
self.graph = defaultdict(list)
def addEdge(self, u,v):
self.graph[u].append(v)
def reverse_graph(self):
reversed = Graph(self.V)
for i in self.graph:
for j in self.graph[i]:
reversed.addEdge(j,i)
return reversed
def BFS(self, s):
visited = [False]*(self.V)
queue = []
queue.append(s)
visited[s]= True
while queue:
s = queue.pop(0)
print(str(s), end='')
for i in self.graph[s]:
if visited[i] == False:
visited[i]= True
queue.append(i)
def DFS_loop(self,v, visited):
visited[v] = True
print(v, end='')
scc.append(v)
for i in self.graph[v]:
if visited[i] == False:
self.DFS_loop(i, visited)
def DFS(self, v, stack):
visited= [False]*(self.V)
self.DFS_loop(v, visited)
def fillOrder(self,v,visited, stack):
# Mark the current node as visited
visited[v]= True
#Recur for all the vertices adjacent to this vertex
for i in self.graph[v]:
if visited[i]==False:
self.fillOrder(i, visited, stack)
stack = stack.append(v)
def printSCC(self):
scc_list = []
visited = [False]*self.V
stack= []
for i in range(self.V):
if visited[i]==False:
self.fillOrder(i,visited, stack)
#print('stack:', stack)
g_rev = g.reverse_graph()
visited = [False]*self.V
scc = []
while stack:
i = stack.pop()
if visited[i]==False:
g_rev.fillOrder(i, visited, scc)
#print('')
scc_list.append(scc)
scc= []
return scc_list
N = 875715
# g = Graph(7)
# g.addEdge(0,1)
# g.addEdge(1,2)
# g.addEdge(1,4)
# g.addEdge(2,0)
# g.addEdge(2,3)
# g.addEdge(2,6)
# g.addEdge(3,0)
# g.addEdge(4,2)
# g.addEdge(4,5)
import os
os.chdir('C:\\Users\\ituki\\Documents\\Classes\\Coursera\\Coursera_Algo')
source=[]
f = lambda x,y : [int(x),int(y)]
for line in open('SCC.txt'):
source.append(f(line.split()[0], line.split()[1]))
N = 875714+1
g = Graph(N)
for i in range(1,len(source)):
u,v = source[i]
g.addEdge(u,v)
import sys
sys.setrecursionlimit(N)
scc_list = g.printSCC()
#[print(scc) for scc in scc_list]
print(scc_list)