-
Notifications
You must be signed in to change notification settings - Fork 1
/
dij_file_creator.py
67 lines (47 loc) · 1.83 KB
/
dij_file_creator.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
import random
import os
import sys
sys.path.append('..')
sys.path.append('../..')
import argparse
import utils
from student_utils_sp18 import *
from dijkstra import *
from os import listdir
from os.path import isfile, join
import pickle
def graph_creator(adjacency_matrix, number_of_kingdoms):
edge_list = []
for i in range(number_of_kingdoms):
for j in range(i):
weight = adjacency_matrix[i][j]
if weight == "x":
continue
edge_list.append((i,j, weight))
G = nx.Graph()
nodelist = range(number_of_kingdoms)
G.add_weighted_edges_from(edge_list, nodelist=nodelist)
return G
# G = graph_creator(adjacency_matrix, number_of_kingdoms)
# print(all_pairs_dijkstra_path_length(G))
# print(all_pairs_dijkstra_path(G))
input_file_path = "./inputs"
# all_input_files = [f for f in listdir(input_file_path) if isfile(join(input_file_path, f))]
path_dict_name = "path_dict.p"
dist_dict_name = "dist_dict.p"
all_input_files = []
for i in range(726,753):
all_input_files.append(str(i) + ".in")
for file_name in all_input_files:
file_num = file_name.split(".")[0]
input_data = utils.read_file(input_file_path + "/" + file_name)
number_of_kingdoms, list_of_kingdom_names, starting_kingdom, adjacency_matrix = data_parser(input_data)
source_index = list_of_kingdom_names.index(starting_kingdom)
G = graph_creator(adjacency_matrix, number_of_kingdoms)
path_dict = all_pairs_dijkstra_path(G)
dist_dict = all_pairs_dijkstra_path_length(G)
path_file_name = "./dict_poly2/shortest_path_dict/" + file_num + "_" + path_dict_name
dist_file_name = "./dict_poly2/shortest_dist_dict/" + file_num + "_" + dist_dict_name
pickle.dump( path_dict, open( path_file_name, "wb" ), protocol = 2 )
pickle.dump( dist_dict, open( dist_file_name, "wb" ), protocol = 2 )
print(file_num, " done")