mirror of
https://github.com/MartinThoma/LaTeX-examples.git
synced 2025-04-26 06:48:04 +02:00
Adjust Python code to follow PEP8
This commit is contained in:
parent
b36776fc27
commit
de1ad26035
14 changed files with 349 additions and 311 deletions
|
@ -1,28 +1,29 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
@source: http://codehiker.wordpress.com/2012/04/06/kosarajus-scc/
|
||||
I made minor changs
|
||||
"""
|
||||
@source: http://codehiker.wordpress.com/2012/04/06/kosarajus-scc/
|
||||
I made minor changs
|
||||
"""
|
||||
|
||||
import sys
|
||||
sys.setrecursionlimit(300000)
|
||||
|
||||
source = 'SCC.txt'
|
||||
N = 875714
|
||||
source = 'SCC.txt'
|
||||
N = 875714
|
||||
|
||||
#globals
|
||||
# globals
|
||||
visited = {}
|
||||
finish = {}
|
||||
leader = {}
|
||||
finish = {}
|
||||
leader = {}
|
||||
|
||||
def getG(source):
|
||||
|
||||
def get_g(source):
|
||||
""" Read the Graph from a textfile """
|
||||
G = {}
|
||||
G = {}
|
||||
Grev = {}
|
||||
for i in range(1,N+1):
|
||||
G[i] = []
|
||||
for i in range(1, N+1):
|
||||
G[i] = []
|
||||
Grev[i] = []
|
||||
fin = open(source)
|
||||
for line in fin:
|
||||
|
@ -33,56 +34,61 @@ def getG(source):
|
|||
fin.close()
|
||||
return G, Grev
|
||||
|
||||
|
||||
def init():
|
||||
for i in range(1,N+1):
|
||||
visited[i] = 0
|
||||
finish[i] = 0
|
||||
leader[i] = 0
|
||||
for i in range(1, N+1):
|
||||
visited[i] = 0
|
||||
finish[i] = 0
|
||||
leader[i] = 0
|
||||
|
||||
|
||||
def dfs(G, i):
|
||||
global t
|
||||
visited[i] = 1
|
||||
leader[i] = s
|
||||
visited[i] = 1
|
||||
leader[i] = s
|
||||
for j in G[i]:
|
||||
if(visited[j]==0): dfs(G,j)
|
||||
if(visited[j] == 0):
|
||||
dfs(G, j)
|
||||
t = t + 1
|
||||
finish[i] = t
|
||||
|
||||
|
||||
def dfs_loop(G):
|
||||
global t
|
||||
global s
|
||||
t = 0 #number of nodes processed so far
|
||||
s = 0 #current source vertex
|
||||
t = 0 # number of nodes processed so far
|
||||
s = 0 # current source vertex
|
||||
i = N
|
||||
while(i>0):
|
||||
if(visited[i]==0):
|
||||
s=i
|
||||
dfs(G,i)
|
||||
i=i-1
|
||||
while(i > 0):
|
||||
if(visited[i] == 0):
|
||||
s = i
|
||||
dfs(G, i)
|
||||
i = i-1
|
||||
|
||||
if __name__ == "__main__":
|
||||
init()
|
||||
g, grev=getG()
|
||||
dfs_loop(grev) #THE FIRST LOOP ON REVERSE GRAPH
|
||||
g, grev = get_g()
|
||||
dfs_loop(grev) # THE FIRST LOOP ON REVERSE GRAPH
|
||||
|
||||
# construct new graph
|
||||
newGraph = {}
|
||||
for i in range(1, N+1):
|
||||
temp = []
|
||||
for x in g[i]: temp.append(finish[x])
|
||||
for x in g[i]:
|
||||
temp.append(finish[x])
|
||||
newGraph[finish[i]] = temp
|
||||
|
||||
init()
|
||||
dfs_loop(newGraph) #THE SECOND LOOP
|
||||
dfs_loop(newGraph) # THE SECOND LOOP
|
||||
|
||||
# statistics
|
||||
lst = sorted(leader.values())
|
||||
lst = sorted(leader.values())
|
||||
stat = []
|
||||
pre = 0
|
||||
for i in range(0,N-1):
|
||||
pre = 0
|
||||
for i in range(0, N-1):
|
||||
if lst[i] != lst[i+1]:
|
||||
stat.append(i + 1 - pre)
|
||||
pre=i+1
|
||||
pre = i+1
|
||||
stat.append(N-pre)
|
||||
L = sorted(stat)
|
||||
L.reverse()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue