dynamic programming

20
7/17/2019 Dynamic Programming http://slidepdf.com/reader/full/dynamic-programming-568c2c0c932a3 1/20 DynamicProgramming Dynamicprogrammingisusedin solvingcombinatorial optimization (i.e. extremum of afunction  f  amongall input combinations, whileexcludingcombinationsaccordingtocertainconstraints), whichinvolvesasequenceof decisions, eachdecisionis achoice among multiple possibilities. The combinatorial problem canbe representedasatree : each  vertexrepresentsthestateafter auniquesequenceof decisionmade, whileheight of thevertexdenotesthenumber of decisions it hasalreadygone through, childrenof avertexform aset of all possible options for the next decision. The set of optionsispath dependent, hencein general, eachvertexhasdifferentnumberofchildren,whichissomehow relatedtothe problem size. Inshort, the complexityof the problem can bedescribedasatree withtree height T  and problem size N . Pleasenote : (1) root vertexv 0  is regardedas the startingpoint withheight zero, (2) for avertexinthe middleof thetree, v 0,a,b,c,…  denotesthestateafterselectingchoicea  fordecision1 , followed forselectingchoiceb  for decision2  andfinallyselecingchoicec  fordecision3  etc,(3) leafvertexv 0,1,2,…T  hasa height of T , isoneoftheall possible input combinations, thusall leaf verticesform afinite(but very large) feasiblesolution set, (4) sincedecision optionset is pathdependent andthe existence of constraints restricting thevalidity of some subtrees, the tree may not  befull norcomplete. Byevaluatingfunction  f  ateach leaf vertex, wecan obtain thesolution tothecombinatorial optimization. This is calledBrute force method, whichcanbeimplementedbytransversing thetree using either depth first searchorbreadth first search. Hereisan examplewith Tequalsto4, wherev n,m  denotesachildof v n , whereas v n,m,k  denotes achildof v n,m  etc. height vertices 0 v 0 1 v 0,0 v 0,1 2 v 0,0,0 v 0,0,1 v 0,0,2 v 0,1,0 v 0,1,1 3 v 0,0,1,0 v 0,0,1,1 v 0,0,1,2 v 0,0,2,0 v 0,0,2,1 v 0,1,1,0 v 0,1,1,1 v 0,1,1,2 4 v 0,0,1,1,0 v 0,0,1,1,1 v 0,0,1,1,2 v 0,0,2,0,0 v 0,0,2,0,1 Fortunately, thecombinatorial problem canbesolvedefficientlybydynamicprogrammingif it canbereformulatedin awaysuchthat(1) thereexistsoptimal substructure, i.e. optimium of theoriginal problem isconstructedfrom the optimium of oneor multiplesubproblem(s) and(2) thesubproblemsoverlapwitheachother. Condition1enablesusto represent theoriginal problem inarecursiveway, whilecondition2impliesthatbruteforcemethoddoeshavesome redundancyandhencethereareroomstospeedup. Furthermore, reformulatingtheproblem inrecursivewaymeans transformingtheoriginal treeintoanew treewith recursiveshape, governed bytherecursiveformula.Pleasenote thatvertexrepresent twodifferentconceptsin thetrees: each leaf vertex  inthebruteforcetreedenoteapossible input combination  in thecombinatorialoptimization, whileeach vertex  inthedynamicprogrammingtreedenotethe optimium of asubproblem. Besides, height of BruteforcetreeTdenotesthenumberof decisionrounds, whileheight of thedynamicprogrammingtreedenotesthenumberof iterationsneededtoreducetheoriginal problem intosimplest form subproblems, please note that Tcannot be visualised inthe dynamic programming tree.  Problem dimensionandproblem size  Verticesinboththebruteforcetree andthedynamicprogrammingtree areidentifiedbyindexvector, eachindexisin fact, abranchID (orachildvertexID). Indexvectorof bruteforcetreedenotesthesequenceof decisionmaking, and thushasadimensionof T+1  (where T  is tree height). Index vector of dynamic programming tree denotes different ways toreduceaproblem intosubproblems(insomeexample, thisisalsocalledthestatevector ), andthushasasmaller dimension, usually1, 2or3(insomeexmample, thisisalsocalledthestate dimension ). Thenumberof branchesin  bruteforcetreevariesfrom vertexto, whilethenumber of branchesindynamicprogrammingtreeisfixed(whichis governedbyrecursiveformula). Wedefineproblem sizeasthemaximum possiblenumberof branchesavertexhas.  Thus, problem size is equivalent tothe number of states indynamic programmingtree.  Typical problem Bruteforcetree Dynamicprogrammingtree  dim-1 size #leaves dim size maximum value contiguous subsequence 2 N N 2 1 N longest increasingsubsequence 2 N N 2 1 N longest commonsubsequence N 2 ≥2 2N 2 N ×N  buildingbridge N 2 ≥2 2N 2 N ×N edit distance N 2 * 2 N ×N car assembly N 2 2 N 1 N (2trees)  balancedpartition N-1 2 2 N-1 2 N ×N twopersons’ traversal of cities N-1 2 2 N-1 2 N × N (2trees)  bitonic tour N-2 2 2 N-2 2 N ×N (2trees) optimal game strategy N-2 2 2 N-2 2 N ×N makingchange (M types, Nfor each) M N M N 1 C 1

Upload: dick-chow

Post on 06-Jan-2016

6 views

Category:

Documents


0 download

DESCRIPTION

Dynamic Programming

TRANSCRIPT

Page 1: Dynamic Programming

7/17/2019 Dynamic Programming

http://slidepdf.com/reader/full/dynamic-programming-568c2c0c932a3 1/20

Dynamic Programming

Dynamic programming is used in solving combinatorial optimization (i.e. extremum of a function f among all input

combinations, while excluding combinations according to certain constraints), which involves a sequence of decisions,

each decision is a choice among multiple possibilities. The combinatorial problem can be represented as a tree : each

 vertex represents the state after a unique sequence of decision made, while height of the vertex denotes the number of

decisions it has already gone through, children of a vertex form a set of all possible options for the next decision. The

set of options is path dependent, hence in general, each vertex has different number of children, which is somehow

related to the problem size. In short, the complexity of the problem can be described as a tree with tree heightT andproblem sizeN. Please note : (1) root vertexv0 is regarded as the starting point with height zero, (2) for a vertex in the

middle of the tree,v0,a,b,c,… denotes the state after selectingchoice a fordecision 1, followed for selectingchoice b for

decision 2 and finally selecingchoice c fordecision 3 etc, (3) leaf vertexv0,1,2, …T has a height ofT, is one of the all

possible input combinations, thus all leaf vertices form a finite (but very large) feasible solution set, (4) since decision

option set is path dependent and the existence of constraints restricting the validity of some subtrees, the tree may not

 be full nor complete. By evaluating function f at each leaf vertex, we can obtain the solution to the combinatorial

optimization. This is called Brute force method, which can be implemented by transversing the tree using either depth

first search or breadth first search. Here is an example with T equals to 4, wherevn,m denotes a child ofvn, whereas

vn,m,k denotes a child ofvn,m etc.

height vertices

0 v01 v0,0 v0,1

2 v0,0,0v0,0,1v0,0,2v0,1,0 v0,1,1

3 v0,0,1,0 v0,0,1,1 v0,0,1,2 v0,0,2,0 v0,0,2,1 v0,1,1,0 v0,1,1,1 v0,1,1,2

4 v0,0,1,1,0 v0,0,1,1,1 v0,0,1,1,2 v0,0,2,0,0 v0,0,2,0,1 …

Fortunately, the combinatorial problem can be solved efficiently by dynamic programming if it can be reformulated in

a way such that (1) there exists optimal substructure, i.e. optimium of the original problem is constructed from the

optimium of one or multiple subproblem(s) and (2) the subproblems overlap with each other. Condition 1 enables us to

represent the original problem in a recursive way, while condition 2 implies that brute force method does have some

redundancy and hence there are rooms to speed up. Furthermore, reformulating the problem in recursive way means

transforming the original tree into a new tree with recursive shape, governed by the recursive formula. Please note

that vertex represent two different concepts in the trees : eachleaf vertex in the brute force tree denotea possible

input combination in the combinatorial optimization, while eachvertex in the dynamic programming tree denote the

optimium of a subproblem. Besides, height of Brute force tree T denotes the number of decision rounds, while height of

the dynamic programming tree denotes the number of iterations needed to reduce the original problem into simplest

form subproblems, please note that T cannot be visualised in the dynamic programming tree.

 Problem dimension and problem size

 Vertices in both the brute force tree and the dynamic programming tree are identified by index vector, each index is in

fact, a branch ID (or a child vertex ID). Index vector of brute force tree denotes the sequence of decision making, and

