CS 483
Fall, 2004
H. Hamburger
Study Sheet for Chapter 8
Dynamic Programming (DP)
This is not a substitute for the usual study methods!
General Idea of DP:
• Many problems lead readily to Divide and Conquer.
• The resulting recursive algorithm may be inefficient.
• That inefficiency may arise from overlapping subproblems.
• DP overcomes this flaw by recording and reusing answers.
• Either recursion or iteration can use this technique.
• DP solves many instances, which can be efficient, if needed.
• You need to understand the algorithms and their efficiency.
Problems and Algorithms Studied
• Fibonacci numbers
• Binomial coefficients
• Warshall: all-pairs existence of paths
• Floyd: all-pairs shortest paths
• Optimal binary search trees
• Knapsack
Fibonacci Numbers
• The usual definition is recursive: fib(n) = fib(n-1) + fib(n-2).
• The definition leads directly to a recursive algorithm.
• That recursive algorithm is inefficient, with exponential execution time.
• Recursion with memos can be done in linear time, that is Q(n).
• Iteration is also linear, but faster.
• These methods compute fib for 1 to n.
• For a single value, there is a formula, which is Q(1).
Computing Binomial Coefficients
• The factorial formula takes about 2n multiplications for one instance.
• A variant of that formulat takes only 2min(k,n-k) multiplications.
• For k near n/2, the recursive formula takes time exponential in n.
• That's because each of the 2 subproblems is reduced only slightly in size.
• There is subproblem overlap, but the recursion doesn't notice that.
• DP keeps tack of solved subproblems and doesn't repeat work.
• DP should be done iteratively here.
• DP does just one addition per instance.