From bcf5beda92d9e81d6376bd87eeeed56a5efb4029 Mon Sep 17 00:00:00 2001 From: Joelkurien Date: Mon, 28 Oct 2024 14:02:29 +1100 Subject: [PATCH] handled name --- graphs/johnson_graph.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/graphs/johnson_graph.py b/graphs/johnson_graph.py index 0f5441a15..5bf8f6ddb 100644 --- a/graphs/johnson_graph.py +++ b/graphs/johnson_graph.py @@ -60,7 +60,8 @@ class JohnsonGraph: for vertex_a in self.graph: for vertex_a, vertex_b, weight in self.edges: - if distances[vertex_a] != sys.maxsize - 1 and distances[vertex_a] + weight < distances[vertex_b]: + if (distances[vertex_a] != sys.maxsize - 1 and + distances[vertex_a] + weight < distances[vertex_b]): distances[vertex_b] = distances[vertex_a] + weight return distances @@ -82,14 +83,24 @@ class JohnsonGraph: for i in range(len(self.edges)): vertex_a, vertex_b, weight = self.edges[i] - self.edges[i] = (vertex_a, vertex_b, weight + hash_path[vertex_a] - hash_path[vertex_b]) + self.edges[i] = (vertex_a, + vertex_b, + weight + hash_path[vertex_a] - hash_path[vertex_b]) self.graph.pop("#") - self.edges = [(vertex1, vertex2, node_weight) for vertex1, vertex2, node_weight in self.edges if vertex1 != "#"] + filtered_edges = [] + for vertex1, vertex2, node_weight in self.edges: + if vertex1 != "#": + filtered_edges.append((vertex1, vertex2, node_weight)) + self.edges = filtered_edges for vertex in self.graph: - self.graph[vertex] = [(vertex2, node_weight) for vertex1, vertex2, node_weight in self.edges if vertex1 == vertex] - + filtered_neighbors = [] + for vertex1, vertex2, node_weight in self.edges: + if vertex1 == vertex: + filtered_neighbors.append((vertex2, node_weight)) + self.graph[vertex] = filtered_neighbors + distances = [] for vertex1 in self.graph: new_dist = self.dijkstra(vertex1)