thus has a dimension ofT+1 (whereT is tree height). Index vector of dynamic programming tree denotes different ways

to reduce a problem into subproblems (in some example, this is also called thestate vector), and thus has a smaller

dimension, usually 1, 2 or 3 (in some exmample, this is also called thestate dimension). The number of branches in

 brute force tree varies from vertex to, while the number of branches in dynamic programming tree is fixed (which is

governed by recursive formula). We define problem size as the maximum possible number of branches a vertex has.

 Thus, problem size is equivalent to the number of states in dynamic programming tree.

 Typical problem Brute force tree Dynamic programming tree

  dim-1 size #leaves dim size

maximum value contiguous subsequence 2 N N2 1 N

longest increasing subsequence 2 N N2 1 N

longest common subsequence N 2 ≥ 22N 2 N×N

 building bridge N 2 ≥ 22N 2 N×N

edit distance N 2 * 2 N×N

car assembly N 2 2N 1 N (2 trees)

 balanced partition N-1 2 2N-1 2 N×N

two persons’ traversal of cities N-1 2 2N-1 2 N×N (2 trees) bitonic tour N-2 2 2N-2 2 N×N (2 trees)

optimal game strategy N-2 2 2N-2 2 N×N

making change (M types, N for each) M N MN 1 C

1

Page 2: Dynamic Programming

7/17/2019 Dynamic Programming

http://slidepdf.com/reader/full/dynamic-programming-568c2c0c932a3 2/20

knapsack problem (M types, N for each) M N MN 1 W

 job scheduling N N-- NPN = N! 1 N

function approximation M N-- NCM 2 N×M

 bin packing (n1+n2+n3= N) N N-- ? 3 n1×n2×n3

 box stacking N N-- N!3N 2 N×3

 boolean parenthesizations (N operators) N ? ? 2 N×N (2 trees)

Bell number, Dijkstra’s shortest path 

TraderRun’s recursive least square Note : N! > 2N > N2 > NlnN > N > lnN

TraderRun’s min max error problem  where * = (NC0)2+(NC1)

2+(NC2)2+…+(NCN)

2≠ 2N

 There are multiple ways to do brute force search, which results in vertex with different dimensions and sizes, there are

also multiple ways to do dynamic programming, which also results in vertex with different dimensions and sizes, thus

the figures in the above table is just a typical value only. Suppose a T-round combinatorial optimization with problem

