import re from findBestPath import findShortestPath, findLongestPath from graph import Graph graph = Graph() with open("in.txt") as file: for line in file: match = re.match(r'^(.*) to (.*) = (.*)$', line, re.I|re.M) graph.addDistance(match.group(1), match.group(2), int(match.group(3))) def printPath(path): output = path[0] for city in path[1:]: output += " -> " + city return output # Part 1 (length, path) = (findShortestPath(graph.getCitiesList(), graph.getDistancesMatrix())) print("the shortest path is {} ==> {} km".format(printPath(path), length)) # Part 2 (length, path) = (findLongestPath(graph.getCitiesList(), graph.getDistancesMatrix())) print("the longest path is {} ==> {} km".format(printPath(path), length))
import re import time sys.path.insert(0, '../') from graph import Graph from findBestPath import findShortestPath, findLongestPath from test import test graph = Graph() graph.addDistance("a", "b", 5) test("cities list", graph.getCitiesList(), ["a", "b"]) test("distances matrix", graph.getDistancesMatrix(), { "a": { "b": 5, "a": 0 }, "b": { "a": 5, "b": 0 } }) test("a -> b", graph.getDistancesMatrix()["a"]["b"], 5) (length, path) = findShortestPath(graph.getCitiesList(), graph.getDistancesMatrix()) test("shortest path a -> b", length, 5) (length, path) = findLongestPath(graph.getCitiesList(), graph.getDistancesMatrix()) test("longest path a -> b", length, 5) graph.addDistance("a", "c", 3) graph.addDistance("b", "c", 4) (length, path) = findShortestPath(graph.getCitiesList(), graph.getDistancesMatrix()) test("shortest path a -> c -> b", length, 7) (length, path) = findLongestPath(graph.getCitiesList(), graph.getDistancesMatrix()) test("longest path a -> b -> c", length, 9) graph.addDistance("a", "d", 15) graph.addDistance("b", "d", 8) graph.addDistance("c", "d", 6) (length, path) = findShortestPath(graph.getCitiesList(), graph.getDistancesMatrix())