dynamic programming

72
Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University Advanced Computer Programming Dynamic Programming

Upload: hugo-conceicao

Post on 25-Nov-2014

148 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

Advanced ComputerProgrammingDynamic Programming

Page 2: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

Dynamic Programming Elements

Table Element DefinitionRecursive FormulaComputing Sequence

Page 3: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

Fundamental Elements in CS

RecursionStructural Complexity

Mathematical InductionLogic

Divide-and-ConquerProgramming Language

Page 4: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

Fibonacci Numbers

DefinitionF[i] = F[i-1] + F[i-2]F[0] = 1F[1] = 1

Page 5: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

DP Elements

Table Element DefinitionF[i] is the i-th element in the sequence.

Recursive FormulaF[i] = F[i-1] + F[i-2]

Computing SequenceCompute F[2], F[3], … etc.

Page 6: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

Comparison

A direct loop implementation.A recursive implementation.One major advance of DP is to reuse the

elements that we know already.

Page 7: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

Partition

Given a set of numbers ei, determine whetherwe can partition it into two subsets, so that thesum of these two subset of numbers are equal.Given 1, 3, 3, 4, 5, the answer is “yes”.One set has 1, 3, and 4.The other set has 3, and 5.

Page 8: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

DP Elements

Table Element DefinitionLet P(i, j) be 1 if it is possible to select from the

first i elements so that the sum is j.

Recursive FormulaP(i, j) is 1 if P(i –1, j) is 1, or P(i -1, j –ei) is 1.

Computing SequenceCompute one row of P at a time.

Page 9: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

Table Computation

The numbers are 1, 3, 3, 4, 5

111110

11111101511111101401101101300001101300000001187654321

Page 10: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

Approximate String Matching

Given two string S1 and S2, find a shortest sequenceof operations that transform s1 to S2.

The operations include the following.Substitution

cat to bat

Insertioncat to cats

Deletioncat to at

Page 11: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

An example

bronzebrownzebrownzbrowncrown

Page 12: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

DP Elements

Table Element DefinitionLet E(i, j) be the minimum number of operations

to transform the first i character to the first jcharacters of S2.Let the i-th char of S1 be c and the j-th char of S2

be d.

Page 13: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

DP Elements

Recursive FormulaIf c is d, E(i, j) = E(i-1, j-1)If c is not d, E(i, j) is the minimum of the following.

E(i –1, j –1) + 1, (substitute c by d).E(i –1, j) + 1, (delete c).E(i, j –1) + 1, (insert d).

Computing SequenceCompute one row of E at a time.

Page 14: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

Table Computation

432345n432234w432123o543212r654321ceznorb

Page 15: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

Longest Common Subsequence

Given two string S1 and S2, find the longestsubstring of both S1 and S2.A is a substring of B if we can find all of A’s

characters in B in the same order they appearin A.abc is a substring of gafbfc.

Page 16: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

An example

The longest common substring of “bronze”and “crown”is “ron”.The LCS is not necessarily unique.

Page 17: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

DP Elements

Let L(i, j) be the length of LCS of the first icharacter to the first j characters of S2.Let the i-th char of S1 be c and the j-th char

of S2 be d.

Page 18: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

DP Elements

If c is d, L(i, j) = L(i, j) + 1If c is not d, E(i, j) is the maximum of the

following.E(i –1, j)E(i, j –1)

Computing SequenceCompute one row of L at a time.

Page 19: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

Table Computation

333210n222210w222210o111110r000000ceznorb

Page 20: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

Longest Increasing Sequence

Given a sequence of numbers, find thelongest subsequence in which each element isat least as large as the previous one.If the sequence is 9, 5, 2, 8, 7, 3, 1, 6, and 4,

the answer is 3.2, 3, 62, 3, 4

Page 21: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

DP Elements

Let L(i) be the length of the longestsubsequence for the first i numbers that endsat the i-th number.L(i) is the maximum of L(j) + 1 for all j that

the i-th number is greater than the j-th number.

Page 22: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

Table Computation

