-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponent.py
59 lines (50 loc) · 1.43 KB
/
component.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
from networkx import read_gexf
def dfs(g, u, stack, explored):
explored[u] = 1
if u in g.graph_dict:
for v in g.graph_dict[u]:
if not explored[v]:
dfs(g, v, stack, explored)
stack.append(u)
def bfs(g, u):
seen = set()
next = {u}
while next:
cur = next
next = set()
for v in cur:
if v not in seen:
yield v
seen.add(v)
next.update(g.succ[v])
next.update(g.pred[v])
def scc(g):
explored = dict.fromkeys(g.graph_dict.keys(), 0)
results = []
# Initial DFS on g
search_stack = []
for v, expl in explored.items():
if not expl and v != list(explored.keys())[0]:
dfs(g, v, search_stack, explored)
# Reverse graph
gr = g.reverse()
exploredr = dict.fromkeys(g.graph_dict.keys(), 0)
# DFS ordered by search_stack
while search_stack:
u = search_stack[-1]
scc_stack = []
dfs(gr, u, scc_stack, exploredr)
for v in scc_stack:
if v in gr.graph_dict:
del gr.graph_dict[v]
search_stack.remove(v)
results.append(scc_stack)
return results
def wcc(g):
g = read_gexf("data/vk-friends-164285180.gexf")
seen = set()
for v in g:
if v not in seen:
c = set(bfs(g, v))
yield c
seen.update(c)