-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.py
executable file
·69 lines (54 loc) · 1.23 KB
/
graph.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
#!/usr/bin/python3
import argparse
import re
import networkx as nx
import os
parser = argparse.ArgumentParser()
parser.add_argument('filename')
args = parser.parse_args()
inFile = open(args.filename)
allcolleges = dict()
keys = list()
for line in inFile:
splitted = re.split('\W+', line)
x = list()
x.extend(splitted)
x.remove(splitted[0])
allcolleges[splitted[0]] = x
inFile.close()
G = nx.Graph()
for key,value in allcolleges.items():
G.add_node(key)
keys.append(key)
#print(keys[1])
#print(allcolleges[keys[1]])
numKeys = len(keys)
x = 0
for i in range(1, numKeys):
looking = keys[0]
values = allcolleges[looking]
del allcolleges[looking]
keys.remove(looking)
for val in values:
if val:
for key in keys:
if val in allcolleges[key]:
if (looking, key) in G.edges():
G[looking]
data = G[looking][key]['weight']
data = data + 1
G[looking][key]['weight'] = data
G[looking]
else:
G.add_edge(looking, key, weight=1)
#print(G.nodes())
#print(G.edges())
test = 0
fileName = '.graphml'
while True:
testFileName = 'out_' + str(test) + fileName
if( not os.path.isfile('./' + testFileName)):
fileName = testFileName
break
test = test + 1
nx.write_graphml(G, fileName)