39
Dynamic Programming Nattee Niparnan

Dynamic Programming Nattee Niparnan. Dynamic Programming Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Embed Size (px)

Citation preview

Page 1: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Dynamic Programming

Nattee Niparnan

Page 2: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Dynamic Programming

Many problem can be solved by D&C (in fact, D&C is a very powerful approach

if you generalized it since MOST problem can be solved by breaking it into parts)

However, some might show special behavior Optimal sub structure Overlapping sub-problem

Page 3: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Dynamic Programming

Optimal sub structure “the best” of sub solution constitute “the

best” solution E.g., MSS, Closest Pair

Overlapping sub-problem Some instances of subproblem occurs

several times

Page 4: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Optimal sub structure

The solution to the sub problem directly constitute the solution of the original problem Finding the best solution for subproblem

helps solving the original question

Page 5: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Overlapping Sub-problem

When a sub-problem of some higher level problem is the same as a sub-problem of other higher level

Page 6: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Example Fibonacci

Problem: compute F(N), the Fibonacci function of N

Def: F(N) = F(N-1) + F(N-2) F(1) = 1 F(2) = 1

Page 7: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Example F(1) = 1 F(2) = 1 F(3) = 2 F(4) = 3 F(5) = 5 F(6) = 8 F(7) = 13 F(8) = 21 F(9) = 34 F(10) = 55

Page 8: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Example F(1) = 1 F(2) = 1 F(3) = F(2) + F(1) F(4) = F(3) + F(2) F(5) = F(4) + F(3) F(6) = F(5) + F(4) F(7) = F(6) + F(5) F(8) = F(7) + F(6) F(9) = F(8) + F(7) F(10) = F(9) + F(8)

Page 9: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Example F(1) = 1 F(2) = 1 F(3) = F(2) + F(1) F(4) = F(3) + F(2) F(5) = F(4) + F(3) F(6) = F(5) + F(4) F(7) = F(6) + F(5) F(8) = F(7) + F(6) F(9) = F(8) + F(7) F(10) = F(9) + F(8)

Page 10: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Solution Tree

F(5) F(4)

F(3) F(2)

F(1)

F(3) F(2)

F(1)F(2)

Page 11: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Key Idea

If there are “overlapping” sub-problem, Why should we do it more than once?

Each subproblem should be solved only once!!!

Page 12: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Dynamic Programming Method

Top-down approach Memoization Remember what have been done, if the

sub-problem is encountered again, use the processed result

Bottom-up approach Use some kind of “table” to build up the

result from the sub-problem

Page 13: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Fibonacci Example: recursiveint fibo(int n) { if (n > 2) { return fibo(n-1) + fibo(n-2); } else return 1;}

Page 14: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Fibonacci Example: Memoization

int fibo(int n) { if (n > 2) { if (stored[n] == 0) { int value = fibo_memo(n-1) + fibo_memo(n-2); stored[n] = value; } return stored[n]; } else return 1;}

Page 15: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Fibonacci Example: Buttom up

int fibo_memo(int n) { value[1] = 1; value[2] = 1; for (int i = 3;i <= n;++i) { value[i] = value[i-1] + value[i-2]; } return value[n];}

Page 16: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Memoization

Remember the solution for the required sub-problem Well, it’s caching

Need some data structure to store the result Must know how to identify each

subproblem

Page 17: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Memoization : Defining Subproblem

The subproblem must be able to be uniquely identified So that, when we need to compute a

subproblem, we can lookup in the data structure to see whether the problem is already solved

So that, when we solve a subproblem, we can lookup in the data structure to store the solution of the subproblem

Page 18: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Code ExampleResultType DandC(Problem p) { if (p is trivial) { solve p directly return the result } else { divide p into p1,p2,...,pn

for (i = 1 to n) ri = DandC(pi) combine r1,r2,...,rn into r return r }}

Trivial Case

Divide

Recursive

Combine

Page 19: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Code ExampleResultType DandC(Problem p) { if (p is trivial) { solve p directly return the result } else { if p is solved return cache.lookup(p); divide p into p1,p2,...,pn

for (i = 1 to n) ri = DandC(pi) combine r1,r2,...,rn into r cache.save(p,r); return r }}

Look up

save

Page 20: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Memoization : Data Structure

Usually, we use an array or multi-dimension array

For example, the Fibonacci

Page 21: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Approach Preference

Bottom up is usually better But it is harder to figure out

Memoization is easy Directly from the recursive

Page 22: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Binomial Coefficient

Cn,r =how to choose r things from n things We have a closed form solution

Cn,r = n!/ ( r!*(n-r)! )

Cn,r = Cn-1,r + Cn-1,r-1

= 1 ; r = 0 = 1 ; r = n

What is the subproblem? Do we have overlapping subproblem?

Page 23: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Binomial Coefficient: sub-problem

Described by two values (n,r)

Data structure should be 2D array

Page 24: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Binomial Coefficient : Code

Can you write the recursive version of the binomial coefficient?

Can you change it into the memoization version?

Page 25: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Binomial Coefficient : Code

int bino_naive(int n,int r) {if (r == n) return 1;if (r == 0) return 1;

int result = bino_naive(n-1,r) + bino_naive(n-1,r-1);

return result;}

Page 26: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Binomial Coefficient : Memoization

