Dynamic Programming Lecture 2
Dynamic Programming Lecture 2
http://www.cs.stonybrook.edu/˜skiena
Topic: Problem of the Day
Problem of the Day
Multisets are allowed to have repeated elements. A multiset
of n items may thus have fewer than n! distinct permutations.
For example, {1, 1, 2, 2} has only six different permutations:
{1, 1, 2, 2}, {1, 2, 1, 2}, {1, 2, 2, 1}, {2, 1, 1, 2}, {2, 1, 2, 1},
and {2, 2, 1, 1}. Design and implement an efficient algorithm
for constructing all permutations of a multiset.
Questions?
Topic: Introduction to Dynamic Programming
Dynamic Programming
Dynamic programming is a very powerful, general tool for
solving optimization problems on left-right-ordered items
such as character strings.
Once understood it is relatively easy to apply, it looks like
magic until you have seen enough examples.
Floyd’s all-pairs shortest-path algorithm was an example of
dynamic programming.
Greedy vs. Exhaustive Search
Greedy algorithms focus on making the best local choice at
each decision point. In the absence of a correctness proof
such greedy algorithms are very likely to fail.
Dynamic programming gives us a way to design custom
algorithms which systematically search all possibilities (thus
guaranteeing correctness) while storing results to avoid
recomputing (thus providing efficiency).
Recurrence Relations
A recurrence relation is an equation which is defined in terms
of itself. They are useful because many natural functions are
easily expressed as recurrences:
Polynomials: an = an−1 + 1, a1 = 1 −→ an = n
Exponentials: an = 2an−1, a1 = 2 −→ an = 2n
Weird: an = nan−1, a1 = 1 −→ an = n!
Computer programs can easily evaluate the value of a given
recurrence even without the existence of a nice closed form.
Questions?
Topic: Fibonacci Numbers
Computing Fibonacci Numbers
Fn = Fn−1 + Fn−2, F0 = 0, F1 = 1
Implementing this as a recursive procedure is easy, but slow
because we keep calculating the same value over and over.
long fib_r(int n) {
if (n == 0) {
return(0);
}
if (n == 1) {
return(1);
}
return(fib_r(n-1) + fib_r(n-2));
}
How Slow?
√
Fn+1/Fn ≈ φ = (1 + 5)/2 ≈ 1.61803
F(6)=8
F(5) F(4)
F(1) F(0)
long fib_c(int n) {
if (f[n] == UNKNOWN) {
f[n] = fib_c(n-1) + fib_c(n-2);
}
return(f[n]);
}
long fib_c_driver(int n) {
int i; /* counter */
f[0] = 0;
f[1] = 1;
return(fib_c(n));
}
What about Dynamic Programming?
We can calculate Fn in linear time by storing small values:
F0 = 0
F1 = 1
For i = 1 to n
Fi = Fi−1 + Fi−2
Moral: we traded space for time.
Fibonacci by Dynamic Programming
long fib_dp(int n) {
int i; /* counter */
long f[MAXN+1]; /* array for caching values */
f[0] = 0;
f[1] = 1;
return(f[n]);
}
Why I Love Dynamic Programming
Dynamic programming is a technique for efficiently comput-
ing recurrences by storing partial results.
Once you understand dynamic programming, it is usually
easier to reinvent certain algorithms than try to look them up!
I have found dynamic programming to be one of the most
useful algorithmic techniques in practice:
• Morphing in computer graphics.
• Data compression for high density bar codes.
• Designing genes to avoid or contain specified patterns.
Questions?
Topic: Binomial Coefficients
Avoiding Recomputation by Storing Results
The trick to dynamic programmming is to see that the
naive recursive algorithm repeatedly computes the same
subproblems over again, so storing the answers in a table
instead of recomputing leads to an efficient algorithm.
We first hunt for a correct recursive algorithm, then we try to
speed it up by using a results matrix.
Binomial Coefficients
The most important class of counting numbers are the
binomial coefficients, where ( ) counts the number of ways
n
k
()=( )+( )
n
k
n−1
k−1
n−1
k
return(bc[n][k]);
}
Questions?