Advanced Algorithm Design and Analysis Techniques: Dynamic Programming
Advanced Algorithm Design and Analysis Techniques: Dynamic Programming
Outline Analysis
Dynamic Programming Greedy Algorithms
1 Dynamic Programming
Rod cutting problem
Elements of dynamic programming
Longest common subsequence
Optimal binary search trees
2 Greedy Algorithms
An activity-selection problem
Chapter 4 Elements of the greedy strategy
Advanced Algorithm Design and Analysis Techniques Huffman Codes
3 Amortized Analysis
Yonas Y. Aggregate analysis
Algorithm Analysis and Design
School of Electrical and Computer Engineering
1 2
Outline Dynamic Programming Greedy Algorithms Amortized
Outline Analysis
Dynamic Programming Greedy Algorithms
Dynamic Programming
Developed back in the day when ”programming” meant 1 Characterize the structure of an optimal solution.
”tabular method” (like linear programming). Doesn’t really
refer to computer programming.
2 Recursively define the value of an optimal solution.
Used for optimization problems: 4 Construct an optimal solution from computed information.
3 4
Outline Dynamic Programming Greedy Algorithms Amortized
Outline Analysis
Dynamic Programming Greedy Algorithms
Rod cutting
Sterling Enterprises buys long steel rods and cuts them into shorter The rod-cutting problem is the following.
rods, which it then sells.
Given a rod of length n inches and a table of prices pi for i =
Each cut is free. 1, 2,..., determine the maximum revenue rn obtainable by
The management of sterling enterprises wants to know the cutting up the rod and selling the pieces.
best way to cut up the rods.
We assume that we know, for i = 1, 2,. . . , the price pi in birr Consider the case when n = 4.
that sterling enterprises charges for a rod of length i inches.
Rod lengths are always an integral number of inches. Figure 4.1 shows all the ways to cut up a rod of 4 inches in
length, including the way with no cuts at all.
length i 1 2 3 4 5 6 7 8 9 10
price pi 1 5 8 9 10 17 17 20 24 30 We see that cutting a 4-inch rod into two 2-inch pieces
produces revenue p2 + p2 = 5 + 5 = 10, which is optimal.
Table: 4.1 A sample price table for rods. Each rod of length i inches
earns the company pi birrs of revenue.
5 6
Outline Dynamic Programming Greedy Algorithms Amortized
Outline Analysis
Dynamic Programming Greedy Algorithms
n = i1 + i2 + . . . + ik
7 8
Outline Dynamic Programming Greedy Algorithms Amortized
Outline Analysis
Dynamic Programming Greedy Algorithms
9 10
Outline Dynamic Programming Greedy Algorithms Amortized
Outline Analysis
Dynamic Programming Greedy Algorithms
Figure: 4.2 The recursion tree showing recursive calls resulting from a call
CUT-ROD(p, n) for n = 4.
11 12
Outline Dynamic Programming Greedy Algorithms Amortized
Outline Analysis
Dynamic Programming Greedy Algorithms
The count includes the initial call at its root. Dynamic programming thus uses additional memory to save
computation time =⇒ time-memory trade-off.
Thus, T(0) = 1 and
A dynamic-programming approach runs in polynomial time
T(n) = 1 + n-1
P
j=0 T(j), when the number of distinct subproblems involved is
which gives an exponential running time of 2n . polynomial in the input size.
13 14
Outline Dynamic Programming Greedy Algorithms Amortized
Outline Analysis
Dynamic Programming Greedy Algorithms
15 16
Outline Dynamic Programming Greedy Algorithms Amortized
Outline Analysis
Dynamic Programming Greedy Algorithms
Subproblem graphs
Figure 4.3 shows the subproblem graph for the rod-cutting problem Figure: 4.3 The subproblem graph for the rod-cutting problem with n = 4.
with n = 4.
The size of the subproblem graph G = (V, E) can help us
It is a directed graph, containing one vertex for each distinct determine the running time of the dynamic programming
subproblem. algorithm.
Reconstructing a solution
Algorithm 5 EXTENDED-BOTTOM-UP-CUT-ROD(p, n)
1: let r[0 .. n] be a new array
2: r[0] = 0
3: for j = 1 to n do
Our dynamic-programming solutions to the rod-cutting problem 4: q = −∞
return the value of an optimal solution. 5: for i = 1 to j do
6: if q < p[i] + r[j-i] then
They do not return an actual solution: a list of piece sizes. 7: q = p[i] + r[j-i]
8: s[j] = i
We can record not only the optimal value computed for each
9: end if
subproblem, but also a choice that led to the optimal value.
10: end for
11: r[j] = q
12: end for
13: return r and s
21 22
Outline Dynamic Programming Greedy Algorithms Amortized
Outline Analysis
Dynamic Programming Greedy Algorithms
Overlapping subproblems
23 24
Outline Dynamic Programming Greedy Algorithms Amortized
Outline Analysis
Dynamic Programming Greedy Algorithms
Optimal substructure
Given this choice, determine which subproblems arise and how That was optimal substructure procedure.
to characterize the resulting space of subproblems.
25 26
Outline Dynamic Programming Greedy Algorithms Amortized
Outline Analysis
Dynamic Programming Greedy Algorithms
27 28
Outline Dynamic Programming Greedy Algorithms Amortized
Outline Analysis
Dynamic Programming Greedy Algorithms
Subtleties
First make a choice that looks best, then solve the resulting V is a set of vertices.
subproblem.
E is a set of edges.
29 30
Outline Dynamic Programming Greedy Algorithms Amortized
Outline Analysis
Dynamic Programming Greedy Algorithms
Shortest path: find path u v with fewest edges. Must be Suppose p is shortest path u v.
simple (no cycles). Let w be any vertex on p.
Longest simple path: find simple path u v with most Let p1 be the portion of p, u w.
edges. Could repeatedly traverse a cycle to make an arbitrarily Then p1 is a shortest u w.
long path.
Same argument applies to p2 .
Overlapping subproblems
Alternative example: memoization
These occur when a recursive algorithm revisits the same problem ”Store, don’t recompute”.
over and over.
Make a table indexed by subproblem.
Good divide-and-conquer algorithms usually generate a brand new
problem at each stage of recursion. When solving a subproblem:
35 36
Outline Dynamic Programming Greedy Algorithms Amortized
Outline Analysis
Dynamic Programming Greedy Algorithms
Biological applications =⇒ To compare the DNA of two (or Example 2: For X = hA, B, C , B, D, A, Bi and
more) different organisms. Y = hB, D, C , A, B, Ai.
For every subsequence of X, check whether it’s a subsequence of Y. Yi = prefix hy1 , ..., yi i
39
3 If xm 6= yn , then zk 6= yn ⇒ Z is an LCS of X and Yn−1 . 40
Outline Dynamic Programming Greedy Algorithms Amortized
Outline Analysis
Dynamic Programming Greedy Algorithms
41 42
Outline Dynamic Programming Greedy Algorithms Amortized
Outline Analysis
Dynamic Programming Greedy Algorithms
Algorithm 6 LCS-LENGTH(X, Y)
1: m = X.length
2: n = Y.length
3: let b[1..m, 1..n] and c[0..m, 0..n] be new tables
4: for i = 1 to m do
5: c[i, 0] = 0
6: end for
7: for j = 1 to n do
8: c[0, j] = 0
9: end for
10: for i = 1 to m do
11: for j = 1 to n do
12: if xi == yj then
13: c[i, j] = c[i-1, j-1] + 1
14: b[i, j] = ” - ”
15: else if c[i-1, j] ≥ c[i, j-1] then
16: c[i, j] = c[i-1, j]
17: b[i, j] = ” ↑ ” Figure: 4.4 The c and b tables computed by LCS-LENGTH on the
18: else c[i, j] = c[i, j-1] sequences X = hA, B, C, B, D, A, Bi and Y = hB, D, C, A, B, Ai.
19: b[i, j] = ” ← ”
20: end if
21: end for The running time of the procedure is Θ(mn), since each table entry
22: end for
23: return c and b 43
takes Θ(1) time to compute. 44
Outline Dynamic Programming Greedy Algorithms Amortized
Outline Analysis
Dynamic Programming Greedy Algorithms
i 0 1 2 3 4 5
pi 0.15 0.10 0.05 0.10 0.20
qi 0.05 0.10 0.05 0.05 0.05 0.10
n
X n
X
pi + qi = 1
Figure: 4.5 Two binary search trees for a set of n = 5 keys. i=1 i=0
49 50
Outline Dynamic Programming Greedy Algorithms Amortized
Outline Analysis
Dynamic Programming Greedy Algorithms
From figure 4.5, we can calculate the expected search cost node by
node:
Let us assume that the actual cost of a search equals the number For the first binary search tree
of nodes examined, i.e., the depth of the node found by the search node depth probablity contribution
in T, plus 1. k1 1 0.15 0.30
k2 0 0.10 0.10
Then the expected cost of a search in T is:
k3 2 0.05 0.15
n
X n
X k4 1 0.10 0.20
E[search cost in T] = (depthT (ki ) + 1) · pi + (depthT (di ) + 1) · qi k5 2 0.20 0.60
i=1 i=0
n n d0 2 0.05 0.15
X X
=1+ depthT (ki ) · pi + depthT (di ) · qi d1 2 0.10 0.30
i=1 i=0 d2 3 0.05 0.20
d3 3 0.05 0.20
d4 3 0.05 0.20
d5 3 0.10 0.40
Total 2.80
51 52
Outline Dynamic Programming Greedy Algorithms Amortized
Outline Analysis
Dynamic Programming Greedy Algorithms
53 54
Outline Dynamic Programming Greedy Algorithms Amortized
Outline Analysis
Dynamic Programming Greedy Algorithms
Greedy Algorithms
Then we’re guaranteed to find an optimal BST for ki , ..., kj . Idea: When we have a choice to make, make the one that
looks best right now.
Computing of the optimal solution using dynamic programming Make a locally optimal choice in hope of getting a globally
requires to store intermediate solutions which can be used to solve optimal solution.
the bigger subproblems.
Greedy algorithms don’t always yield an optimal solution.
But sometimes they do.
55 56
Outline Dynamic Programming Greedy Algorithms Amortized
Outline Analysis
Dynamic Programming Greedy Algorithms
An activity-selection problem
Note: Could have many other objectives:
Suppose we have a set S = {a1 , a2 , ..., an } of n activities require
exclusive use of a common resource. Schedule room for longest time.
For example, scheduling the use of a classroom. Maximize income rental fees.
ai needs resource during period [si , fi ), which is a half-open For example, consider the following set S of activities for the
interval, where si = start time and fi = finish time. activity selection problem:
i 1 2 3 4 5 6 7 8 9
Goal: Select the largest possible set of nonoverlapping (mutually
si 1 2 4 1 5 8 9 11 13
compatible) activities.
fi 3 5 7 8 9 10 11 14 16
We assume that the activities are sorted in monotonically
increasing order of finish time.
57 58
Outline Dynamic Programming Greedy Algorithms Amortized
Outline Analysis
Dynamic Programming Greedy Algorithms
59 60
Outline Dynamic Programming Greedy Algorithms Amortized
Outline Analysis
Dynamic Programming Greedy Algorithms
Sij = {ak ∈ S : fi ≤ sk < fk ≤ sj } Aik contains the activities in Aij that finish before ak starts
and Akj contains the activities in Aij that start after ak
activities that start after ai finishes and finish before aj starts. finishes.
61 62
Outline Dynamic Programming Greedy Algorithms Amortized
Outline Analysis
Dynamic Programming Greedy Algorithms
If we denote the size of an optimal solution for the set Sij by What if we could choose an activity to add to our optimal solution
c[i, j], then we would have the recurrence
without having to first solve all the subproblems?
c[i, j] = c[i, k] + c[k, j] + 1.
That could save us from having to consider all the choices
inherent in recurrence.
If we did not know that an optimal solution for the set Sij
includes activity ak , we would have to examine all activities in In fact, for the activity-selection problem, we need consider
Sij to find which one to choose, so that only one choice: the greedy choice.
0 if Sij = ∅
c[i, j] = Now we can solve the problem Sij top down,
max {c[i, k] + c[k, j] + 1}, if Sij 6= ∅
ak ∈Sij
Choose am ∈ Sij with earliest finish time: the greedy choice.
63 64
Outline Dynamic Programming Greedy Algorithms Amortized
Outline Analysis
Dynamic Programming Greedy Algorithms
Each subproblem is Smi ,n+1 , i.e., the last activities to finish. And Size n of the original problem.
the subproblems chosen have finish times that increase.
It returns a maximum-size set of mutually compatible
Therefore, we can consider each activity just once, in activities in Sk .
monotonically increasing order of finish time.
Assumes activities are already sorted by monotonically increasing
finish time. (If not, then sort in O(n lg n) time.)
65 66
Outline Dynamic Programming Greedy Algorithms Amortized
Outline Analysis
Dynamic Programming Greedy Algorithms
Algorithm 8 RECURSIVE-ACTIVITY-SELECTOR(s, f, k, n)
1: m = k + 1 Example: Consider the following set S of activities:
2: while m ≤ n and s[m] < f[k] do . find the first activity in Sk
to finish i 1 2 3 4 5 6 7 8 9 10 11
3: m=m+1
si 1 3 0 5 3 5 6 8 8 2 12
4: end while
5: if m ≤ n then
fi 4 5 6 7 9 9 10 11 12 14 16
6: return {am } ∪ RECURSIVE-ACTIVITY-SELECTOR(s, f, m, n)
7: else return ∅
8: end if
Algorithm 9 GREEDY-ACTIVITY-SELECTOR(s, f)
1: n = s.length
2: A = {a1 }
3: k=1
4: for m = 2 to n do
5: if s[m] ≥ f[k] then
6: A = A ∪ {am }
7: k=m
8: end if
9: end for
10: return A
2 Prove that there is always an optimal solution to the original Optimal substructure
problem that makes the greedy choice, so that the greedy
choice is always safe.
71 72
Outline Dynamic Programming Greedy Algorithms Amortized
Outline Analysis
Dynamic Programming Greedy Algorithms
A globally optimal solution can be arrived at by making a locally 0-1 knapsack problem:
optimal (greedy) choice.
n items.
Greedy:
Item i is worth vi birr, weighs wi pounds.
Make a choice at each step. Find a most valuable subset of items with total weight ≤ W.
Make the choice before solving the subproblems.
Have to either take an item or not take it-can’t take part of it.
Solve top-down.
Greedy doesn’t work for the 0-1 knapsack problem. Might get
empty space, which lowers the average value per pound of the Figure: 4.7 An example showing that the greedy strategy does not work
items taken. for the 0-1 knapsack problem.
75 76
Outline Dynamic Programming Greedy Algorithms Amortized
Outline Analysis
Dynamic Programming Greedy Algorithms
Huffman Codes
Represent via a binary character code ⇒ unique binary string
Huffman codes compress data very effectively. (codeword).
Savings of 20% to 90% are typical, depending on the
1 Fixed-length code:
characteristics of the data being compressed.
3 bits to represent 6 characters =⇒ 300,000 bits to code
Based on how often each character occurs(i.e frequency).
the entire file.
Example: Suppose we have a 100,000 - character data file. 2 Variable-length code:
Frequent characters =⇒ short codewords.
a b c d d f
Frequency (in thousands) 45 13 12 16 9 5 From Table 4.1 the code requires
Fixed-length codeword 000 001 010 011 100 101 (45· 1 + 1· 3 + 12·3 + 16· 3 + 19·4 + 5·4)·1000 = 224,000
Variable-length codeword 0 101 100 111 1101 1100 bits
to represent the file, a savings of approximately 25%.
Table: 4.2 A Character-coding problem.
77 78
Outline Dynamic Programming Greedy Algorithms Amortized
Outline Analysis
Dynamic Programming Greedy Algorithms
Prefix Codes
For example: abc =⇒ 0 · 101 · 100 = 0101100. Binary codeword for a character =⇒ simple path from the
root to that character.
They simplify decoding.
0 means ”go to the left child” and 1 means ”go to the right
Decoding Procedure: child.”
Identify the initial codeword,
Translate it back to the original character, and
Repeat the decoding process on the remainder of the encoded
file.
79 80
Outline Dynamic Programming Greedy Algorithms Amortized
Outline Analysis
Dynamic Programming Greedy Algorithms
Figure: 4.8 Trees corresponding to the coding schemes in Table 4.2. Builds the tree T corresponding to the optimal code in a
An optimal code for a file is always represented by a full bottom-up manner.
binary tree, in which every nonleaf node has two children.
The number of bits required to encode a file is thus Performs a sequence of |C| - 1 ”merging” operations to
X create the final tree.
B(T ) = c.freq · dT (c),
c∈C
which we define as the cost of the tree T. 81 82
Outline Dynamic Programming Greedy Algorithms Amortized
Outline Analysis
Dynamic Programming Greedy Algorithms
Algorithm 10 HUFFMAN(C)
1: n = |C|
2: Q = C
3: for i = 1 to n-1 do
4: allocate a new node z
5: z.left = x = EXTRACT-MIN(Q)
6: z.right = y = EXTRACT-MIN(Q)
7: z.freq = x.freq + y.freq
8: INSERT(Q, z)
9: end for
10: return EXTRACT-MIN(Q) . return the root of the tree
Figure: 4.9 The steps of Huffmans algorithm for the frequencies given in
83 Figure 4.2. 84
Outline Dynamic Programming Greedy Algorithms Amortized
Outline Analysis
Dynamic Programming Greedy Algorithms
Huffman Codes
Amortized Analysis
To analyze the running time of Huffman’s algorithm:
Assume that Q is implemented as a binary min-heap. Analyze a sequence of operations on a data structure.
For a set C of n characters, we can initialize Q in line 2 in Goal: Show that although some individual operations may be
O(n) time using the BUILD-MIN-HEAP. expensive, on average the cost per operation is small.
85 86
Outline Dynamic Programming Greedy Algorithms Amortized
Outline Analysis
Dynamic Programming Greedy Algorithms
87 88
Outline Dynamic Programming Greedy Algorithms Amortized
Outline Analysis
Dynamic Programming Greedy Algorithms
Figure: 4.10 The action of MULTIPOP on a stack S. The top 4 objects Have n operations.
are popped by MULTIPOP(S, 4), whose result is shown in (b). The next
operation is MULTIPOP(S, 7)-shown in (c). Therefore, worst-case cost of sequence is O(n2 ).
Running time of MULTIPOP: Although this analysis is correct, the worst-case cost of each
operation individually, is not tight.
Linear in # of POP operations.
# of iterations of while loop is min(s, k), where s = # of
objects on stack.
Therefore, total cost = min(s, k). 89 90
Outline Dynamic Programming Greedy Algorithms Amortized
Outline Analysis
Dynamic Programming Greedy Algorithms
Each object can be popped only once per time that it’s
pushed.
Have ≤ n PUSHes ⇒ ≤ n POPs, including those in Consider the problem of implementing a k-bit binary counter
MULTIPOP. A[0..k - 1] of bits, where A[0] is the least significant bit and
A[k - 1] is the most significant bit.
Therefore, total cost = O(n).
Average over the n operations ⇒ O(1) per operation on Counts upward from 0.
average. k-1
A[i] · 2i .
P
Value of counter is
i=0
Again, notice no probability.
Initially, counter value is 0, so A[0..k - 1] = 0.
Showed worst-case O(n) cost for sequence.
Therefore, O(1) per operation on average.
This technique is called aggregate analysis. 91 92
Outline Dynamic Programming Greedy Algorithms Amortized
Outline Analysis
Dynamic Programming Greedy Algorithms
Algorithm 12 INCREMENT(A)
1: i=0
2: while i < A.length and A[i]==1 do
3: A[i]=0
4: i = i+1
5: end while
6: if i < A.length then
7: A[i]=1
8: end if
Figure: 4.11 An 8-bit binary counter as its value goes from 0 to 16 by a
sequence of 16 INCREMENT operations.
Cost of INCREMENT = Θ(# of bits flipped).
Analysis: Each call could flip k bits, so n INCREMENTs takes
93 O(nk) time. 94
Outline Dynamic Programming Greedy Algorithms Amortized
Outline Analysis
Dynamic Programming Greedy Algorithms
Observation
Not every bit flips every time.
bit flips how often times in n INCREMENTs
0 every time n
1 1/2 the time bn/2c End of Chapter 4
2 1/4 the time bn/4c
.
.
i 1/2i the time bn/2i c
.
Questions?
.
i≥ k never 0
k-1 ∞
i
b1/2i c = 2n.
P P
Therefore, total # of flips = bn/2 c < n
i=0 i=0
As a result, n INCREMENTs costs O(n).
Average cost per operation = O(1). 95 96