int bino_memoize(int n,int r) {if (r == n) return 1;if (r == 0) return 1;

if (storage[n][r] != -1)return storage[n][r];

int result = bino_memoize(n-1,r) + bino_memoize(n-1,r-1);

storage[n][r] = result;

return result;}

Page 27: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Binomial Coefficient: bottom up Pascal Triangle

C4,2

C4,1

C5,2

Page 28: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Island Problem Given a bitmap of

aerial photographer of archipelago (chain of islands) Bitmap is black &

white White means land Black means sea

Find the largest possible square land

Page 29: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Island Problem

Input 2D array of boolean called “Land”

Size m x n Land[x,y] is true means location x,y is a

land

Output x,y the top-left coordinate of the largest

square land

Page 30: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Example0 0 0 0 0 0 0 0 1 1

0 0 0 0 0 1 0 0 0 0

0 1 1 0 0 0 1 1 1 0

0 1 1 1 1 0 1 1 1 0

0 1 1 1 1 0 1 1 1 0

0 0 1 1 1 1 0 0 1 1

0 1 1 1 1 1 0 0 1 1

0 0 1 1 1 1 0 0 0 0

1 0 1 1 1 1 1 1 0 0

0 0 0 1 0 0 0 0 0 0

(0,0)

Y-axis

x-axis

Page 31: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Example : Some of 2x2 square0 0 0 0 0 0 0 0 1 1

0 0 0 0 0 1 0 0 0 0

0 1 1 0 0 0 1 1 1 0

0 1 1 1 1 0 1 1 1 0

0 1 1 1 1 0 1 1 1 0

0 0 1 1 1 1 0 0 1 1

0 1 1 1 1 1 0 0 1 1

0 0 1 1 1 1 0 0 0 0

1 0 1 1 1 1 1 1 0 0

0 0 0 1 0 0 0 0 0 0

2x2

2x2

2x2

2x2

Page 32: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Solution : The largest square0 0 0 0 0 0 0 0 1 1

0 0 0 0 0 1 0 0 0 0

0 1 1 0 0 0 1 1 1 0

0 1 1 1 1 0 1 1 1 0

0 1 1 1 1 0 1 1 1 0

0 0 1 1 1 1 0 0 1 1

0 1 1 1 1 1 0 0 1 1

0 0 1 1 1 1 0 0 0 0

1 0 1 1 1 1 1 1 0 0

0 0 0 1 0 0 0 0 0 0

4 x 4

Page 33: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Example0 0 0 0 0 0 0 0 1 1

0 0 0 0 0 1 0 0 0 0

0 1 1 0 0 0 1 1 1 0

0 1 1 1 1 0 1 1 1 0

0 1 1 1 1 0 1 1 1 0

0 0 1 1 1 1 0 0 1 1

0 1 1 1 1 1 0 0 1 1

0 0 1 1 1 1 0 0 0 0

1 0 1 1 1 1 1 1 0 0

0 0 0 1 0 0 0 0 0 0

3x3

3x3

3x3

Page 34: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Sub-problem

What should be our subproblem? Smaller maps? Divide maps by half? Smaller square?

Page 35: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Sub-problem

What if we find all possible square? How the solution of n x n square

constitute the solution of (n+1) x (n+1)?

Page 36: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Sub-problem0 0 0 0 0 0 0 0 1 1

0 0 0 0 0 1 0 0 0 0

0 1 1 0 0 0 1 1 1 0

0 1 1 1 1 0 1 1 1 0

0 1 1 1 1 0 1 1 1 0

0 0 1 1 1 1 0 0 1 1

0 1 1 1 1 1 0 0 1 1

0 0 1 1 1 1 0 0 0 0

1 0 1 1 1 1 1 1 0 0

0 0 0 1 0 0 0 0 0 0

3x3

3x3

3x3

Land here

Page 37: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Sub-problem0 0 0 0 0 0 0 0 1 1

0 0 0 0 0 1 0 0 0 0

0 1 1 0 0 0 1 1 1 0

0 1 1 1 1 0 1 1 1 0

0 1 1 1 1 0 1 1 1 0

0 0 1 1 1 1 0 0 1 1

0 1 1 1 1 1 0 0 1 1

0 0 1 1 1 1 0 0 0 0

1 0 1 1 1 1 1 1 0 0

0 0 0 1 0 0 0 0 0 0

1x1

Land here

2x21x1

Page 38: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Recursion

Biggest[x,y] = Min(Biggest[x+1,y+1],

Biggest[x ,y+1],Biggest[x+1,y ]) + 1 if

Land[x,y]

0 if not land[x,y]

Page 39: Dynamic Programming Nattee Niparnan. Dynamic Programming  Many problem can be solved by D&C (in fact, D&C is a very powerful approach if you generalized

Sub-problem0 0 0 0 0 0 0 0 1 1

0 0 0 0 0 1 0 0 0 0

0 2 1 0 0 0 3 2 1 0

0 2 3 2 1 0 2 2 1 0

0 1 3 2 1 0 1 1 1 0

0 0 4 3 2 1 0 0 2 1

0 1 3 3 2 1 0 0 1 1

0 0 2 2 2 1 0 0 0 0

1 0 1 1 1 1 1 1 0 0

0 0 0 1 0 0 0 0 0 0