forked from SciSpark/SciSpark
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStronglyConnectedComponents.py
More file actions
86 lines (65 loc) · 2.37 KB
/
StronglyConnectedComponents.py
File metadata and controls
86 lines (65 loc) · 2.37 KB
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
import json
from itertools import groupby
from ast import literal_eval
''' Purpose: generate graph from edge list
Inputs: edgelist from function in sciSpark
'''
def main():
print 'begin!'
edges = []
allEdges = []
groupededges = {}
vertices = set()
vertexArray = []
# Assume edgelist is saved in file as a list of tuples where, [((frameNum, CE),(frameNum, CE))]
edgelistFile = '/home/rahul/IdeaProjects/SciSpark/EdgeList.txt'
jsonFile = '/home/rahul/IdeaProjects/SciSpark/graph.json'
with open(edgelistFile,'r') as f:
for line in f:
edges.extend(literal_eval(line.strip()))
print 'There are %d edges' %(len(edges))
# sort out the naming and change to [(fromNode, toNode)] to remove edgelist as list of tuples of tuples to a list of tuples
for eachtuple in edges:
fromNode = 'F'+str(eachtuple[0][0])+'CE'+str(int(eachtuple[0][1]))
toNode = 'F'+str(eachtuple[1][0])+'CE'+str(int(eachtuple[1][1]))
allEdges.append((fromNode,toNode))
print 'sanity check: length after naming is %d ' %(len(allEdges))
for i in allEdges:
vertices.add(i[0])
vertices.add(i[1])
for i in vertices:
vertexArray.append(i)
for key, group in groupby(allEdges, lambda x: x[0]):
groupededges[key] = []
for k in group:
groupededges[key].append(k)
print 'Number of vertices %d' %(len(vertexArray))
solution = {}
nodesJSON = []
linksJSON = []
for x in vertexArray:
solution[x] = stronglycc(x, groupededges)
nodesJSON.append({"name":x})
for i in solution.items()[0:10]:
source = vertexArray.index(solution.items()[0][0])
print source
if len(i[1]) < 100:
for vertexPair in i[1]:
source = vertexArray.index(vertexPair[0])
target = vertexArray.index(vertexPair[1])
linksJSON.append({'source':source, 'target':target})
with open(jsonFile,'w') as outfile:
json.dump({"nodes":nodesJSON,"links":linksJSON}, outfile)
def stronglycc(x, groupedges):
Edgelist = []
Stack = [x]
while len(Stack) > 0:
z = Stack.pop()
Edges = []
if groupedges.has_key(z):
Edges = groupedges[z]
for i in Edges:
Edgelist.append(i)
Stack.append(i[1])
return Edgelist
main()