[WIP] Clean up
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
from itertools import permutations
|
||||||
|
|
||||||
import networkx as nx
|
import networkx as nx
|
||||||
import networkx.algorithms.approximation as nx_app
|
import networkx.algorithms.approximation as nx_app
|
||||||
from networkx.classes.function import path_weight
|
from networkx.classes.function import path_weight
|
||||||
@@ -5,28 +7,19 @@ from networkx.classes.function import path_weight
|
|||||||
with open("files/P9.txt") as f:
|
with open("files/P9.txt") as f:
|
||||||
routes = [line for line in f.read().strip().split("\n")]
|
routes = [line for line in f.read().strip().split("\n")]
|
||||||
|
|
||||||
# print(routes)
|
|
||||||
G = nx.Graph()
|
G = nx.Graph()
|
||||||
for route in routes:
|
for route in routes:
|
||||||
cities, dist = route.split(" = ")
|
cities, dist = route.split(" = ")
|
||||||
from_, to_ = cities.split(" to ")
|
from_, to_ = cities.split(" to ")
|
||||||
# print(from_, to_, dist)
|
|
||||||
G.add_edge(from_, to_, weight=int(dist))
|
G.add_edge(from_, to_, weight=int(dist))
|
||||||
|
|
||||||
# print(G.nodes())
|
|
||||||
|
|
||||||
|
|
||||||
cycle = nx_app.christofides(G, weight="weight")
|
cycle = nx_app.christofides(G, weight="weight")
|
||||||
print(cycle)
|
|
||||||
edge_list = list(nx.utils.pairwise(cycle))
|
|
||||||
print(edge_list)
|
|
||||||
|
|
||||||
paths = []
|
paths = []
|
||||||
for f in cycle:
|
for f, t in permutations(cycle, 2):
|
||||||
for t in cycle:
|
for path in nx.all_simple_paths(G, source=f, target=t):
|
||||||
for path in nx.all_simple_paths(G, source=f, target=t):
|
if len(path) == len(cycle) - 1:
|
||||||
if len(path) == len(cycle) - 1:
|
paths.append(path_weight(G, path, weight="weight"))
|
||||||
paths.append(path_weight(G, path, weight="weight"))
|
|
||||||
|
|
||||||
print(f"Solution {min(paths)}")
|
print(f"Solution part 1: {min(paths)}")
|
||||||
print(f"Solution {max(paths)}")
|
print(f"Solution part 2: {max(paths)}")
|
||||||
|
|||||||
Reference in New Issue
Block a user