0% found this document useful (0 votes)
4 views

Untitled document (5)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Untitled document (5)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Bellman Ford Algorithm

➢Bellman ford algorithm is a single-source shortest path


algorithm.
➢This algorithm is used to find the shortest distance from the
single vertex to all the other vertices of a weighted graph.
➢Various other algorithms are used to find the shortest path like
Dijkstra algorithm, etc.
➢If the weighted graph contains the negative weight values, then
the Dijkstra algorithm does not confirm whether it produces the
correct answer or not.
➢In contrast to Dijkstra algorithm, bellman ford algorithm
guarantees the correct answer even if the weighted graph
contains the negative weight values.

Bellman-Ford is a single source shortest path algorithm. It


effectively works in the cases of negative edges and is able to
detect negative cycles as well. It works on the principle of
relaxation of the edges.

the Bellman-Ford algorithm can be implemented to safely detect


and report the presence of negative cycles.

Rule of this algorithm

​ We will go on relaxing all the edges (n - 1) times where,


​ n = number of vertices
Limitation of Dijkstra’s Algorithm:

Since, we need to find the single source shortest path, we might initially
think of using Dijkstra’s algorithm. However, Dijkstra is not suitable when
the graph consists of negative edges. The reason is, it doesn’t revisit those
nodes which have already been marked as visited. If a shorter path exists
through a longer route with negative edges, Dijkstra’s algorithm will fail to
handle it.

Bellman-Ford Algorithm – O(V*E) Time and O(V) Space

Time Complexity:

● Best Case: O(E), when distance array after 1st and 2nd relaxation

are same , we can simply stop further processing.

● Average Case: O(V*E)

● Worst Case: O(V*E)

Drawbacks of Bellman ford algorithm

○ The bellman ford algorithm does not produce a correct answer if


the sum of the edges of a cycle is negative. Let's understand this
property through an example. Consider the below graph.

You might also like