Dynamic programming notes
Yuxuan Wu Lv13

Dynamical programming

Definition

  1. Extremely general algorithm design technique
  2. Similar to divide and conquer idea
    1. Build up answer from smaller subproblem
    2. More general than “simple” divide and conquer
  3. Generally applies to algorithms where the brute force algorithm would be more exponential

General DP principles

  1. Optimal value of the original problem can be computed easily from some subproblem

    OPT(j,w) = max of two subproblems

  2. There are only a polynomial # of subproblems

    {(j,w)} for j = 1,…,n and w = 0,…W

  3. 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

  1. Optimal value of the original problem can be computed from some similar subproblems
  2. There are only a polynomial number of subproblems
  3. 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

image-20211126190855744

image-20211126190833866

Pseudocode and Run time analysis

image-20211126191021431

image-20211126191042181

Weighted interval scheduling

image-20211126195827075

image-20211126205148937

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

image-20211126192408375

Segmented least squares problem

Internet resources

https://kartikkukreja.wordpress.com/2013/10/21/segmented-least-squares-problem/

image-20211126193218598

image-20211126194510385

image-20211126201738137

image-20211126202242108

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

image-20211126212828442

image-20211126212844038

Optimal Binary Search Tree

image-20211128085930738

image-20211128085948209

Maximum subarray problem

Goal: Given an array x of n integer (positive or negative) find a continuous subarray whose sum is maximum

image-20211128090842263

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

image-20211128094749956

image-20211128095114799

  • 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.