Longest increasing subsequence (LIS) Matrix chain multiplication

Slides:



Advertisements
Similar presentations
Dynamic Programming Introduction Prof. Muhammad Saeed.
Advertisements

Multiplying Matrices Two matrices, A with (p x q) matrix and B with (q x r) matrix, can be multiplied to get C with dimensions p x r, using scalar multiplications.
Dynamic Programming Reading Material: Chapter 7..
Dynamic Programming Technique. D.P.2 The term Dynamic Programming comes from Control Theory, not computer science. Programming refers to the use of tables.
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
UNC Chapel Hill Lin/Manocha/Foskey Optimization Problems In which a set of choices must be made in order to arrive at an optimal (min/max) solution, subject.
Dynamic Programming Reading Material: Chapter 7 Sections and 6.
© 2004 Goodrich, Tamassia Dynamic Programming1. © 2004 Goodrich, Tamassia Dynamic Programming2 Matrix Chain-Products (not in book) Dynamic Programming.
Dynamic Programming Introduction to Algorithms Dynamic Programming CSE 680 Prof. Roger Crawfis.
Dynamic Programming UNC Chapel Hill Z. Guo.
Dynamic Programming. Well known algorithm design techniques:. –Divide-and-conquer algorithms Another strategy for designing algorithms is dynamic programming.
CS 5243: Algorithms Dynamic Programming Dynamic Programming is applicable when sub-problems are dependent! In the case of Divide and Conquer they are.
COSC 3101A - Design and Analysis of Algorithms 7 Dynamic Programming Assembly-Line Scheduling Matrix-Chain Multiplication Elements of DP Many of these.
CSC401: Analysis of Algorithms CSC401 – Analysis of Algorithms Chapter Dynamic Programming Objectives: Present the Dynamic Programming paradigm.
Optimization Problems In which a set of choices must be made in order to arrive at an optimal (min/max) solution, subject to some constraints. (There may.
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
Computer Sciences Department1.  Property 1: each node can have up to two successor nodes (children)  The predecessor node of a node is called its.
1 Chapter 2 Program Performance. 2 Concepts Memory and time complexity of a program Measuring the time complexity using the operation count and step count.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
CSE15 Discrete Mathematics 03/01/17
Dynamic Programming Typically applied to optimization problems
Theoretical analysis of time efficiency
Advanced Algorithms Analysis and Design
Applied Discrete Mathematics Week 2: Functions and Sequences
Matrices Rules & Operations.
Lecture 3: Parallel Algorithm Design
Data Structures: Disjoint Sets, Segment Trees, Fenwick Trees
Merge Sort 5/28/2018 9:55 AM Dynamic Programming Dynamic Programming.
Analysis of Algorithms
5 Systems of Linear Equations and Matrices
Algorithmics - Lecture 11
Advanced Algorithms Analysis and Design
Chapter 6 Transform-and-Conquer
Seminar on Dynamic Programming.
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Matrix Chain Multiplication
Chapter 4 Divide-and-Conquer
Computation.
Lecture 22: Parallel Algorithms
Data Structures: Segment Trees, Fenwick Trees
Chapter 8 Dynamic Programming.
Algorithm Analysis (not included in any exams!)
CSCE 411 Design and Analysis of Algorithms
Dynamic Programming.
Dynamic Programming.
Unit-5 Dynamic Programming
Matrix Chain Multiplication
Topic: Divide and Conquer
Merge Sort 1/12/2019 5:31 PM Dynamic Programming Dynamic Programming.
Searching CLRS, Sections 9.1 – 9.3.
Dynamic Programming 1/15/2019 8:22 PM Dynamic Programming.
Dynamic Programming Dynamic Programming 1/15/ :41 PM
Dynamic Programming Dynamic Programming 1/18/ :45 AM
Merge Sort 1/18/ :45 AM Dynamic Programming Dynamic Programming.
Dynamic Programming Merge Sort 1/18/ :45 AM Spring 2007
Merge Sort 2/22/ :33 AM Dynamic Programming Dynamic Programming.
Chapter 15: Dynamic Programming II
Algorithms CSCI 235, Spring 2019 Lecture 28 Dynamic Programming III
Dynamic Programming-- Longest Common Subsequence
Dynamic Programming II DP over Intervals
Matrix Chain Product 張智星 (Roger Jang)
Merge Sort 4/28/ :13 AM Dynamic Programming Dynamic Programming.
At the end of this session, learner will be able to:
Matrix Chain Multiplication
CSCI 235, Spring 2019, Lecture 25 Dynamic Programming
Dynamic Programming Merge Sort 5/23/2019 6:18 PM Spring 2008
Algorithms CSCI 235, Spring 2019 Lecture 27 Dynamic Programming II
Seminar on Dynamic Programming.
Data Structures and Algorithms Dynamic Programming
Presentation transcript:

Longest increasing subsequence (LIS) Matrix chain multiplication A4B33ALG 2015/11 ALG 11 Dynamic programming Longest increasing subsequence (LIS) Matrix chain multiplication

Longest increasing subsequence (LIS) A4B33ALG 2015/11 Longest increasing subsequence (LIS) The longest increasing subsequence may not be contiguous. 5 4 9 11 5 3 2 10 0 8 6 1 7 Solution: 4 5 6 7 Possible problem modifications Subsequence properties: decreasing, non-decreasing, non-increasing, arithmetic, with bounded growth rate, with weighted elements, ... etc., ... not explicitly analysed here Standard DP approach Transform to known problem, define appropriate DAG according to the subsequence properties, find longest path in DAG. 1

Longest increasing subsequence (LIS) A4B33ALG 2015/11 Longest increasing subsequence (LIS) Standard DP approach Transformation to the earlier problem The sequence elements are DAG nodes. DAG is topologically sorted, position in sequence = position in top. ordering. Edge x --> y exists if and only if order of x is lower than order of y and also x<y. Find longest path in this DAG. 5 4 9 11 5 3 2 10 8 6 1 7 Algorithm is known, its complexity is (N+M)  O(N2). If the sequence is increasing then the complexity is (N2). 2

Longest increasing subsequence (LIS) A4B33ALG 2015/11 Longest increasing subsequence (LIS) Original faster DP approach Regster optimal subsequences of all possible lengths and in each step update one of them. DP table: k .. index of element V .. value of element p .. predecessor iL .. index of the last element in an increasing optimal subsequence with length d = 1, 2, ..., N. k 1 2 3 4 5 6 7 8 9 10 11 12 V 5 4 9 11 5 3 10 1 8 6 2 7 p -- iL -- For each index k: Let d be the index of the biggest element, which satisfies V[iL[d]] < V[k]. Set iL[d+1] := k, p[k] := iL[d], if such d exists. Else iL[1] := k, p[k] := null. 3

Longest increasing subsequence (LIS) A4B33ALG 2015/11 Longest increasing subsequence (LIS) k .. index of element V .. value of element p .. predecessor iL .. index of the last element in an increasing optimal subsequence with length d = 1, 2, ..., N. k 1 2 3 4 5 6 7 8 9 10 11 12 V 4 3 8 10 4 2 9 7 5 1 6 k = 11 p -- -- 2 3 2 -- 5 -- 5 5 8 iL 8 11 10 V[iL] 1 5 For each index k: Let d be an index of max. elem. which satisfies V[iL[d]] < V[k]. Then iL[d+1] := k, p[k] := iL[d], if such d exists. Else iL[1] := k, p[k] := null. k = 12 p -- -- 2 3 2 -- 5 -- 5 5 8 10 iL 8 11 10 12 V[iL] 1 5 6 4

Longest increasing subsequence (LIS) A4B33ALG 2015/11 Longest increasing subsequence (LIS) k 1 2 3 4 5 6 7 8 9 10 11 12 k 1 2 3 4 5 6 7 8 9 10 11 12 V 4 3 8 10 4 2 9 7 5 1 6 V 4 3 8 10 4 2 9 7 5 1 6 p -- p -- -- 2 3 2 iL 1 iL 2 5 4 V[iL] 4 V[iL] 3 4 10 p -- -- p -- -- 2 3 2 -- iL 2 iL 6 5 4 V[iL] 3 V[iL] 2 4 10 p -- -- 2 p -- -- 2 3 2 -- 5 iL 2 3 iL 6 5 7 V[iL] 3 8 V[iL] 2 4 9 p -- -- 2 3 p -- -- 2 3 2 -- 5 -- iL 2 3 4 iL 8 5 7 V[iL] 3 8 10 V[iL] 4 9 5

Longest increasing subsequence (LIS) A4B33ALG 2015/11 Longest increasing subsequence (LIS) k 1 2 3 4 5 6 7 8 9 10 11 12 k 1 2 3 4 5 6 7 8 9 10 11 12 V 4 3 8 10 4 2 9 7 5 1 6 V 4 3 8 10 4 2 9 7 5 1 6 p -- -- 2 3 2 -- 5 -- p -- -- 2 3 2 -- 5 -- 5 5 iL 8 5 7 iL 8 5 10 V[iL] 4 9 V[iL] 4 5 p -- -- 2 3 2 -- 5 -- 5 p -- -- 2 3 2 -- 5 -- 5 5 8 VL 8 5 9 iL 8 11 10 V[iL] 4 7 V[iL] 1 5 p -- -- 2 3 2 -- 5 -- 5 5 8 10 iL 8 11 10 12 V[iL] 1 5 6 Optimal path reconstruction The last defined element in iL is the index of the last element of one of the optimal subsequences of the whole sequence. The references in array p represent this subsequence. 6

Longest increasing subsequence (LIS) A4B33ALG 2015/11 Longest increasing subsequence (LIS) Asymptotic complexity For each index k: Let d be the index of the biggest element, which satisfies V[iL[d]] < V[k]. k 1 2 3 4 5 6 7 8 9 10 11 12 V 4 3 8 10 4 2 9 7 5 1 6 p -- -- 2 3 2 -- 5 -- 5 5 8 10 iL 8 11 10 12 V[iL] 1 5 6 The values V[iL[d]], d = 1,2, ... form a non-decreasing sequence. In each step k the value V[k] is fixed. The biggest element V[iL[d]] which satisfies V[iL[d]] < V[k] can be found in time O(log N) by binary search. There are N steps, the resulting asymptotic complexity is O(N ∙ log N), it can be shown to be exactly (N ∙ log N). 6

Matrix chain multiplication A4B33ALG 2015/11 Matrix chain multiplication Example instance of the problem Compute in most effective way the matrix product A1  A2  A3  A4  A5  A6, where the dimensions of the matrices are (in the given order) 30  35, 35  15, 15  5, 5  10, 10  20, 20  25. (The dimesion of the resulting matrix D is 30  20). Matrices dimensions depicted to scale A1 A2 A3 A4 A5 A6 D      = Example follows [CLRS], chapter 15. 7

Matrix chain multiplication A4B33ALG 2015/11 Matrix chain multiplication Number of multiplications in two matrices product ... ... ... ...  ... ... = ... b ... ... a ... ... ... a ... ... ... ... ... ... ... ... c ... ... b c  = 1 ... b b ... 1 b multiplications yield one element of the result matrix. a * c elements in the result matrix Calculating product of two matrices of sizes ab and bc require a * b * c multiplications of numbers (floats, doubles, etc.). We do not consider summation here, it can be analysed analogously. 8

Matrix chain multiplication A4B33ALG 2015/11 Matrix chain multiplication A1 A2 A3 A4 A5 A6 D      = We consider different parenthesizations and thus different orders of calculations induced by those parenthesizations. Evaluation order Corresponding expression # operations left to right ((((A1  A2)  A3)  A4)  A5)  A6 43 500 right to left A1  (A2  (A3  (A4  (A5  A6)))) 47 500 worst A1  ((A2  ((A3  A4)  A5))  A6) 58 000 best (A1  (A2  A3))  ((A4  A5)  A6) 15 125 9

Matrix chain multiplication A4B33ALG 2015/11 Matrix chain multiplication Example: Comparison of multiplication of 3 matrices 30 + 24 = 54 op. 40 + 60 = 100 op. 3 3 4 4 3* 2 * 4 = 24 op. 5 * 3 * 4 = 60 op. 2 3 3 5 4 5 2 4 5 * 3 * 2 = 30 op. 0 op. 0 op. 2 * 5 * 4 = 40 op. 2 2 3 3 5 5 4 4 5 5 2 2 10

Matrix chain multiplication A4B33ALG 2015/11 Matrix chain multiplication A1 = A2 = A3 = 2 3 5 4 5 2 Product (A1  A2)  A3 requires 54 multiplications . Product A1  (A2  A3) requires 100 multiplications. Obviously, the order of parentheses is important . Catalan numbers CN Product A1  A2  A3  ...  AN can be parenthesized in CN = Comb(2N, N) / (N+1) different ways. C1, C2, ..., C7 = 1, 1, 2, 5, 14, 42, 132. CN > 2N pro N > 7. In general, checking each way of parenthesization separately leads to the exponencial complexity of the task. 11

Matrix chain multiplication A4B33ALG 2015/11 Matrix chain multiplication Illustration All 14 different possibilities of product parenthesization of 5 factors A1  (A2  (A3  (A4  A5))) A1  (A2  ((A3  A4)  A5)) A1  ((A2  A3)  (A4  A5)) A1  ((A2  (A3  A4))  A5) A1  (((A2  A3)  A4)  A5) (A1  A2)  (A3  (A4  A5)) (A1  A2)  ((A3  A4)  A5) (A1  (A2  A3))  (A4  A5) ((A1  A2)  A3)  (A4  A5) (A1  (A2  (A3  A4)))  A5 (A1  ((A2  A3)  A4))  A5 ((A1  A2)  (A3  A4))  A5 ((A1  (A2  A3))  A4)  A5 (((A1  A2)  A3)  A4)  A5 12

Matrix chain multiplication A4B33ALG 2015/11 Matrix chain multiplication A1  (A2  A3  A4 ...  AN-1  AN) N  1 possible places where the expression is divided in two subexpressions, those are processed separately and finally multiplied together. (A1  A2)  (A3  A4 ...  AN-1  AN) (A1  A2  A3)  (A4 ...  AN-1  AN) (A1  A2  A3  A4)  ( ...  AN-1  AN) . . . (A1  A2  A3  A4  ...)  (AN-1  AN) (A1  A2  A3  A4  ...  AN-1)  AN Let us suppose (as is usual in DP) that the optimum parenthesization is precomputed for all blue subexpressions. 13

Matrix chain multiplication A4B33ALG 2015/11 Matrix chain multiplication A1  (A2  A3  A4 ...  AN-1  AN) = B[1,1]  B[2,N] (A1  A2)  (A3  A4 ...  AN-1  AN) = B[1,2]  B[3,N] (A1  A2  A3)  (A4 ...  AN-1  AN) = B[1,3]  B[4,N] (A1  A2  A3  A4)  ( ...  AN-1  AN) = B[1,4]  B[5,N] . . . . . . (A1  A2  A3  A4  ...)  (AN-1  AN) = B[1,N-2]  B[N-1,N] (A1  A2  A3  A4  ...  AN-1)  AN = B[1,N-1]  B[N,N] Matrix B[i, j] is the product of the corresponding subexpression. Denote by r(X) resp. s(X) the number of rows resp. columns of matrix X. The matrix multiplication rules say: r(B[i, j]) = r(Ai), s(B[i, j]) = s(Aj), 1 ≤ i ≤ j ≤ N. 14

Matrix chain multiplication A4B33ALG 2015/11 Matrix chain multiplication Let MO[i, j] be minimum number of multiplications needed to compute B[i, j], i.e. minimum number of multiplications needed to compute the matrix Ai  Ai+1  ...  Aj-1  Aj. B[1,1]  B[2,N] MO[1,1] + r(A1)*s(A1)*s(AN) + MO[2, N] B[1,2]  B[3,N] MO[1,2] + r(A1)*s(A2)*s(AN) + MO[3, N] B[1,3]  B[4,N] MO[1,3] + r(A1)*s(A3)*s(AN) + MO[4, N] . . . B[1,N-2]  B[N-1,N] MO[1,N-2] + r(A1)*s(AN-2)*s(AN) + MO[N-1, N] B[1,N-1]  B[N,N] MO[1,N-1] + r(A1)*s(AN-1)*s(AN) + MO[N, N] # of multiplications in the left subexpression # of multiplications in B[1,.]  B[.,N] # of multiplications in the right subexpression For MO[1,N], which is the solution of the whole problem, we get MO[1,N] = min {MO[1,k] + r(A1)*s(Ak)*s(AN) + MO[k+1, N] | k = 1..N-1} 15

Matrix chain multiplication A4B33ALG 2015/11 Matrix chain multiplication MO[1,N] = min {MO[1,k] + r(A1)*s(Ak)*s(AN) + MO[k+1, N] | k = 1..N-1} When values MO[i, j] for subexpressions shorter than [1, N] is known then the problem solution ( = value MO[1, N]) , can be found in time (N). (*) Recursive and repeated exploitation of smaller subproblems solutions The analysis which we performed with the whole expression A1  A2  A3  ...  AN , can be analogously performed for each contiguous subexpression ... AL  AL+1  ...  AR-1  AR ..., 1 ≤ L ≤ R ≤ N. The number of these subexpressions is the same as the number of index pairs (L, R), 1 ≤ L ≤ R ≤ N. it is equal to Comb(N, 2)  (N2). A particular subproblem specified by (L, R) can be solved according to (*) in time O(N), the whole solution time is therefore O(N*N2) = O(N3). 16

Matrix chain multiplication A4B33ALG 2015/11 Matrix chain multiplication * MO[L,R] = min {MO[L,k] + r(AL)*s(Ak)*s(AR) + MO[k+1,R] | k = L..R-1} Values MO[L,R] can be stored in 2D array at position [L][R]. Calcultion of MO[L,R] according to depends on values MO[x,y] in which the difference y - x is less then the difference R - L. The DP table is thus filled in the order of increasing difference R - L. 0. Calculate MO[L][R], where R-L = 0, it is the main diagonal. 1. Calculate MO[L][R], where R-L = 1, it is the diagonal just above the main diagonal. 2. Calculate MO[L][R], where R-L = 1, it is the diagonal just above the previous diagonal. ... N-1. Calculate MO[L][R], where R-L = N-1, it is the upper right corner of the table. * 17

Matrix chain multiplication A4B33ALG 2015/11 Matrix chain multiplication Calculation of the DP table -- progress scheme R - L = 0 R - L = 1 R - L = 2 R - L = 3 R - L = N-2 R - L = N-1 Stop 18

Matrix chain multiplication A4B33ALG 2015/11 Matrix chain multiplication MO[L,R] = min {MO[L,k] + r(AL)*s(Ak)*s(AR) + MO[k+1,R] | k = L..R-1} Example of one cell calculation MO[3,8] = min { MO[3,3] + r(A3)*s(A3)*s(A8) + MO[4,8], MO[3,4] + r(A3)*s(A4)*s(A8) + MO[5,8], MO[3,5] + r(A3)*s(A5)*s(A8) + MO[6,8], MO[3,6] + r(A3)*s(A6)*s(A8) + MO[7,8], MO[3,7] + r(A3)*s(A7)*s(A8) + MO[8,8]} Denote P[L, R] := r(AL)*s(AR). Then 0 + s(A3)*P[3,8] + w, a + s(A4)*P[3,8] + x, b + s(A5)*P[3,8] + y, c + s(A6)*P[3,8] + z, d + s(A7)*P[3,8] + 0}. MO 8 3 3 a b c d w x y z 19