9, 5, 2, 8, 7, 3, 1, 6, and 4.The final answer is the maximum of all L(i).

2, 3, 12, 3, 1N/A22, 52, 5N/AN/AN/A

331222111

461378259

Page 23: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

Matrix Multiplication

Given a sequence of matrixes M1, … Mn, find theorder to multiply them so that the number ofoperations is minimized.

The number of operations of multiplying a a bmatrix to an b c matrix is ac(2b-1).Multiplication:

abc

Additiona(b-1)c

Page 24: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

An Example

(((A B) C) D)3 2 4 = 243 5 2 = 303 4 5 = 60

Total is 114.

1 1 1 11 1 1 11 1 1 1

1 1 1 1 11 1 1 1 1

1 11 11 11 1

1 1 1 11 1 1 11 1 1 11 1 1 11 1 1 1

A B C D

Page 25: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

DP Elements

Let M(i, j) be the minimum number ofmultiplications to compute the product form the i-thto the j-th matrix.

M(i, j) = mink (M(i, k) + M(k + 1, j) + rickcj)k is where we “break”the product into two halves.ri is the number of rows in the i-th matrix.ck is the number of columns in the k-th matrix.cj is the number of columns in the j-th matrix.

Page 26: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

Table Computation

432i = 1

0j = 1

03 4 2 = 242

04 2 5 = 40Min(24 + 3 2 5,40 + 3 4 5) = 54

3

02 5 4 = 40Min(40 + 4 5 4,40 +4 2 5) = 80

Min(54 + 3 5 4,24 + 40 + 3 2 4,80 + 3 4 4) = 88

4

1 1 1 11 1 1 11 1 1 1

1 1 1 1 11 1 1 1 1

1 11 11 11 1

1 1 1 11 1 1 11 1 1 11 1 1 11 1 1 1

Page 27: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

Grammar and Words

Given a binary operator on an alphabet A,and a table of results of , and a string ofcharacters taken from A, find all the possibleresuls.

Page 28: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

An Example

A = {a, b, c} ((b b) b) a

= (a b) a= c a = c

(b (b b)) a= (b a) a= a a = a

There is no way toconstruct “b”. cccc

baab

ccaa

cba

Page 29: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

DP Elements

Let C(i, j) be the set of characters we canconstruct from the substring of S from the i-thto the j-th character.C(i, j)=m{c1c2|c1C(i, m), c2C(m+1, j)}We “break”the product into two halves at m.

Page 30: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

Table Computation

2

baa aac, a4

43i = 1

bj = 1

a2

bc, a3

cccc

baab

ccaa

cba

S = b b b a

Page 31: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

DP Elements

We could also use the following definition.Let C(i, j, k) be 1 if we can construct the character k

with the substring of S from the i-th to the j-thcharacter.

C(i, j, k) = 1There exist k1, k2 in A, and m between i and j so that both

C(i, m, k1) is 1 and C(m+1, j, k2) is 1, and k1k2 = k.We “break”the product into two halves at m.

Page 32: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

Minimum Length Triangulation

Given a convex polygon, find thetriangulation that has the minimum totallength.A Triangulation is a set of non-intersecting

edges that partition a polygon into triangles.

Page 33: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

A Triangulation Example

Page 34: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

A Triangle

Every polygon edge is associated with one triangle,which can be identified by a vertex.

e e e

e e

Page 35: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

Recursion

Let T(i, j) be the minimum triangulation from vi tovj.

T(i, j) = min(T(i, k) + T(k, j) + dik + dkj), for all kbetween i and j.

i

j

k

Page 36: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

One-dimensional Shortest Path

Given a directed graph with all of its edgegoing from left to right, find a shortest pathfrom the leftmost node to the rightmost node.

Page 37: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

An Example

4 8

6

4 3 24

Page 38: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

DP Elements

Let L(i) be the length of the shortest path thatends at the i-th node from the left.L(i) = minj L(j) + distance from j to i

Page 39: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

Table Computation

4 8

6

4 3 2