size N being reformulated as a two dimensional (for example) dynamic programming with problem size M×N, then :

)( ...,,,],1[].1[

210   T 

iiiiT t  N i

v  f  opt ∈∀∈  =

  )( ,,

mn M m N n

u  f  opt ∈∈ =    

 

 

 

 

 

−∈−∈)( ,

1,10   mn

 M m N n

u  f  opt  g  or

=   

 

 

 

 

−∈∈

)( ,1,

1   mn M m N n

u  f  opt  g  or

=   

 

 

 

 

∈−∈)( ,

,12   mn

 M m N n

u  f  opt  g  or other recursive forms

Maximum value contiguous subsequenceGiven a sequence ofN real numbersx1,x2, … xN, find the maximum sum of a contiguous subsequence. If all numbers

are positive, then sum of the whole sequence is the result.

Brute force solution

Letn1 andn2 be the starting position and ending position of the optimal contiguous subsequence, such thatn1,n2 ∈

[1,N]. The complexity of brute force search is thusO(N2). This problem can also be visualized in the form of a tree with

height2. There is one root vertex at height0, it hasN children (denotingN choices forn1), while each vertex at height

1 hasN children (denotingN choices forn2), forming a feasible solution withN2 leaves.

Dynamic programming (failed)

Let f(n) be the optimum of the problem with{x1,x2, … xn}, in order to reduce it into subproblems, we also need to know

 whether f(n-1) includesxn-1, thus we defineg(n) as :

)(n  f   =   10],1[],,1[

1

010

max   nn st  xnni   innnn

≤∑  =∈∈

)(n g  =  

<=

nnif  

nnif  

1

1

0

1 where },{ 10   nn   =

10],1[],,1[

1

010

maxarg   nn st  xnni   i

nnnn≤∑  =∈∈

 The recursion is :

)(n  f   =  

=−≤∃−+++

=−−+−

−   0)1())1(,...max(

1)1())1(,)1(max(

1   n g if  nmn  f   x x x

n g if  n  f   xn  f  

nnm

n

)(n g  =  

=−≤∃−≥+++

=−−≥+−

−   0)1()1(...

1)1()1()1(

1   n g if  nmn  f   x x x

n g if  n  f   xn  f  

nnm

n

 With the above definition of f(n) andg(n),m is unknown, we fail to establish dynamic programming, as it does not

show any optimal substructure property. Thus we need to reformulate the problem.

Dynamic programming

Let f(n) be the optimum of the problem with{x1,x2, … xn}, we defineg(n) as the optimum of a redefined problem.

)(n g  =   ∑ =∈

nni   i

nn x

00   ],1[max

2

Page 3: Dynamic Programming

7/17/2019 Dynamic Programming

http://slidepdf.com/reader/full/dynamic-programming-568c2c0c932a3 3/20

⇒   )(n  f   =   10],1[],,1[

..max   1

010

nnt  s xn

ni   innnn

≤∑ =∈∈

=   ∑ =∈∈

1

0010   ],[],1[

maxmax   n

ni   innnnn

 x (1) Condition 10   nn   ≤ is included in the 2 max

operators.

=   ∑ =∈∈

1

0101   ],1[],1[

maxmax   n

ni   innnn

 x (2) Equation (1)&(2) are identical. Both are brute

 force.=

  )(max],1[

i g ni∈ (3)

 Although f(n) does not exhibit optimal substructure,g(n) does !! The recursion is :

)(n g  =   ∑ =∈

nni   i

nn x

00   ],1[max

=    

  

 +∑   −

=−∈  nn

nni   i

nn x x x   ,maxmax

  1

]1,1[   00

=

 

=

>+−

1

1),)1(max(

1   nif   x

nif   x xn g  nn(4)

Hence, we firstly perform dynamic programming using equation (4), and finally solve f(N) using equation 3.

Longest increasing subsequence

Given a sequence ofN real numbersx1,x2, … xN, find the longest contiguous subsequence with increasing value. The

 brute force solution is omitted, it is the same as that of maximum value contiguous subsequence.

Dynamic programming

Let f(n) be the optimum of the problem with{x1,x2, … xn}, we defineg(n) as the optimum of a redefined problem.

)(n g  =  )1(max 0],1[0

+−∈

nnnn

  ]1,[.. 01   −∈∀≤   +   nni x xt  s ii

)(n  f   =  )1(maxmax 01

],[],1[ 010

+−∈∈

nnnnnnn

  ]1,[.. 101   −∈∀≤   +   nni x xt  s ii (5)

=  )1(maxmax 01

],1[],1[ 101

+−∈∈

nnnnnn

  ]1,[.. 101   −∈∀≤   +   nni x xt  s ii (6) Equation

(5)&(6) are identical.

=  )(max

],1[i g 

ni∈

 Although f(n) does not exhibit optimal substructure,g(n) does !! The recursion is :

)(n g  =  )1(max 0],1[0

+−∈

nnnn

=  

=>

≤++−

−11

1)1(

1

1

nor  x xif  

 x xif  n g 

nn

nn

Longest common subsequence

Given a sequence ofN symbolsx1,x2, … xN and another sequence of M symbolsy1,y2, … yM, find the longest common

subsequence, i.e. the maximum set of matched pairs{(xi1, y j1), (xi2, y j2), …}, such thati1, i2,…∈ [1,N] and j1, j2,…∈ [1,M],

 wherexi1 = y j1, xi2 = y j2, …etc. The longest common subsequence is not necessarily contiguous. Windows software –

 windiff is an example of longest common subsequence.

Brute force solution

Consider the case when there are exactlyn matched pairs, we have to selectn symbols from thexn sequence andnsymbols from theyn sequence, once they are selected, they are automatically paired in ascending order, i.e.i1 < i2 < …

< in and j1 < j2 < … < jn, the number of combinations is thusNCn ×  MCn. Since the possible number of matched pairs

ranges from0 toN, hence brute force search involves :

3

Page 4: Dynamic Programming

7/17/2019 Dynamic Programming

http://slidepdf.com/reader/full/dynamic-programming-568c2c0c932a3 4/20

leaves# =   ∑   =),min(

0 M  N 

n

 M n

 N n   C C 

=   ∑   = N nn N C 

02

)( ifN = M

≥   (   )20∑   = N n N nC  special case of Cauchy Schwarz inequality

=   N 22

Dynamic programming 1

Let f(n,m) be the length of the longest common subsequence betweenx1,x2, … xn andy1,y2, … ym. Consider the case in

 whichxik = y jk andxik+1 = y jk+1, if f(ik, jk) = k, then f(ik+1,jk+1) = k+1. Lets look at how f(n,m) changes, horizontal axis denotes

n, while vertical axis denotesm.

 f(n,m) ik-1 ik ik+1 ik+2 ik+3 ik+4 … ik+1-1 ik+1 ik+1+1 ik+1+2

 jk-1 k-1 k-1 k-1 k-1 k-1 k-1 … k-1 k-1 k-1 k-1

 jk k-1 k k k k k … k k k k

 jk+1 k-1 k k k k k … k k k k

 jk+2 k-1 k k k k k … k k k k

 jk+3 k-1 k k k k k … k k k k

 jk+4 k-1 k k k k k … k k k k

… … … … … … … … … … … jk+1-1 k-1 k k k k k … k k k k

 jk+1 k-1 k k k k k … k k+1 k+1 k+1

 jk+1+1 k-1 k k k k k … k k+1 k+1 k+1

 jk+1+2 k-1 k k k k k … k k+1 k+1 k+1

 The recursive formula is different for different values ofn andm. For convenience, lets defineg(n,m) as a state

 variable, which has 4 possible states : red state, green state, blue state and black state. The recursion of g(n,m) is :

),(   mn g  =

≠=

=−=−

otherwiseif   y xif  black  y xif  red 

 greenred mn g if   green

bluered mn g if  blue

mn

mn

,)1,(

,),1(

=−≠

=−≠

=

otherwiseand  y xif  black 

 greenred mn g and  y xif   green

bluered mn g and  y xif  blue

 y xif  red 

mn

mn

mn

mn

,)1,(

,),1(

RHS of the above recursion formula is incorrect, which may result in one to many matching or many to one matching.

For example, consider the case whenxik = y jk = xik+1 or xik = y jk= y jk+1. Now, with the help ofg(n,m), the recursion of

 f(n,m) is obvious :

),(   mn  f   =

=−=−=−−=−=+−=+−−=+−=−=+−−=+−=+−=+−−

black mn g if  mn  f  mn  f  mn  f  

 greenmn g if  mn  f  mn  f  mn  f  

bluemn g if  mn  f  mn  f  mn  f  

red mn g if  mn  f  mn  f  mn  f  

),()1,(),1()1,1(

),()1,(1),1(1)1,1(

),(1)1,(),1(1)1,1(

),(1)1,(1),1(1)1,1(

(7)

Dynamic programming 2

However, this is not the simplest recursion. Now, we will try to rewrite the recursion for f(n,m) andeliminateg(n,m) by

grouping the cases in equation 7.

4

Page 5: Dynamic Programming

7/17/2019 Dynamic Programming

http://slidepdf.com/reader/full/dynamic-programming-568c2c0c932a3 5/20

),(   mn  f   =

≠=−=−=−−

≠=−=+−=+−−

==−=+−=+−−

≠=+−=−=+−−

==+−=−=+−−

==+−=+−=+−−

mn

mn

mn

mn

mn

mn

 y ximplieswhichblack mn g if  mn  f  mn  f  mn  f  

 y xand  greenmn g if  mn  f  mn  f  mn  f  

 y xand  greenmn g if  mn  f  mn  f  mn  f  

 y xand bluemn g if  mn  f  mn  f  mn  f  

 y xand bluemn g if  mn  f  mn  f  mn  f  

 y ximplieswhichred nm g if  mn  f  mn  f  mn  f  

 _ ),()1,(),1()1,1(

),()1,(1),1(1)1,1(

),()1,(1),1(1)1,1(

),(1)1,(),1(1)1,1(

),(1)1,(),1(1)1,1(

 _ ),(1)1,(1),1(1)1,1(

=

≠=−=−=−−

≠=−=+−=+−−

≠=+−=−=+−−

==−=+−=+−−

==+−=−=+−−

==+−=+−=+−−

mn

mn

mn

mn

mn

mn

 y ximplieswhichblack mn g if  mn  f  mn  f  mn  f  

 y xand  greenmn g if  mn  f  mn  f  mn  f  

 y xand bluemn g if  mn  f  mn  f  mn  f  

 y xand  greenmn g if  mn  f  mn  f  mn  f  

 y xand bluemn g if  mn  f  mn  f  mn  f  

 y ximplieswhichred nm g if  mn  f  mn  f  mn  f  

 _ ),()1,(),1()1,1(

),()1,(1),1(1)1,1(

),(1)1,(),1(1)1,1(

),()1,(1),1(1)1,1(

),(1)1,(),1(1)1,1(

 _ ),(1)1,(1),1(1)1,1(

(swapping rows)

=  

≠−−−−=+−−−−

mnmn

 y xif  mn  f  mn  f  mm  f  

 y xif  mn  f  mn  f  mm  f  

))1,(),,1(),1.1(max(

1))1,(),.1(),1.1(min(

Since ))1,(),.1(),1.1(min(   −−−−   mn  f  mn  f  mm  f   =   )1.1(   −−   mm  f  

))1,(),.1(),1.1(max(   −−−−   mn  f  mn  f  mm  f   =   ))1,(),.1(max(   −−   mn  f  mn  f  

Hence   ),(   mn  f   =  

≠−−=+−−

mn

mn

 y xif  mn  f  mn  f  

 y xif  mn  f  

))1,(),,1(max(

1)1,1( 

LIS vs LCS

 The longest increasing subsequence ofxn is equivalent to the longest common subsequence betweenxn andyn, where

yn is sortedxn.Can you prove it?

Building bridge

GivenN cities along the north coast of a horizonal river, having x-coordinates :x1,x2, … xN, and M cities along the

south of the river, having x-coordinates :y1,y2, … yN. Suppose there areK = max(N,M) unique labelsL1,L2,L3, … LK,

 which are randomly assigned to the cities on the north coast and the cities in the south coast, so that cities on the

same coast have different labels. We can now build a bridge joining a city on the north coast and a city on the south

coast with the same label. What is the maximum number of bridge that we can build without crossing each other?

Dynamic programming

If we sort the cities by x-coordinates, and draw a line joining cities with the same label, we have for example :

L2  L6  L5  L3  L7  L1  L4

x1  x2  x3  x4  x5  x6  x7

y1  y2  y3  y4  y5  y6  y7

L5  L3  L1  L7  L2  L4  L6

Maximizing the number of city pair having identical label without crossing is thus equivalent to a special case of the

longest common subsequence, in which all elements in the sequence are unique (city label does not duplicate). There

exist some solutions in the web claiming that the building bridge problem can be solved by applying the longest

increasing subsequence, however this is incorrect. Applying thelongest increasing subsequence for solving building bridge problem provides a feasible butnon optimium result only !!

5

Page 6: Dynamic Programming

7/17/2019 Dynamic Programming

http://slidepdf.com/reader/full/dynamic-programming-568c2c0c932a3 6/20

T2(4)

T1(4)

Edit distance

Given two strings with lengthN andM, A={a1,a2,a3,…,aN} andB = {b1,b2,b3,…,bM}, edit distance is defined as the

minimum number of operations needed to convert string A into stringB, providing 3 different valid operations : (1)

delete a character from A, (2) insert a character into A, (3) modify a character in A.

Brute force solution

Suppose thereK modifications, wheremin(N,M)≤  K≤  max(N,M), then there will beN-K deletions andM-K insertions.

 The number of combinations will then beNCK× MCK, as we need to pickK characters from string A and from stringB.

Besides,K ranges from zero tomin(N,M), hence the total number of combinations are :

 M  M  N 

 N  M  N 

 M  N  M  N  M  N  C C C C C C C C ),min(),min(221100

  ...++++

Dijkstra’s algorithm

 This is the same as finding Dijkstra’s shortest path in a graph. Each vertex denotes a unique string, there are infinity

many vertices. There exists an edge between vertexi and j if stringi can be converted into string j by one operation,

hence a vertex denoting an-characters string has28n+1 neighbours (which includesn possible deletions,n+1 possible

insertions and26n possible modifications). The problem becomes finding the shortest path from vertex representing

string A to vertex representing string B with minimum cost, providing that cost of each edge is1. This is a huge graph

 with numerous edges. Running Dijkstra’s algorithm is equivalent to region growing from vertex A, it stops when vertex

B is poped from the priority queue (please refer to ASM notes for details of Dijkstra’s algorithm), this algorithm will be

slow.

Dynamic programming

If this problem is handled using dynamic programming, it is similar to the longest common subsequence. Let f(n,m) be

the edit distance between string{a1,a2,a3,…,an} and string{b1,b2,b3,…,bm}, then recursion is :

subproblem with string {a1,a2,a3,…,an-1} and string {b1,b2,b3,…,bm} plus one deletion

subproblem with string {a1,a2,a3,…,an} and string {b1,b2,b3,…,bm-1} plus one insertion

subproblem with string {a1,a2,a3,…,an-1} and string {b1,b2,b3,…,bm-1} plus one modification if an ≠  bm

⇒   ),(   mn  f   =  

=−−+−+−≠+−−+−+−

mn

mn

baif  mn  f  mn  f  mn  f  

baif  mn  f  mn  f  mn  f  

))1,1(,1),1(,1)1,(min(

)1)1,1(,1),1(,1)1,(min(

If we visualise the above dynamic programming as a graph, i.e. f(n,m) is a value assigned to vertexvn,m, and each edge

denotes an operation, then each vertex has3 neighbours only, which is simpler than the Dijkstra’s algorithm.

Car assembly

Suppose there are 2 production lines producing the same product, each line has N steps. Letx1, x2, x3, …, xN andy1,

y2, y3, …, yN be the processing time for each step in production line 1 and production line 2 respectively. After

completing stepn, we can transfer the intermediate product between the two lines, so that it can continue with step

n+1. Lett1,n andt2,n be the transfer time from line 1 to line 2 and from line 2 to line 1 respectively. What is the

minimum time for completing all steps in sequence?

Brute force solutionBefore starting step 1, we need to select one production line out of two. After completing stepn∀ n∈[1,N-1], we need to

choose between keeping the same production line or switching to another production line. Thus, the total number of

possible routes is2N.

Dynamic programming

LetT1(n) andT2(n) be the minimum time needed to complete stepnin production line 1 and stepnin production line 2

respectively. By visualising two production lines, we can derive recursion formula as :

x1 x2 x3 x4 … xN-2 xN-1 xN  T1(N)

t1,1 t2,1 t1,2 t2,2 t1,3 t2,3 t1,4 t2,4 t1,N-2 t2,N-1t1,N-1 t2,N-1

y1 y2 y3 y4 … yN-2 yN-1 yN T2(N)

6

Page 7: Dynamic Programming

7/17/2019 Dynamic Programming

http://slidepdf.com/reader/full/dynamic-programming-568c2c0c932a3 7/20

)(1   nT  =   ))1(,)1(min( ,221   nnn   t  ynT  xnT    ++−+−

)(2   nT  =   ))1(,)1(min( ,112   nnn   t  xnT  ynT    ++−+−

 where )1(1T  =   1 x

)1(2T  =   1 y

7

Page 8: Dynamic Programming

7/17/2019 Dynamic Programming

http://slidepdf.com/reader/full/dynamic-programming-568c2c0c932a3 8/20

Balanced partitions

Given a set ofN floating points{x1, x2, …, xN}, design an algorithm to separate the set into two, such that the absolute

difference between the sum of each set is minimised.

Brute force solution

Brute force search can be implemented by putting the first integerx1 into set1, all the otherN-1 integers should then

 be put into either set1 or set2. Thus there are2N-1 combinations in total.

Dynamic programming

If we define f(N) as the minimum absolute difference between the sum of each set, we cannot derive recursion, because

the minimum absolute difference f(N) is nothing related to f(N-1) or f(N-2)… etc. We need to reformulate the problem.

Instead of directly make a recursion on the absolute difference, we may think about the question ‘How many different

sums can we make with the subsets of X’ ? The total number of subset of X isNC0 +NC1 +NC2 + … = 2N. The possible

sums of Xn = {x1, x2, …, xn} can be constructed from the possible sums of Xn-1 = {x1, x2, …, xn-1} by :

},{ nY  y

 X Y  y   ⊂∀∑∈ =

  },{},{ 11   −∈

−∈

⊂∀+∩⊂∀   ∑∑   nY  y

nnY  y

 X Y  x y X Y  y

 Thus here is our dynamic programming : let f(n,:) be the set of sums constructed from{x1, x2, …, xn}.

)0,1(  f   =   0

)1,1(  f   =   0 x

)2,(   mn  f   =   ),1(   mn  f     −   ]12,0[  1 −∈∀   −n

m

)12,(   +mn  f   =   n xmn  f     +−   ),1(   ]12,0[   1 −∈∀   −nm

Hence the sum of all numbers in set from{x1, x2, …, xn} is :

∑   ∈   n X  x  x =   )1)12(2,(

  1 +−−nn  f  

=   )12,(   −nn  f  

Scan through the set f(N,:), find the sum that is nearest to f(N,2N-1)/2, then the minimum absolute difference is :

∑∑∈∈=∩⊂⊂

'0',',min

Y   yY   yY  Y   X  Y   X  Y   y y

 N  N 

=   |))12,((|   vv N   f     N  −−−  where

|2/)12,(|minarg:),(

−−=∈

 N 

 N   f  u N   f  uv

=   |2)12,(|   v N   f     N  −−  

Modified problem : integer

If the given set is bounded integers instead of floating points, i.e.xn∈[0,M]∀ n∈[1,N], the dynamic programming can be

further speeded up by redefining f(n,m) as a boolean, which is true if there exists a subset of{x1, x2, …, xn} with sum

equals tom and false otherwise.

),1(   m  f   =

==

otherwiseif    false

 xmif  true

mif  true

1

0

],0[   M m∈∀

),(   mn  f   =

=−−

=−

otherwiseif    false

true xmn  f  if  true

truemn  f  if  true

n ),1(

),1(

],0[   n M m   ×∈∀

 The main difference in implementation between the floating point scenario and the integer scenario is thatf(n,:) is a

set<float> with variable size in the former case, while it is avector<bool> with fixed size in the latter case.

 Two persons’ traversal of cities

Given an ordered sequence ofN cities (in any dimensional space) and the matrixxn,m ∀ n,m∈[1,N] denoting the distance

 between every pair of cities, find algorithm to divide the set into two, so that when person A visits all the set 1 cities in

8

Page 9: Dynamic Programming

7/17/2019 Dynamic Programming

http://slidepdf.com/reader/full/dynamic-programming-568c2c0c932a3 9/20

sequence, while person B travels all the set 2 cities in sequence, the total travelling distance is minimised. This can be

regarded as a special case of travelling salesman problem with : (1) two salesmen and (2) predefined ordered cities.

Brute force solution

Brute force search can be implemented by putting the first city into set1, all the otherN-1 cities should then be put

into either set1 or set2. Thus there are2N-1 combinations in total.

Dynamic programming (fail)

 This is completely different from the car assembly problem, we cannot apply the car assembly solution here. However,

car assembly problem can be made closer to this problem by introducing many more machines in the production

lines, which can perform multiple contiguous steps, we are then given a processing time matrixxn,m n<m, which

denotes the processing time needed for completing stepsn+1, n+2, …, m continuously. In this case, the solution of

traversal of cities problem may be applicable to the car assembly problem after some modifications : (1) there are two

processing time matrices, one for production line 1 and one for production line 2, (2) switching between the two

production lines involves transfer time.

Dynamic programming

 We need to define astate variable on which recursion formula is constructed (in this example, we can treat the index

 vector as a state vector). Recursion formula is the relationship between the optimium of problem with the optimium of

subproblem plus delta change. In this problem, the delta change would probably be the distance matrix. In order to

apply distance matrix, we need to know where A and B is standing right now. For car assembly problem, the optimum

depends only on the last completed task. For traversal of cities, the optimum does not only depend on the last visitedcity, but depends on the last visited city by A and the last visited city by B. In other words, this is a two dimensional

state variable(n,m). Lets define f(n,m) be the shortest total distance travelled by A and B, when A ends at cityn and B

ends at citym, this is equivalent to saying that :

 Among the N ordered cities, the frst max(n,m) o them orm a subproblem, which has an optimum o (n,m), inwhich the last city visited by A is n, the last city visited by B is m, the range o cities starting rom min(n,m)+,min(n,m)+!, min(n,m)+", #and up to max(n,m) are visited by the same traveller, we don$t %now whether he is Aor B, we %now that he is the one who stopped at city max(n,m)& Besides, we do not %now the starting point o Aand B, we only %now that i the starting point o A and B are n ' and m' respectively, then min(n', m' ), and thetraveller who starts with city should also visit the ollowing cities in order , !, ", # max(n', m' )*& urthermore,(,m) means A visited city and stops here, all the other cities are visited by B in order !, ", , #, m, while (n,)means B visied city and stops here, all the other cities are visited by A in order !, ", , #, n&

-n general, we have (n,m) (m,n), because the path o A and B can be swapped without a.ecting the result, please note that (n,n) is invalid, as A, B cannot visit the same city& /hus, there is no need to calculate all elementsin matrix (n,m), we can consider cases with n0m only& 1ith n0m, we are not  assuming that B visits the last city,because (m,n) does exist and valid, it e2uals to (n,m), we s%ip it in dynamic programming simply or the sa%e o e3ciency& -n other words, (n,m) is not a triangular matrix, but a symmetric matrix, excluding diagonal& 4emindthat (n,m) is a state vector, while (n,m) is the optimum at this state&

How many paths which can reach the state(n,m) (supposen<m)? The answer is2n-1, because cityn is visited by A, city

n+1, n+2, …, m are visited by B, thus the combinations are generated by the firstn-1 cities. Since cityn+1, n+2, …, m

must be visited by B, the subproblem of state(n,m) is straight forward : f(n,m) = f(n,m-1)+xm-1,m. However this equation

doesn’t work when m = n+1, as f(n,n) is invalid, in this case, f(n,m) should be reduced into f(1,m), f(2,m), … f(n-1,m).

Lets think about how we proceed from state(n’m) to state(n,m) wheren’ < n < m = n+1.

state city 1 2 … n’ n’+1 … n-1 n m=n+1

(n’m) traveller ? ? ? A B B B B B

(n,m) traveller ? ? ? A B B B A B

distance travelled by A to reach (n’,m)= a

distance travelled by B to reach (n’,m)= b

distance travelled by A to reach (n,m) = a + xn’,n

distance travelled by B to reach (n,m) = b- xn-1,n - xn,n+1 + xn-1,n+1

total distance to reach (n,m) = total distance to reach (n’,m) + xn’,n - xn-1,n - xn,n+1 + xn-1,n+1(8)

 The only exception to equation (8) is when n’=n-1.

state city 1 2 … n’=n-1 n m=n+1

(n’m) traveller ? ? ? A B B

(n,m) traveller ? ? ? A A B

9

Page 10: Dynamic Programming

7/17/2019 Dynamic Programming

http://slidepdf.com/reader/full/dynamic-programming-568c2c0c932a3 10/20

distance travelled by A to reach (n’,m)= a

distance travelled by B to reach (n’,m)= b

distance travelled by A to reach (n,m) = a + xn’,n

distance travelled by B to reach (n,m) = b- xk,n - xn,n+1 + xk,n+1

total distance to reach (n,m) = total distance to reach (n’,m) + xn’,n - xk,n - xn,n+1 + xk,n+1

Indexk is the third last visited city by traveller B. Ifk doesn’t exist (i.e. when B visits one or two cities only), then :

distance travelled by A to reach (n,m) = a + xn’,n

distance travelled by B to reach (n,m) = b- xn,n+1

total distance to reach (n,m) = total distance to reach (n’,m) + xn’,n - xn,n+1

Recursion cannot be completed without solving indexk. Lets defineg(n,m) as A’s path (a set of city indices) so as to

reach the optimum f(n,m), while functionh(path) returns the third last visited city by traveller B given an input path, it

returns zero if the third last visited city doesn’t exist. We also definexn,0=0 andx0,m=0 for consistence of formula.

10

Page 11: Dynamic Programming

7/17/2019 Dynamic Programming

http://slidepdf.com/reader/full/dynamic-programming-568c2c0c932a3 11/20

),1(   m  f   = distance travelled by A + distance travelled by B from city 2, 3, … to m

=   )..()( ,15,44,33,21,1   mm x x x x x −++++

=   ∑   −=   +

12   1,mi   ii x (1) This is invalid for m=1.

(2) This is zero for m=2.

),1(   m g  = {1} (3) Traveller A visits city 1 only.

 The recursion of f(n,m) andg(n,m) are :

),(   mn  f   =

+=+−−+

+>+−

++−∈

+1)),'((min

