Docsity

Dynamic Programming: Solving the Knapsack Problem with Two Main Ideas - Prof. Eric Torng, Study notes of Computer Science

Toggle sidebar
bg1
Lecture 9
Dynamic Programming
Knapsack Problem
Basic Idea
Two main ideas behind dynamic programming
1) The principle of optimality
This principle applies to some problems but not for others.
For problems that it does apply to, dynamic programming is a viable design technique.
a) Given an optimal sequence of decisions, any subsequence represents
must also be optimal.
b) Suppose solution S is optimal for problem P. Suppose we decompose
problem P into subproblems P1 through Pk and that S can be decomposed
into pieces S1 through Sk corresponding to the subproblems. Then
solution Si is optimal for subproblem Pi.
Example:
Shortest paths: A shortest path from s to t visits node u.
We can decompose the path into s-u and u-t.
The s-u path must be a shortest path from s to u.
Likewise for the path from u-t.
Conclusion: Dynamic programming can be used for shortest paths.
Longest paths: A longest path from s to t visits node u.
We can decompose the path into s-u and u-t.
However, the s-u path is not necessarily the longest
path from s to u.
Likewise for the path from u-t.
Why?
Conclusion: Dynamic programming cannot be used for longest paths.
2) Recursion in a bottom up fashion.
Computing Fibonacci numbers
- F(n) = F(n-1) + F(n-2)
- Top-down recursion is very inefficient
- Many F(i) values are computed multiple times
- Bottom-up computation is much more efficient
- Each F(i) value is computed just once
Computing binomial coefficient
- C(n,k) = C(n-1,k-1) + C(n-1,k)
- Top-down computation is very inefficient
- Many C(i,j) values are computed multiple times
- Bottom-up computation is much more efficient
- Each C(i,j) value is computed once
Key implementation issues
1) Identify subsolutions that may be useful in computing whole solution
- often need to introduce parameters
2) Develop a recurrence relation
3) Setting up the table of values to be computed
- the dimensionality of the table is determined by the number of parameters
- Steps (2) and (3) are often the same step
4) Determining the order of computation of values
5) Backtracking through the table to obtain complete solution, not just solution value
pf3

Partial preview of the text

Download Dynamic Programming: Solving the Knapsack Problem with Two Main Ideas - Prof. Eric Torng and more Study notes Computer Science in PDF only on Docsity!

Lecture 9 Dynamic Programming Knapsack Problem Basic Idea Two main ideas behind dynamic programming

  1. The principle of optimality This principle applies to some problems but not for others. For problems that it does apply to, dynamic programming is a viable design technique. a) Given an optimal sequence of decisions, any subsequence represents must also be optimal. b) Suppose solution S is optimal for problem P. Suppose we decompose problem P into subproblems P 1 through Pk and that S can be decomposed into pieces S 1 through Sk corresponding to the subproblems. Then solution Si is optimal for subproblem Pi. Example: Shortest paths: A shortest path from s to t visits node u. We can decompose the path into s-u and u-t. The s-u path must be a shortest path from s to u. Likewise for the path from u-t. Conclusion: Dynamic programming can be used for shortest paths. Longest paths: A longest path from s to t visits node u. We can decompose the path into s-u and u-t. However, the s-u path is not necessarily the longest path from s to u. Likewise for the path from u-t. Why? Conclusion: Dynamic programming cannot be used for longest paths.
  2. Recursion in a bottom up fashion. Computing Fibonacci numbers
  • F(n) = F(n-1) + F(n-2)
  • Top-down recursion is very inefficient
    • Many F(i) values are computed multiple times
  • Bottom-up computation is much more efficient
    • Each F(i) value is computed just once Computing binomial coefficient
  • C(n,k) = C(n-1,k-1) + C(n-1,k)
  • Top-down computation is very inefficient
  • Many C(i,j) values are computed multiple times
  • Bottom-up computation is much more efficient
  • Each C(i,j) value is computed once Key implementation issues
  1. Identify subsolutions that may be useful in computing whole solution
  • often need to introduce parameters
  1. Develop a recurrence relation
  2. Setting up the table of values to be computed
  • the dimensionality of the table is determined by the number of parameters
  • Steps (2) and (3) are often the same step
  1. Determining the order of computation of values
  2. Backtracking through the table to obtain complete solution, not just solution value