0% found this document useful (0 votes)
46 views36 pages

Bellman Ford

- Bellman-Ford is an algorithm that finds the shortest paths from a source node to all other nodes in a graph with positive or negative edge weights. - It can detect negative weight cycles that occur when the total weight of a cycle is negative. - If Bellman-Ford does not converge after running V-1 times, where V is the number of vertices, then there must be a negative weight cycle present.

Uploaded by

vishalatdwork573
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)
46 views36 pages

Bellman Ford

- Bellman-Ford is an algorithm that finds the shortest paths from a source node to all other nodes in a graph with positive or negative edge weights. - It can detect negative weight cycles that occur when the total weight of a cycle is negative. - If Bellman-Ford does not converge after running V-1 times, where V is the number of vertices, then there must be a negative weight cycle present.

Uploaded by

vishalatdwork573
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/ 36

Bellman Ford

Negative Edge
• A negative edge is simply an edge having a negative weight.
• For example, the edge X-V in the graph is a negative edge. For a negative
weight you could simply perform the calculation as you would have done for
positive weight edges. u v
5
–2
6 –3
8
z 7
–4
7 2

9
x y
Negative Weight Cycle..
• What is the shortest path between A and E?
• You might at first feel as if its ABCE costing 6 ( 2+1+3 ). But actually, taking a
deeper look, you would observe a negative cycle, which is BCD. The weight of
BCD is 1+(-4)+2 = (-1).
• While traversing from A to E, i could keep cycling around inside BCD to reduce
my cost by 1 each time. Like, the path A(BCD)BCE costs 5 (2+(-1)+1+3).
• Now repeating the cycle infinite times would keep reducing the cost by 1 each
time. I could achieve a negative infinite shortest path between A and E.
• The problem is evident for any negative cycle in a graph. Hence, whenever a
negative cycle is present, the minimum weight is not defined or is negative
infinity.
Bellman-Ford returns a compact representation of the set of
shortest paths from s to all other vertices in the graph reachable
from s. This is contained in the predecessor subgraph.

If Bellman-Ford has not converged after V(G) - 1 iterations, then


there cannot be a shortest path tree, so there must be a negative
weight cycle.
Can have negative-weight edges.
Will “detect” reachable negative-weight cycles.
Example Order of Edges
(U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X)

u v
5
 
–2
6 Vertex D[v] ∏[v]
–3
Z 0 Nil
8
U ∞ Nil
z 0 7
–4 V ∞ Nil

7 2 X ∞ Nil
Y ∞ Nil
 
9
x y
Example Order of Edges
(U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X)

u v
5
6 
–2
6 Vertex D[v] ∏[v]
–3
Z 0 Nil
8
U 6 z
z 0 7
–4 V ∞ Nil

7 2 X ∞ Nil
Y ∞ Nil
7 
9
x y

Pass 1
Example Order of Edges
(U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X)

u v
5
6 
–2
6 Vertex D[v] ∏[v]
–3
Z 0 Nil
8
U 6 z
z 0 7
–4 V ∞ Nil

7 2 X 7 z
Y ∞ Nil
7 
9
x y

Pass 1
Example Order of Edges
(U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X)

u v
5
6 11
–2
6 Vertex D[v] ∏[v]
–3
Z 0 Nil
8
U 6 z
z 0 7
–4 V 11 U

7 2 X 7 z
Y ∞ Nil
7 
9
x y

Pass 2
Example Order of Edges
(U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X)

u v
5
6 11
–2
6 Vertex D[v] ∏[v]
–3
Z 0 Nil
8
U 6 z
z 0 7
–4 V 11 U

7 2 X 7 z
Y 2 U
7 2
9
x y

Pass 2
Example Order of Edges
(U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X)

u v
5
6 4
–2
6 Vertex D[v] ∏[v]
–3
Z 0 Nil
8
U 6 z
z 0 7
–4 V 4 X

7 2 X 7 z
Y 2 U
7 2
9
x y

Pass 2
Example Order of Edges
(U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X)

u v
5
2 4
–2
6 Vertex D[v] ∏[v]
–3
Z 0 Nil
8
U 2 v
z 0 7
–4 V 4 X

