Dynamic Programming

253 downloads 4503 Views 172KB Size Report
Jun 29, 2015 ... Outline. Dynamic Programming. 1-dimensional DP. 2-dimensional DP. Interval DP. Tree DP. Subset DP. Dynamic Programming. 2 ...
Dynamic Programming Jaehyun Park CS 97SI Stanford University

June 29, 2015

Outline

Dynamic Programming 1-dimensional DP 2-dimensional DP Interval DP Tree DP Subset DP

Dynamic Programming

2

What is DP?



Wikipedia definition: “method for solving complex problems by breaking them down into simpler subproblems”



This definition will make sense once we see some examples – Actually, we’ll only see problem solving examples today

Dynamic Programming

3

Steps for Solving DP Problems

1. Define subproblems 2. Write down the recurrence that relates subproblems 3. Recognize and solve the base cases



Each step is very important!

Dynamic Programming

4

Outline

Dynamic Programming 1-dimensional DP 2-dimensional DP Interval DP Tree DP Subset DP

1-dimensional DP

5

1-dimensional DP Example



Problem: given n, find the number of different ways to write n as the sum of 1, 3, 4



Example: for n = 5, the answer is 6 5 = 1+1+1+1+1 = 1+1+3 = 1+3+1 = 3+1+1 = 1+4 = 4+1

1-dimensional DP

6

1-dimensional DP Example



Define subproblems – Let Dn be the number of ways to write n as the sum of 1, 3, 4



Find the recurrence – Consider one possible solution n = x1 + x2 + · · · + xm – If xm = 1, the rest of the terms must sum to n − 1 – Thus, the number of sums that end with xm = 1 is equal to Dn−1 – Take other cases into account (xm = 3, xm = 4)

1-dimensional DP

7

1-dimensional DP Example



Recurrence is then Dn = Dn−1 + Dn−3 + Dn−4



Solve the base cases – D0 = 1 – Dn = 0 for all negative n – Alternatively, can set: D0 = D1 = D2 = 1, and D3 = 2



We’re basically done!

1-dimensional DP

8

Implementation

D[0] = D[1] = D[2] = 1; D[3] = 2; for(i = 4; i