Dynamic Programming-2
Dynamic Programming-2
University of Hyderabad
Shortest Path Problems
• Input: t x
6
3 9
– Directed graph G = (V, E)
3
4
– Weight function w : E → R s 0
2 1
2 7
3
• Weight of path p = ⟨v0, v1, . . . , vk⟩5
k 5 11
6
w( p ) w( vi 1 , vi ) y z
i 1
University of Hyderabad
Negative-Weight Edges
• s → e: infinitely many paths: a b
-4
⟨s, e⟩, ⟨s, e, f, e⟩, ⟨s, e, f, e, f, e⟩ 3 -1
3 4
c d g
– cycle ⟨e, f, e⟩ has negative 5
6
8
s 0 5 11 -∞
weight: -3
2 3 7
3 + (- 6) = -3 -∞ -∞
-6 f
– can find paths from s to e with e
• Similarly: 𝛅(s, f) = - ∞,
exists between s and e ∞ from s
University of Hyderabad
Negative-Weight Edges
a b
• Negative-weight edges may form -4
3 4
negative-weight cycles c 6 d g
5 8
s 0
-3
2 3 7
• If such cycles are reachable from
University of Hyderabad
Cycles
• Can shortest paths contain cycles?
• Negative-weight cycles No!
• 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
• We will assume that when we are finding
shortest paths, the paths will have no cycles
University of Hyderabad
Shortest-Path Representation
For each vertex v ∈ V:
t x
• d[v] = δ(s, v): a shortest-path 3
6
9
estimate 3
4
2 1
– Initially, d[v]=∞ s 0
2 7
3
– Reduces as algorithms progress 5
• 𝛑[v] = predecessor of v on a
5 11
6
y z
University of Hyderabad
Relaxation
• 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)
s s
u v u v
5
2
9
2 After relaxation:
5 6
d[v] ≤ d[u] +
RELAX(u, v, w) RELAX(u, v,w(u,
w) v)
u v u v
2 2
5 7 5 6
University of Hyderabad
RELAX(u, v, w)
1. if d[v] > d[u] + w(u, v)
𝛑[v] ← u
2. then d[v] ← d[u] + w(u, v)
3.
University of Hyderabad
Bellman-Ford Algorithm
• Single-source shortest paths problem
– Computes d[v] and 𝛑[v] for all v ∈ V
• Allows negative edge weights and cycles
• Returns:
– TRUE if no negative-weight cycles are
reachable from the source s
– FALSE otherwise ⇒ no solution exists
• Idea:
– Traverse all the edges |V| – 1 times, every time
performing a relaxation step of each edge
University of Hyderabad
BELLMAN-FORD(V, E, w, s)
t x
1. INITIALIZE-SINGLE-SOURCE(V, 5
∞ ∞
s) 6 -2
-3
8
2. for i ← 1 to |V| - 1 s 0
-4
7
8. return TRUE 7 2
∞
7 ∞
9
y z
University of Hyderabad
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
6 4
∞
11
6 -2 6 -2
-3 -3
8 7 8 7
s 0 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
Figure 1
Example 1
University of Hyderabad
Example 1 Sol
University of Hyderabad
Detecting Negative Cycles
• for each edge (u, v) ∈ E s b
• do if d[v] > d[u] + w(u, v) 0
2
∞
• then return FALSE -8 3
• return TRUE ∞
c
University of Hyderabad
Shortest Path Properties
• Upper-bound property
We always have d[v] ≥ δ(s, v) for all v.
Once d[v] = δ(s, v), it never changes.
– The estimate never goes up – relaxation only
lowers the estimate
v 5 x v 5 x
6 4
∞
11 2
6 4
∞
11
6 -2 Relax (x, v) 6 -2
-3 -3
8 7 8 7
s 0 s 0
-4 -4
7 2 7 2
7 2
∞ 7 2
∞
9 9
y z y z
University of Hyderabad
Shortest Path Properties
• No-path property
If there is no path from s to v then d[v] = ∞
always.
– δ(s, h) = ∞ and d[h] ≥ δ(s, h) ⇒ d[h] = ∞
a b
-4 h i
3 -1 2
3 4 ∞ ∞
c 6 d g h, i, j not
5 8
s 0 5 11 -∞ -8 3 reachable
-3 ∞
2
y from s
3 7
University of Hyderabad
Shortest Path Properties
• Convergence property
If s u → v is a shortest path, and if d[u] =
δ(s, u) at any time prior to relaxing edge (u,
v), then
d[v] = δ(s, v) at all times afterward
u v • If d[v] > δ(s, v) ⇒ after
2
5
6 8
∞
11 relaxation:
5
d[v] = d[u] + w(u, v)
s 0 d[v] = 5 + 2 = 7
4
• Otherwise, the value remains
4
7 unchanged, because it must
University have been the shortest path
of Hyderabad
Shortest Path Properties
• Path relaxation property
Let p = ⟨v0, v1, . . . , vk⟩ be a shortest path
from s = v0 to vk. If we relax, in order, (v0,
v1), (v1, v2), . . . , (vk-1, vk), even
intermixed with other relaxations, then d[vk ]
= δ(s, vvk). 1 2 v2
d[v2] = δ(s, v2)
∞
6
5 7
∞
11 v4
5 14
∞
v3 3
d[v1] = δ(s, v1) 4
s 0 ∞
11 d[v4] = δ(s, v4)
University of Hyderabad
All-Pairs Shortest Paths
• Given: 2
3 4
– Directed graph G = (V, E)
8
1 3
– Weight function w : E → R 2
1
• Compute: -4 7 -5
5 4
6
– The shortest paths between all
pairs of vertices in a graph
– Representation of the result: an
n × n matrix of shortest-
path distances δ(u, v)
University of Hyderabad
All-Pairs Shortest Paths
• Assume the graph (G) is given as 2
4
adjacency matrix of weights 3
8
– W = (wij), n x n matrix, |V| = n 1
2
3
– Vertices numbered 1 to n -4 7
1
-5
0 if i = j 5
6
4
∞ if i ≠ j
– No edges allowed
University of Hyderabad
Recursive Solution
• lij(m) = weight of shortest path i j that
at most m edges
contains at most m edges
∞
11
j
i
k
University of Hyderabad
The Structure of a Shortest
Path
3
• Vertices in G are given by 6
0.5
1 2
V = {1, 2, …, n} 3 4
2 1
• Consider a path p = ⟨v1, v2, …, 2
5
vl ⟩
– An intermediate vertex of p is
1 }
pt
University of Hyderabad
Example
dij(k) = the weight of a shortest path from
vertex i to vertex j with all intermediary
vertices drawn from {1, 2, …, k}
• d13(0) = 6
= 6
3
• d13 (1) 6
0.5
1 2
5 4
• d13 (2)
= 3
2 1
5
• d13 (3)
=
4.5
• d13(4) =
University of Hyderabad
A Recursive Solution
• k is not an intermediate vertex of path p k
– Shortest path from i to j with intermediate i j
University of Hyderabad
A Recursive Solution
dij(k) = the weight of a shortest path
from vertex i to vertex j with all
intermediary vertices drawn from {1,
2, …, k}
• k≥1
• Case 1: k is not an
intermediate vertex of path k
p dij(k-1) i j
• dij(k) =
University of Hyderabad
A Recursive Solution
dij(k) = the weight of a shortest path
from vertex i to vertex j with all
intermediary vertices drawn from {1,
2, …, k}
• k≥1
• Case 2: k is an
∞
k
intermediate vertex of path
i
j
p dik(k-1) + dkj(k-1)
• dij(k) =
University of Hyderabad
Computing the Shortest Path
Weights
• dij(k) = wij if k = 0
min {dij(k-1) , dik(k-1) + dkj(k-1) } if
k≥1
• The final solution: D(n) = (dij(n)):
dij = 𝛅(i, j) ∀ i, j ∈ V
(n) j j
+ (k, j)
i i
(i, k)
D(k-1) D(k)
-4
1
-5
3 ∞ 4 0 ∞ ∞ 3 ∞ 4 0 ∞ ∞
7
5 4 4 2 ∞ -5 0 ∞ 4 2 5 -5 0 -2
6
D(2) 5 ∞ ∞ ∞ 6 0 5 ∞ ∞ ∞ 6 0
1 2 3 4 5 D(3) D(4)
1 0 3 8 4 -4 0 3 8 4 -4 0 3 -1 4 -4
2 ∞ 0 ∞ 1 7 ∞ 0 ∞ 1 7 3 0 -4 1 -1
3 ∞ 4 0 5 11 ∞ 4 0 5 11 7 4 0 5 3
4 2 5 -5 0 -2 2 -1 -5 0 -2 2 -1 -5 0 -2
5 ∞ ∞ ∞ 6 0 ∞ ∞ ∞ 6 0 8 5 1 6 0
CS 477/677 - Lecture 24
University of Hyderabad
University of Hyderabad
Example 1
University of Hyderabad
Solution
University of Hyderabad