From: Skullheadx <704277@pdsb.net> Date: Wed, 28 Dec 2022 00:20:29 +0000 (-0500) Subject: change name of calculate dist func X-Git-Url: http://git.skullheadx.com/nixos/static/git-logo.png?a=commitdiff_plain;h=a7b91772120052f6c2d0ee8dee34726a70648872;p=The-Traveling-Salesman-Problem.git change name of calculate dist func --- diff --git a/brute_force.py b/brute_force.py index 6800e3c..97244b9 100644 --- a/brute_force.py +++ b/brute_force.py @@ -1,5 +1,5 @@ from queue import Queue -from graph import calculate_distance +from graph import calculate_route def brute_force(graph: list) -> list: @@ -23,7 +23,7 @@ def brute_force(graph: list) -> list: shortest_distance = None shortest_route = [] for route in routes: - distance = calculate_distance(route) + distance = calculate_route(route) if shortest_distance is None or distance < shortest_distance: shortest_distance = distance shortest_route = route diff --git a/graph.py b/graph.py index 06a4705..d30f652 100644 --- a/graph.py +++ b/graph.py @@ -26,27 +26,25 @@ def create(path: str, width: int, height: int, nodes: int): return graph, filename -def distance(x1: int, x2: int, y1: int, y2: int) -> float: - return pow(pow(x1 - x2, 2) + pow(y1 - y2, 2), 0.5) +def distance(town1: tuple, town2: tuple) -> float: + return pow(pow(town1[0] - town2[0], 2) + pow(town1[1] - town2[1], 2), 0.5) def get_distances(graph: list) -> dict: distances = dict() - for i in graph: - distances[i] = dict() - x1, y1 = i - for j in graph: - x2, y2 = j - distances[i][j] = distance(x1, x2, y1, y2) + for town1 in graph: + distances[town1] = dict() + for town2 in graph: + distances[town1][town2] = distance(town1, town2) return distances -def calculate_distance(route: list) -> float: - x1, y1 = route[0] - x2, y2 = route[-1] - d = distance(x1, x2, y1, y2) +def calculate_route(route: list) -> float: + town1 = route[0] + town2 = route[-1] + d = distance(town1, town2) for i, node in enumerate(route[:-1]): - x2, y2 = route[i + 1] - d += distance(x1, x2, y1, y2) - x1, y1 = x2, y2 + town2 = route[i + 1] + d += distance(town1, town2) + town1 = town2 return d