-
Notifications
You must be signed in to change notification settings - Fork 22
/
rosalind_scc.py
70 lines (58 loc) · 1.56 KB
/
rosalind_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
# ^_^ coding:utf-8 ^_^
"""
Strongly Connected Components
url: http://rosalind.info/problems/scc/
Given: A simple directed graph with n≤103 vertices in the edge list format.
Return: The number of strongly connected components in the graph.
"""
# the input:
# A simple directed graph with n≤10**3 vertices in the edge list format.
# ==============================
data = "../data/rosalind_scc.txt"
with open(data, "r") as f:
vertice, edge = map(int, f.readline().strip().split(" "))
graph = {v:[] for v in range(1, vertice+1)}
for line in f:
vertice1, vertice2 = map(int, line.strip().split(" "))
graph[vertice1].append(vertice2)
print(graph)
print(vertice)
# the solution:
# ==============================
def DFSUtil(graph, v, visited):
visited[v] = True
# print(v,)
SCCs.append(v)
for i in graph[v]:
if visited[i] == False:
DFSUtil(graph, i, visited)
def fillOrder(v, visited, stack):
visited[v] = True
for i in graph[v]:
if visited[i]==False:
fillOrder(i, visited, stack)
stack.append(v)
def getTranspose(graph, vertice):
gr = {v:[] for v in range(1, vertice+1)}
for i in graph:
for j in graph[i]:
gr[j].append(i)
return gr
def printSCCs(graph, vertice):
stack = []
visited = [False] * (vertice+1)
for ve in range(1, vertice+1):
if visited[ve]==False:
fillOrder(ve, visited, stack)
gr = getTranspose(graph, vertice)
visited = [False] * (vertice+1)
while stack:
i = stack.pop()
if visited[i]==False:
DFSUtil(gr, i, visited)
# print("")
SCCs.append("")
SCCs = []
printSCCs(graph, vertice)
print(SCCs)
# print(SCCs.count(""))