Dynamic Programming
Dynamic Programming
Dynamic Programming
• An algorithm design technique (like divide and
conquer)
n n
=1 =1
1 n
Comb (6,4)
= 3 + 2 + 1 + 2 + 1 + 1 + 2 + 1 + 1 + 1
n n-1 n-1
= +
k k k-1
4
Dynamic Programming Algorithm
1. Characterize the structure of an optimal
solution
2. Recursively define the value of an optimal
solution
3. Compute the value of an optimal solution in a
bottom-up fashion
4. Construct an optimal solution from computed
information (not always necessary)
5
Matrix Chain
Multiplication
Matrix Chain Multiplication
• Given some matrices to multiply, determine the best order to
multiply them so you minimize the number of single element
multiplications.
– i.e. Determine the way the matrices are parenthesized.
• Matrix compatibility:
C=AB C=A1 A2 Ai Ai+1 An
colA = rowB coli = rowi+1
rowC = rowA rowC = rowA1
colC = colB colC = colAn
10
MATRIX-MULTIPLY(A, B)
if columns[A] rows[B]
then error “incompatible dimensions”
else for i 1 to rows[A]
do for j 1 to columns[B] rows[A] cols[A] cols[B]
do C[i, j] = 0 multiplications
for k 1 to columns[A]
k do C[i, j] C[i, j] + A[i, k] B[k, j]
j cols[B]
j cols[B]
i = i
* k
A B C
rows[A]
11 rows[A]
Matrix-Chain Multiplication
• In what order should we multiply the matrices?
A1 A2 An
• Parenthesize the product to get the order in which
matrices are multiplied
• E.g.: A1 A2 A3 = ((A1 A2) A3)
= (A1 (A2 A3))
• Which one of these orderings should we choose?
– The order in which we multiply the matrices has a
significant impact on the cost of evaluating the product
12
Example
A1 A2 A3
• A1: 10 x 100
• A2: 100 x 5
• A3: 5 x 50
1. ((A1 A2) A3): A1 A2 = 10 x 100 x 5 = 5,000 (10 x 5)
((A1 A2) A3) = 10 x 5 x 50 = 2,500
Total: 7,500 scalar multiplications
2. (A1 (A2 A3)): A2 A3 = 100 x 5 x 50 = 25,000 (100 x 50)
(A1 (A2 A3)) = 10 x 100 x 50 = 50,000
Total: 75,000 scalar multiplications
one order of magnitude difference!!
13
Matrix-Chain Multiplication:
Problem Statement
14
1. The Structure of an Optimal
Parenthesization
• Notation:
Ai…j = Ai Ai+1 Aj, i j
16
2. A Recursive Solution
Consider the subproblem of parenthesizing
Ai…j = Ai Ai+1 Aj for 1 i j n
pi-1pkpj
= Ai…k Ak+1…j for i k < j
m[i, k] m[k+1,j]
• Assume that the optimal parenthesization splits
the product Ai Ai+1 Aj at k (i k < j)
m[i, k] + m[k+1, j] + pi-1pkpj
m[i, j] =
min # of multiplications min # of multiplications # of multiplications
to compute Ai…k to compute Ak+1…j to compute Ai…kAk…j
17
2. A Recursive Solution (cont.)
m[i, j] = m[i, k] + m[k+1, j] + pi-1pkpj
• We do not know the value of k
– There are j – i possible values for k: k = i, i+1, …, j-1
• Minimizing the cost of parenthesizing the product Ai Ai+1
Aj becomes:
0 if i = j
m[i, j] = min {m[i, k] + m[k+1, j] + pi-1pkpj} if i < j
ik<j
3. Computing the Optimal Costs
0 if i = j
m[i, j] = min {m[i, k] + m[k+1, j] + pi-1pkpj} if i < j
ik<j
20
3. Computing the Optimal Costs (cont.)
0 if i = j
m[i, j] = min {m[i, k] + m[k+1, j] + pi-1pkpj} if i < j
ik<j
• Length = 1: i = j, i = 1, 2, …, n
• Length = 2: j = i + 1, i = 1, 2, …, n-1
1 2 3 n
n
m[1, n] gives the optimal
solution to the problem
j
Compute rows from bottom to top 3
and from left to right 2
1
i 21
Example: min {m[i, k] + m[k+1, j] + pi-1pkpj}
m[2, 2] + m[3, 5] + p1p2p5 k=2
1 2 3 4 5 6
6
5
• Values m[i, j] depend only on
4
j values that have been
3 previously computed
2
1
i
22
Example min {m[i, k] + m[k+1, j] + pi-1pkpj}
1 2 3
Compute A1 A2 A3 2 2
3 7500 25000 0
• A1: 10 x 100 (p0 x p1)
1
2 0
• A2: 100 x 5 (p1 x p2) 5000
m[i, i] = 0 for i = 1, 2, 3
m[1, 2] = m[1, 1] + m[2, 2] + p0p1p2 (A1A2)
= 0 + 0 + 10 *100* 5 = 5,000
m[2, 3] = m[2, 2] + m[3, 3] + p1p2p3 (A2A3)
= 0 + 0 + 100 * 5 * 50 = 25,000
m[1, 3] = min m[1, 1] + m[2, 3] + p0p1p3 = 75,000 (A1(A2A3))
m[1, 2] + m[3, 3] + p0p2p3 = 7,500 ((A1A2)A3)
23
Matrix-Chain-Order(p)
O(N3)
24
4. Construct the Optimal Solution
• s[i, j] = value of k such that the optimal
parenthesization of Ai Ai+1 Aj splits
the product between Ak and Ak+1
1 2 3 4 5 6
6 3 3 3 5 5 -
• s[1, n] = 3 A1..6 = A1..3 A4..6
5 3 3 3 4 -
• s[1, 3] = 1 A1..3 = A1..1 A2..3
4 3 3 3 -
• s[4, 6] = 5 A4..6 = A4..5 A6..6
3 1 2 -
j
2 1 -
1 -
i
25
4. Construct the Optimal Solution (cont.)
PRINT-OPT-PARENS(s, i, j)
1 2 3 4 5 6
if i = j 6 3 3 3 5 5 -
then print “A”i 5 3 3 3 4 -
else print “(” 4 3 3 3 -
j
3 1 2 -
PRINT-OPT-PARENS(s, i, s[i, j])
2 1 -
PRINT-OPT-PARENS(s, s[i, j] + 1, j)
1 -
print “)”
i
26
Example: A1 A6 ( ( A1 ( A 2 A3 ) ) ( ( A 4 A5 ) A 6 ) )
28
Elements of Dynamic Programming
• Optimal Substructure
– An optimal solution to a problem contains within it an
optimal solution to subproblems
– Optimal solution to the entire problem is build in a
bottom-up manner from optimal solutions to
subproblems
• Overlapping Subproblems
– If a recursive algorithm revisits the same subproblems
over and over the problem has overlapping
subproblems
29
Parameters of Optimal Substructure
1..
4
2..2 3..4 2..3 4..4 1..1 2..2 3..3 4..4 1..1 2..3 1..2 3..3
RECURSIVE-MATRIX-CHAIN (p, i, j)
1 if i = j
2 then return 0
3 m[i,j] ←∞
4 for k←i to j-1
5 do q←RECURSIVE-MATRIX-CHAIN (p, i, k)
+ RECURSIVE-MATRIX-CHAIN (p, k+1, j)+ pi-1 pk pj
6 if q < m[i,j]
7 then m[i,j] ←q
8 return m[i,j]
33
Elements of dynamic programming (cont.)
T (1) 1,
n 1
T (n) 1 (T (k ) T (n k ) 1) for n 1
k 1
n 1
2 T (i ) n
i 1
34
Elements of dynamic programming (cont.)
Overlapping subproblems: (cont.)
Memoization
There is a variation of dynamic programming that often offers the
efficiency of the usual dynamic-programming approach while
maintaining a top-down strategy.
The idea is to memoize the the natural, but inefficient, recursive
algorithm.
We maintain a table with subproblem solutions, but the control
structure for filling in the table is more like the recursive algorithm.
36
Elements of dynamic programming (cont.)
Memoization
There is a variation of dynamic programming that often offers the
efficiency of the usual dynamic-programming approach while
maintaining a top-down strategy.
The idea is to memoize the the natural, but inefficient, recursive
algorithm.
We maintain a table with subproblem solutions, but the control
structure for filling in the table is more like the recursive algorithm.
37
Elements of dynamic programming (cont.)
• Memoization (cont.)
• An entry in a table for the solution to each subproblem is maintained.
• Eech table entry initially contains a special value to indicate that the
entry has yet to be filled.
• When the subproblem is first encountered during the execution of
the recursive algorithm, its solution is computed and then stored in
the table.
• Each subsequent time that the problem is encountered, the value
stored in the table is simply looked up and returned.
38
Elements of dynamic programming (cont.)
1 MEMOIZED-MATRIX-CHAIN(p)
2 n←length[p]-1
3 for i←1 to n
4 do for j←i to n
do m[i,j] ←∞
return LOOKUP-CHAIN(p,1,n)
39
Elements of dynamic programming (cont.)
Memoization (cont.)
LOOKUP-CHAIN(p,1,n)
1 if m[i,j] < ∞
2 then return m[i,j]
3 if i=j
4 then m[i,j] ←0
5 else for k←1 to j-1
6 do q← LOOKUP-CHAIN(p,i,k)
+ LOOKUP-CHAIN(p,k+1,j) + pi-1 pk pj
7 if q < m[i,j]
8 then m[i,j] ←q
9 return m[i,j]
40