Matrix Chain Product 張智星 (Roger Jang)

Slides:



Advertisements
Similar presentations
Dynamic Programming ACM Workshop 24 August Dynamic Programming Dynamic Programming is a programming technique that dramatically reduces the runtime.
Advertisements

Dynamic Programming Introduction Prof. Muhammad Saeed.
CSC 252 Algorithms Haniya Aslam
Dynamic Programming Lets begin by looking at the Fibonacci sequence.
Data Structures Lecture 10 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Dynamic Programming Reading Material: Chapter 7..
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)
CSC401 – Analysis of Algorithms Lecture Notes 12 Dynamic Programming
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 Programming1 Modified by: Daniel Gomez-Prado, University of Massachusetts Amherst.
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.
Lecture 8: Dynamic Programming Shang-Hua Teng. First Example: n choose k Many combinatorial problems require the calculation of the binomial coefficient.
11-1 Matrix-chain Multiplication Suppose we have a sequence or chain A 1, A 2, …, A n of n matrices to be multiplied –That is, we want to compute the product.
Finding the Inverse of a Matrix
Dynamic Programming Introduction to Algorithms Dynamic Programming CSE 680 Prof. Roger Crawfis.
First Ingredient of Dynamic Programming
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.
CS 8833 Algorithms Algorithms Dynamic Programming.
Dynamic Programming 張智星 (Roger Jang) 多媒體資訊檢索實驗室 台灣大學 資訊工程系.
Meeting 18 Matrix Operations. Matrix If A is an m x n matrix - that is, a matrix with m rows and n columns – then the scalar entry in the i th row and.
8.2 Operations With Matrices
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)
2.5 – Determinants and Multiplicative Inverses of Matrices.
1 Chapter 15-2: Dynamic Programming II. 2 Matrix Multiplication Let A be a matrix of dimension p x q and B be a matrix of dimension q x r Then, if we.
Dynamic Programming Typically applied to optimization problems
CSE 504 Discrete Mathematics & Foundations of Computer Science
Advanced Algorithms Analysis and Design
Merge Sort 5/28/2018 9:55 AM Dynamic Programming Dynamic Programming.
10.5 Inverses of Matrices and Matrix Equations
Finding the Inverse of a Matrix
Advanced Design and Analysis Techniques
Matrix Chain Multiplication
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Chapter 8 Dynamic Programming.
CSCE 411 Design and Analysis of Algorithms
Dynamic Programming.
Matrix Chain Multiplication
Merge Sort 1/12/2019 5:31 PM Dynamic Programming Dynamic Programming.
Dynamic Programming 1/15/2019 8:22 PM Dynamic Programming.
Dynamic Programming Dynamic Programming 1/15/ :41 PM
CS6045: Advanced Algorithms
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
Matrices.
Merge Sort 2/22/ :33 AM Dynamic Programming Dynamic Programming.
Chapter 15: Dynamic Programming II
Lecture 8. Paradigm #6 Dynamic Programming
Matrix Algebra.
Algorithms CSCI 235, Spring 2019 Lecture 28 Dynamic Programming III
Dynamic Programming-- Longest Common Subsequence
Longest increasing subsequence (LIS) Matrix chain multiplication
Dynamic Programming CISC4080, Computer Algorithms CIS, Fordham Univ.
Dimensions matching Rows times Columns
Dynamic Programming 動態規劃
Merge Sort 4/28/ :13 AM Dynamic Programming Dynamic Programming.
3.6 Multiply Matrices.
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
Applied Discrete Mathematics Week 4: Functions
Presentation transcript:

Matrix Chain Product 張智星 (Roger Jang) jang@mirlab.org http://mirlab.org/jang 多媒體資訊檢索實驗室 台灣大學 資訊工程系

Matrix Chain Products (MCP) Review: Matrix Multiplication. C = A*B A is p × q and B is q × r O(pqr ) time A C B p r q i j i,j for (i=0; i<p; i++) for (j=0; j<r; j++){ c[i,j]=0; for (k=0; k<q; k++) c[i,j]+=a[i,k]*b[k,j]; }

