You are on page 1of 14

Dynamic Programming:

Example 1: Assembly line scheduling. Instance:








Task: Find the fastest way through the factory?

Solution 1: brute force search calculates times for 2
n

many possible paths, and compares them.

Solution 2: recursion:









Denote the fastest way through the node a
j,k

by
m(j,k), j=1,2. The fastest way through a
1k

is either
t
1(k-1)
t
2(k-1)
a
1
1
a
21
t
21
t
11
a
12
a
2
t
22
t
12
a
1(k-1)
a
2(k-1)
a
1k
a
2k
t
2k
t
1k
a
1(n-1)
a
2(n-1)
t
2(n-1)
a
1n
t
1(n-1)
a
2n
x
2
x
1
e
1
e
2
a
1(k-1)
a
2(k-1)
t
2(k-1)
a
1k
t
1(k-1)
a
2k
t
2k
t
1k
t
1(k-2)
t
2(k-2)
1
through a
1(k-1)
or through a
2(k-1)

via t
2(k-1)
.
Similarly,
the fastest way through a
2k
is either through a
2(k-1)
or
through a
1(k-1)

via t
1(k-1)
.
Thus:


m(1, k) =
min{m(1,k-1) + a
1k
, m(2,k-1) + t
2(k-1)
+ a
1k
}


m(2, k) =
min{m(2, k-1) + a
2k
, m(1, k-1) + t
1(k-1)
+ a
2k
}

Fastest-Way(a,t,e,x,n)
1. f
1
[1] e
1

+ a
1,1

2. f
2
[1] e
2

+ a
2,1

3. for j 2 to n

4.

do if f
1
[j-1] + a
1,j


f
2
[j-1] + t
2,j-1
+

a
1,j

5.

then

f
1
[j] f
1
[j-1] + a
1,j


6. else f
1
[j] f
2
[j-1] + t
2,j-1
+

a
1,j

7.

if f
2
[j-1] + a
2,j


f
1
[j-1] + t
1,j-1
+

a
2,j

8.

then

f
2
[j] f
2
[j-1] + a
2,j


9. else f
1
[j] f
1
[j-1] + t
1,j-1
+

a
2,j

10.

if f
1
[n] + x
1

f
2
[n] + x
2

11. then f* = f
1
[n] + x
1

12.

else f* = f
2
[n] + x
2

2
Example 2: Matrix chain multiplication.

Instance: A sequence of matrices A
1
A
2
... A
n
.

Task: Group them in such a way as to minimize the
number of multiplications.

Assume for example that the matrices have the
following dimensions:

A = 10 X 100
B = 100 X 5
C = 5 X 50








(AB)C: 10 X 5 X 100+10 X 50 X 5=5000 + 2500=7500

A(BC): 100 X 5 X 50+10 X 100 X 50=
25000 + 50000=75000

100
10
100
5
50 50
5
10
5
5
10
100
100
50
A
B
C
A
BC
AB C
(AB)C
A
50
10
(BC)
3
Thus, order of matrix multiplication can change the
number of multiplications many times.

How many possible distribution of brackets are there
to try out?

