Dynamical programming
Definition
- Extremely general algorithm design technique
- Similar to divide and conquer idea
- Build up answer from smaller subproblem
- More general than “simple” divide and conquer
- Generally applies to algorithms where the brute force algorithm would be more exponential
General DP principles
Optimal value of the original problem can be computed easily from some subproblem
OPT(j,w) = max of two subproblems
There are only a polynomial # of subproblems
{(j,w)} for j = 1,…,n and w = 0,…W
There is a natural ordering of the subproblems from smallest to the largest such that you can obtain the solutions for a subproblem by only looking at
smaller
subproblems.Considering items {1,2,3} is a smaller problem than considering items {1,2,3,4}
Requirements for DP to apply
- Optimal value of the original problem can be computed from some similar subproblems
- There are only a polynomial number of subproblems
- There is a natural ordering of subproblems, so that you can solve a subproblem by only looking at the small subproblems
DP problems
Subset sum problem
Pseudocode and Run time analysis
Weighted interval scheduling
0-1 Knapsack problem
Difference with the subset sum: want to maximize value instead of the weight
Noted that the greedy algorithm would not work because we are not allowed to take part of the item
Segmented least squares problem
Internet resources
https://kartikkukreja.wordpress.com/2013/10/21/segmented-least-squares-problem/
Solution
Notation:
= minimum cost for points = SSE for points
To compute
- Last segment uses points
for some - Cost =
- Optimal substructure property (proof via exchange argument)
Matrix-chain multiplication
Optimal Binary Search Tree
Maximum subarray problem
Goal: Given an array x of n integer (positive or negative) find a continuous subarray whose sum is maximum
Def:
OPT(i) = max sum of any subarray of x whose rightmost index is I:
Goal:
max OPT(i)
Word wrap
https://www.geeksforgeeks.org/word-wrap-problem-dp-19/
String alignment problem
- Post title:Dynamic programming notes
- Post author:Yuxuan Wu
- Create time:2021-11-26 09:10:23
- Post link:yuxuanwu17.github.io2021/11/26/2021-11-18-Dynamic-programming-notes/
- Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.