SDSU CS660 Combinatorial Algorithms
Fall Semester, 1996
Dynamic Programming

[To Lecture Notes Index]
San Diego State University -- This page last updated Nov 5, 1996
----------

Contents of Dynamic Programming

  1. References
  2. Optimal BST
  3. Dynamic Programming

Dynamic Programming Slide # 1

References

Mehlhorn's Text, pages 177-187

Introduction to Algorithms, Chapter 16


Dynamic Programming Slide # 2

Optimal BST


Assume:
We have a list of n items: a1, a2, ..., an
Key(ak) = ak
Probability of accessing item ak is known in advance and is P(ak)
The list is ordered by keys, a1 <= a2 <= ... an

How to produce the BST that has the least search cost given the access probability for each key?




Dynamic Programming Slide # 3
Optimal BST
Greedy Algorithm fails

Example:
list = a, b, c, d
P(a) = .1, P(b) = .2, P(c) = .3, P(d) = .4
Average Cost = 1*0.4 + 2*0.3 + 3*0.2 + 4*0.1 = 2.0
Optimal BST
Average Cost = 1*0.3 + 2*0.4 + 2*0.2 + 3*0.1 = 1.8

Dynamic Programming Slide # 4
Finding the Optimal BST

Let A[j, k] = minimum average search time for a binary search tree with items aj <= aj+1 <= ... ak
How to find A[1,n]?Set p = 1, 2, ..., n



Dynamic Programming Slide # 5



j\k012345
10P(a1)A[1,2]
200P(a2)A[2,4]
3000P(a3)
40000P(a4)
500000P(a5)
6000000


Time Complexity for finding Optimal BST [[Theta]](n3)


Dynamic Programming Slide # 6

Dynamic Programming


"Dynamic programming algorithm stores the results for small subproblems and looks them up, rather than recomputing them, when it needs them later to solve larger subproblems"

Baase


"Dynamic programming algorithm is very useful approach to many optimization problems. ... Usually, we can find a tailor-made algorithm which is more efficient than the straight-forward algorithm based on dynamic programming"


T. C. Hu

Steps in developing a dynamic programming algorithm


Characterize the structure of an optimal solution

Recursively define the value of an optimal solution

Compute the value of an optimal solution in a bottom-up fashion

Construct an optimal solution from computed information

Dynamic Programming Slide # 7
Matrix-chain Multiplication
The Problem

Let A be a 10 * 100 matrix,
B be a 100 * 5 matrix
C be a 5 * 50 matrix
Then
A * B is a 10 * 5 matrix
B * C is a 100 * 50 matrix


Computing
A * B takes 10 * 100 * 5 = 5,000 multiplications
(A * B) * C takes 10 * 5 * 50 = 2,500 additional mult.
So (A * B) * C requires total of 7,500 multiplications


Computing
B * C takes 100 * 5 * 50 = 25,000 multiplications
A * (B * C) takes 10 * 100 * 50 = 50,000 additional mult.
So A * (B * C) requires total of 75,000 multiplications


But (A * B) * C = A * (B * C)

Dynamic Programming Slide # 8
Matrix-chain Multiplication
The Problem

Given a chain <A1, A2, ..., An> matrices, where matrix Ak has dimension pk-1 * pk fully parenthesize the product A1* A2*... *An, in a way that minimizes the number of scalar multiplications

Characterize the structure of an optimal solution


Optimal solution is of the form:
(A1* ... *Ak) * (Ak+1* ... *An)

not showing the parentheses in (A1* ... *Ak) or in (Ak+1* ... *An)


We must use the optimal parenthesization of A1* ... *Ak and the optimal parenthesization of Ak+1* ... *An


Thus the optimal solution to an instance of the matrix-chain multiplication problem contains within it optimal solutions to subproblem instances

That is it is recursive

Dynamic Programming Slide # 9
Recursively define the value of an optimal solution

Recall matrix Ak has dimension pk-1 * pk for k = 1,..., n



Let m[k, w] be the minimum the number of scalar multiplications needed to compute Ak* ... *Aw


