lecture21: dynamic programming bohyung han cse, postech [email protected] csed233: data structures...
TRANSCRIPT
Lecture21: Dynamic Programming
Bohyung HanCSE, POSTECH
CSED233: Data Structures (2014F)
2 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014
Matrix Chain Products
• Matrix Multiplication
Size of : Size of : takes times of basic operations
and produces a matrix.
A C
B
d d
f
e
f
e
i
j
i,j
𝑪 [ 𝑖 , 𝑗 ]=∑𝑘=1
𝑒
𝑨 [ 𝑖 ,𝑘 ]𝑩 [𝑘 , 𝑗 ]
3 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014
Matrix Chain Products
• Matrix chain multiplication Associative: Compute Size of Observation: The time complexity of a matrix chain multiplication
depends on the sequence of multiplications. Problem: We want to find a way to compute the result with the
minimal number of operations.
• Example Matrix size: , , : ops : ops
4 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014
Brute Force Algorithm‐
• An enumeration approach• Matrix chain product algorithm
Try all possible ways to parenthesize Calculate number of operations for each one. Pick the one that is best.
• Running time The number of parenthesizations is equal to the number of binary
trees with nodes. This is an exponential number. This is a terrible algorithm!
5 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014
Greedy Algorithm
• Choose the local optimal iteratively• Repeatedly select the product that uses the fewest operations.• Example: computation of
Size of : Size of : Size of : Size of : vs. vs. : ops
6 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014
Another Example
• Another example of Size of : Size of : Size of : Size of :
• The greedy approach Best solution: Number of operations:
• Another method
Number of operations:
The greedy approach does not give us an optimal solution necessarily.
7 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014
Dynamic Programming
• Concept Primarily for optimization problems that may require a lot of time
otherwise Simplifying a complicated problem by breaking it down into simpler
subproblems in a recursive manner
• Two key observations: The problem can be split into subproblems. The optimal solution can be defined in terms of optimal subproblems.
• Condition for dynamic programming Simple subproblems: The subproblems can be defined in terms of a
few variables. Subproblem optimality: The global optimum value can be defined in
terms of optimal subproblems Subproblem overlap: The subproblems are not independent, but
instead they overlap (hence, should be constructed bottom up).‐
8 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014
Dynamic Programming for Matrix Chain Product
• Problem definitionWhole problem: computation of Find the best parenthesization of Let denote the number of operations done by this subproblem. The optimal solution for the whole problem is .
• Approach There has to be a final multiplication (root of the expression tree) for
the optimal solution; for example, The optimal solution is the sum of two optimal subproblems, and
plus the time for the last multiplication. We perform this procedure in a recursive manner.
9 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014
A Characterizing Equation
• Let us consider all possible places for that final multiply: Recall that is dimensional matrix. So, a characterizing equation for is the following:
Note that subproblems overlap and hence we cannot divide the prob-lem into completely independent subproblems (divide and conquer).
• Bottom up computation‐ Base case:
Until you get all .
𝑁 𝑖 , 𝑗=min𝑖 ≤𝑘< 𝑗
{𝑁 𝑖 ,𝑘+𝑁 𝑘+1 , 𝑗+𝑑𝑖𝑑𝑘+1𝑑 𝑗+1 }
10 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014
Visualization of Dynamic Programming
• Table construction The bottom up construction fills the ‐
upper diagonal matrix. gets values from pervious entries
in -th row and -th column Filling in each entry in the table takes
time. Total running time: Getting actual parenthesization can
be done by remembering for each entry.
Final answer
N 0 1
01
2 …
𝑛−1
…
𝑗
𝑖
𝑁 𝑖 , 𝑗=min𝑖 ≤𝑘< 𝑗
{𝑁 𝑖 ,𝑘+𝑁 𝑘+1 , 𝑗+𝑑𝑖𝑑𝑘+1𝑑 𝑗+1 }
(𝒊 , 𝒊 )(𝒊+𝟏 . 𝒋 )
𝑛−1
(𝒊 , 𝒊+𝟏 )
(𝒊+𝟐 . 𝒋 )
(𝒊 , 𝒊+𝟐 )
(𝒊+𝟑 . 𝒋 )
11 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014
A Dynamic Programming Algorithm
• Characteristics Since subproblems overlap,
we don’t use recursion. Instead, we construct
optimal subproblems bottom up.‐
’s are easy, so start with them.
Then do subproblems with larger lengths.
The running time is .
Algorithm matrixChain(S): Input: sequence S of n matrices to be multiplied Output: number of operations in an optimal
paranethization of S for i 0 to n-1 do Ni,i 0 for b 1 to n-1 do for i 0 to n-b-1 do j i+b Ni,j +infinity for k i to j-1 do Ni,j min{Ni,j , Ni,k +Nk+1,j +di dk+1 dj+1}
12 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014
Similarity between Strings
• A common text processing problem: Two strands of DNA Two versions of source code for the same program diff: a built in program for comparing text files in unix or linux.‐
13 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014
The Longest Common Subsequence
• Subsequences A string of the form from a character string ,
where . Not necessary contiguous but taken in order Not the same as substring!
• Examples Original string: ABCDEFGHIJK Subsequence: ACEGIJK (ABCDEFGHIJK), DFGHK (ABCDEFGHIJK) Not subsequence: DAGH
• Longest Common Subsequence (LCS) problem Given two strings and , the longest common subsequence (LCS)
problem is to find a longest subsequence common to both and .
14 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014
A Poor Approach to the LCS Problem
• A Brute force solution:‐ Enumerate all subsequences of Test which ones are also subsequences of Pick the longest one.
• Analysis: If is of length , then it has subsequences. If is of length , the time complexity is This is an exponential time algorithm!
15 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014
A Dynamic Programming Approach
• Procedure Define to be the length of the longest common subsequence of
and . Allow for 1 as an index, so and to indicate ‐
that the null part of and has no match with the other. Then we can define in the general case as follows:
If , then If , then
Match
No match
16 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014
Visualizing the LCS Algorithm
𝑿=𝑮𝑻𝑻𝑪𝑪𝑻𝑨𝑨𝑻𝑨01234 56789
𝒀=𝑪𝑮𝑨𝑻𝑨𝑨𝑻𝑻𝑮𝑨𝑮𝑨01234 567891011
17 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014
Visualizing the LCS Algorithm
𝑿=𝑮𝑻𝑻𝑪𝑪𝑻𝑨𝑨𝑻𝑨01234 56789
𝒀=𝑪𝑮𝑨𝑻𝑨𝑨𝑻𝑻𝑮𝑨𝑮𝑨01234 567891011
• LCS is not unique.
18 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014
Analysis of LCS Algorithm
• We have two nested loops The outer one iterates times. The inner one iterates times. A constant amount of work is done inside each iteration of the inner
loop. Thus, the total running time is .
• Solution The final answer is contained in . The subsequence can be recovered from the table by backtracking.
19 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014
Pseudocode of LCS Algorithm
Algorithm LCS(X,Y) Input: Strings X = x0x1x2…xn-1 and Y = y0y1y2…ym-1 Output: L[i, j] (i = 0,…,n-1, j = 0,…,m-1) of a longest subsequence of X and Y
for i 0 to n-1 do L[i,-1] 0 for j 0 to m-1 do L[-1,-j] 0
for i 0 to n-1 do for j 0 to m-1 do if xi = yj then L[i, j] L[i-1, j-1] + 1 else L[i, j] max{L[i-1, j] , L[i, j-1]}
return L
20