-
Notifications
You must be signed in to change notification settings - Fork 0
/
assignment2_1_strongly_connected_alt.py
108 lines (85 loc) · 2.99 KB
/
assignment2_1_strongly_connected_alt.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
103
104
105
106
107
108
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 23 18:24:15 2018
@author: User
Another version of the strongly connected components using stack instead of
recursion.
Unfinished.
"""
import numpy as np
import pandas as pd
# file = 'SCC_test.txt'
# n = 15
file = 'SCC.txt'
n = 875714
data = pd.read_csv(file, sep=' ', header=None, usecols=[0,1],
names = ['tail', 'head'])
# minus one to become compatible with Python's array ordering
data = data - 1
G = {k:[] for k in range(n)}
for i,p in data.iterrows():
G[p['tail']].append(p['head'])
G_rev = {k:[] for k in range(n)}
for i,p in data.iterrows():
G_rev[p['head']].append(p['tail'])
###############################################################################
# 1. Compute topological ordering on the reverse graph.
###############################################################################
# value at the k-th position is the visit time of node k
# I believe that finish time is in the reverse order of visit time
# =============================================================================
# DFS(source):
# s <- new stack
# visited <- {} // empty set
# s.push(source)
# while (s is not empty):
# current <- s.pop()
# if (current is in visited):
# continue
# visited.add(current)
# // do something with current
# for each node v such that (current,v) is an edge:
# s.push(v)
# =============================================================================
visi_time = np.array([0]*n, dtype=int)
visited = np.array([False]*n)
count = 0
for s in range(0,n):
if not visited[s]:
visited[s] = True
visi_time[s] = count
stk_s = [s]
while stk_s:
current = stk_s.pop()
if not visited[current]:
visited[current] = True
visi_time[current] = count
for i in G_rev[current]:
stk_s.append(i)
count += 1
# value at the k-th position is the 'finish time' of node k
fini_time = n-1 - visi_time
# ---- revert fini_time such that fini_time[k] = s means that the 'finish
# time' of s is k
fini_time2 = {fini_time[s]: s for s in range(len(fini_time))}
###############################################################################
# 2. Compute the strongly connected components.
###############################################################################
XXXXXXXXXXXX
source_scc = np.array([0]*n, dtype=int)
visited = np.array([False]*n)
count = 0
for s in range(0,n):
if not visited[s]:
visited[s] = True
visi_time[s] = count
stk_s = [s]
while stk_s:
current = stk_s.pop()
if not visited[current]:
visited[current] = True
visi_time[current] = count
for i in G[current]:
stk_s.append(i)
count += 1
fini_time = n-1 - visi_time