-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_graph.py
58 lines (49 loc) · 1.79 KB
/
parse_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
#!/usr/bin/python
import os
OUTPUT_FILENAME = "data/graph.dzn"
def read_nodes(edge, parse_edges):
ed = [int(x) for x in edge.split()]
parse_edges[ed[0]].append(ed[1])
parse_edges[ed[1]].append(ed[0])
def parse_graph(initial_graph):
try:
global V
os.makedirs(os.path.dirname(OUTPUT_FILENAME), exist_ok=True)
fp = open(f"{initial_graph}",'r',encoding = 'utf-8')
V = fp.readline().strip()
while(V[0] == "#"):
V = fp.readline().strip()
E = fp.readline().strip()
edges = fp.readlines()
graph = open(f"{OUTPUT_FILENAME}",'w',encoding = 'utf-8')
graph.write(f"V={V};\n")
#graph.write(f"E={E};\n")
global parse_edges
parse_edges = {List: [] for List in range(1, int(V)+1) }
for edge in edges:
read_nodes(edge, parse_edges)
max_len = int(len(max(parse_edges.values(), key=len)))
graph.write(f"MAXLEN={max_len};\n")
graph.write(f"EDGES=[|\n")
for key, value in parse_edges.items():
filler = 0;
for e in value:
if(value.index(e) == max_len-1):
graph.write(f"{e}")
else:
graph.write(f"{e}, ")
filler += 1
while(filler < max_len and len(value) < max_len):
if (filler == max_len -1 ):
graph.write(f"0")
else:
graph.write(f"0, ")
filler += 1
if (key == len(parse_edges)):
graph.write('|];\n')
else:
graph.write('|\n')
finally:
fp.close()
graph.close()
return f"{OUTPUT_FILENAME}"