🌡
Christian's Wiki
/
πŸ“–
Data Structures & Algorithms
/
Algorithm
/
Dynamic Programming

Dynamic Programming

β€£
What are the steps for solving a dynamic programming problem?
  1. Define (overlapping) subproblems
  2. Write down the recurrence that relates subproblems
  3. Recognize and solve the base cases
β€£
What kinds of dynamic programming problems are there?
  • 1-dimensional DP
  • 2-dimensional DP
  • Interval DP
  • Tree DP
  • Subset DP
β€£
What is the recurrence for the LCSLCSLCS (longest common subsequence) problem?

Let XXX and YYY be the two strings we want to find the length of the LCSLCSLCS of, and XiX_iXi​ === <X0X_0X0​ , X1X_1X1​,..., XiX_iXi​> and YjY_jYj​ === <Y0Y_0Y0​, Y1Y_1Y1​,..., YjY_jYj​>.

XiX_iXi​ and YjY_jYj​ are called prefixes of the strings XXX and YYY.

Further, let c[i,j]c[i,j]c[i,j] be the length of the optimal LCSLCSLCS of the prefixes XiX_iXi​ and YjY_jYj​. Then we can have the relation:

c[i,j]={0Β ifΒ i=0Β orΒ j=0c[iβˆ’1][jβˆ’1]+1Β ifΒ i,j>0Β andΒ xi=yimax⁑(c[i,jβˆ’1],c[iβˆ’1,j])Β ifΒ i,j>0Β andΒ xiβ‰ yjc[i, j]=\left\{\begin{array}{ll}0 & \text { if } i=0 \text { or } j=0 \\ c[i-1][j-1]+1 & \text { if } i, j>0 \text { and } x_{i}=y_{i} \\ \max (c[i, j-1], c[i-1, j]) & \text { if } i, j>0 \text { and } x_{i} \neq y_{j}\end{array}\right.c[i,j]=βŽ©βŽ¨βŽ§β€‹0c[iβˆ’1][jβˆ’1]+1max(c[i,jβˆ’1],c[iβˆ’1,j])​ ifΒ i=0Β orΒ j=0Β ifΒ i,j>0Β andΒ xi​=yi​ ifΒ i,j>0Β andΒ xi​=yj​​

β€£
What is the recurrence for the LPS (longest palindromic subsequence) problem?
β€£
What is the recurrence for the LRS (longest repeated subsequences) problem?
β€£
What is the recurrence for the SCS (shortest common supersequence) problem?
β€£
What is the recurrence for the LIS (longest increasing subsequence) problem?
β€£
What is the recurrence for the Matrix Chain Multiplication problem?
β€£
0-1 Knapsack problem (discrete)
β€£
0-1 Knapsack problem (continuous)
β€£
Diff Utility
04-dynamic-programming.pdf171.6KB