Min(4+8, 6+2)Min(0+6, 8+3)Min(0+4, 4+4)40

86840

54321

4

Page 40: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

Stereo Sets

We want to ship stereo sets. Each stereo sethas a receiver and a pair of speakers.A receiver weights R, and a speaker weights S.We ship the sets by C containers. The i-th

container can ship up to Wi in weight.Find the maximum number of set that we can

ship.

Page 41: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

DP Elements

Let N(i, r) be the maximum number of speakers wecan ship with the first i container when the numberof receivers is r.

N(i, r)=maxk (S(i –1, r –k) + Wi –k R)/S)k is the number of receiver we placed in the i-th container.

The answer is the maximum r such that N(C, r) 2r.

Page 42: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

Table Computation

R = 3, S = 2, W1 = 8, W2 = 8, W3 = 5.

5

0

0

Don’t care

4

0

2

max(4+(5-3)/2,2+5/2) = 5

3

0

max(2+(8-6)/2,1+ (8–3)/2,0+ 8/2) = 4

max(5+(5-3)/2,4+5/2) = 6

21r = 0

124i = 1

max(4+(8-6)/2,2+(8-3)/2,1+8/2) = 5

max(4+(8-3)/2,2+(8/2)) = 6

4+4 = 82

max(6+(5-3)/2,5+5/2) = 7

max(8+(5-3)/2,6+(5/2)) = 10

8+2= 103

Page 43: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

DP Elements

Let N(i, r) be the maximum number of speakers wecan ship with the first i container when the numberof receivers is r.

N(i, r)=maxk (S(i –1, r –k) + Wi –k R)/S)k is the number of receiver we placed in the i-th container.

The answer is the maximum r such that N(C, r) 2r.

Page 44: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

Fast Food Chain

There are n fast food restaurants along ahighway. Their positions are given as p1,p2, …pn.Select k restaurants as suppliers, so that the

sum of distance between restaurants and theircorresponding suppliers is minimized.

Page 45: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

An Example

n = 7, k = 3p1=0, p2 = 1, p3 = 3, p4=6, p5 = 7, p6 = 9, p7 = 10.By selecting p2 , p4 , and p6 ,the sum of distance is

1 + 0 + 2 + 0 + 1 + 0 + 1 = 5

Page 46: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

DP Elements

Let C(i, j) be the minimum cost of selecting jsuppliers in the first i restaurants.C(i, j)=mink (C(i –k, j - 1)+Mk+1,i)Mk+1,i is the minimum cost of selecting a supplier

from the pk+1 to pi.

Page 47: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

Table Computation

765432i = 1

2218128310j = 1

Not usedmin(0+12,1+7,3+3,8+2,12+0)=6

min(0+9,1+4,3+1,8+0)= 4

min(0+5,1+3,3+0) = 3

min(0+2,1+0)=1

0N/A2

min(0+9,1+6,3+3,4+1,6+0)=5

Not usedNot usedNot used0N/AN/A3

Page 48: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

Just Talking

There are m male students, f female students, and ateacher in a classroom.

The teacher wants to compute the total amount ofmoney in the classroom by a “reduction”procedure.

A student tells his/her amount of money to anotherstudent (or to the teacher). The receiver computesthe sum, and relay the information to the nextreceiver.

Page 49: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

Reduction

Everyone can only speak to one person,everyone can only listen to one person, andno one can speak and listen at the same time.It takes 3 seconds for a female student to tell

a number, and 5 seconds for a male student.How to do this in the least amount of time?

Page 50: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

Examples

We have the teacher, Tom, John and Mary in theclassroom.

Tom tells John, Mary tells the teacher, then Johntells the teacher.10 seconds.

Tom tells teacher, John tells Mary, then Mary tellsteacher.8 seconds.

Page 51: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

Minimum Critical Path

A path is a route from a leaf to the root.A path is critical if it has the longest length, where

the length is defined as the sum of node costs.The minimum critical path problem is to arrange a

set of nodes into a binary tree so that the length ofthe critical path is minimized.

Page 52: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

