Created
April 16, 2012 19:30
-
-
Save cammckinnon/2400946 to your computer and use it in GitHub Desktop.
olivia's code - fixed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict | |
def Dictionary(Filename): | |
dic = {} | |
dicR = {} | |
i = 0 | |
with open(Filename) as f: | |
for line in f: | |
l, r = line.split() | |
l, r = int(l), int(r) | |
if l == r: | |
continue | |
if l in dic: | |
heads = dic[l] | |
heads.append(r) | |
else: | |
dic[l] = [0, r] | |
if r in dicR: | |
tails = dicR[r] | |
tails.append(l) | |
else: | |
dicR[r] = [0, l] | |
if not l in dicR: | |
dicR[l] = [0] | |
if not r in dic: | |
dic[r] = [0] | |
i = i + 1 | |
return dic, dicR, i | |
def DFSLoop1(Graph, n): | |
t = 0 ## current finishing time | |
f = {} ##dictionary of finishing times already found | |
i = n | |
while i > 0: | |
heads = Graph[i] | |
if heads[0] == 0: | |
Graph, t, f = DFS1(Graph, i, t, f) | |
i = i - 1 | |
return f | |
def DFS1(Graph, node, t, f): | |
heads = Graph[node] | |
heads[0] = 1 | |
for j in heads[1:]: | |
startlist = Graph[j] | |
if startlist[0] == 0: | |
Graph, t, f = DFS1(Graph, j, t, f) | |
t = t + 1 | |
f[t] = node | |
return Graph, t, f | |
def DFSLoop2(Graph, f, n): | |
i = n | |
leaders = {} | |
while i > 0: | |
if Graph[f[i]][0] != 1: | |
Graph, leaders = DFS2(Graph, f[i], f[i], leaders, f) | |
i = i - 1 | |
return leaders | |
def DFS2(Graph, node, s, leaders, f): | |
heads = Graph[node] | |
heads[0] = 1 | |
leaders[node] = s | |
for j in heads[1:]: | |
startlist = Graph[j] | |
if startlist[0] == 0: | |
Graph, leaders = DFS2(Graph, j, s, leaders, f) | |
return Graph, leaders | |
def main1(): | |
dic, dicR, i = Dictionary('test4.txt') | |
print "Completed Read" | |
n = len(dic.keys()) | |
f = DFSLoop1(dicR, n) | |
f_inverse = dict([(v,k) for k,v in f.iteritems()]) | |
print "Finishing Order Found" | |
leaders = DFSLoop2(dic, f, n) | |
# leaders is a dict {node => leader} | |
# collect the leaders into a set of leaders: | |
leaders_set = set() | |
for leader in leaders.values(): | |
leaders_set.add(leader) | |
# print out the leaders: | |
print "Leaders: ", len(leaders_set) | |
for leader in leaders_set: | |
print leader, ' (finishing score ', f_inverse[leader], ')' | |
if __name__ == "__main__": | |
import sys, thread, threading, time | |
sys.setrecursionlimit(1000000) # don't know what I actually needed here | |
thread.stack_size(2**27) # largest power of 2 that didn't give me an error, don't know what I needed | |
t1 = threading.Thread( target = main1, args = () ) # creates a thread to call my function scc(graph) | |
begin = time.clock() | |
t1.start() # start the scc thread | |
t1.join() # and wait for it to finish | |
print time.clock() - begin |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment