CH 08
CH 08
• Main idea:
- set up a recurrence relating a solution to a larger instance
to solutions of some smaller instances
- solve smaller instances once
- record solutions in a table
- extract solution to the initial instance from that table
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 1
Example 1: Fibonacci numbers
• Recall definition of Fibonacci numbers:
F(n)
F(n-1) + F(n-2)
F(0) = 0
F(1) = 1
F(2) = 1+0 = 1
…
F(n-2) =
F(n-1) =
F(n) = F(n-1) + F(n-2)
Efficiency:
- time
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson
- space Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 3
Example 2: Coin-row problem
There is a row of n coins whose values are some positive integers
c₁, c₂,...,cn, not necessarily distinct. The goal is to pick up the
maximum amount of money subject to the constraint that no two
coins adjacent in the initial row can be picked up.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 4
DP solution to the coin-row problem
Let F(n) be the maximum amount that can be picked up from the
row of n coins. To derive a recurrence for F(n), we partition all
the allowed coin selections into two groups:
F(0) = 0, F(1)=c₁
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 5
DP solution to the coin-row problem (cont.)
F(n) = max{cn + F(n-2), F(n-1)} for n > 1,
F(0) = 0, F(1)=c₁
index 0 1 2 3 4 5 6
coins -- 5 1 2 10 6 2
F( )
Max amount:
Coins of optimal solution:
Time efficiency:
Space efficiency:
Note: All smaller instances were solved.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 6
Example 3: Path counting
1
Consider the problem of
A
counting the number of
shortest paths from point A
to point B in a city with
perfectly horizontal streets
and vertical avenues
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 7
Example 4: Coin-collecting by robot
Several coins are placed in cells of an n×m board. A robot,
located in the upper left cell of the board, needs to collect as
many of the coins as possible and bring them to the bottom right
cell. On each step, the robot can move either one cell to the right
or one cell down from its current location.
1 2 3 4 5 6
5
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 8
Solution to the coin-collecting problem
Let F(i,j) be the largest number of coins the robot can collect
and bring to cell (i,j) in the ith row and jth column.
The recurrence:
F(i, j) = max{F(i-1, j), F(i, j-1)} + cij for 1 ≤ i ≤ n, 1 ≤ j ≤ m
where cij = 1 if there is a coin in cell (i,j), and cij = 0 otherwise
5
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 10
Other examples of DP algorithms
• Computing a binomial coefficient (# 9, Exercises 8.1)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 11
Knapsack Problem by DP
Given n items of
integer weights: w1 w2 … wn
values: v1 v2 … vn
a knapsack of integer capacity W
find most valuable subset of the items that fit into the knapsack
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 14
DP for Optimal BST Problem
Let C[i,j] be minimum average number of comparisons made in
T[i,j], optimal BST for keys ai < …< aj , where 1 ≤ i ≤ j ≤ n.
Consider optimal BST among all BSTs with some ak (i ≤ k ≤ j )
as their root; T[i,j] is the best among them.
ak C[i,j] =
min {pk · 1 +
i≤k≤j
k-1
∑ ps (level as in T[i,k-1] +1) +
Optimal Optimal s=i
BST for BST for
a i , ..., a k-1 a k+1 , ..., a j
j
∑ ps (level as in T[k+1,j] +1)}
s =k+1
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 15
DP for Optimal BST Problem (cont.)
After simplifications, we obtain the recurrence for C[i,j]:
j
C[i,j] = min {C[i,k-1] + C[k+1,j]} + ∑ ps for 1 ≤ i ≤ j ≤ n
i≤k≤j s=i
C[i,i] = pi for 1 ≤ i ≤ j ≤ n
0 1 j n
1 0 p1 goal
0 p2
i C[i,j]
pn
n+1 0
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 16
Example: key A B C D
probability 0.1 0.2 0.4 0.3
The tables below are filled diagonal by diagonal: the left one is filled
using the recurrence j
C[i,j] = min {C[i,k-1] + C[k+1,j]} + ∑ ps , C[i,i] = pi ;
i≤k≤j s=i
the right one, for trees’ roots, records k’s values giving the minima
i
j 0 1 2 3 4 i
j 0 1 2 3 4
C
1 0 .1 .4 1.1 1.7 1 1 2 3 3
2 2 3 3 B D
2 0 .2 .8 1.4
3 0 .4 1.0 3 3 3
A
4 0 .3 4 4
optimal BST
5 0 5
Optimal Binary Search Trees
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 18
Analysis DP for Optimal BST Problem
Time efficiency: Θ(n3) but can be reduced to Θ(n2) by taking
advantage of monotonicity of entries in the
root table, i.e., R[i,j] is always in the range
between R[i,j-1] and R[i+1,j]
Space efficiency: Θ(n2)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 19
Warshall’s Algorithm: Transitive Closure
• Computes the transitive closure of a relation
3 3
1 1
4 4 0 0 1 0
2 0 0 1 0 2
1 0 0 1 1 1 1 1
0 0 0 0 0 0 0 0
0 1 0 0 1 1 1 1
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 20
Warshall’s Algorithm
Constructs transitive closure T as the last matrix in the sequence
of n-by-n matrices R(0), … , R(k), … , R(n) where
R(k)[i,j] = 1 iff there is nontrivial path from i to j with only first k
vertices allowed as intermediate
Note that R(0) = A (adjacency matrix), R(n) = T (transitive closure)
3 3 3 3 3
1 1 1 1 1
4 4 4 2 4 4
2 2 2 2
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 21
Warshall’s Algorithm (recurrence)
On the k-th iteration, the algorithm determines for every pair of
vertices i, j if a path exists from i and j with just vertices 1,…,k
allowed as intermediate
R(k)[i,j] =
{ R(k-1)[i,j]
or
(path using just 1 ,…,k-1)
j
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 22
Warshall’s Algorithm (matrix generation)
Recurrence relating elements R(k) to elements of R(k-1) is:
3
1 0 0 1 0 0 0 1 0
1 0 0 1 1 0 1 1
R(0) = 0 0 0 0 R(1) = 0 0 0 0
2 4
0 1 0 0 0 1 0 0
0 0 1 0 0 0 1 0 0 0 1 0
1 0 1 1 1 0 1 1 1 1 1 1
R(2) = 0 0 0 0 R(3) = 0 0 0 0 R(4) = 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 24
Warshall’s Algorithm (pseudocode and analysis)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 25
Floyd’s Algorithm: All pairs shortest paths
Problem: In a weighted (di)graph, find shortest paths between
every pair of vertices
Example: 4 3
1
1
6
1 5
2 4
3
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 26
Floyd’s Algorithm (matrix generation)
On the k-th iteration, the algorithm determines shortest paths
between every pair of vertices i, j that use only vertices among 1,
…,k as intermediate
D(k-1)[i,k]
k
i
D(k-1)[k,j]
D(k-1)[i,j]
j
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 27
Floyd’s Algorithm (example)
1
2 2 0 ∞ 3 ∞ 0 ∞ 3 ∞
3 6 7 2 0 ∞ ∞ 2 0 5 ∞
D(0) = ∞ 7 0 1 D(1) = ∞ 7 0 1
3
1
4 6 ∞ ∞ 0 6 ∞ 9 0
0 ∞ 3 ∞ 0 10 3 4 0 10 3 4
2 0 5 ∞ 2 0 5 6 2 0 5 6
D(2) = 9 7 0 1 D(3) = 9 7 0 1 D(4) = 7 7 0 1
6 ∞ 9 0 6 16 9 0 6 16 9 0
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 28
Floyd’s Algorithm (pseudocode and analysis)