If (Ak* ... *Az) * (Az+1* ... *Aw) is the optimal solution then
m[k, w] = m[k, z] + m[z+1, w] + pk-1 * pz * pk


since (Ak* ... *Az) is a pk-1 * pz matrix

and (Az+1* ... *Aw) is a pz * pw matrix



But what is z?



Dynamic Programming Slide # 10
Compute the optimal solution in a bottom-up fashion



Example
Matrixdimensionrpr
A110*20010
A220*3120
A33*523
A45*3035
430
1234
106007501950
203002250
30450
40




Dynamic Programming Slide # 11
Construct an optimal solution
from computed information

Example
Matrixdimensionrpr
A110*20010
A220*3120
A33*523
A45*3035
430

1234
1A1A1A2(A1A2)A3(A1A2)(A3A4)
2A2A2A3A2(A3A4)
3A3A3A4
4A4




Dynamic Programming Slide # 12
Time Complexity of building the OBST?

We have a list of n items: a1, a2, ..., an


Probability of accessing item ak is P(ak)


Let A[j, k] = minimum average search time for a binary search tree with items aj <= aj+1 <= ... ak






Let root[j,k] = p that gave the minimum value for A[j, k]

That is root[j,k] = root of OBST for items aj, a2, ..., ak



Let w[j,k] = P(aj) + P(aj+1) + ... + P(ak)


Dynamic Programming Slide # 13
Constructing the OBST

for k = 1 to n do
A[k, k] = P(ak)
A[k, k-1] = 0
root[k,k] = k
w[k, k] = P(ak)
end
A[n+1, n] = 0


for diagonal = 1 to n -1 do
for j = 1 to n - diagonal do
k = j + diagonal
w[j, k] = w[j, k - 1] + P(ak)
let p, j <= p <= k, be the value minimizes:
root[j,k] = p
A[j, k] = A[j, p-1] + A[p+1, k] + w[j, k]
end for
end for



Dynamic Programming Slide # 14
Example 1
k123456
akabcdef
P(ak)'s = 0.40.050.150.050.10.25

root
111113
023335
003335
000456
000056
000006

A
00.40.50.8511.352.1
000.050.250.350.61.2
0000.150.250.51.05
00000.050.20.6
000000.10.45
0000000.25
0000000

Dynamic Programming Slide # 15
Example 2

P(ak)'s = (0.15 0.025 .05 .025 .05 .125 .025 .075 0.075 .05 .15 .075 .05 .025 .05)

Root
111113366666666
0233356666699911
0033356666999911
0004566666999911
00005666689991111
000006668899111111
0000007889911111111
0000000889911111111
00000000991111111111
000000000101111111111
00000000001111111111
0000000000012121213
000000000000131313
00000000000001415
0000000000000015

A[1,15] = 2.925

Dynamic Programming Slide # 16
Modified Algorithm

for diagonal = 1 to n -1 do
for j = 1 to n - diagonal do
k = j + diagonal
w[j, k] = w[j, k - 1] + P(ak)
let p, root[j,k-1] <= p <= root[j+1,k], be the value minimizes:
root[j,k] = p
A[j, k] = A[j, p-1] + A[p+1, k] + w[j, k]
end for
end for

Time Complexity



Dynamic Programming Slide # 17
General Theorem

Let H(i, j) be a real number for 1 <= i < j <= n

Let c(i, j) be defined by:
c(i, i) = 0
c(i, j) = H(i, j) +
Let K(i, j) = largest k, i <= k <=j, that minimizes c(i, k-1) + c(k, j)

H(i, j) is monotone with respect to set inclusion of intervals if
H(j, k) <= H(x, y) if j <= x < y <= k

H(i, j) satisfies the quadrangle inequality (QI) if
H(j, k) + H(x, y) <= H(x, k) + H(j, y) if j <= x < k <= y

Theorem. If H(i, j) satisfies QI and is monotone then c(i, j) can be computed in time O(n2)

Lemma. If H(i, j) satisfies QI and is monotone then c(i, j) satisfies QI
Lemma. If c(i, j) satisfies QI then we have
K(i, j) <= K(i, j+ 1) <= K(i+1, j+1)


----------