Lecture#4 & 5 Shortest Path (Bellman Ford & Dijkstra)
Lecture#4 & 5 Shortest Path (Bellman Ford & Dijkstra)
Shortest Paths
Chapter 24
Shortest Path Problems
• How can we find the shortest route between two
points on a road map?
• Model the problem as a graph problem:
– Road map is a weighted graph:
vertices = cities
edges = road segments between cities
edge weights = road distances
– Goal: find a shortest path between two vertices (cities)
2
Shortest Path Problem
• Input: t x
6
3 9
– Directed graph G = (V, E) 3
4
2 1
– Weight function w : E → R s 0
2 7
5 3
• Weight of path p = v0, v1, . . . , vk 5 11
k 6
w( p ) w( vi 1 , vi ) y z
i 1
∞ otherwise
• Note: there might be multiple shortest paths from u to v
3
Variants of Shortest Path
• Single-source shortest paths
– G = (V, E) find a shortest path from a given source
vertex s to each vertex v V
• Single-destination shortest paths
– Find a shortest path to a given destination vertex t
from each vertex v
– Reversing the direction of each edge single-source
4
Variants of Shortest Paths (cont’d)
• Single-pair shortest path
– Find a shortest path from u to v for given vertices u
and v
• All-pairs shortest-paths
– Find a shortest path from u to v for every pair of
vertices u and v
5
Negative-Weight Edges
a b
• Negative-weight edges may form -4
3 4
c d g
negative-weight cycles 5
6
8
s 0
-3
y
• If such cycles are reachable from 2 3 7
e -6 f
the source, then δ(s, v) is not properly
defined!
– Keep going around the cycle, and get
6
Negative-Weight Edges
8
Cycles
• Can shortest paths contain cycles?
• Negative-weight cycles No!
– Shortest path is not well defined
• Positive-weight cycles: No!
– By removing the cycle, we can get a shorter path
• Zero-weight cycles
– No reason to use them
– Can remove them to obtain a path with same weight
9
Algorithms
• Dijkstra’s algorithm
– Negative weights are not allowed
• Bellman-Ford algorithm
– Negative weights are allowed
– Negative cycles reachable from the source
are not allowed.
• Operations common in both algorithms:
– Initialization
– Relaxation
10
Shortest-Paths Notation
For each vertex v V:
t x
• δ(s, v): shortest-path weight 3
6
9
3
• d[v]: shortest-path weight estimate 2 1
4
s 0
– Initially, d[v]=∞ 2 7
5 3
– d[v]δ(s,v) as algorithm progresses 5 11
6
[v] = predecessor of v on a shortest y z
path from s
– If no predecessor, [v] = NIL
induces a tree—shortest-path tree
11
Initialization
Alg.: INITIALIZE-SINGLE-SOURCE(V, s)
1. for each v V
2. do d[v] ←
3. [v] ← NIL
4. d[s] ← 0
12
Relaxation Step
• Relaxing an edge (u, v) = testing whether we
can improve the shortest path to v found so far
by going through u
If d[v] > d[u] + w(u, v)
we can improve the shortest path to v
d[v]=d[u]+w(u,v)
[v] ← u After relaxation:
s s d[v] d[u] +
u v u v w(u, v)
2 2
5 9 5 6
RELAX(u, v, w) RELAX(u, v, w)
u v u v
2 2
5 7 5 6 no change
13
Dijkstra’s Algorithm
• Single-source shortest path problem:
– No negative-weight edges: w(u, v) > 0, (u, v) E
15
Dijkstra (G, w, s)
16
Example (cont.)
t 1 x t 1 x
8
10 14
8 13
14
10 9 10 9
2 3 4 6 2 3 4 6
s 0 s 0
5 7 5 7
5 7
5 7
2 2
y z y z
17
Example (cont.)
t x t 1 x
1
8 13
9 8 9
10 9 10 9
2 4 2 3 4 6
s 0 3 6 s 0
7 5 7
5
5 7 5 7
2 2
y z y z
18
Dijkstra (G, w, s)
1. INITIALIZE-SINGLE-SOURCE(V, s) (V)
2. S←
3. Q ← V[G] O(V) build min-heap
4. while Q Executed O(V) times
O(lgV) O(VlgV)
5. do u ← EXTRACT-MIN(Q)
6. S ← S {u}
7. for each vertex v Adj[u] O(E) times
(total) O(ElgV)
8. do RELAX(u, v, w)
9. Update Q (DECREASE_KEY) O(lgV)
s u 1 w
1
1 -1
1st iteration v
– d[y]=4, d[x]= 5
• Pass 3 -> S=<wyx>, Q=<z>
2
y z
– d[x]= 5, d[z]=8
– Selecting z results in empty Q, so algorithm
terminates
– But…
– D[y] can be set to d[x]+c[x,y] = -1 but as we have
visited y we cannot update it again!
Bellman-Ford Algorithm
t 5 x Pass 1 t 5 x
6
6 -2 6 -2
-3 -3
8 7 8 7
s 0 s 0
-4 -4
7 2 7 2
7
9 9
y z y z
E: (t, x), (t, y), (t, z), (x, t), (y, x), (y, z), (z, x), (z, s), (s, t), (s, y)
Example (t, x), (t, y), (t, z), (x, t), (y, x), (y, z), (z, x), (z, s), (s, t), (s, y)
t 5 x t x
Pass 1 5
6 Pass 2
(from 6
4
11
6 -2 6 -2
previous -3 -3
slide) s 0 8 7 8 7
s 0
-4 -4
7 2 7 2
7 7 2
9 9
y z y z
Pass 3 t 5 x Pass 4 t 5 x
2
6
4
11 2
6
4
11
6 -2 6 -2
-3 -3
8 7 8 7
s 0 s 0
-4 -4
7 2 7 2
7 2
7 2
-2
9 9
y z y z
BELLMAN-FORD(V, E, w, s)
1. INITIALIZE-SINGLE-SOURCE(V, s) (V)
2. for i ← 1 to |V| - 1 O(V)
O(VE)
3. do for each edge (u, v) E O(E)
4. do RELAX(u, v, w)
5. for each edge (u, v) E O(E)
6. do if d[v] > d[u] + w(u, v)
7. then return FALSE
8. return TRUE
4 -6 3
2
y z
w 5 x
0
4 -6 3
2
y z