COMP3010: Algorithm Theory and Design

Daniel Sutantyo,  Department of Computing, Macquarie University

4.2 - Developing a DP Algorithm

4.2 - Developing a DP Algorithm

Developing a DP Algorithm

  • Questions:
    • Does the problem have the optimal substructure property?
    • Does the problem have overlapping subproblems?
  • If the answer is yes to both of these questions, then we can use dynamic programming to solve the problem

4.2 - Developing a DP Algorithm

Developing a DP Algorithm

  • In developing a dynamic-programming solution, we follow a sequence of four steps (from CLRS, page 359)
    1. Characterise the structure of an optimal solution
    2. Recursively define the value of an optimal solution
    3. Compute the value of an optimal solution
    4. (optional) Construct an optimal solution from computed information

4.2 - Developing a DP Algorithm

Developing a DP Algorithm

  • ... or informally:
    1. Show that there is an optimal substructure
    2. Show the recursive relation that gives optimal solution
    3. Find the VALUE of the optimal solution
      • e.g. find the length of the LCSS
    4. (optional) Find the COMBINATION that gives the optimal solution
      • e.g. find the LCSS itself (not just the length)

4.2 - Developing a DP Algorithm

Rod Cutting

  • We have one piece of rod that we can cut into smaller pieces. Suppose that cutting a rod is free and we can sell a rod of length \(i\) for \(p_i\). Given a rod of length \(n\), how should we cut it up to maximise our revenue?

\(i\)

\(p_i\)

1     2     3     4     5     6     7     8     9     10

1     5     8     9    10   17   17   20   24    30

4.2 - Developing a DP Algorithm

Rod Cutting

\(i\)

\(p_i\)

1     2     3     4     5     6     7     8     9     10

1     5     8     9    10   17   17   20   24    30

$9

$1

$8

$8

$1

$5

$5

$1

$1

$1

$1

$1

$1

$1

$1

$1

$1

$5

$5

$5

4.2 - Developing a DP Algorithm

Rod Cutting

\(i\)

\(p_i\)

1     2     3     4     5     6     7     8     9     10

1     5     8     9    10   17   17   20   24    30

$20

4.2 - Developing a DP Algorithm

Rod Cutting

\(i\)

\(p_i\)

1     2     3     4     5     6     7     8     9     10

1     5     8     9    10   17   17   20   24    30

$9

$9

4.2 - Developing a DP Algorithm

Rod Cutting

\(i\)

\(p_i\)

1     2     3     4     5     6     7     8     9     10

1     5     8     9    10   17   17   20   24    30

$9

$9

4.2 - Developing a DP Algorithm

Rod Cutting

\(i\)

\(p_i\)

1     2     3     4     5     6     7     8     9     10

1     5     8     9    10   17   17   20   24    30

$9

$9

$17

$5

4.2 - Developing a DP Algorithm

Rod Cutting

\(i\)

\(p_i\)

1     2     3     4     5     6     7     8     9     10

1     5     8     9    10   17   17   20   24    30

4.2 - Developing a DP Algorithm

Rod Cutting

\(i\)

\(p_i\)

1     2     3     4     5     6     7     8     9     10

1     5     8     9    10   17   17   20   24    30

4.2 - Developing a DP Algorithm

Rod Cutting

4.2 - Developing a DP Algorithm

Rod Cutting

4.2 - Developing a DP Algorithm

Rod Cutting

4.2 - Developing a DP Algorithm

Rod Cutting

4.2 - Developing a DP Algorithm

Rod Cutting

4.2 - Developing a DP Algorithm

Rod Cutting

1

4

6

4

1

4.2 - Developing a DP Algorithm

Rod Cutting

  • no cut: \(\binom{4}{0}\) = 1 way
  • cut in 1 place: \(4 \choose 1\)  = 4 ways
  • cut in 2 places: \(4 \choose 2\)  = 6 ways
  • cut in 3 places: \(4 \choose 3\)  = 4 ways
  • cut in 4 places: \(4 \choose 4\)  = 1 way

4.2 - Developing a DP Algorithm

Rod Cutting

  • If n = 5, you have 4 possible places to cut
  • In total, there are \(2^{n-1}\) possible combinations
    • hence the brute-force approach is \(O(2^n)\)

4.2 - Developing a DP Algorithm

Rod Cutting

  • Do you see the problems and subproblems?
  • Do you see the overlapping subproblems?

4.2 - Developing a DP Algorithm

Rod Cutting

4.2 - Developing a DP Algorithm

Rod Cutting - Overlapping Subproblems

4.2 - Developing a DP Algorithm

Rod Cutting - Overlapping Subproblems

4.2 - Developing a DP Algorithm

Rod Cutting - Overlapping Subproblems

4.2 - Developing a DP Algorithm

Rod Cutting - Overlapping Subproblems

4.2 - Developing a DP Algorithm

Rod Cutting - Overlapping Subproblems

4.2 - Developing a DP Algorithm

Rod Cutting - Overlapping Subproblems

4.2 - Developing a DP Algorithm

Rod Cutting - Overlapping Subproblems

4.2 - Developing a DP Algorithm

  • New rule:
    • After each cut, don't cut the left most segment any further
    • Why? Symmetry

Rod Cutting - Overlapping Subproblems

4.2 - Developing a DP Algorithm

so where are the overlapping subproblems?

Rod Cutting - Overlapping Subproblems