7 2 X 7 z
Y 2 U
7 2
9
x y

Pass 3
Example Order of Edges
(U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X)

u v
5
2 4
–2
6 Vertex D[v] ∏[v]
–3
Z 0 Nil
8
U 2 v
z 0 7
–4 V 4 X

7 2 X 7 z
Y -2 U
7 -2
9
x y

Pass 4
Example Order of Edges
(U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X)

“detect” reachable negative- u v


weight cycles. 5
2 4
–2
6 Vertex D[v] ∏[v]
–3
Z 0 Nil
8
U 2 v
z 0 7
–4 V 4 X

7 2 X 7 z
Y -2 U
7 -2
9
x y
Example Order of Edges
(U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X)

“detect” reachable negative- u v


weight cycles. 5
2 4
–2
6 Vertex D[v] ∏[v]
–3
Z 0 Nil
8
U 2 v
z 0 7
–4 V 4 X

7 2 X 7 z
Y -2 U
7 -2
9
x y
Example Order of Edges
(U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X)

“detect” reachable negative- u v


weight cycles. 5
2 4
–2
6 Vertex D[v] ∏[v]
–3
Z 0 Nil
8
U 2 v
z 0 7
–4 V 4 X

7 2 X 7 z
Y -2 U
7 -2
9
x y
Example Order of Edges
(U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X)

“detect” reachable negative- u v


weight cycles. 5
2 4
–2
6 Vertex D[v] ∏[v]
–3
Z 0 Nil
8
U 2 v
z 0 7
–4 V 4 X

7 2 X 7 z
Y -2 U
7 -2
9
x y
Example Order of Edges
(U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X)

“detect” reachable negative- u v


weight cycles. 5
2 4
–2
6 Vertex D[v] ∏[v]
–3
Z 0 Nil
8
U 2 v
z 0 7
–4 V 4 X

7 2 X 7 z
Y -2 U
7 -2
9
x y
Example Order of Edges
(U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X)

“detect” reachable negative- u v


weight cycles. 5
2 4
–2
6 Vertex D[v] ∏[v]
–3
Z 0 Nil
8
U 2 v
z 0 7
–4 V 4 X

7 2 X 7 z
Y -2 U
7 -2
9
x y
Example Order of Edges
(U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X)

“detect” reachable negative- u v


weight cycles. 5
2 4
–2
6 Vertex D[v] ∏[v]
–3
Z 0 Nil
8
U 2 v
z 0 7
–4 V 4 X

7 2 X 7 z
Y -2 U
7 -2
9
x y
Example Order of Edges
(U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X)

“detect” reachable negative- u v


weight cycles. 5
2 4
–2
6 Vertex D[v] ∏[v]
–3
Z 0 Nil
8
U 2 v
z 0 7
–4 V 4 X

7 2 X 7 z
Y -2 U
7 -2
9
x y
Example Order of Edges
(U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X)

“detect” reachable negative- u v


weight cycles. 5
2 4
–2
6 Vertex D[v] ∏[v]
–3
Z 0 Nil
8
U 2 v
z 0 7
–4 V 4 X

7 2 X 7 z
Y -2 U
7 -2
9
x y
Example Order of Edges
(U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X)

“detect” reachable negative- u v


weight cycles. 5
2 4
–2
6 Vertex D[v] ∏[v]
–3
Z 0 Nil
8
U 2 v
z 0 7
–4 V 4 X

7 2 X 7 z
Y -2 U
7 -2
9
x y
Example Order of Edges
(U, V), (U,X), (U,Y), (V,U), (X.V), (X,Y), (Y,V), (Y,Z), (Z,U), (Z,X)

No reachable negative-weight u v
cycles. 5
2 4
–2
6 Vertex D[v] ∏[v]
–3
Z 0 Nil
8
U 2 v
z 0 7
–4 V 4 X

7 2 X 7 z
Y -2 U
7 -2
9
x y
Example
Bellman Ford Algorithm Analysis
O(V)

O(E) O(VE)
O(1)

O(E)
Repeat V-1 times: relax all E edges.

Initialize
Pass 1
Pass 1
Pass 1
Pass 1
Pass 2
Pass 2
Pass 2
Pass 3

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy