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

Dynamic Programming-2

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)
5 views

Dynamic Programming-2

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

Algorithms

Shortest Path Problems


• How can we find the shortest route between
two points on a 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)

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

• Shortest-path weight from u to v:


p
δ(u, v) = min w(p) : u v if there exists a path from u
to v
∞ otherwise
University of Hyderabad
• Shortest path u to v is any path p such that w(p) = δ(u,
Variants of Shortest Paths
• Single-source shortest path
– G = (V, E) ⇒ find a shortest path from a given source
vertex s to each vertex v ∈ V
• Single-destination shortest path
– Find a shortest path to a given destination vertex t from
each vertex v
– Reverse the direction of each edge ⇒ single-source
• Single-pair shortest path
– Find a shortest path from u to v for given vertices u and
v
– Still have to solve the single-source problem
• All-pairs shortest-paths
– Find a shortest path from u to v for every pair of vertices
u and v University of Hyderabad
Negative-Weight Edges
• s → a: only one path What if we have negative-
𝛅(s, a) = w(s, a) = 3 weight edges?
a b
• s → b: only one path 3
-4
-1

𝛅(s, b) = w(s, a) + w(a, b) = -1


3 4
c 6 d g
5 8
s 0 5 11 -∞
-3
• s → c: infinitely many paths 2 3 7
-∞ -∞
⟨s, c⟩, ⟨s, c, d, c⟩, ⟨s, c, d, c, d, c⟩ e -6 f

cycle ⟨c, d, c⟩ has positive weight (6 - 3 = 3)


⟨s, c⟩ is shortest path with weight 𝛅(s, b) = w(s, c) = 5

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

arbitrarily large negative h i


2
weights
– 𝛅(s, e) = - ∞ ⇒ no shortest path
∞ ∞
h, i, j not
-8 3 reachable

• Similarly: 𝛅(s, f) = - ∞,
exists between s and e ∞ from s

𝛅(s, h) = 𝛅(s, i) = 𝛅(s, j) = ∞


𝛅(s, g) = - ∞
j

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

the source: 𝛅(s, v) is not properly


e -6 f

defined for any node v on the cycle


– Keep going around the cycle, and get

w(s, v) = - ∞ for all v on the cycle

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

shortest path from s


– If no predecessor, 𝛑[v] = NIL
– 𝛑 induces a tree—shortest-path
tree
• Shortest paths & shortest path
University of Hyderabad
Initialization
Alg.: INITIALIZE-SINGLE-SOURCE(V, s)
1. for each v ∈ V
2. do d[v] ← ∞
3. 𝛑[v] ← NIL
4. d[s] ← 0

• All the shortest-paths algorithms start


with INITIALIZE-SINGLE-SOURCE

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)

⇒ update d[v] and 𝛑[v]


we can improve the shortest path to 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.

• All the single-source shortest-paths


algorithms
– start by calling INIT-SINGLE-SOURCE
– then relax edges
• The algorithms differ in the order and
how many times they relax each edge

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

3. do for each edge (u, v) ∈ E 7 2


∞ ∞
4. do RELAX(u, v, w) y
9
z
5. for each edge (u, v) ∈ E t 5 x
6
∞ ∞
6. do if d[v] > d[u] + w(u, v) 6 -2
-3
8
7. then return FALSE 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

s b s b Look at edge (s, b):


2 2
0
-3 ∞
2 -6
-3 -1
2
d[b] = -1
-8 3 -8 3

5 5
2 d[s] + w(s, b) = -4
c c
⇒ d[b] > d[s] + w(s,
b)
University of Hyderabad
Shortest Path Properties
• Triangle inequality s
u v
For all (u, v) ∈ E, we have: 5
2
7

δ(s, v) ≤ δ(s, u) + w(u, v)


s
u v
2
5 6

• If u is on the shortest path to v we have the


equality sign

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

𝛅(s, h) = 𝛅(s, i) = 𝛅(s, j) = ∞


-∞ -∞ j
e -6 f

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)

d[v3] = δ(s, v3)

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

wij = weight of (i, j) if i ≠ j , (i, j) ∈ E


∞ if i ≠ j , (i, j) ∉ E
• Output the result in an n x n matrix
D = (dij), where dij = δ(i, j)
• Solve the problem using dynamic
programming University of Hyderabad
Optimal Substructure of a Shortest
Path
at most m edges
• All subpaths of a

11
j
shortest path are i
k
shortest paths
at most m - 1 edges
• Let p: a shortest path
from vertex i to j that • If i ≠ j: p = ip’ k→j
– p’ has at most m-1
contains at most m
edges
edges – p’ is a shortest path
• If i = j δ(i, j) =δ(i, k) + wkj
– w(p) = 0 and p has no of Hyderabad
University
Recursive Solution
• lij(m) = weight of shortest path i j that
at most m edges
contains at most m edges

11
j
i
• m = 0: lij (0)
= 0 if i = j k

∞ if i ≠ j
– No edges allowed

• m = 1: lij(1) wij L(1) = W


=
– The path between i and j is restricted to 1 edge

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

• m > 1: lij(m) min


= { l ij
(m-1) ,min {l (m-1) + w } }
ik kj
1≤k≤n
= min {lik(m-1) + wkj}
1≤k≤n

– Shortest path from i to j with at most m – 1 edges


– Shortest path from i to j containing at most m
edges, considering all possible predecessors (k)
University of Hyderabad
of j
Computing the Shortest Paths
• m = 1: lij(1) = wij L(1) = W
– The path between i and j is restricted to 1 edge

• Given W = (wij), compute: L(1), L(2), …, L(n-1), where


L(m) = (lij(m))
• L(n-1) contains the actual shortest-path weights
Given L(m-1) and W ⇒ compute L(m)
– Extend the shortest paths computed so far by one more
edge
• If the graph has no negative cycles: all simple
shortest paths contain at most n - 1 edges
(n+1) = lij
(n-1)
δ(i, j) = l (n-1)
and l
ij
(n)
l ...
ij , ij
University of Hyderabad
The Floyd-Warshall Algorithm
• Given: 2
4
– Directed, weighted graph G = (V, 3
8
E) 1 3
2
– Negative-weight edges may be -4 1
-5
7
present 5 4
6
– No negative-weight cycles could
be present in the graph
• Compute:
– The shortest paths between all
pairs of vertices in a graph

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

any vertex in the set {v2, v3, …, vl-

1 }

– E.g.: p = ⟨1, 2, 4, 5⟩: {2, 4}


University of Hyderabad
The Structure of a Shortest
Path
• For any pair of vertices i, j ∈ V, consider all
paths from i to j whose intermediate
vertices are all drawn from a subset {1, 2,
…, k}
– Find p, a minimum-weight
p1 path from these
pu
paths i j

pt

No vertex on these paths has index > k

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

vertices from {1, 2, …, k} is a shortest path


from i to j with intermediate vertices from {1,
2, …, k - 1}

• k is an intermediate vertex of path p p1 ∞


k p2
j
– p1 is a shortest path from i to k i

– p2 is a shortest path from k to j


– p1 and p2 are shortest paths from i to k and k
to j respectively, with vertices from {1, 2, …,
k - 1} 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=0
• dij(k) = wij

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)

Compute D(0), D(1), D(2),…, D(n)


University of Hyderabad
FLOYD-WARSHALL(W)
1. n ← rows[W]
2. D(0) ← W
3. for k ← 1 to n
4. do for i ← 1 to n
5. do for j ← 1 to n
6. do dij(k) ← min (dij(k-1), dik(k-1) +
dkj(k-1))
7. return D(n)

• Running time: Θ(n3)


University of Hyderabad
Example dij(k) = min {dij(k-1) , dik(k-1) + dkj(k-1) }
D(0) = W D(1)
2 1 2 3 4 5 1 2 3 4 5
3 4 1 0 3 8 ∞ -4 1 0 3 8 ∞ -4
8
1 3 2 ∞ 0 ∞ 1 7 2 ∞ 0 ∞ 1 7
2

-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

You might also like