1)1,(

1)),,'((1,)),,'((,']1,1['

1,

nmif   x x x xmn  f  

nmif   xmn  f  

nmn g hnnnmn g hnnnn

mm

),(   mn g  =  

+=∪+>−1}{),(

1)1,(

nmif  nmk  g 

nmif  mn g  where

)),'((minarg 1)),,'((1,)),,'((,']1,1['

++−∈

+−−++=   nmn g hnnnmn g hnnnn

 x x x xmn  f  k 

Finally : opt   f   =  ),(min]1,1[

 N n  f   N n   −∈

In order to implement the recursion, we have to scan f(n,m) by iterating throughn externally and iterating throughm

internally. We can see that the pathg(n,m) doesn’t grow asm increases,until++n, such thatm = n+1.

n 1 2 3 4 5 6m1 x x x x x x (1) Diagonal elements are invalid

2 ! x x x x x (2) " denotes elements #$dated b% : f(n,m) = f(n,m-1)+xm-1,m 

3 " ! x x x x (3) ! denotes elements #$dated b% : f(n,m) = min(…)4 " " ! x x x5 " " " ! x x for(n&1:'1)6 " " " " ! x for(m&n*1:') if(m&&n*1) f(n,m)&min()+ " " " " " ! else f(n,m)&f(n,m1)*x(m1,m)+ -

fo$t&min(f(1:'1,'))+

 This is what happens inside functionh.

#nsigned long void .(std::set<#nsigned long>/ g, #nsigned long m)

std::set<#nsigned long>::reverse0iterator i&grbegin()+ #nsigned long co#nt&, mm&m+.ile(i&grend() // mm>) if (mm>"i) **co#nt+ if (co#nt&&3) ret#rn mm+ -

else **i+mm+ -

ret#rn +-

 The official solution provided by MIT is incorrect as (1) it ignores the red part of the above equations, and therefore (2)

it does not record traveller A’s optimal path.Can you check it again?

Bitonic tour

 This is another specialized travelling salesman problem : travelling salesman problem with bitonic tour. Bitonic tour is

defined as a path joining multiple points in 2D space, such that the x coordinate of the path keeps increasing until it

reaches a maximum, and then decreases until it reaches the starting point. Another equivalent definition of bitonic

tour is a path that always cuts vertical lines at exactly two points.

Brute force solution

Brute force search can be implemented by dividing theN-2 cities (excluding the LHS-most and the RHS-most cities)

into two sets, the first point set constitutes the path for out-bound trip, while second point set constitutes the path for

in-bound trip. Thus the total number of combination is2N-2.

Dynamic programming

For convenience, we assume that there areN cities (excluding the LHS-most and the RHS-most cities), with coordinate

(xn, yn)∀ n∈[1,N], while the coordinate of the LHS-most and the RHS-most cities are(x0, y0) and(xN+1, yN+1) respectively,

so thatx0 < x1 ≤  x2 ≤  x3 ≤  …≤  xN < xN+1. Since (1) cities in the bitonic tour problem have predefined order, based on x

coordinates, (2) distance matrix can be calculated using the 2D coordinates of cities, (3) the out-bound trip and in-

11

Page 12: Dynamic Programming

7/17/2019 Dynamic Programming

http://slidepdf.com/reader/full/dynamic-programming-568c2c0c932a3 12/20

 bound trip can be regarded as two travellers starting with citiesxn0 andxm0 respectively, wheremin(n0, m0) = 1., and

ending with citiesnlast andmlast, wherenlast < mlast = N. Now, letdn,m be the distance matrix. The only minor difference

 between these two combinatoric problems is the overhead time cost in the bitonic tour :

traversal of cities : min (total distance of A’s path+ total distance of B’s path)

 bitonic tour : min (total distance of outbound + total distance of inbound) + overhead

 where : overhead =   1,1),.min(),max(,01,0 00   ++   +++  N  N  N mnmn   d d d d last last 

 Therefore there is only a slight change in the initialization and final step. The recursion is exactly the same. Letdn,m bethe distance matrix.

),1(   m  f   = distance of outbound + distance of inbound

=   )..()( ,14,33,22,01,0   mmd d d d d  −++++

=   ∑  −=   +++

  12   1,2,01,0mi   ii

d d d 

),1(   m g  = {1}

Finally : opt   f   =   1,1,]1,1[

),(min ++−∈

++   N  N  N n N n

d d  N n  f  

Optimal game strategy

Consider a row ofN coins with values{x1, x2, x3, …, xN}, whereN is even. We play a game against an opponent by

alternating turns. In each turn, a player selects either the first or last coin from the row, removes it from the row

permanently, and receives the value of the coin. Determine the maximum possible amount of money we can definitely

 win if we move first. The opponent is as clever as the user.

Brute force solution

Since no decision is needed when there are two or less coins left, the total number of combination is2N-2.

Dynamic programming

Let f(n,m) be the maximum values we can get by finishing the whole subgame, which containing coins {xn, xn+1, xn+2, …,

xm}, where 0≤ n<m≤ N andm-n+1 is even. If we pick coinn, our opponent will then either pick coinn+1, leaving us withsubgame optimum f(n+2,m), or pick poinm, leaving us with subgame optimum f(n+1,m-1), our opponent will pick in a

 way so as to minimise our subgame optimum, thus he will pick coinn+1 if f(n+2,m) < f(n+1,m-1), and vice versa. The

same logic applies when we pick coinm, hence the recursion is :

),(   mn  f   =   )))2,(),1,1(min()),1,1(),,2(min(max(   −−++−+++   mn  f  mn  f   xmn  f  mn  f   x mn

Lets initialise matrix f(n,m), fill the diagonal with zero, i.e. f(n,n) = 0, and fill the lower subdiagonal with a max

operator, i.e. f(n,n+1) = max(xn, xn+1), then perform region growing with subdiagonal as multiple seeds. For example, the

 value atred * depends on the values of all the blue elements.

n 1 2 3 4 5 6 m1 x x x x x x x2 ! x x x x x x ! & s#bdiagonal3 " ! x x x x x4 " " !   0 x x x x5 " " " !   0 x x x6 " " "   * !   0 x x " " " " " ! x " " " " " " !

Please note that we should scan through f(n,m) diagonally as shown above, implementation like ‘loopingn externally,

loopingm internally’ or ‘loopingm externally, loopingn internally’ do not work.

Making change

GivenN types of coins with integer values{x1, x2, x3, …, xN}, such thatx1 = 1 < x2 < x3 … < xN, there are infinite many

number for each of them. Design an algorithm for finding the smallest number of coins, which sum to a given integer

 valueC exactly.

12

Page 13: Dynamic Programming

7/17/2019 Dynamic Programming

http://slidepdf.com/reader/full/dynamic-programming-568c2c0c932a3 13/20

Brute force solution

LetMn be the minimum number of coins needed to give a sum greater than or equal toC, when only the same kind of

coin with valuexn is used, then the total number of combination is : M1 ×  M2 ×  M3 ×  …×  MN-1 ×  MN.

Dijkstra’s algorithm

Lets review the Dijkstra algorithm. Given a graph G = {V,E},V = {v1, v2, v3, …, vN}, whereen,m = {vn, vm} andcn,m denotes

the cost for travelling from vertexn to vertexm. Our aim is to find the route with minimum cost from v1 to reachvN. We

define f(n) be the minimum cost neededv1 from to reachvn. Recursion is simple :

)(n  f   =  ))((min ,

, ,

nmem

cm  f  nm

+∃∀

Please note that the main difference between the dynamic programming in Dijkstra’s algorithm with all the previous

dynamic programming is the fact that, f(n) is updated multiple times in Dijkstra, and converges to optimal, yet f(n) is

usually calculated once in the previous dynamic programming. This is because the staten keeps increasing until it

reachesN or keeps decreasing until it reaches1 as decisions are made in the previous problems, yet in Dijkstra’s

algorithm, we can keep travelling to a new vertex within the graph unstopped, i.e. we can route an infinite long path.

Dijkstra algorithm can be implemented in two ways. (1) Direct implementation of the recursive formula using recursive

function call : this is in fact a region growing starting from vN, there is no need to implement a stack nor a queue for

the region growing, since the region growing is done using the ‘function call stack’. (2) Iterative implementation, this is

in fact a region growing starting from v1 (please refer to ASM notes), you need to implementa stack, a queue or apriority queue explicitly. However, the first method is infeasible as infinite long paths can blow up function call stack.

Dynamic programming

 The making change problem is a special case of Dijkstra algorithm, with each vertex representing a unique statec,

 which is defined as the sum of selected coins. There exists an edge between statea and stateb (whereb > a) if there

exists a coin with valuexn such thatb – a = xn. The cost of each edge is1. Now, we are looking for the minimum cost

 f(c) to reach statec starting from state0. Note : here I use state c instead of vertex v, recall that vertex is just a point on a

graph, state is a variable associated to the vertex, while f(c) is the optimum cost to reach state c.

)(c  f   =  )1)((min

],,1[+−

≥∈  m

 xc N m xc  f  

m

 where )0(  f   =   0

Unlike Dijkstra’s algorithm : (1) all edges are unidirectional, (2) there exists no edge from stateb to statea whereb > a,

and therefore (3) there exists finite number of routes. Thus we can implement the dynamic programming either by (1)

recursive function call or (2) iterative method. The recursive implementation is straight forward, while the iterative

implementation is a little bit tricky : if you do the region growing using a stack or a queue, you should wait until all

 f(c) to converge before quiting, this is slow of course, or you can do the region growing with a priority queue, like

Dijkstra’s algorithm.Thus for Dijkstra’s algorithm, we prefer iterative method, for making change, we prefer recursive

method.

Knapsack problem

GivenN types of objects with weights{x1, x2, x3, …, xN} and values{v1, v2, v3, …, vN}, there are infinite many number foreach of them. Design an algorithm for finding the maximum value of objects packed inside a knapsack, which has a

maximum load ofW . Allxn,vn andW  are real values.

Dynamic programming

Knapsack problem is very similar to the making change problem, with the following correspondence.

making change problem knapsack problem

coins’ num =   },...,,,{ 321   N mmmm objects’ num =   },...,,,{ 321   N mmmm  

coins’ value =   },...,,,{ 321   N  x x x x o bjects’ weight = },...,,,{ 321   N  x x x x

coins’ count =   1 o bjects’ value =   },...,,,{ 321   N vvvv

objective   ∑   = N n   nm1min objective   ∑   = N n   nnvm1max

constrain   C  xm N n   nn   =∑   =1 constrain   W  xm N n   nn   ≤∑   =1

 vertex state   ℵ∈c  vertex state   ℜ∈w

13

Page 14: Dynamic Programming

7/17/2019 Dynamic Programming

http://slidepdf.com/reader/full/dynamic-programming-568c2c0c932a3 14/20

Each vertex denotes a unique statec orw, which denotes the sum of selected coins’ value (in making change problem)

or the sum of selected objects’ weight (in knapsack problem). Each edge denotes a state transition, there exists an edge

 between two states (or two vertices) when there exists a coin or an object that can fulfill the state change. We define

 f(c) and f(w) as the optimum of objective functions to reach statec (in making change problem) and to reach statew (in

knapsack problem). The recursion for knapsack is then straight forward :

)(w  f   =  ))((max

],,1[mm

 xw N mv xw  f  

m

+−≥∈

 where )(w  f   =   0  when ),...,,,min( 321   N  x x x xw <

 Job scheduling

Given one machine andN jobs, each job requires processing timexn and generates profit pn if it can be completed

 within deadlinetn. The machine can process one job at a time, running job should not be interrupted until completion.

Find an algorithm to maximise profit, assumingxn ≤  tn ∀ n∈[1,N] andt1 ≤  t2 ≤  t3 ≤  … ≤  tn (processing time and deadlines

can be real values).

Dynamic programming

 Job scheduling problem can be regarded as generalised version knapsack problem. The main differences are (1) there

is only one weight limit in knapsack, while there are different deadlines for different jobs, (2) multiple objects of the

same type can be packed in knapsack, while the same job can either be done once or skipped in job scheduling.

 

knapsack  job scheduling

object’s value job’s profit

object’s weight job’s processing time

knapack max load job’s deadline

knapsack  job scheduling

objective   ∑   = N n   nnvm1max  where ℵ∈nm ∑   =

 N n   nn pm1max  where }1,0{∈nm

constrain W  xm N n   nn

  ≤∑   =1   11

1   t  xmn   nn   ≤∑   =

221  t  xm

n   nn   ≤∑   =

 N  N n   nn   t  xm   ≤∑   =1

Let f(t) be the maximum profit we can make within timet. Can we simply copy the recursion from knapsack problem?

)(t   f   =  ))((max

],,1[mm

t t  x N m p xt   f  

mm

+−≤≤∈

 where )(t   f   =   0  when ),...,,,min( 321   N t t t t t  <

 The answer is no, because it ignores the fact that each job can either be done once or skipped. Thus we should recorda set of job doneD, so that we don’t considerD in subproblem, i.e.

)(t   f   =  ))((max

,],,1[mm

t t  x Dm N m p xt   f  

mm

+−≤≤∉∈ (9)

D is different for different decision paths, it is terrible to record all possibleD. Since we make one decision for a job (we

don’t need to reconsider a job again), instead of considering which job we should eliminate to reduce to subproblem

(i.e. integerm in equation 9), we can scan through the job with nearest deadline, and consider whether to do or to

skip the job so as to reduce to subproblem (i.e. booleanm in equation 10). We can define f(n,t) as the maximum profit

that we can generate fromn jobs within final deadlinet.

),(   t n  f   =   )),1((max],1,0[

nnt t  xm

mpmxt n  f  nn

+−−≤≤∈ (10)

=  

≤≤+−−−

otherwiseif  t n  f  

t t  xif   p xt n  f  t n  f   nnnn

),1(

)),1(),,1(max(

14

Page 15: Dynamic Programming

7/17/2019 Dynamic Programming

http://slidepdf.com/reader/full/dynamic-programming-568c2c0c932a3 15/20

 where   ),(   t n  f   =   0  when ),...,,,min( 321   N t t t t t  <

Function approximation

Given a sequence ofN data points{(x1,y1), (x2,y2), (x3,y3), …, (xN,yN)}, wherex1 < x2 < x3 … < xN, all generated by function

y = f(x), we need an algorithm to fit the data points using a piecewise linear functiongM(x), havingM segments (defined

 byM+1 control points) to minimise the fitting error. TheM+1 control points must be a subset of theN data points,

 with(x1,y1) and(xN,yN) as the first and the last control point. Fitting error ofN data points usingM segments is :

),(   M  N e =   ∑   =   − N n   nn M    y x g 

12

))((

Dynamic programming

 The problem can be reduced in two dimensions : number of data points and number of control points. Lets define

(n,m) as the state,e(n,m) be the minimum error of fittingn data points usingm segments. The recurion cannot simply

 be :

),(   mne =   ...),1(   +−   mne

nor   ),(   mne =   ...)1,(   +−mne i.e.e(n-1,m) ande(n,m-1) are not the subproblem ofe(n,m)

 because all the existing best-fit segments (or control points) in the problem will changed completely (as if redoing the

regression again) when we add a new data point or add a new segment, we can understand why by considering these

two cases :

15

add a new point

add a new segment

Page 16: Dynamic Programming

7/17/2019 Dynamic Programming

http://slidepdf.com/reader/full/dynamic-programming-568c2c0c932a3 16/20

However, the optimal substructure happens when we consider the following problem and subproblem :

 This is Bellman's principle of optimality : if{a1, a2, a3, …, aK, …, aN} is the optimal path to reach stateaN, then{a1, a2,a3, …, aK} is the optimal path to reach stateaK. Hence as shown in the above example, if control points{(x1,y1), (x3,y3),

(x6,y6), (x11,y11)} is optimal path of the problem with data size 11, then control points{(x1,y1), (x3,y3), (x6,y6)}is also the

optimal path for subproblem with data size6. Thus the recursion for function approximation is :

),(   mne =   )))(()1,((min1

2

]1,1[∑   +=−∈

−+−   nk i   ii

nk  y xhmk e

 where   )1,(ne =   )))(()1,((min1

2

]1,1[∑   +=−∈

−+−   nk i   ii

nk  y xhmk e

 whereh(x) is the straight line joining the points(xk,yk)and (xn,yn) :

)( xh =   k k k n

k n y x x

 x x

 y y+−×

−)(

 The implementation is : loopingm externally and loopingn internally.

for(m&1+m&+**m)

for(n&1+n&'+**n) e(n,m) & min()+-

Bin packing

GivenN1 objects with sizes1,N2 objects with sizes2 andN3 objects with sizes3, find the minimum number of boxesneeded to pack all objects, assuming the capacity of box is C, such thats1 ≤  s2 ≤  s3 ≤  C.

Dynamic programming

 This is a little bit similar to the function approximation problem that an increase by 1 in the problem size (or state

 variable)n1,n2 orn3 will result in a complete different distribution of objects in the boxes), hence state(n1–1,n2,n3)is

not the subproblem of state(n1,n2,n3) and so on. Letm = f(n1,n2,n3) be the minimum number of boxes needed to pack all

then = n1+n2+n3 objects, and suppose the distribution of objects is :

  object 1 object 2 object 3 remaining capacity

box 1   1a 1b 1c   312111   sc sb saC    −−−

box 2   2a 2b 2c   322212   sc sb saC    −−−

… … … …

box m   ma mb mc 321   sc sb saC  mmm   −−−

subtotal   ∑   ==   mk    k an

11   ∑   ==   mk    k bn

12   ∑   ==   mk    k cn

13 table#

 With Bellman's principle of optimality, we can claim that :

),,( 111   cba  f   =   1   ),,( 212121   ccbbaa  f     +++ =   2

),,( 222   cba  f   =   1   ),,( 313131   ccbbaa  f     +++ =   2

… …

),,( mmm   cba  f   =   1   ),,( 313131   ccbbaa  f     +++ =   2

and in general we have :

16

reduce into subproblem

Page 17: Dynamic Programming

7/17/2019 Dynamic Programming

http://slidepdf.com/reader/full/dynamic-programming-568c2c0c932a3 17/20

∑⊂∈   ],1[,

),,(m    k 

k k k    cba  f  =   ||    (10) where ||     is cardinality

(size) of setK

otherwise, if any one of the above statements is not fulfilled, we can then reduce the number of boxes used to pack all

the(n1,n2,n3) objects, i.e. f(n1,n2,n3) is not the optimum, contradicting our assumptions. Now we have :

⇒    ),,( 321   nnn  f   =  ),,(),,(

'

∑∑∈∈

+  k 

k k k   k 

k k k    cba  f  cba  f  (11) where

0'},,...,2,1{'   =∩=∪∀      m    

⇒    ),,( 321   nnn  f     ≠   )',','(),,(   cba  f  cba  f     + (12) where 1'   naa   =+

2'   nbb   =+   3'   ncc   =+  

Equation(11) is valid because all the object combinations(ak,bk,ck) are one of those combinations listed in table#,

Bellman’s principle of optimality implies that each of the row or sum of the rows in the table# are optimal, as stated in

equation(10). Althougha+a’=n1,b+b’=n2 andc+c’=n3 in equation(12),(a,b,c) and(a’,b’,c’) may not be one of the rows in

table#, Bellman’s principle of optimality doesn’t imply equation(12). Thus we need to add minimise operator in the

recursion : as we loop through(a,b,c), there must exists some(a,b,c) combinations that can be found in table#.

),,( 321   nnn  f   =  )),,(),,((min 321

],0[],,0[],,0[ 321

cba  f  cnbnan  f  

ncnbna

+−−−

∈∈∈ where   ),,( 321   nnn  f   =   1  where C  sn sn sn   ≤++   332211

Box stacking

GivenN boxes, having sidessn,1,sn,2 andsn,3 ∀ n∈[1,N]. What is the highest stack of boxes, so that base of each box in

the stack isstrictly smaller than the base of the box lying below in both dimensions. The orientation of boxes can be

changed. Each box can be used once only.

Brute force solution

 To stack all boxes, with3 orientations for each box (select one out of the three faces as the base), while ignoring the

 base constraint, there areN!3N permutations in total. For each permutation, we can check the stack against the base

constraint, as we scan through the boxes in the stack starting from bottom, the scanning stop once we come across a

 violation of base constraint, we can then calculate the sum of height for the valid section of the stack. We repeat this

calculation for all permutations and return the highest one.

Dynamic programming

Lets define orientationi, wherei = {1,2,3}, then height, width and depth of box n with orientationiarehn,i,wn,i anddn,i.

inh , =   in s , i.e. different orientation will result in different height

inw , =  )(max ,},3,2,1{

  !ni  !  !

 s≠∈

ind  , =  )(min ,},3,2,1{

  !ni  !  !

 s≠∈ i.e. nn   wd    ≤

Lets define the state variable (or state vector) to describe a building stack, we should include orientation of the highest

 box in the state vector to handle the base constraint, yet this is implying that we know which one is the highest box in

a stack. Let f(n,i) be the maximum height achieved bya subset of first n boxes, with boxn having orientationi at the

 bottom of stack. Recursive approach starts stacking from the bottom, while iterative approach starts stacking from the

top. By including boxn in the subproblem optimum, that means we need to scan through f(n,i) as a final step to get

the global optimum (this is similar to the longest increasing subsequence problem, LIS). The recursion is :

),(   in  f   =   in  !nm

h  !m  f   ,}3,2,1{,1

),(max   +∈<<

opt   f   =  ),(max

}3,2,1{,1in  f  

i N n   ∈≤≤

Enhancement 1

However, there are problems with the above recursion : (1) it ignores base constraint, (2) it ignores possible optimum

having boxm at the bottom and boxn lying above it (wherem<n). To solve the problem, we need to modify the problem

a little bit : change in orientation is limited, we can swap between width and depth only (sn,2 andsn,3), while height is

17

Page 18: Dynamic Programming

7/17/2019 Dynamic Programming

http://slidepdf.com/reader/full/dynamic-programming-568c2c0c932a3 18/20

fixed (sn,1), besides we need to modify the solution a little bit : boxes should be sorted in ascending order of base area

 before recursion starts, so that if boxm is selected as the bottom of the stack, then boxn will never be included in the

stack (wherem<n). The recursion becomes :

),(   in  f   =    

  

 +

<<∈<<  1,1,

,},3,2{,1,),(maxmax

,,,,

nnd d ww  !nm

hh  !m  f  in  !min  !m

(13)

opt   f   =  ),(max

}3,2{,1in  f  

i N n   ∈≤≤

Conditionswm,j<wn,i anddm,j<dn,i are added to equation (13), since area comparison is not sufficient to execute base

constraint. Besides j=2,3 only as the change in orientation is limited. Finally, if nom fulfills base constraint, then f(n,i)

equals to height of boxn, i.e. it is the only box in the stack.

Enhancement 2

Now, lets relax the restriction, if orientation is free to change (i.e. width, depth and height can be interchanged), then

how can we enhance the algorithm? The answer is to generate3 copies for a box, with heighthn,1,hn,2 andhn,3, then

sort all3N boxes by base area, perform recursion in equation (14). In this case, each box may be used more than once.

)(n  f   =    

  

 +

<<<<  nn

d d ww N mhhm  f  

nmnm

,)(maxmax,,31

(14)

opt   f   =  )(max31

n  f   N n≤≤

In conclusion, this problem can be solved using dynamic programming if (1) orientation of boxes cannot be changed, each

box can be used once or (2) orientation of boxes can be changed, each box can be used more than once. In both cases, we

should sort the box by base area. The implementation for enhancement 2 is :

for(n&1+n<&3"'+**n)

do#ble max0val#e & +for(m&1+m&n+**m) if ((m)<(n) // d(m)<d(n) // 7an e c.ec8 against t.e o$tim#m $at. of f(m),

max0val#e<f(m)*.(n)) to see if box n is alread% #sed in f(m)9max0val#e&f(m)*.(n)+

-f(n)&.(n)+if (max0val#e>) f(n)*&max0val#e+ 7an e associate an o$tim#m $at. it. f(n) .ere9

-

Enhancement 3

Now, lets consider the original version, in which orientation is free to change and each box can be used once only, can

 we solve it by dynamic programming? Can we solve by recording the optimum path and associate with f(n,i) (see the

comments in the above code), so that we can check if a box is used already in the stack? The answer is no, because

 f(n,1) is a the subproblem of f(n,2), f(n,2) is a the subproblem of f(n,1) neither(beware : if A is a subproblem of B and if

B is a subproblem of C, then A is a subproblem of C). Without optimal substructure, we cannot formulate the recursion.

Boolean parenthesizations

Given an expression withN binary operators andN+1 operands, find the number of different parenthesizations that

givesTRUE as the final result, providing that (1) there are3 possible operators : AND,OR, XOR (which returnsTRUE

 when inputs are different), (2) there are 2 possible operands :TRUE andFALSE. In this example, we assume no

default operator precedence, the priority of operator execution depends only on parenthesization, hence bracket is

needed to resolve the priority between operators whenever there exist more than one operators in an expression or a

sub-expression.

# operand # parenthesization parenthesization # bracket

1 1 x1 0

2 1 x1 op1 x2 0

3 1× 1+1× 1 x1 op1 (x2 op2 x3) 1

= 2 (x1 op1 x2) op2 x3 1

4 1× 2+1× 1+2× 1 x1 op1 (x2 op2 (x3 op3 x4)) 2

= 5 x1 op1 ((x2 op2 x3) op3 x4) 2

(x1 op1 x2) op2 (x3 op3 x4) 2

(x1 op1 (x2 op2 x3)) op3 x4 2

((x1 op1 x2) op2 x3) op3 x4 2

5 1× 5+1× 2+2× 1+5× 1 x1 op1 (x2 op2 (x3 op3 (x4 op4 x5))) 3

18

Page 19: Dynamic Programming

7/17/2019 Dynamic Programming

http://slidepdf.com/reader/full/dynamic-programming-568c2c0c932a3 19/20

= 14 x1 op1 (x2 op2 ((x3 op3 x4) op4 x5)) 3

x1 op1 ((x2 op2 x3) op3 (x4 op4 x5)) 3

x1 op1 ((x2 op2 (x3 op3 x4)) op4 x5) 3

x1 op1 (((x2 op2 x3) op3 x4) op4 x5) 3

... etc

 Thus the following parenthesizations are not allowed, as there exists operators having precedence unresolved.

x1 op1 x2 op2 x3 op3 x4

(x1 op1 x2) op2 x3 op3 x4(x1 op1 x2 op2 x3) op3 x4(x1 op1 x2 op2 x3 op3 x4)   ← Need 2 brackets for 3 operator-expression.

• # bracket of an expression withN operators =N-2

• # parenthesization of an expression

= # parenthesization of an expression that returnsTRUE +

# parenthesization of an expression that returnsFALSE   ← These values can be found by dyn prog.

Dynamic programming

 We perform counting for expressions returningTRUE and expressions returningFALSE together. Like the bin packing

problem, we can divide theN+1 operands into2 groups, separated by a binary operator. Suppose we pick operatork,

then operand subset(1,k) and operand subset(k+1,N+1) are the subproblems of the original problem.Why not divide

the operators into 3 or even more groups? No, we don’t do that because we have binary operator only, we don’t have

trinary operator. Hence, if we are given unary operator, binary operator and trinary operator, the way we divide the n

operators will become very complicated. Given a sub-expression starting with operandn and ending with operandm,

 where1≤ n<m≤ N+1, let f(n,m) andg(n,m)respectively be the number of parenthesizations that giveTRUE output and

FALSE, then recursion is :

),(   mn  f   = ∑−∈  

+++++++++++

]1,[   ,,,

,,,),1(),(),1(),(),1(),(

),1(),(),1(),(),1(),(

mnk    ork ork ork 

 xork  xork and k mk  f k n g mk  g k n f mk  f k n f 

mk  f k n g mk  g k n f mk  f k n f δ δ δ 

δ δ δ 

),(   mn g  = ∑−∈  

+++++

++++++

]1,[   ,,,

,,,),1(),(),1(),(),1(),(

),1(),(),1(),(),1(),(mnk    and k and k and k 

 xork  xork ork mk  f k n g mk  g k n f mk  g k n g 

mk  g k n g mk  f k n f mk  g k n g δ δ δ 

δ δ δ 

 whereδ k,or equals to1 if operatork isOR and0 otherwise, whileδ k,and andδ k,xor are defined similarly. Besides, property

3 and4 in the problem statement are implemented as the boundary cases :

19

Page 20: Dynamic Programming

7/17/2019 Dynamic Programming

http://slidepdf.com/reader/full/dynamic-programming-568c2c0c932a3 20/20

),(   mn  f   =

==+

==+

==

==

true xop xand mnif  

  false xop xand mnif  

true xand mnif  

  false xand mnif  

mnn

mnn

n

n

11

10

1

0

),(   mn g  =

==+==+

==

==

  false xop xand mnif  true xop xand mnif  

  false xand mnif  

true xand mnif  

mnn

mnn

n

n

1110

1

0

 To implement the recursion, need to perform scanning starting from diagonal of the lower triangular matrix f(n,m).

n 1 2 3 4 5 6m1 ! x x x x x scanning se#ence & !;="92 ; ! x x x x3 ; ! x x x4 = ; ! x x5 " = ; ! x6 9 " = ; ! 9 " = ;

Hint : In this problem, recursion is derived from divide and conquer, while scanning the division point within a

range, and finally combine the result of subproblems through appropriate logic. Recall that the box packing problem is

solved in a similar approach.

Bell number

Bell number is the maximum number of ways to partitionN elements.

Dynamic programming

Lets try induction approach. Suppose f(n) is the Bell number :

 f(0) = 1 () f(1) = 1 (x1)

 f(2) = 2 (x2)(x1)

(x2x1)

 f(3) = 5 (x3)(x2)(x1) (x3)(x2x1)

(x3x2)(x1) (x3x1)(x2)

(x3x2x1)

 f(4) = 5 (x4)(x3)(x2)(x1)(x4)(x3)(x2x1) (x4)(x3x2)(x1) (x4)(x3x1)(x2) (x4)(x3x2x1)

(x4x3)(x2)(x1) (x4x3)(x2x1) (x4x2)(x3)(x1) (x4x2)(x3x1) (x4x1)(x3)(x2) (x4x1)(x3x2)

(x4x3x2)(x1) (x4x3x1)(x2) (x4x2x1)(x3)

(x4 x3 x2 x1)

 The algorithm is : when we add then+1

th

 element to a problem with sizen, the first thing we should determine is thesize of the partition that elementn+1 belongs to, suppose it isk, then we need to pickk elements out ofn elements

(recall that problem size isn just before elementn+1 is added), thesek elements together with elementn+1 forms one

partition, while there aren-k elements left, the number of possible partitions formed by thesen-k elements are f(n-k).

Since there arenCk ways to pickk elements, andk ranges from0 ton, hence :

)1(   +n  f   =   ),1()1,1(...)2,1()1,1()0,1(   nk n  f  nk n  f  k n  f  k n  f  k n  f     =++−=+++=++=++=+

=   )(...)2()1()0(210

  n  f  C   f  C   f  C   f  C    nn

nnn ++++