Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit debba8c

Browse files
committedOct 28, 2024·
Handled names
1 parent 8fc6818 commit debba8c

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed
 

‎graphs/johnson_graph.py

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,54 +14,54 @@ def __init__(self) -> None:
1414
self.graph: dict[str, list[tuple[str, int]]] = {}
1515

1616
# add vertices for a graph
17-
def add_vertices(self, u: str) -> None:
17+
def add_vertices(self, vertex: str) -> None:
1818
"""
1919
Adds a vertex `u` to the graph with an empty adjacency list.
2020
"""
21-
self.graph[u] = []
21+
self.graph[vertex] = []
2222

2323
# assign weights for each edges formed of the directed graph
24-
def add_edge(self, u: str, v: str, w: int) -> None:
24+
def add_edge(self, vertex_a: str, vertex_b: str, weight: int) -> None:
2525
"""
2626
Adds a directed edge from vertex `u` to vertex `v` with weight `w`.
2727
"""
28-
self.edges.append((u, v, w))
29-
self.graph[u].append((v, w))
28+
self.edges.append((vertex_a, vertex_b, weight))
29+
self.graph[vertex_a].append((vertex_b, weight))
3030

3131
# perform a dijkstra algorithm on a directed graph
32-
def dijkstra(self, s: str) -> dict:
32+
def dijkstra(self, start: str) -> dict:
3333
"""
3434
Computes the shortest path from vertex `s`
3535
to all other vertices using Dijkstra's algorithm.
3636
"""
3737
distances = {vertex: sys.maxsize - 1 for vertex in self.graph}
38-
pq = [(0, s)]
39-
distances[s] = 0
38+
pq = [(0, start)]
39+
distances[start] = 0
4040
while pq:
41-
weight, v = heapq.heappop(pq)
41+
weight, vertex = heapq.heappop(pq)
4242

43-
if weight > distances[v]:
43+
if weight > distances[vertex]:
4444
continue
4545

46-
for node, w in self.graph[v]:
47-
if distances[v] + w < distances[node]:
48-
distances[node] = distances[v] + w
46+
for node, node_weight in self.graph[vertex]:
47+
if distances[vertex] + node_weight < distances[node]:
48+
distances[node] = distances[vertex] + node_weight
4949
heapq.heappush(pq, (distances[node], node))
5050
return distances
5151

5252
# carry out the bellman ford algorithm for a node and estimate its distance vector
53-
def bellman_ford(self, s: str) -> dict:
53+
def bellman_ford(self, start: str) -> dict:
5454
"""
5555
Computes the shortest path from vertex `s`
5656
to all other vertices using the Bellman-Ford algorithm.
5757
"""
5858
distances = {vertex: sys.maxsize - 1 for vertex in self.graph}
59-
distances[s] = 0
59+
distances[start] = 0
6060

61-
for u in self.graph:
62-
for u, v, w in self.edges:
63-
if distances[u] != sys.maxsize - 1 and distances[u] + w < distances[v]:
64-
distances[v] = distances[u] + w
61+
for vertex_a in self.graph:
62+
for vertex_a, vertex_b, weight in self.edges:
63+
if distances[vertex_a] != sys.maxsize - 1 and distances[vertex_a] + weight < distances[vertex_b]:
64+
distances[vertex_b] = distances[vertex_a] + weight
6565

6666
return distances
6767

@@ -74,28 +74,28 @@ def johnson_algo(self) -> list[dict]:
7474
all pairs of vertices using Johnson's algorithm.
7575
"""
7676
self.add_vertices("#")
77-
for v in self.graph:
78-
if v != "#":
79-
self.add_edge("#", v, 0)
77+
for vertex in self.graph:
78+
if vertex != "#":
79+
self.add_edge("#", vertex, 0)
8080

81-
n = self.bellman_ford("#")
81+
hash_path = self.bellman_ford("#")
8282

8383
for i in range(len(self.edges)):
84-
u, v, weight = self.edges[i]
85-
self.edges[i] = (u, v, weight + n[u] - n[v])
84+
vertex_a, vertex_b, weight = self.edges[i]
85+
self.edges[i] = (vertex_a, vertex_b, weight + hash_path[vertex_a] - hash_path[vertex_b])
8686

8787
self.graph.pop("#")
88-
self.edges = [(u, v, w) for u, v, w in self.edges if u != "#"]
88+
self.edges = [(vertex1, vertex2, node_weight) for vertex1, vertex2, node_weight in self.edges if vertex1 != "#"]
8989

90-
for u in self.graph:
91-
self.graph[u] = [(v, weight) for x, v, weight in self.edges if x == u]
90+
for vertex in self.graph:
91+
self.graph[vertex] = [(vertex2, node_weight) for vertex1, vertex2, node_weight in self.edges if vertex1 == vertex]
9292

9393
distances = []
94-
for u in self.graph:
95-
new_dist = self.dijkstra(u)
96-
for v in self.graph:
97-
if new_dist[v] < sys.maxsize - 1:
98-
new_dist[v] += n[v] - n[u]
94+
for vertex1 in self.graph:
95+
new_dist = self.dijkstra(vertex1)
96+
for vertex2 in self.graph:
97+
if new_dist[vertex2] < sys.maxsize - 1:
98+
new_dist[vertex2] += hash_path[vertex1] - hash_path[vertex2]
9999
distances.append(new_dist)
100100
return distances
101101

0 commit comments

Comments
 (0)
Please sign in to comment.