2020-09-23 11:30:13 +00:00
|
|
|
from __future__ import annotations
|
2019-11-19 21:52:55 +00:00
|
|
|
|
|
|
|
|
2021-06-24 06:50:23 +00:00
|
|
|
def print_distance(distance: list[float], src):
|
|
|
|
print(f"Vertex\tShortest Distance from vertex {src}")
|
|
|
|
for i, d in enumerate(distance):
|
|
|
|
print(f"{i}\t\t{d}")
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-07-17 04:07:25 +00:00
|
|
|
|
2021-06-24 06:50:23 +00:00
|
|
|
def check_negative_cycle(
|
|
|
|
graph: list[dict[str, int]], distance: list[float], edge_count: int
|
|
|
|
):
|
|
|
|
for j in range(edge_count):
|
|
|
|
u, v, w = [graph[j][k] for k in ["src", "dst", "weight"]]
|
|
|
|
if distance[u] != float("inf") and distance[u] + w < distance[v]:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def bellman_ford(
|
|
|
|
graph: list[dict[str, int]], vertex_count: int, edge_count: int, src: int
|
|
|
|
) -> list[float]:
|
2020-03-04 12:40:28 +00:00
|
|
|
"""
|
2020-05-22 06:10:11 +00:00
|
|
|
Returns shortest paths from a vertex src to all
|
2019-11-19 21:52:55 +00:00
|
|
|
other vertices.
|
2021-06-24 06:50:23 +00:00
|
|
|
>>> edges = [(2, 1, -10), (3, 2, 3), (0, 3, 5), (0, 1, 4)]
|
|
|
|
>>> g = [{"src": s, "dst": d, "weight": w} for s, d, w in edges]
|
|
|
|
>>> bellman_ford(g, 4, 4, 0)
|
|
|
|
[0.0, -2.0, 8.0, 5.0]
|
|
|
|
>>> g = [{"src": s, "dst": d, "weight": w} for s, d, w in edges + [(1, 3, 5)]]
|
|
|
|
>>> bellman_ford(g, 4, 5, 0)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
Exception: Negative cycle found
|
2019-11-19 21:52:55 +00:00
|
|
|
"""
|
2021-06-24 06:50:23 +00:00
|
|
|
distance = [float("inf")] * vertex_count
|
|
|
|
distance[src] = 0.0
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2021-06-24 06:50:23 +00:00
|
|
|
for i in range(vertex_count - 1):
|
|
|
|
for j in range(edge_count):
|
|
|
|
u, v, w = [graph[j][k] for k in ["src", "dst", "weight"]]
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2021-06-24 06:50:23 +00:00
|
|
|
if distance[u] != float("inf") and distance[u] + w < distance[v]:
|
|
|
|
distance[v] = distance[u] + w
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2021-06-24 06:50:23 +00:00
|
|
|
negative_cycle_exists = check_negative_cycle(graph, distance, edge_count)
|
|
|
|
if negative_cycle_exists:
|
|
|
|
raise Exception("Negative cycle found")
|
2019-07-17 04:07:25 +00:00
|
|
|
|
2021-06-24 06:50:23 +00:00
|
|
|
return distance
|
2019-07-17 04:07:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-06-24 06:50:23 +00:00
|
|
|
import doctest
|
|
|
|
|
|
|
|
doctest.testmod()
|
|
|
|
|
2019-07-17 04:07:25 +00:00
|
|
|
V = int(input("Enter number of vertices: ").strip())
|
|
|
|
E = int(input("Enter number of edges: ").strip())
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2021-06-24 06:50:23 +00:00
|
|
|
graph: list[dict[str, int]] = [dict() for j in range(E)]
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-11-19 21:52:55 +00:00
|
|
|
for i in range(E):
|
2021-06-24 06:50:23 +00:00
|
|
|
print("Edge ", i + 1)
|
|
|
|
src, dest, weight = [
|
|
|
|
int(x)
|
|
|
|
for x in input("Enter source, destination, weight: ").strip().split(" ")
|
|
|
|
]
|
|
|
|
graph[i] = {"src": src, "dst": dest, "weight": weight}
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2021-06-24 06:50:23 +00:00
|
|
|
source = int(input("\nEnter shortest path source:").strip())
|
|
|
|
shortest_distance = bellman_ford(graph, V, E, source)
|
|
|
|
print_distance(shortest_distance, 0)
|