You are on page 1of 25

Bellman Ford Algorithm

(Single Source Shortest Path Algorithm :


Dynamic Programming)

Prachi Joshi
Bellman Ford Algorithm

-1
2 5

-2 3
6 1

5 7
1 3

-2
3
5

4 6
-1
Bellman Ford Algorithm

-1
2 5 How many times
Relaxation has to be
3
6 -2 1 done??

5 7
1 3 |v|=n=7
Relaxation for |v|-1
times. Hence for
-2
5
3 this graph 6 times!

4 6 Relaxation
-1 if(d[u]+c[u,v] < d[v])
then d[v]=d[u]+c[u,v]
Set of Edges
|E| = {(1,2),(1,3),(1,4),(2,5),(3,2),(3,5),(4,3),(4,6),(5,7),(6,7)}

-1
2 5

-2 3
6 1

5 7
1 3

-2
3
5

4 6
-1
Mark all the distances initially
|E| = {(1,2),(1,3),(1,4),(2,5),(3,2),(3,5),(4,3),(4,6),(5,7),(6,7)}


  ∞
 
-1
2 5

-2 3
6 1

5 7 ∞
 
0 1 3 ∞
 

-2
3
5

4 6
-1

  ∞
 
Ist Cycle of Updation : Apply Relaxation to all edges
|E| = {(1,2),(1,3),(1,4),(2,5),(3,2),(3,5),(4,3),(4,6),(5,7),(6,7)}
6 3 5

  ∞
 
-1
2 5

-2 3
6 1

5 8 7
0 1 3 ∞
  7 ∞
 
5 3

-2 3
5

4 6 4
-1

  ∞
 
5
IInd Cycle of Updation : Apply Relaxation to all edges
|E| = {(1,2),(1,3),(1,4),(2,5),(3,2),(3,5),(4,3),(4,6),(5,7),(6,7)}
1 2
3 5
-1
2 5

-2 3
6 1

5 7 7 5
0 1 3 3

-2 3
5

4 6
-1
5 4
IIIrd Cycle of Updation : Apply Relaxation to all edges
|E| = {(1,2),(1,3),(1,4),(2,5),(3,2),(3,5),(4,3),(4,6),(5,7),(6,7)}
0
1 2
-1
2 5

-2 3
6 1

5 5 3
0 1 3 7
3

-2 3
5

4 6
-1 4
5 4
IVth Cycle of Updation : Apply Relaxation to all edges
|E| = {(1,2),(1,3),(1,4),(2,5),(3,2),(3,5),(4,3),(4,6),(5,7),(6,7)}

1 0
-1
2 5
No Changes!
-2 3
6 1

5 7 3
0 1 3 3

-2 3
5

4 6
-1 4
5 4
Write distance for every vertex in table

v d[v]
1 0
2 1 Some graphs will get
3 3 shoretest path may be in
4 5 first relaxation and some
5 0 graphs will get it in last
relaxation
6 4
7 3
What will be the time complexity of Bellman Ford
Algorithm?
Relaxing Edges
How many edges |E| edges
For How many times |V|- 1

Complexity will be,


O(|E|.|V|-1)
O(|E|.|V|)
O(n.n)
O(n2)
What if given graph is complete graph?

What is the complexity


of Bellman Ford
Algorithm is if applied
on complete graph?

Complexity will be,


O(|E|.|V|-1)
=O(n(n-1)/2.(n-1))
=O(n3)
Another Example for Bellman Ford

4
1 2

5 -10
Edges |E|={(3,2)(4,3)
(1,4)(1,2)
4 3
3
Mark all distances initially

0 ∞
4
1 2

5 -10
Edges |E|={(3,2)(4,3)
(1,4)(1,2)}
4 3
3
∞ ∞
Ist Cycle of Updation

4
0 ∞
4
1 2

5 -10
Edges |E|={(3,2)(4,3)
(1,4)(1,2)
4 3
3
∞ ∞
5
IInd Cycle of Updation

0 4
4
1 2

5 -10 Edges |E|={(3,2)(4,3)


(1,4)(1,2)
4 3
3
5 ∞
8
IIIrd Cycle of Updation
-2
0 4
4
1 2

5 -10 Edges |E|={(3,2)(4,3)


(1,4)(1,2)
4 3
3
5 8
Write distance for every vertex in table

v d[v]
1 0
2 -2
3 8
4 5
Change previous graph by adding one more
edge

4
1 2

5 5 -10
Edges |E|={(3,2)(4,3)(1,4)(1,2)
(2,4)}
4 3
3
Mark all distances initially

0 ∞
4
1 2

5 5 -10
Edges |E|={(3,2)(4,3)(1,4)
(1,2)(2,4)}
4 3
3
∞ ∞
Ist Cycle of Updation

4
0 ∞
4
1 2

5 5 -10
Edges |E|={(3,2)(4,3)(1,4)
(1,2)(2,4)}
4 3
3
∞ ∞
5
IInd Cycle of Updation

0 4
4
1 2

5 5 -10
Edges |E|={(3,2)(4,3)(1,4)
(1,2)(2,4)}
4 3
3
5 ∞
8
IIIrd Cycle of Updation

-2
0 4
4
1 2

5 5 -10
Edges |E|={(3,2)(4,3)(1,4)
(1,2)(2,4)}
4 3
3
5 8
3
Try One More Updation…

0 -2
4
1 2

5 5 -10
Edges |E|={(3,2)(4,3)(1,4)
(1,2)(2,4)}
4 3
3
3 8
6
Its happening because of negative weight
cycle!

0 -2
4 2
1 2

5
5 5 -10 -10

4 3 4 3
3 3
3 8
6
Total Weight = 5+3-10=-2

You might also like