Title: Analysis of Algorithms CS 477677
1Analysis of AlgorithmsCS 477/677
- Dynamic Programming
- Instructor George Bebis
- (Chapter 15)
2Dynamic Programming
- An algorithm design technique (like divide and
conquer) - Divide and conquer
- Partition the problem into independent
subproblems - Solve the subproblems recursively
- Combine the solutions to solve the original
problem
3Dynamic Programming
- Applicable when subproblems are not independent
- Subproblems share subsubproblems
- E.g. Combinations
- A divide and conquer approach would repeatedly
solve the common subproblems - Dynamic programming solves every subproblem just
once and stores the answer in a table
n n
n 1
1
1
4Example Combinations
5Dynamic Programming
- Used for optimization problems
- A set of choices must be made to get an optimal
solution - Find a solution with the optimal value (minimum
or maximum) - There may be many solutions that lead to an
optimal value - Our goal find an optimal solution
6Dynamic 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 (not always necessary)
7Assembly Line Scheduling
- Automobile factory with two assembly lines
- Each line has n stations S1,1, . . . , S1,n and
S2,1, . . . , S2,n - Corresponding stations S1, j and S2, j perform
the same function but can take different amounts
of time a1, j and a2, j - Entry times are e1 and e2 exit times are x1
and x2
8Assembly Line Scheduling
- After going through a station, can either
- stay on same line at no cost, or
- transfer to other line cost after Si,j is ti,j ,
j 1, . . . , n - 1
9Assembly Line Scheduling
- Problem
- what stations should be chosen from line 1 and
which from line 2 in order to minimize the total
time through the factory for one car?
10One Solution
- Brute force
- Enumerate all possibilities of selecting stations
- Compute how long it takes in each case and choose
the best one - Solution
- There are 2n possible ways to choose stations
- Infeasible when n is large!!
111. Structure of the Optimal Solution
- How do we compute the minimum time of going
through a station?
121. Structure of the Optimal Solution
- Lets consider all possible ways to get from the
starting point through station S1,j - We have two choices of how to get to S1, j
- Through S1, j - 1, then directly to S1, j
- Through S2, j - 1, then transfer over to S1, j
Line 1
Line 2
131. Structure of the Optimal Solution
- Suppose that the fastest way through S1, j is
through S1, j 1 - We must have taken a fastest way from entry
through S1, j 1 - If there were a faster way through S1, j - 1, we
would use it instead - Similarly for S2, j 1
Line 1
Optimal Substructure
Line 2
14Optimal Substructure
- Generalization an optimal solution to the
problem find the fastest way through S1, j
contains within it an optimal solution to
subproblems find the fastest way through S1, j
- 1 or S2, j 1. - This is referred to as the optimal substructure
property - We use this property to construct an optimal
solution to a problem from optimal solutions to
subproblems
152. A Recursive Solution
- Define the value of an optimal solution in terms
of the optimal solution to subproblems
162. A Recursive Solution (cont.)
- Definitions
- f the fastest time to get through the entire
factory - fij the fastest time to get from the starting
point through station Si,j -
- f min (f1n x1, f2n x2)
172. A Recursive Solution (cont.)
- Base case j 1, i1,2 (getting through station
1) - f11 e1 a1,1
- f21 e2 a2,1
182. A Recursive Solution (cont.)
- General Case j 2, 3, ,n, and i 1, 2
- Fastest way through S1, j is either
- the way through S1, j - 1 then directly through
S1, j, or - f1j - 1 a1,j
- the way through S2, j - 1, transfer from line 2
to line 1, then through S1, j - f2j -1 t2,j-1 a1,j
- f1j min(f1j - 1 a1,j ,f2j -1 t2,j-1
a1,j)
Line 1
Line 2
192. A Recursive Solution (cont.)
- e1 a1,1 if j 1
- f1j
- min(f1j - 1 a1,j ,f2j -1 t2,j-1
a1,j) if j 2 - e2 a2,1 if j 1
- f2j
- min(f2j - 1 a2,j ,f1j -1 t1,j-1
a2,j) if j 2
203. Computing the Optimal Solution
- f min (f1n x1, f2n x2)
- f1j min(f1j - 1 a1,j ,f2j -1 t2,j-1
a1,j) - f2j min(f2j - 1 a2,j ,f1j -1
t1,j-1 a2,j) - Solving top-down would result in exponential
running time
1
2
3
4
5
f1j
f1(5)
f1(4)
f1(3)
f1(2)
f1(1)
f2j
f2(5)
f2(4)
f2(3)
f2(2)
f2(1)
2 times
4 times
213. Computing the Optimal Solution
- For j 2, each value fij depends only on the
values of f1j 1 and f2j - 1 - Idea compute the values of fij as follows
- Bottom-up approach
- First find optimal solutions to subproblems
- Find an optimal solution to the problem from the
subproblems
1
2
3
4
5
f1j
f2j
22Example
e1 a1,1, if j 1 f1j
min(f1j - 1 a1,j ,f2j -1 t2,j-1 a1,j)
if j 2
1
2
3
4
5
f1j
9
181
202
241
321
f 351
f2j
12
161
222
251
302
23FASTEST-WAY(a, t, e, x, n)
- f11 ? e1 a1,1
- f21 ? e2 a2,1
- for j ? 2 to n
- do if f1j - 1 a1,j f2j - 1 t2,
j-1 a1, j - then f1j ? f1j - 1 a1, j
- l1j ? 1
- else f1j ? f2j - 1 t2, j-1
a1, j - l1j ? 2
- if f2j - 1 a2, j f1j - 1
t1, j-1 a2, j - then f2j ? f2j - 1 a2, j
- l2j ? 2
- else f2j ? f1j - 1 t1, j-1
a2, j - l2j ? 1
O(N)
Compute the values of f1j and l1j
Compute the values of f2j and l2j
24FASTEST-WAY(a, t, e, x, n) (cont.)
- if f1n x1 f2n x2
- then f f1n x1
- l 1
- else f f2n x2
- l 2
Compute the values of the fastest time through
the entire factory
254. Construct an Optimal Solution
- Alg. PRINT-STATIONS(l, n)
- i ? l
- print line i , station n
- for j ? n downto 2
- do i ?lij
- print line i , station j - 1
1
2
3
4
5
f1j/l1j
9
181
202
241
321
l 1
f2j/l2j
12
161
222
251
302
26Matrix-Chain Multiplication
- Problem given a sequence ?A1, A2, , An?,
compute the product - A1 ? A2 ??? An
- Matrix compatibility
- C A ? B CA1 ? A2 ??? Ai ?
Ai1 ??? An - colA rowB coli rowi1
- rowC rowA rowC rowA1
- colC colB colC colAn
-
-
27MATRIX-MULTIPLY(A, B)
- if columnsA ? rowsB
- then error incompatible dimensions
- else for i ? 1 to rowsA
- do for j ? 1 to columnsB
- do Ci, j 0
- for k ? 1 to columnsA
- do Ci, j ? Ci, j Ai, k Bk, j
j
colsB
j
colsB
i
i
B
A
C
rowsA
rowsA
28Matrix-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
29Example
- 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!!
30Matrix-Chain MultiplicationProblem Statement
- Given a chain of matrices ?A1, A2, , An?, where
Ai has dimensions pi-1x pi, fully parenthesize
the product A1 ? A2 ??? An in a way that
minimizes the number of scalar multiplications. - A1 ? A2 ??? Ai ? Ai1 ???
An - p0 x p1 p1 x p2 pi-1 x pi pi x
pi1 pn-1 x pn
31What is the number of possible parenthesizations?
- Exhaustively checking all possible
parenthesizations is not efficient! - It can be shown that the number of
parenthesizations grows as O(4n/n3/2) - (see page 333 in your textbook)
321. The Structure of an Optimal Parenthesization
- Notation
- Aij Ai Ai1 ??? Aj, i ? j
- Suppose that an optimal parenthesization of Aij
splits the product between Ak and Ak1, where
i ? k lt j - Aij Ai Ai1 ??? Aj
- Ai Ai1 ??? Ak Ak1 ??? Aj
- Aik Ak1j
33Optimal Substructure
- Aij Aik Ak1j
- The parenthesization of the prefix Aik must be
an optimal parentesization - If there were a less costly way to parenthesize
Aik, we could substitute that one in the
parenthesization of Aij and produce a
parenthesization with a lower cost than the
optimum ? contradiction! - An optimal solution to an instance of the
matrix-chain multiplication contains within it
optimal solutions to subproblems
342. A Recursive Solution
- Subproblem
- determine the minimum cost of parenthesizing
Aij Ai Ai1 ??? Aj for 1 ? i ? j ? n - Let mi, j the minimum number of
multiplications needed to compute Aij - full problem (A1..n) m1, n
- i j Aii Ai ? mi, i
0, for i 1, 2, , n
352. A Recursive Solution
- Consider the subproblem of parenthesizing Aij
Ai Ai1 ??? Aj for 1 ? i ? j ? n - Aik Ak1j for i ? k lt j
- Assume that the optimal parenthesization splits
the product Ai Ai1 ??? Aj at k (i ? k lt j) - mi, j
mi, k mk1, j
pi-1pkpj
min of multiplications to compute Aik
of multiplications to compute AikAkj
min of multiplications to compute Ak1j
362. A Recursive Solution (cont.)
- mi, j mi, k mk1, j
pi-1pkpj - We do not know the value of k
- There are j i possible values for k k i,
i1, , j-1 - Minimizing the cost of parenthesizing the product
Ai Ai1 ??? Aj becomes - 0 if i j
- mi, j min mi, k mk1, j pi-1pkpj
if i lt j - i?kltj
373. Computing the Optimal Costs
- 0 if i j
- mi, j min mi, k mk1, j pi-1pkpj
if i lt j - i?kltj
- Computing the optimal solution recursively takes
exponential time! - How many subproblems?
- Parenthesize Aij
- for 1 ? i ? j ? n
- One problem for each
- choice of i and j
1
2
3
n
n
? ?(n2)
j
3
2
1
i
383. Computing the Optimal Costs (cont.)
- 0 if i j
- mi, j min mi, k mk1, j pi-1pkpj
if i lt j - i?kltj
- How do we fill in the tables m1..n, 1..n?
- Determine which entries of the table are used in
computing mi, j - Aij Aik Ak1j
- Subproblems size is one less than the original
size - Idea fill in m such that it corresponds to
solving problems of increasing length
393. Computing the Optimal Costs (cont.)
- 0 if i j
- mi, j min mi, k mk1, j pi-1pkpj
if i lt j - i?kltj
- Length 1 i j, i 1, 2, , n
- Length 2 j i 1, i 1, 2, , n-1
1
2
3
n
n
j
3
Compute rows from bottom to top and from left to
right
2
1
i
40Example min mi, k mk1, j pi-1pkpj
- m2, 2 m3, 5 p1p2p5
- m2, 3 m4, 5 p1p3p5
- m2, 4 m5, 5 p1p4p5
k 2
m2, 5 min
k 3
k 4
1
2
3
6
4
5
6
5
- Values mi, j depend only on values that have
been previously computed
4
j
3
2
1
i
41Example min mi, k mk1, j pi-1pkpj
1
2
3
- Compute A1 ? A2 ? A3
- A1 10 x 100 (p0 x p1)
- A2 100 x 5 (p1 x p2)
- A3 5 x 50 (p2 x p3)
- mi, i 0 for i 1, 2, 3
- m1, 2 m1, 1 m2, 2 p0p1p2 (A1A2)
- 0 0 10 100 5 5,000
- m2, 3 m2, 2 m3, 3 p1p2p3 (A2A3)
- 0 0 100 5 50 25,000
- m1, 3 min m1, 1 m2, 3 p0p1p3
75,000 (A1(A2A3)) - m1, 2 m3, 3 p0p2p3 7,500
((A1A2)A3)
3
2
1
42Matrix-Chain-Order(p)
O(N3)
434. Construct the Optimal Solution
- In a similar matrix s we keep the optimal values
of k - si, j a value of k such that an optimal
parenthesization of Ai..j splits the product
between Ak and Ak1
1
2
3
n
n
j
3
2
1
444. Construct the Optimal Solution
- s1, n is associated with the entire product
A1..n - The final matrix multiplication will be split at
k s1, n - A1..n A1..s1, n ? As1, n1..n
- For each subproduct recursively find the
corresponding value of k that results in an
optimal parenthesization
1
2
3
n
n
j
3
2
1
454. Construct the Optimal Solution
- si, j value of k such that the optimal
parenthesization of Ai Ai1 ??? Aj splits the
product between Ak and Ak1
1
2
3
6
4
5
6
- s1, n 3 ? A1..6 A1..3 A4..6
- s1, 3 1 ? A1..3 A1..1 A2..3
- s4, 6 5 ? A4..6 A4..5 A6..6
5
4
3
j
2
1
i
464. Construct the Optimal Solution (cont.)
PRINT-OPT-PARENS(s, i, j) if i j then print
Ai else print ( PRINT-OPT-PARENS(s,
i, si, j) PRINT-OPT-PARENS(s, si, j
1, j) print )
1
2
3
6
4
5
6
5
4
j
3
2
1
i
47Example A1? ? ?A6
(
( ( A4 A5 ) A6 ) )
A1
(
A2
A3
)
)
(
s1..6, 1..6
1
2
3
6
4
5
PRINT-OPT-PARENS(s, i, j) if i j then print
Ai else print ( PRINT-OPT-PARENS(s,
i, si, j) PRINT-OPT-PARENS(s, si, j
1, j) print )
6
5
4
j
3
2
1
P-O-P(s, 1, 6) s1, 6 3 i 1, j 6 (
P-O-P (s, 1, 3) s1, 3 1 i 1, j 3
( P-O-P(s, 1, 1) ? A1 P-O-P(s, 2, 3)
s2, 3 2 i 2, j 3 ( P-O-P (s,
2, 2) ? A2 P-O-P (s, 3, 3) ?
A3 ) )
i
48Memoization
- Top-down approach with the efficiency of typical
dynamic programming approach - Maintaining an entry in a table for the solution
to each subproblem - memoize the inefficient recursive algorithm
- When a subproblem is first encountered its
solution is computed and stored in that table - Subsequent calls to the subproblem simply look
up that value
49Memoized Matrix-Chain
- Alg. MEMOIZED-MATRIX-CHAIN(p)
- n ? lengthp 1
- for i ? 1 to n
- do for j ? i to n
- do mi, j ? ?
- return LOOKUP-CHAIN(p, 1, n)
Initialize the m table with large values that
indicate whether the values of mi, j have been
computed
Top-down approach
50Memoized Matrix-Chain
- Alg. LOOKUP-CHAIN(p, i, j)
- if mi, j lt ?
- then return mi, j
- if i j
- then mi, j ? 0
- else for k ? i to j 1
- do q ? LOOKUP-CHAIN(p, i, k)
- LOOKUP-CHAIN(p, k1, j) pi-1pkpj
- if q lt mi, j
- then mi, j ? q
- return mi, j
Running time is O(n3)
51Dynamic Progamming vs. Memoization
- Advantages of dynamic programming vs. memoized
algorithms - No overhead for recursion, less overhead for
maintaining the table - The regular pattern of table accesses may be used
to reduce time or space requirements - Advantages of memoized algorithms vs. dynamic
programming - Some subproblems do not need to be solved
52Matrix-Chain Multiplication - Summary
- Both the dynamic programming approach and the
memoized algorithm can solve the matrix-chain
multiplication problem in O(n3) - Both methods take advantage of the overlapping
subproblems property - There are only ?(n2) different subproblems
- Solutions to these problems are computed only
once - Without memoization the natural recursive
algorithm runs in exponential time
53Elements 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
54Parameters of Optimal Substructure
- How many subproblems are used in an optimal
solution for the original problem - Assembly line
- Matrix multiplication
- How many choices we have in determining which
subproblems to use in an optimal solution - Assembly line
- Matrix multiplication
One subproblem (the line that gives best time)
Two subproblems (subproducts Ai..k, Ak1..j)
Two choices (line 1 or line 2)
j - i choices for k (splitting the product)
55Parameters of Optimal Substructure
- Intuitively, the running time of a dynamic
programming algorithm depends on two factors - Number of subproblems overall
- How many choices we look at for each subproblem
- Assembly line
- ?(n) subproblems (n stations)
- 2 choices for each subproblem
- Matrix multiplication
- ?(n2) subproblems (1 ? i ? j ? n)
- At most n-1 choices
?(n) overall
?(n3) overall
56Longest Common Subsequence
- Given two sequences
- X ?x1, x2, , xm?
- Y ?y1, y2, , yn?
- find a maximum length common subsequence (LCS)
of X and Y - E.g.
- X ?A, B, C, B, D, A, B?
- Subsequences of X
- A subset of elements in the sequence taken in
order - ?A, B, D?, ?B, C, D, B?, etc.
57Example
- X ?A, B, C, B, D, A, B? X ?A, B, C, B,
D, A, B? - Y ?B, D, C, A, B, A? Y ?B, D, C, A,
B, A? - ?B, C, B, A? and ?B, D, A, B? are longest common
subsequences of X and Y (length 4) - ?B, C, A?, however is not a LCS of X and Y
58Brute-Force Solution
- For every subsequence of X, check whether its a
subsequence of Y - There are 2m subsequences of X to check
- Each subsequence takes ?(n) time to check
- scan Y for first letter, from there scan for
second, and so on - Running time ?(n2m)
59Making the choice
- X ?A, B, D, E?
- Y ?Z, B, E?
- Choice include one element into the common
sequence (E) and solve the resulting subproblem - X ?A, B, D, G?
- Y ?Z, B, D?
- Choice exclude an element from a string and
solve the resulting subproblem
60Notations
- Given a sequence X ?x1, x2, , xm? we define
the i-th prefix of X, for i 0, 1, 2, , m - Xi ?x1, x2, , xi?
- ci, j the length of a LCS of the sequences
Xi ?x1, x2, , xi? and Yj ?y1, y2, , yj?
61A Recursive Solution
- Case 1 xi yj
- e.g. Xi ?A, B, D, E?
- Yj ?Z, B, E?
- Append xi yj to the LCS of Xi-1 and Yj-1
- Must find a LCS of Xi-1 and Yj-1 ? optimal
solution to a problem includes optimal solutions
to subproblems
ci, j
ci - 1, j - 1 1
62A Recursive Solution
- Case 2 xi ? yj
- e.g. Xi ?A, B, D, G?
- Yj ?Z, B, D?
- Must solve two problems
- find a LCS of Xi-1 and Yj Xi-1 ?A, B, D? and
Yj ?Z, B, D? - find a LCS of Xi and Yj-1 Xi ?A, B, D, G? and
Yj ?Z, B? - Optimal solution to a problem includes optimal
solutions to subproblems
max ci - 1, j, ci, j-1
ci, j
63Overlapping Subproblems
- To find a LCS of X and Y
- we may need to find the LCS between X and Yn-1
and that of Xm-1 and Y - Both the above subproblems has the subproblem of
finding the LCS of Xm-1 and Yn-1 - Subproblems share subsubproblems
643. Computing the Length of the LCS
- 0 if i 0 or j 0
- ci, j ci-1, j-1 1 if xi yj
- max(ci, j-1, ci-1, j) if xi ? yj
0
1
2
n
yj
y1
y2
yn
xi
0
x1
1
x2
2
i
xm
m
j
65Additional Information
- A matrix bi, j
- For a subproblem i, j it tells us what choice
was made to obtain the optimal value - If xi yj
- bi, j
- Else, if ci - 1, j ci, j-1
- bi, j ?
- else
- bi, j ?
- 0 if i,j 0
- ci, j ci-1, j-1 1 if xi yj
- max(ci, j-1, ci-1, j) if xi ? yj
0
1
2
n
3
b c
yj
A
C
F
D
xi
0
A
1
B
2
i
3
C
D
m
j
66LCS-LENGTH(X, Y, m, n)
- for i ? 1 to m
- do ci, 0 ? 0
- for j ? 0 to n
- do c0, j ? 0
- for i ? 1 to m
- do for j ? 1 to n
- do if xi yj
- then ci, j ? ci - 1, j - 1 1
- bi, j ?
- else if ci - 1, j ci, j - 1
- then ci, j ? ci - 1, j
- bi, j ? ?
- else ci, j ? ci, j - 1
- bi, j ? ?
- return c and b
The length of the LCS if one of the sequences is
empty is zero
Case 1 xi yj
Case 2 xi ? yj
Running time ?(mn)
67Example
0 if i 0 or j 0 ci, j
ci-1, j-1 1 if xi yj
max(ci, j-1, ci-1, j) if xi ? yj
- X ?A, B, C, B, D, A?
- Y ?B, D, C, A, B, A?
0
1
2
6
3
4
5
yj
B
D
A
C
A
B
0
xi
1
A
? 0
? 0
? 0
?1
2
B
? 1
?1
?1
?2
3
C
4
B
5
D
6
A
7
B
684. Constructing a LCS
- Start at bm, n and follow the arrows
- When we encounter a in bi, j ? xi yj
is an element of the LCS
0
1
2
6
3
4
5
yj
B
D
A
C
A
B
0
xi
1
A
? 0
? 0
? 0
?1
2
B
? 1
?1
?1
?2
3
C
4
B
5
D
6
A
7
B
69PRINT-LCS(b, X, i, j)
- if i 0 or j 0
- then return
- if bi, j
- then PRINT-LCS(b, X, i - 1, j - 1)
- print xi
- elseif bi, j ?
- then PRINT-LCS(b, X, i - 1, j)
- else PRINT-LCS(b, X, i, j - 1)
- Initial call PRINT-LCS(b, X, lengthX,
lengthY)
Running time ?(m n)
70Improving the Code
- What can we say about how each entry ci, j is
computed? - It depends only on ci -1, j - 1, ci - 1, j,
and ci, j - 1 - Eliminate table b and compute in O(1) which of
the three values was used to compute ci, j - We save ?(mn) space from table b
- However, we do not asymptotically decrease the
auxiliary space requirements still need table c
71Improving the Code
- If we only need the length of the LCS
- LCS-LENGTH works only on two rows of c at a time
- The row being computed and the previous row
- We can reduce the asymptotic space requirements
by storing only these two rows