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
 References
 Optimal BST
 Dynamic Programming
Mehlhorn's Text, pages 177187
Introduction to Algorithms, Chapter 16
Assume:
 We have a list of n items: a_{1}, a_{2}, ...,
a_{n}
 Key(a_{k}) = a_{k}
 Probability of accessing item a_{k} is known in advance and is
P(a_{k})
 The list is ordered by keys, a_{1 }<= a_{2} <= ...
a_{n}
How to produce the BST that has the least search cost given the access
probability for each key?
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
Finding the Optimal BST
Let A[j, k] = minimum average search time for a binary search tree with items
a_{j }<= a_{j+1} <= ... a_{k}^{ }
How to find A[1,n]?Set p = 1, 2, ..., n
_{
}
_{
}
j\k  0  1  2  3  4  5 
1  0  P(a_{1})  A[1,2]    
2  0  0  P(a_{2})   A[2,4]  
3  0  0  0  P(a_{3})   
4  0  0  0  0  P(a_{4})  
5  0  0  0  0  0  P(a_{5}) 
6  0  0  0  0  0  0 
Time Complexity for finding Optimal BST [[Theta]](n^{3})
"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 tailormade algorithm which is more
efficient than the straightforward 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 bottomup fashion
Construct an optimal solution from computed information
Matrixchain 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)
Matrixchain Multiplication
The Problem
Given a chain <A_{1}, A_{2}, ..., A_{n}>
matrices, where matrix A_{k} has dimension p_{k1 }*
p_{k} fully parenthesize the product A_{1}* A_{2}*...
*A_{n}, in a way that minimizes the number of scalar
multiplications
Characterize the structure of an optimal solution
Optimal solution is of the form:
 (A_{1}* ... *A_{k}) * (A_{k+1}* ... *A_{n})
not showing the parentheses in (A_{1}* ... *A_{k}) or in
(A_{k+1}* ... *A_{n})
We must use the optimal parenthesization of A_{1}* ... *A_{k}
and the optimal parenthesization of A_{k+1}* ... *A_{n}
Thus the optimal solution to an instance of the matrixchain multiplication
problem contains within it optimal solutions to subproblem instances
That is it is recursive
Recursively define the value of an optimal solution
Recall matrix A_{k} has dimension p_{k1 }* p_{k} for
k = 1,..., n
Let m[k, w] be the minimum the number of scalar multiplications needed to
compute A_{k}* ... *A_{w}
If (A_{k}* ... *A_{z}) * (A_{z+1}* ... *A_{w})
is the optimal solution then
 m[k, w] = m[k, z] + m[z+1, w] + p_{k1 }* p_{z }*
p_{k}
since (A_{k}* ... *A_{z}) is a p_{k1 }* p_{z}
matrix
and (A_{z+1}* ... *A_{w}) is a p_{z }* p_{w}
matrix
But what is z?
_{
}
Compute the optimal solution in a bottomup fashion
_{
}
Example
Matrix  dimension  r  p_{r} 
A_{1}  10*20  0  10 
A_{2}  20*3  1  20 
A_{3}  3*5  2  3 
A_{4}  5*30  3  5 
  4  30 
 1  2  3  4 
1  0  600  750  1950 
2   0  300  2250 
3    0  450 
4     0 
_{
}
_{
}
Construct an optimal solution
from computed information
Example
Matrix  dimension  r  p_{r} 
A_{1}  10*20  0  10 
A_{2}  20*3  1  20 
A_{3}  3*5  2  3 
A_{4}  5*30  3  5 
  4  30 
 1  2  3  4 
1  A_{1}  A_{1}A_{2}  (A_{1}A_{2})A_{3}  (A_{1}A_{2})(A_{3}A_{4}) 
2   A_{2}  A_{2}A_{3}  A_{2}(A_{3}A_{4}) 
3    A_{3}  A_{3}A_{4} 
4     A_{4} 
_{
}
_{
}
Time Complexity of building the OBST?
We have a list of n items: a_{1}, a_{2}, ..., a_{n}
_{}
Probability of accessing item a_{k} is P(a_{k})
Let A[j, k] = minimum average search time for a binary search tree with items
a_{j }<= a_{j+1} <= ... a_{k}^{ }
^{}
^{}
^{}_{
}
Let root[j,k] = p that gave the minimum value for A[j, k]
That is root[j,k] = root of OBST for items a_{j}, a_{2}, ...,
a_{k}
_{}
_{}
_{}
_{}Let w[j,k] = P(a_{j}) + P(a_{j+1}) + ... +
P(a_{k})
Constructing the OBST
for k = 1 to n do
 A[k, k] = P(a_{k})
 A[k, k1] = 0
 root[k,k] = k
 w[k, k] = P(a_{k})
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(a_{k})

 let p, j <= p <= k, be the value minimizes:
 _{
}

 root[j,k] = p

 A[j, k] = A[j, p1] + A[p+1, k] + w[j, k]
 end for
end for
Example 1
k  1  2  3  4  5  6 
a_{k}  a  b  c  d  e  f 
P(a_{k})'s =  0.4  0.05  0.15  0.05  0.1  0.25 
root
1  1  1  1  1  3 
0  2  3  3  3  5 
0  0  3  3  3  5 
0  0  0  4  5  6 
0  0  0  0  5  6 
0  0  0  0  0  6 
A
0  0.4  0.5  0.85  1  1.35  2.1 
0  0  0.05  0.25  0.35  0.6  1.2 
0  0  0  0.15  0.25  0.5  1.05 
0  0  0  0  0.05  0.2  0.6 
0  0  0  0  0  0.1  0.45 
0  0  0  0  0  0  0.25 
0  0  0  0  0  0  0 
Example 2
P(a_{k})'s = (0.15 0.025 .05 .025 .05 .125 .025 .075 0.075 .05 .15 .075
.05 .025 .05)
Root
1  1  1  1  1  3  3  6  6  6  6  6  6  6  6 
0  2  3  3  3  5  6  6  6  6  6  9  9  9  11 
0  0  3  3  3  5  6  6  6  6  9  9  9  9  11 
0  0  0  4  5  6  6  6  6  6  9  9  9  9  11 
0  0  0  0  5  6  6  6  6  8  9  9  9  11  11 
0  0  0  0  0  6  6  6  8  8  9  9  11  11  11 
0  0  0  0  0  0  7  8  8  9  9  11  11  11  11 
0  0  0  0  0  0  0  8  8  9  9  11  11  11  11 
0  0  0  0  0  0  0  0  9  9  11  11  11  11  11 
0  0  0  0  0  0  0  0  0  10  11  11  11  11  11 
0  0  0  0  0  0  0  0  0  0  11  11  11  11  11 
0  0  0  0  0  0  0  0  0  0  0  12  12  12  13 
0  0  0  0  0  0  0  0  0  0  0  0  13  13  13 
0  0  0  0  0  0  0  0  0  0  0  0  0  14  15 
0  0  0  0  0  0  0  0  0  0  0  0  0  0  15 
A[1,15] = 2.925
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(a_{k})

 let p, root[j,k1] <= p <= root[j+1,k], be the value
minimizes: _{
}

 root[j,k] = p

 A[j, k] = A[j, p1] + A[p+1, k] + w[j, k]
 end for
end for
Time Complexity
_{
}
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, k1) + 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(n^{2})
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)