CS 411 Fall 2025 > Outline for October 29, 2025
CS 411 Fall 2025
Outline
for October 29, 2025
Outline
Knapsack Problem & Memory Functions
- Memory Functions
  - In top-down dynamic programming, computation is driven by recursive calls. We only do required computations.
- Needs a function that remembers its results:
  a memory function.
    - Such a function must be deterministic:
    its output depends only on its input (parameters).
      - Examples of functions that are not deterministic: get a line from standard input, pseudorandom-number generators.
 
- Save results in a table, as in bottom-up DP.
- Table might be a data member of some class. It also might be passed around by reference. I would avoid making the table a global, or a static local variable in the memory function.
- We do not know in what order we will compute values; we need to be able to mark table items as “not computed yet”.
 
- Such a function must be deterministic:
    its output depends only on its input (parameters).
      
- Using this method can often involve only a small change to an existing recursive function, particularly if the function is written so that it only has one exit point.
- This method, also called memoizing, can sometimes be significantly faster than bottom-up DP.
 
- Knapsack Problem
  - The Knapsack Problem. Given a collection of objects, each with a value and a weight, along with the capacity of a knapsack, find the subcollection of greatest total value that will fit in the knapsack (i.e., whose total weight is at most the capacity).
- This is an optimization problem.
    - Feasible solution: a subcollection that fits in the knapsack.
- Objective function: total value (maximize).
- Optimal solution: a subcollection that fits in the knapsack and has maximum possible total value, among all such subcollections.
 
 
- Knapsack: Brute-Force Solution
  - We can apply the same approach as we did with the coin row problem: split our solution space into those that use the last item, and those that do not. Make a recursive call for each.
- When we use the last item, our recursive call can reduce the capacity by the weight of the item.
- Note that we need to check whether the last item fits in the knapsack. If it does not, then no feasible solutions use it.
 
- Knapsack: Top-Down DP Solution
  - We write a top-down dynamic-programming solution for the Knapsack Problem.
- Note that the Principle of Optimality holds for the Knapsack Problem.
- Since we can make the DP version out of the brute-force version, our work on the brute-force solution was not wasted effort.
- Our table is a 2-D array. Dimensions are the number of objects and the capacity. Our algorithm will be efficient if the capacity is small; it may be very inefficient if the capacity is large.