-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
58 lines (40 loc) · 1.59 KB
/
test.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
from itertools import combinations
import networkx as nx
import json as JSON
import matplotlib.pyplot as plt
def save_json(dict, filename):
with open(filename, "w") as file:
JSON.dump(dict, file)
def get_graph(locations, edges):
G = nx.Graph()
G.add_nodes_from(locations)
G.add_edges_from(edges)
return G
def save_graph_png(G, filename):
pos = nx.spring_layout(G)
nx.draw(G, pos, with_labels=True, font_weight='bold', node_size=700, node_color='skyblue', font_size=8)
plt.savefig(filename)
plt.show()
def get_all_possible_routes(G, locations):
combinations_list = list(combinations(locations, 2))
print(combinations_list)
all_possible_routes = {}
for i in combinations_list:
start_location, end_location = i
all_routes = list(nx.all_simple_paths(G, source=start_location, target=end_location))
all_routes = sorted(all_routes, key= lambda x : len(x))
all_possible_routes[f"{start_location}-{end_location}"] = all_routes
return all_possible_routes
if __name__ == "__main__":
locations = ["A", "B", "C", "D", "E", "F", "G", "H"]
edges = [("A", "B"),("B", "G"),("G", "H"),("A", "D"),("D", "H"),("A", "C"),("C", "E"),("E", "F"),("F", "H"), ("E", "D")]
G = get_graph(locations, edges)
# save_graph_png(G, "test_1.png")
all_possible_routes = get_all_possible_routes(G, locations)
# save_json(all_possible_routes, "test_routes.json")
s = ""
count = 1
for i in all_possible_routes.keys():
s+=f"'{i}' : '{count}',\n"
count += 1
print(s)