Observation

It is always possible to find an optimalarrangement where the cost of every parent isno greater than both of its children.This implies we can always put the smallest

node at the root.

Page 53: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

An Example

3

5 5

8 8

5

5 3

10 8

Page 54: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

Two Classes of Gossips

If there are only two kinds of nodes, theminimum critical path problem can be solvedby dynamic programming.

Page 55: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

DP Elements

Let C(f, s) be the minimum critical path for a treewith f fast talkers and s slow talkers.

If f > 0, C(f, s) = T(f)+min(C(fl, sl) + C(fr, sr))fl, and sl are the number of fast/slow talkers in the left

subtree.fr, and sr are the number of fast/slow talkers in the right

subtree.

If f is 0, C(f, s) = log(s+1)

Page 56: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

Cutting Pieces

IOI 2004Given a plate of size w by h, we want to cut it

into pieces so that minimum area is wasted..There are only k sizes we can sell these pieces.Every cut must go through the entire width or

the entire length.

Page 57: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

An Example

The size of the plate is5 by 7.

The sellable sizes are 2by 3, 5 by 2 and, 7 by10.

Page 58: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

DP Elements

Let M(w, h) be the minimum waste area.M(w, h) = min (M(wl, h) + M(wr, h), M(w, hl)

+ M(w, hu))wl and wr are the width after a vertical cut.hl and hu are the height after a horizontal cut.

Page 59: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

Possible Saving

Do not cut a piece that is too narrow or tooshort.Cut till the middle is fine due to symmetry.Is it possible to prove that there exist an

optimal solution that we alwaysCut horizontally with the one of the height?Cut vertically with one of the width?

Page 60: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

A Similar Problem

What if the problem is reduced to onedimension?We have a problem similar to partition, butThe elements can be selected multiple times.We are minimize the “leftover”.

In this case you can prove the “pick one ofthe sizes”is a correct strategy.

Page 61: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

The Largest Square

We have a rectangular construction site, inwhich we want to build a square house.Some part of the land is not suitable for

construction.What is the largest house you can build?

Page 62: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

An Example

Page 63: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

DP Elements

Let S(i, j) be the maximum size one can build for ahouse whose lower left corner is at (i, j).

S(i+1,j) == S(i,j+1)S(i,j) = S(i,j+1)+1 if (i+S(i,j+1), j+S(i,j+1)) is subitable

for construction.S(i,j) = S(i,j+1) otherwise.

S(i+1,j) != S(i,j+1)S(i,j) = min(S(i+1,j), S(i,j+1)) + 1

Page 64: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

An Illustration

(i,j) (i,j)

Page 65: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

The Result

1

Page 66: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

An Example

122233201

111222112

101112012

122012211

111211201

101201222

111111111

Page 67: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

Maximum Triangle

The problem is similar to the maximumsquare.Find the maximum sized triangle in a

triangular area.The only complication is that you might have

a “up-side-down”triangle.

Page 68: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

An Example

111

21

11

2

Page 69: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

Postage Stamps Problem

We have infinite amount of stamps ofdifferent values.What is the smallest postage amount that

could not be paid by these stamps?

Stamps : 1 , 3 , 5

Need : 14 = 5 + 5 + 3 + 1

Page 70: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

DP Elements

Let A(i) be 1 if we could pay for the postageof i units, 0, otherwise.Let the postage stamps be of s1, s2, … sk units.A(i) = 1 if and only if there is an j such that

A(i –sj) is 1, where j is between 1 and k.The smallest i such that A(i) is 0 is the answer.

Page 71: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

The Messenger

There is a messenger and N recipients on a unit gridmesh.

The messenger can talk to a recipient as long as theyhave the same x or y coordinate, so he only needs tomove to the same row or column of the recipient.

Given the coordinates of all recipients, how to movethe messenger so that he could send the message toall recipients with the shortest moving distance.

Page 72: Dynamic programming

Pangfeng Liu, Advanced Computer Programming 2004, National Taiwan University

An Illustration

R

R

R

R

M

R