=
=
1
1
) ( ) ( ) (
n
i
i n T i T n T

The solution is a huge number (exponential in terms
of the number of matrices: T(n) = (2
n
); it is equal to
the number of binary trees with n leaves, which is
easy to see that it is exponential).

Thus, we cannot check them all. So what do we do?
Eliminate repetitions in the calculation, and avoid
calculations!!!! These different distributions of
brackets significantly overlap!

We apply the same technique as in the previous
example, building the table of optimal distribution of
brackets for all sequences A
i
...A
j
, such that in the m
th

raw we have all optimal distribution of brackets for
all sequences A
i
...A
j
for j-i =m 2. In order to find
optimal distribution for sequences A
i
...A
j
of length
j-i = m+1 we consider all splits (A
i
...A
k
)(A
k+1
...A
j
) for
all k i < j and use the table for k-i and j-(k+1)
which have already been calculated:
4


Here A
i
is of size p
i-1
p
i
the
product A
i
A
k
is of size p
i-1
p
k
and
the product A
k+1
A
j
of size p
k+1
p
j

m[i,j] = min{m[i, k] + m[k+1, j] + p
i-1
p
k
p
j
, i k< j}
mat dim
A
1
30 X 35
A
2
35 X 15
A
3
15 X 5
A
4
5 X 10
A
5
10 X 20
A
6
20 X 25











Principal split points k i.e. A
i
...A
j
=(A
i
...A
k
)(A
k+1
...A
j
) :










3
3
3
3
3
1
1
A
1

3
2
3

3
5
5
4

5
4
3
6
3
2
2
3
4
5
1
A
2

A
3

A
4

A
5

A
6


4

3

2

1

1
2

3

4
5
6

15125
6

11875
5

10500

5375
7125
9375

3500 2500
7875 4375


5000
1000
15750 2625 750


A
1



A
2


A
3


A
4


A
5


A
6


5
Example 3: Longest Common Subsequence

Assume we want to compare two DNA sequences A
and B, to see if two viruses are related. Then this
notion of similarity must be robust with respect to
both
short insertions and
short deletions.

Thus, the best way is to see if two sequences share a
long subsequence. Longer the joint subsequence
more similar A and B are.

Instance: Two sequence X and Y over certain
alphabet.

Problem: Given X and Y find a longest common
subsequence (LCS) for X and Y.

6
Proposition: If X=< x
1
, x
2
,...., x
m
> and
Y=< y
1
, y
2
,...., y
n
> are two sequences and
Z=< z
1
, z
2
,...., z
k
> is a LCS of X and Y, then

1. If x
m
=y
n
then z
k
=x
m
=y
n
and
Z*=< z
1
, z
2
,...., z
k-1
> is an LCS for
X*=< x
1
, x
2
,...., x
m-1
> and Y*=< y
1
, y
2
,...., y
n-1
>.

2. If x
m
y
n
and z
k
x
m
then Z is a LCS of
X*=< x
1
, x
2
,...., x
m-1
> and Y=< y
1
, y
2
,...., y
n
>.

3. If x
m
y
n
and z
k
y
n
then Z is a LCS of
X=< x
1
, x
2
,...., x
m
> and Y*=< y
1
, y
2
,...., y
n-1
>.

Thus, the following recursion formula can holds:

0 if i=0 or j=0
c[i,j]= c[i-1, j-1] +1 if i,j>0 and x
i
= y
j

max{c[i, j-1], c[i-1, j]} if i,j>0 and x
i
y
j

If c[i, j-1] =c[i-1, j] we chose (arbitrarily) which one
we take.

Also, instead of a recursive procedure which would
be exponential, we use dynamic programming to
avoid repetitions of computations. The arrows encode
which subsequences are taken, as it is clear from
below:
7




8

Example 4: Optimal binary search trees

Instance: Given are: (1) A sequence of n distinct
keys K = <k
1
, k
2
, ... ,k
n
> in sorted order. For each
key k
i
we have the probability p
i
that a search will be
for k
i
; (2) A sequence of dummy keys
D= <d
0
, d
1
, ... ,d
n
> with the corresponding
probabilities q
i
. The key d
i
represent values which
would be between k
i
and k
i+1
, but are not in K. Thus,
searches for values between k
i
and k
i+1
terminate by
reaching d
i
, which happens with probability q
i
.

Problem: Find a binary search tree with minimal
expected value of the search time.

Note that q
0
is the probability of a search for keys
smaller than k
1
, and q
n
is the probability of a search
for keys larger than k
n
.
Thus, the probabilities should satisfy
1
0 1
= +

= =
n
i
i
n
i
i
q p

Recall: a binary search tree is a binary tree with
keys as nodes such that every node (key) in the left
sub-tree of any key k is smaller (or equal) than k and
every key in the right sub-tree of k is larger (or equal)
than k. (In our case all keys are assumed to be
different).
9

What is the expected search time for a binary search
tree?






Notice that the optimal tree (b) is less balanced than
the tree (a)!




10

How to get optimal binary search tree? We use the
(obvious) fact that if r is the root of an optimal binary
search tree, then both of its sub-trees are optimal
binary search trees for the corresponding subsets of
keys. Let and let us denote the
expected search time for the optimal binary search
tree for keys k
i
, k
i+1
,...., k
j
by e[i, j].

= =
+ =
j
i l
l
j
i l
l
q p j i w
1
) , (

The depth of each element in the entire tree is 1 plus
its depth in the sub-tree. Thus, if the root is k
r
we get,
by multiplying this 1 by the corresponding
probability and taking all of these probabilities
together:+(P
k
stands for either p
k
or q
k
)
] , [ ] , 1 [ ] 1 , [
) ( ] , 1 [ ] 1 , [
) 1 ( ) 1 ( ] , [
j i w j r e r i e
P P p j r e r i e
P P p P d P d
P P d P P d p
P d P d p j i e
TR k
k
TL k
k r
TR k
k
TL k
k r k
TR k
k k
TL k
k
TR k
k k
TR k
k
TL k
k k
TL k
k r
k
TR k
k k
TL k
k r
+ + +
= + + + + +
= + + + +
= + + + +
= + + + + =











depth in the
sub-tree
depth in the tree =depth
in the sub-tree +1
sub-tree TL
= T[i,r-1]
sub-tree TR
= T[r+1,j]
r
k
11
We now proceed as before by building the table for
all sub-trees over m consecutive keys k
i
, k
j,
j-i = m.















12
Example: for


e get w











13
Example 5: Optimal resource use

Instance: A list of activities a
i
, 1 i m with
starting times s
i
and finishing times f
i
. No two
activities can take place simultaneously.

Task: Find a subset of compatible activities of
maximal total duration.

Order activities according to non decreasing finishing
times f
1
f
m
. For each i we find optimal choice
among all sequences which end with activity a
i
. For
i=1 simply take a
1
. Assume that we have solved the
problem for all j<i, obtaining maximal duration time
T
j
of all sequences of activities which end with
activity a
j
.
14







Ti =max

{Tj + f
i
-s
i ,
j < i and activity a
j
finishes
before the starting time of activity a
i
}

Finally let T
max
=max{T
k
: k n}.
T
k

a
i

T
k-1

f
k-1 k
s
i
f
i-1
f
i
f

You might also like