Matrix chain multiplication A4B33ALG 2015/11 Matrix chain multiplication Matrices A1 A2 A3 A4 A5 A6 D      = 30  35 35  15 15  5 5  10 10  20 20  25 30  25 MO 1 2 3 4 5 6 1 0 15750 7875 9375 11875 15125 2 0 0 2625 4375 7125 10500 3 0 0 0 750 2500 5375 4 0 0 0 0 1000 3500 5 0 0 0 0 0 5000 6 0 0 0 0 0 0 Optimum 20

Matrix chain multiplication A4B33ALG 2015/11 Matrix chain multiplication MO[L,R] = min {MO[L,k] + r(AL)*s(Ak)*s(AR) + MO[k+1,R] | k = L..R-1} When the value of MO[L,R] is established we store in the 2D reconstruction table RT at the position [L][R] the walue of k in which the minimum in was attained. The value k = RT[L][R] defines the division of the subexpression into two smaller optimal subexpressions The value RT[1, N] defines the division of the whole expression into the first two optimal subexpressions And then the reconstruction continues recursively analogously * * (AL  AL+1  ...  AR) (AL  AL+1  ...  Ak)  (Ak+1  Ak+2  ...  AR). A1  A2  ...  AN (A1  A2  ...  Ak)  (Ak+1  Ak+2  ...  AN). for (A1  A2  ...  Ak) and for (Ak+1  Ak+2  ...  AN) and so on. 21

