Dynamic Programming
Dynamic Programming
Motivation
DP can be used to easily reduce recursive problems from exponential runtime to down polynomial and oftentimes even linear runtime by using tabulation (bottom-up) and memoization (top-down). It makes use of the overlapping subproblems structure and optimal substructure properties of certain problems.
General Roadmap for DP Problems
- Definition of the DP table:
- What dimensions does the table have?
- What is the meaning of each entry?
- Computation of an entry:
- How can an entry be computed from the values of other entries?
- Specify the base cases, i.e., the entries that do not depend on others.
- Calculation order:
- In which order can entries be computed so that values needed for each entry have been determined in previous steps?
- Extracting the solution: How can the final solution be extracted once the table has been filled?
- Running time:
- What is the running time of your solution?
Subset Sum Problem
We have a set of elements that need to be split into two sets such that the sum of those sets is equal. Let be that sum.
Idea: is subset sum of or is subset sum of . Let be a predicate defined by .
Recurrence:
computes a new entry.
gives the solution.
Knapsack Problem
We have a set of elements. Every element has a value and a weight . We have a maximum weight limit . We are looking to maximize the summed value of the elements such that the sum of their weights is smaller than . Let be a function such that gives the maximum achievable value from values such that their weights summed up remain smaller than .
Recurrence:
computes an entry.
gives the solution.