4.2 - Developing a DP Algorithm

  • so our game plan is to find the optimal price when we have a rod of length 1, length 2, length 3, etc
  • this is NOT the same with the price of a rod of length 1, length 2, length 3, etc

Rod Cutting - Overlapping Subproblems

4.2 - Developing a DP Algorithm

Rod Cutting - Overlapping Subproblems

\(i\)

\(p_i\)

1     2     3     4     5     6     7     8     9     10

1     5     8     9    10   17   17   20   24    30

\(i\)

optimal

1     2     3     4     5     6     7     8     9     10

1     5     8    10   13   17   ??     ??     ??     ??

4.2 - Developing a DP Algorithm

Rod Cutting - Optimal Substructure

  • Does the problem exhibit optimal substructure?
  • You have to show that the optimal solution to the problem contains the optimal solutions to the subproblems

4.2 - Developing a DP Algorithm

Rod Cutting - Optimal Substructure

  • To solve the problem, you have to make a choice

Rod Cutting - Optimal Substructure

  • The choice you make results in more subproblems
  • You have to show that the optimal solution to your problem MUST contain the optimal solution to your subproblems

4.2 - Developing a DP Algorithm

Rod Cutting - Optimal Substructure

  • Imagine that you already have the optimal solution to the problem
    • i.e. you already know which path to follow
    • we can show that we are using the optimal solution to the subproblem as well
  • Suppose I have the optimal solution to the problem of cutting a rod of length \(n\)
    • the solution requires that I cut the rod to length \(r\) and \((n-r)\) (the next subproblem)
    • the solution to cutting \((n-r)\) must be optimal

4.2 - Developing a DP Algorithm

Rod Cutting - Optimal Substructure

  • Suppose that the solution to the subproblem of cutting a rod of length \((n-r)\) gives you \(y\) dollars (reminder, this is NOT the price of a rod of length \((n-r)\))
  • Suppose that the optimal solution to the problem of cutting a rod of length \(n\) gives you \(\$(x+y)\) 

\(\$x\)

\(\$y\)

\(\$y\)

  • then \(\$y\) must be the best that you can do, i.e. it is optimal

4.2 - Developing a DP Algorithm

\(<\$y\)

\(<\$y\)

\(<\$y\)

Rod Cutting - Optimal Substructure

  • Why? Because if the subproblem with \((n-r)\) has a better solution, then you WILL be able to find a better solution for the problem
    • e.g. if \(z > y\), then the optimal solution should be \(\$(x+z)\), not \(\$(x+y)\)
    • so if the solution to the problem is \(\$(x+y)\), then it must be the case that \(y > z\)

\(\$x\)

\(\$y\)

\(\$y\)

\(<\$y\)

4.2 - Developing a DP Algorithm

\(<\$y\)

\(\$z>\)

\(<\$y\)

Rod Cutting - Optimal Substructure

  • Make sure you understand the logic, this is a proof by contradiction (we're doing something similar next week with greedy algorithm)
  • Summary:
    • FACT: we have the optimal solution
      • don't worry about how you get it, all it matters is that you have it
    • PROPOSITION: we must have the optimal solution to the subproblem as well
      • because if we don't, then there is a better solution than the one we are using, which means, our FACT is wrong, that's a contradiction

4.2 - Developing a DP Algorithm

Rod Cutting - Optimal Substructure

  • Another way to think about this:
    • I have the most expensive computer in the world
    • So, every component must also be the most expensive in the world
      • if my monitor is not the most expensive in monitor in the world, then I can find another monitor which is more expensive, and so my computer wouldn't be the most expensive
      • that's a contradiction

4.2 - Developing a DP Algorithm

Optimal Substructure

4.2 - Developing a DP Algorithm

  • In general, to show the existence of optimal substructure, you always follow the same steps
    1. Show that the optimal solution requires you to solve one or more subproblems and assume that you have the optimal solution 
    2. You argue that the solution to the subproblems must also be optimal
      • otherwise, you can construct a better solution for the problem using cut-and-paste technique (cut the better solution, and paste it)
      • but this is a contradiction: we assumed we have the optimal solution!
  • See page 379 of CLRS if you want a more detailed breakdown

Rod Cutting - Optimal Substructure

  • One more time, just to be clear:
  • Show that the rod cutting problem exhibits optimal structure:
    • Suppose that we have the solution to the problem of cutting a rod of length \(n\)
    • I would also have the solution to the subproblems of cutting the rods of length \((n-r)\), \(0 < r < n\), and my solution to the problem will include one of these (whichever gives me the most value)
    • The solution to the subproblem that I use, say \(y\), must also be optimal, because if it is not, then I can find a better solution to the subproblem, say \(z > y\), and this results in the better solution than what we claim to be the optimal solution

4.2 - Developing a DP Algorithm

4.2 - Developing a DP Algorithm

Developing a DP Algorithm

  • Remember that there are 4 steps:
    1. Show that there is an optimal substructure
    2. Show the recursive relation that gives optimal solution
    3. Find the VALUE of the optimal solution
    4. (optional) Find the COMBINATION that gives the optimal solution
  • We have only shown the first one, but we are going slowly with this, so don't get too worried
  • The rest should be simpler since optimal substructure is usually the part where students have problems

COMP3010 - 4.2 - Developing a DP Algorithm - Part 1 - Optimal Substructure

By Daniel Sutantyo

COMP3010 - 4.2 - Developing a DP Algorithm - Part 1 - Optimal Substructure

Identifying overlapping subproblems, optimal substructure, rod-cutting example

  • 148