Matrix chain multiplication A4B33ALG 2015/11 Matrix chain multiplication 1 2 3 4 5 6 1 0 1 1 3 3 3 2 0 0 2 3 3 3 3 0 0 0 3 3 3 4 0 0 0 0 4 5 5 0 0 0 0 0 5 6 0 0 0 0 0 0 A1 A6 A2 A3 A4 A5 A1 A6 A1 (A1  (A2  A3))  ((A4  A5)  A6) D  = A1 A3 A4 A6 A1 A1 A2 A3 A4 A5 A6 A6 RT 22

Matrix chain multiplication A4B33ALG 2015/11 Matrix chain multiplication Asymptotic complexity The complexity of calculating one cell value is proportional to the number of other cells in the table used to perform this calculation. Row index Row sums 1/2 * (N1) * N 1 2 3 N3 N2 N1 k = N1 1/2 * (N2) * (N1) 1 2 N4 N3 N2 k = N2 1/2 * (N3) * (N2) 1 N5 N4 N3 k = N3 k = k 1/2 * k * (k+1) 1 Nk2 Nk1 Nk k = 3 1/2 * 3 * 4 1 2 3 k = 2 1/2 * 2 * 3 1 2 k = 1 1/2 * 1 * 2 1 1/2 * k * (k+1) =  N-1 k=1 1/2 * k2 +  N-1 k=1 1/2 * k  N-1 k=1 Total = 1/2 * (N1) * N * (2N1)/6 + 1/2 * (N1) * N/2  (N3) 23