0% found this document useful (0 votes)
58 views14 pages

Bellman Ford Alg

The Bellman Ford algorithm is a single-source shortest path algorithm that can find the shortest paths from a single source vertex to all other vertices in a weighted graph, even if negative edge weights are present. It works by relaxing all edges up to n-1 times, where n is the number of vertices, to calculate the shortest distances. The algorithm can detect negative cycles in a graph if one exists.

Uploaded by

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

Bellman Ford Alg

The Bellman Ford algorithm is a single-source shortest path algorithm that can find the shortest paths from a single source vertex to all other vertices in a weighted graph, even if negative edge weights are present. It works by relaxing all edges up to n-1 times, where n is the number of vertices, to calculate the shortest distances. The algorithm can detect negative cycles in a graph if one exists.

Uploaded by

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

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.
• There are various other algorithms 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.
Rule of this algorithm
• We will go on relaxing all the edges (n - 1) times where,
• n = number of vertices

• Edge relaxation:
Bellman Ford Pseudocode
Example

Vertex A B C D E F
Distance 0 ∞ ∞ ∞ ∞ ∞

Parent -- -- -- --- -- --
Identify the all edge pairs
• From the graph, the edge pairs:
• From vertex A, edge pairs are: (A,B) (A,C) (A,D).
• From vertex B, edge pairs: (B,E)
• From vertex C, edge pairs: (C,B) (C,E)
• From vertex D, edge pairs: (D,C) (D,F)
• From vertex E, edge pairs: (E,F)
• From vertex F, edge pairs: ---
Do the 1st iteration

From vertex A, edge pairs are: (A,B) (A,C) (A,D).

Vertex A B C D E F
Distance 0 6 4 5 ∞ ∞

Parent -- A A A -- --
Do the 2nd iteration

From vertex A, edge pairs are: (A,B) (A,C) (A,D).


From vertex B, edge pairs: (B,E)
From vertex C, edge pairs: (C,B) (C,E)
From vertex D, edge pairs: (D,C) (D,F)

Vertex A B C D E F
Distance 0 2 3 5 5 4

Parent -- C D A B D
Do the 3rd iteration

From vertex A, edge pairs are: (A,B) (A,C) (A,D).


From vertex B, edge pairs: (B,E)
From vertex C, edge pairs: (C,B) (C,E)
From vertex D, edge pairs: (D,C) (D,F) and From vertex E, edge pairs: (E,F)

Vertex A B C D E F
Distance 0 1 3 5 1 4

Parent -- C D A B D
Do the 4th iteration

From vertex A, edge pairs are: (A,B) (A,C) (A,D).


From vertex B, edge pairs: (B,E)
From vertex C, edge pairs: (C,B) (C,E)
From vertex D, edge pairs: (D,C) (D,F) and From vertex E, edge pairs: (E,F)

Vertex A B C D E F
Distance 0 1 3 5 0 3

Parent -- C D A B E
Do the 5th iteration

From vertex A, edge pairs are: (A,B) (A,C) (A,D).


From vertex B, edge pairs: (B,E)
From vertex C, edge pairs: (C,B) (C,E)
From vertex D, edge pairs: (D,C) (D,F) and From vertex E, edge pairs: (E,F)

Vertex A B C D E F
Distance 0 1 3 5 0 3

Parent -- C D A B 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.

Total cost for


cycle is:
-6+3+2= -1
• Negative cycle is: 1 2  3
Cost is: -3-1+2 = -2

• If a graph contains a "negative cycle" (i.e. a cycle whose edges


sum to a negative value) that is reachable from the source, then
there is no cheapest path.
• Any path that has a point on the negative cycle can be made
cheaper by one more walk around the negative cycle. In such a
case, the Bellman–Ford algorithm can detect and report the
negative cycle.
Time & Space Complexity
Best case time complexity O(E)

Average case time complexity O(V * E)

Worst case time complexity O(V * E)

If the given graph is complete graph, the maximum


time complexity is : O(n³)

Space Complexity O(V)

You might also like