Week 9 - Dynamic Programming Flashcards
What is dynamic programming
Dynamic programming is a method for solving complex problems by breaking them down into simpler overlapping subproblems, solving each subproblem once, and storing their solutions to avoid redundant computations.
How does dynamic programming work, step by step?
Here’s how dynamic programming works, explained step by step:
- Initialize a Grid: Start with an empty grid where rows represent items and columns represent sub-wallet sizes.
- Fill the First Row: For each cell in the first row, determine if the first item fits into the sub-wallet. If it fits, place the item’s value in that cell.
- Move to the Next Row: For each subsequent item (row), check if it fits into each sub-wallet size (column). If it doesn’t fit, retain the previous value. If it fits, compare the value of including the item with the current best and update the cell accordingly.
- Combine Values: When an item fits and leaves extra space, add the best value from the remaining space to maximize the total value.
- Repeat Until Complete: Continue this process until all items and sub-wallet sizes are considered, filling in the grid to reflect the best possible value at each step.
How does dynamic programming solve the knapsack problem?
Dynamic programming solves the knapsack problem by starting with smaller “sub-knapsacks” and progressively building up to the original problem. It uses a grid where rows represent items and columns represent knapsack sizes, filling in the grid with sub-knapsack values step by step. By solving these smaller sub-problems first, the algorithm efficiently calculates the optimal value for the full knapsack.
Can you add more items to the knapsack after solving the problem?
Yes! But new maximum values need to be calculated. Dynamic programming will continue to build on the existing grid to update the solution.
Can you change the order of the rows in the dynamic programming grid?
Yes! Changing the order of the rows has no effect on the outcome, as the row order doesn’t matter.
Can you fill the grid column-wise instead of row-wise in dynamic programming?
For this problem, it works, but it may not be effective for others.
Can you add an item with a smaller size to the knapsack?
Yes! However, you would likely need to recompute the entire grid to account for the new item.
Does dynamic programming handle splitting items (and their values)?
No! Dynamic programming only works for whole items. But a greedy algorithm would work!
Can dynamic programming handle items that depend on each other?
No, dynamic programming only works when each subproblem is discrete and does not depend on other subproblems.