0% found this document useful (0 votes)
13 views21 pages

Lecture 23

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)
13 views21 pages

Lecture 23

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

The Bellman-Ford Shortest Path Algorithm

1
Class Overview
➢ The shortest path problem

➢ Differences

➢ The Bellman-Ford algorithm

➢ Time complexity

2
Shortest Path Problem
➢ Weighted path length (cost): The sum of the weights of all
links on the path.

➢ The single-source shortest path problem: Given a weighted


graph G and a source vertex s, find the shortest (minimum
cost) path from s to every other vertex in G.

3
Differences
➢ Negative link weight: The Bellman-Ford algorithm works;
Dijkstra’s algorithm doesn’t.

➢ Distributed implementation: The Bellman-Ford algorithm


can be easily implemented in a distributed way. Dijkstra’s
algorithm cannot.

➢ Time complexity: The Bellman-Ford algorithm is higher


than Dijkstra’s algorithm.

4
The Bellman-Ford Algorithm

t x
5
6 ,nil -2 ,nil
-3
0 8
s -4 7
2
7
,nil 9 ,nil
y z

5
The Bellman-Ford Algorithm
t x t x
5 5
6 ,nil ,nil 6 6,s -2 ,nil
-2
-3 -3
0 8 0 8
s -4 7 s -4 7
2 2
7 7
,nil ,nil 7,s 9 ,nil
9
y Initialization z y After pass 1 z
t x t x
5 5
6 6,s -2 4,y 6 2,x 4,y
-2
-3 -3
0 8 0 8
s -4 7 s -4 7
2 2
7 7
7,s 9 2,t 7,s 2,t
9
y After pass 2 z
y After pass 3 z
The order of edges examined in each pass:
(t, x), (t, z), (x, t), (y, x), (y, t), (y, z), (z, x), (z, s), (s, t), (s, y)
6
The Bellman-Ford Algorithm

t x
5
6 2,x -2 4,y
-3
0 8
s -4 7
2
7
7,s 9 -2,t
y z

After pass 4

The order of edges examined in each pass:


(t, x), (t, z), (x, t), (y, x), (y, t), (y, z), (z, x), (z, s), (s, t), (s, y)

7
The Bellman-Ford Algorithm
Bellman-Ford(G, w, s)
1. Initialize-Single-Source(G, s)
2. for i := 1 to |V| - 1 do
3. for each edge (u, v)  E do
4. Relax(u, v, w)
5. for each vertex v  u.adj do
6. if d[v] > d[u] + w(u, v)
7. then return False // there is a negative cycle
8. return True
Relax(u, v, w)
if d[v] > d[u] + w(u, v)
then d[v] := d[u] + w(u, v)
parent[v] := u

8
Time Complexity
Bellman-Ford(G, w, s)
1. Initialize-Single-Source(G, s) O(|V|)
2. for i := 1 to |V| - 1 do
3. for each edge (u, v)  E do
O(|V||E|)
4. Relax(u, v, w)
5. for each vertex v  u.adj do O(|E|)
6. if d[v] > d[u] + w(u, v)
7. then return False // there is a negative cycle
8. return True

Time complexity: O(|V||E|)

9
Bellman-Ford
Algorithm
Bellman-Ford Algorithms

• Simple and efficient algorithm to find


the shortest paths
• Shortest Path is from a single source
vertex to all other vertexes
• Used for a weighted, directed graph
• Weights may be negative
Bellman-Ford Algorithms
• Bellman-Ford algorithm returns a Boolean
value indicating whether or not there is a
negative-weight cycle that is reachable from
the source
• If there is such cycle, the algorithm
indicates that no solution exists
• If there is no such cycle, the algorithm
produces the shortest paths and their
weights
• Like Dijkstra’s algorithm, the Bellman-Ford
algorithm uses the technique of relaxation
Operation
• The algorithm initializes the distance
to the source vertex to 0 and all other
vertexes to ∞
• It then does V-1 passes (V is the
number of vertexes) over all edges
relaxing, or updating, the distance to
the destination of each edge
• Finally it checks each edge again to
detect negative weight cycles, in
which case it returns false
Bellman-Ford Algorithm - Operation
• Initialize d and Π
• For each vertex, v, in V
• d[v] = ∞ (is the shortest path estimate)
• Π[v]= Nil (is the predecessor)
• Source distance, d[s] = 0
• Repeat the following process |V| – 1 times:
• For each edge (u, v) in E, relax on the edge (u, v)
• Check for the negative cycle:
• If there exists an edge (u, v) such that
d[v] > d[u] + w[u,v]
then
there exists a negative cycle containing (u, v)
• Initial estimates are all ∞
• No connections
Algorithm
BellmanFord(graph (G,w), vertex s)
InitializeSingleSource(G, s)
for i ← 1 to |V[G] − 1|
do for
For each
each edge(u,v)
vertex ∈ E[G]
v ∈ V[G]
do Relax(u,v,w)
for each ← ∞ ∈ E[G]
edge(u,v)
do d[v]
do if d[v] > d[u] + w(u,v)
Π[v] ←Nil
then return false
return true
d[s] ←0
Algorithm

BellmanFord(graph (G,w), vertex s)


InitializeSingleSource(G, s)
for i ← 1 to |V[G] − 1|
do for each edge(u,v) ∈ E[G]
do Relax(u,v,w)
for each edge(u,v) ∈ E[G]
do if d[v] > d[u] + w(u,v)
then return false
return true
Relaxation

• For Each Vertex v Є V d[v] is


maintained
• d[v] is a shortest path estimate
• Relax(u,v,w)
If d[v] > d[u]+w(u,v)
then d[v] ←d[u]+w(u,v)
Π[v] ←u
Relaxation

• Progressively decreasing an estimate d[v]


on the weight of a shortest path from the
source s to each other vertex v Є V until it
achieves the actual shortest-path weight
• The algorithm returns TRUE if and only if
the graph contains no negative-weight
cycles that are reachable from the source.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Running-Time

• Each of the |V|-1 passes:


O(E)
• “For” loop: O(E)
• Overall-running-time: O(VE)

You might also like