Matrix Chain-Products Problem definition Given n matrices A0, A1, …, An-1, where Ai is of dimension di×di+1 How to parenthesize A0*A1*…*An-1 to minimize the overall cost?

Example of MCP The product A (2×3), B (3×5), C (5×2), D (2×4) can be fully parenthesized in 5 distinct ways: (A (B (C D)))  5×2×4 + 3×5×4 + 2×3×4 = 124 (A ((B C) D))  3×5×2 + 3×2×4 + 2×3×4 = 78 ((A B) (C D))  2×3×5 + 5×2×4 + 2×5×4 = 110 ((A (B C)) D)  3×5×2 + 2×3×2 + 2×2×4 = 58 (((A B) C) D)  2×3×5 + 2×5×2 + 2×2×4 = 66 The way the chain is parenthesized can have a dramatic impact on the cost of evaluating the product.

An Enumeration Approach Matrix Chain Product Alg.: Try all possible ways to parenthesize A=A0*A1*…*An-1 Calculate total number of operations for each way Pick the one that is best Running time: The number of ways of parenthesizations is equal to the number of binary trees with n leave nodes It is called the Catalan number, and it is almost 4n  exponential! ((A0(A1A2))A3)binary tree Dynamic Programming

Observations Leading to DP Define subproblems: Find the best parenthesization of Ai*Ai+1*…*Aj. Let Ni,j denote the minimum number of operations required by this subproblem. The optimal solution for the whole problem is N0,n-1. Subproblem optimality: The optimal solution can be defined in terms of optimal subproblems There has to be a final multiplication (root of the expression tree) for the optimal solution. Say, the final multiply is at index i: (A0*…*Ai)*(Ai+1*…*An-1). Then the optimal solution N0,n-1 is the sum of two optimal subproblems, N0,i and Ni+1,n-1 plus the time for the last multiply.

(Ai*Ai+1*…*Ak)(Ak+1*Ak+2*…*Aj) Three-Step DP Formula To solve matrix chain-product with DP Optimum-value function Ni,j: the minimum number of operations required by parenthesizing Ai*Ai+1*…*Aj. Recurrent equation Answer N0, n-1 (Ai*Ai+1*…*Ak)(Ak+1*Ak+2*…*Aj)

Subproblem Overlap 0..3 (A0)( A1A2A3) (A0A1A2)(A3) (A0 A1)( A2A3) 0..0 1..3 0..1 2..3 0..2 3..3 1..1 0..0 1..1 2..2 3..3 ... 2..3 1..2 3..3 2..2 3..3 1..1 2..2 Due to the overlap, we need to keep track of previous results

Hint: IJ-mode vs. XY-mode Table Filling for DP The bottom-up approach fills in the upper-triangle of the n×n array by diagonals, starting from Ni,i’s. Ni,j gets values from pervious entries in row i and column j. Filling in each entry in the N table takes O(n) time  Total time O(n3) Actual parenthesization can be found by storing the best “k” for each entry N n-1 1 2 j … Answer! 1 … i n-1 Easy for back tracking Hint: IJ-mode vs. XY-mode

Walkthrough of an MCP Example Quiz! Product of A0 (2×3), A1 (3×5), A2 (5×2), A3 (2×4) A0 2×3 A1 3×5 A2 5×2 A3 2×4 2×3 30 2×5 k=0 42 2×2 58 2×4 k=2 3×5 3×2 k=1 54 3×4 5×2 40 5×4 A0 2×3 A1 3×5 A2 5×2 A3 2×4 Optimum value of k (for back tracking) Solution (after back tracking)  (A0A1A2)(A3)=(A0(A1A2))(A3)

Exercise Product of A0 (2×3), A1 (3×5), A2 (5×2), A3 (2×4), A4 (4×1) Quiz! Product of A0 (2×3), A1 (3×5), A2 (5×2), A3 (2×4), A4 (4×1) A0 2×3 A1 3×5 A2 5×2 A3 2×4 A4 4×1 2×3 30 2×5 k=0 42 2×2 58 2×4 k=2 2×1 k= 3×5 3×2 k=1 54 3×4 3×1 5×2 40 5×4 5×1 k=3 4×1 A0 2×3 A1 3×5 A2 5×2 A3 2×4 A4 4×1 Solution 