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

Bellman Ford Algorithm

The Bellman-Ford algorithm is a single-source shortest path algorithm that can be used to find shortest paths in a weighted graph with positive or negative edge weights, but no negative cycles. It works by relaxing each edge up to |V|-1 times, where |V| is the number of vertices. This allows it to detect any negative cycles that may be present. The time complexity of the Bellman-Ford algorithm is O(|V||E|), where |V| is the number of vertices and |E| is the number of edges.

Uploaded by

abc
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)
137 views

Bellman Ford Algorithm

The Bellman-Ford algorithm is a single-source shortest path algorithm that can be used to find shortest paths in a weighted graph with positive or negative edge weights, but no negative cycles. It works by relaxing each edge up to |V|-1 times, where |V| is the number of vertices. This allows it to detect any negative cycles that may be present. The time complexity of the Bellman-Ford algorithm is O(|V||E|), where |V| is the number of vertices and |E| is the number of edges.

Uploaded by

abc
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/ 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