BellmanFord Algorithm JavaIn dynamic programming, there are many algorithms to find the shortest path in a graph. Some of them are Dijkstra's algorithm, BFS, DFS, Floyd, allpair shortest path problem, and bidirectional algorithm. The most commonly used algorithm is Dijkstra's algorithm. The limitation of the algorithm is that it cannot be applied if the graph has negative edge weights. Another difference is that the Dijkstra algorithm looks only to the immediate neighbors of a vertex, BellmanFord goes through each edge in every iteration. To overcome this problem, the BellmanFord algorithm can be applied. It deals with the negative edge weights. In this section, we will understand the BellmanFord algorithm with example and also implement the Bellman ford algorithm in a Java program. BellmanFord AlgorithmIt is a singlesource shortest path (minimum weight) algorithm very similar to Dijkstra's algorithm. It can be applied in a graph if we want to find the shortest path. Note that it deals with the negative edge weights. The limitation of the algorithm is that there should not be negative cycles (a cycle whose sum of edges produces a negative value) in the graph. Consider the following graph with cycle. The runtime complexity of the algorithm is O(v*e) and space complexity is O(v). One should use the algorithm if the graph has negative edge weights. Therefore, the BellmanFord algorithm can be applied in the following situations:
The algorithm is slower than Dijkstra's algorithm when all arcs are negative. Working of The BellmanFord AlgorithmThe working of the BellmanFord algorithm is the same as Dijkstra's algorithm. The only difference is that it does not use the priority queue. It repetitively loops over all the edges and updates the distances at the start node, the same as in Dijkstra's algorithm. If a graph G=(V, E) contains a negative weight cycle, then some shortest paths may not exist. BellmanFord algorithm finds all shortest path lengths from a source s ϵ V to all v ϵ V or determines that a negative weight cycle exists. Given a weighted directed graph G(V, E) with source (s) and weight function w: E > R, the algorithm returns a boolean value TRUE if and only if the graph contains no negativeweight cycles that are reachable from the source. The algorithm produces the shortest path and its weights. If there is such a cycle, the algorithm indicates that no solution exists. AlgorithmBellman Ford Pseudo CodeComplexity
Let's understand the algorithm with an example. BellmanFord ExampleConsider the following directed graph (G). Order of edges: (B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D) In the above graph (G), A is the vertex node for all other vertexes. In order to find the shortest path, first, we will initialize the source vertex (A) as 0 and other vertices with infinity (∞). After that, we will traverse towards each vertex from the source node. Update the value of the node during the traversal. We have created the following table for distance updation. From the source vertex A, we can move to vertex B and C. After updating the distances, we get the following graph. From vertex B, we can move to vertex C, D and E. Calculate the distance from B to other vertices, we get After updating the distances, we get the following graph. From vertex C we cannot move to any vertex, so we will visit the next vertex i.e. D. From vertex D, we can move to vertex B and C. Calculate the distance from vertex D to other vertices. Moving D > B, we observe that the vertex B is already has the minimum distance, so we will not update the distance at this time. Moving D> C, we observe that the vertex C already has the minimum distance, so we will not update the distance at this time. After updating the distances, we get the following graph. From vertex E, we can move to vertex D only. Calculate the distance from vertex E to D. After updating the distances, we get the following graph. We observe that values decrease monotonically. BellmanFord Implementation in JavaBellmanFordExample1.java Output: ConclusionFind the shortest path in a graph having nonnegative edges weight is an NPhard problem. For solving such problems, there is no polynomialtime algorithm exists. Consider a scenario, in which each edge has a negative edge weight, we can apply the BellmanFord algorithm. There might be a negativeweight cycle that is reachable from the source. In such a case the algorithm will be terminated. In the same way, if we want to find the longest simple path from source (s) to vertex (v) and the graph has negative cycles, we cannot apply the BellmanFord algorithm.
Next TopicBigDecimal toString() in Java
