From c64c4d1f6652ebbfb1676d37829fa9eba0ac7616 Mon Sep 17 00:00:00 2001 From: Francesco Franco <130352141+Francesco601@users.noreply.github.com> Date: Mon, 29 May 2023 13:29:28 +0200 Subject: [PATCH] Bellman-Ford algorithm added to Pull Request Details about the algorithm and time/space complexity in the comments. --- Bellman_Ford.py | 78 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 Bellman_Ford.py diff --git a/Bellman_Ford.py b/Bellman_Ford.py new file mode 100644 index 00000000..6d5ec4e8 --- /dev/null +++ b/Bellman_Ford.py @@ -0,0 +1,78 @@ +# Python program that implements Bellman-Ford's single source +# shortest path algorithm. The algorithm computes the shortest paths +# from a single source vertext to all the other vertices in a weighted +# digraph. It is slower than Dijkstra's algorithm for the same problem, +# but more versatile, as it is capable of handling graphs in which +# some of the edge weigths are negative numbers. Worst case peformace +# is O(V *E) but best case performance can achieve O(E) time. Worst case +# space complexity is O(V). + + +# Class to represent a graph +class Graph: + + def __init__(self, vertices): + self.V = vertices # No. of vertices + self.graph = [] + + # function to add an edge to graph + def addEdge(self, u, v, w): + self.graph.append([u, v, w]) + + # utility function used to print the solution + def printArr(self, dist): + print("Vertex Distance from Source") + for i in range(self.V): + print("{0}\t\t{1}".format(i, dist[i])) + + # The main function that finds shortest distances from src to + # all other vertices using Bellman-Ford algorithm. The function + # also detects negative weight cycle + def BellmanFord(self, src): + + # Step 1: Initialize distances from src to all other vertices + # as INFINITE + dist = [float("Inf")] * self.V + dist[src] = 0 + + # Step 2: Relax all edges |V| - 1 times. A simple shortest + # path from src to any other vertex can have at-most |V| - 1 + # edges + for _ in range(self.V - 1): + # Update dist value and parent index of the adjacent vertices of + # the picked vertex. Consider only those vertices which are still in + # queue + for u, v, w in self.graph: + if dist[u] != float("Inf") and dist[u] + w < dist[v]: + dist[v] = dist[u] + w + + # Step 3: check for negative-weight cycles. The above step + # guarantees shortest distances if graph doesn't contain + # negative weight cycle. If we get a shorter path, then there + # is a cycle. + + for u, v, w in self.graph: + if dist[u] != float("Inf") and dist[u] + w < dist[v]: + print("Graph contains negative weight cycle") + return + + # print all distance + self.printArr(dist) + + +# Driver code +if __name__ == '__main__': + g = Graph(5) + g.addEdge(0, 1, -1) + g.addEdge(0, 2, 4) + g.addEdge(1, 2, 3) + g.addEdge(1, 3, 2) + g.addEdge(1, 4, 2) + g.addEdge(3, 2, 5) + g.addEdge(3, 1, 1) + g.addEdge(4, 3, -3) + + # function call + g.BellmanFord(0) + +