-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapp.py
55 lines (40 loc) · 1.26 KB
/
mapp.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
from sys import argv
from greedy import greedy
from graph import Graph
from tools import NUM_VERTICES
# from planar import planar
def main():
if len(argv) != 2:
raise ValueError("enter command: python mapp.py <input file name>")
input_file = argv[1]
graph, pairs = build_graph(input_file)
paths = greedy(graph, pairs)
with open("4231output.txt", "x") as f:
for path in paths:
print(*path)
f.write(" ".join(str(x) for x in path))
f.write("\n")
def build_graph(input_file):
pairs = []
graph = Graph(NUM_VERTICES)
f = open(input_file, "r")
f.readline()
while(True):
line = f.readline().strip().split(" ")
if line[0] == "PAIRS":
break
src = line[0][:-1]
for x in range(1, len(line)):
graph.add_edge(src, int(line[x]))
while(True):
line = f.readline()
line = line.strip().split(" ")
if not line or line[0] == "SOLUTIONS":
break
pairs.append((int(line[0]), int(line[1])))
f.close()
return graph, pairs
# graph structure from https://algotree.org/algorithms/data_structures/graph_as_adjacency_list_python/
# adjacency list representation
main()
"""MULTI-START GREEDY APPROACH"""