Matrix Multiplication Chains

Slides:



Advertisements
Similar presentations
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.
Advertisements

Algorithm Design Methodologies Divide & Conquer Dynamic Programming Backtracking.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2006 Lecture 1 (Part 3) Design Patterns for Optimization Problems.
Dynamic Programming Carrie Williams. What is Dynamic Programming? Method of breaking the problem into smaller, simpler sub-problems Method of breaking.
CPSC 311, Fall 2009: Dynamic Programming 1 CPSC 311 Analysis of Algorithms Dynamic Programming Prof. Jennifer Welch Fall 2009.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2009 Design Patterns for Optimization Problems Dynamic Programming.
Dynamic Programming Code
Dynamic Programming Optimization Problems Dynamic Programming Paradigm
CPSC 411 Design and Analysis of Algorithms Set 5: Dynamic Programming Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 5 1.
1 Dynamic Programming Andreas Klappenecker [based on slides by Prof. Welch]
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.
Analysis of Algorithms CS 477/677
Dynamic Programming Reading Material: Chapter 7 Sections and 6.
Analysis of Algorithms
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.
Algorithms and Data Structures Lecture X
Dynamic Programming UNC Chapel Hill Z. Guo.
October 21, Algorithms and Data Structures Lecture X Simonas Šaltenis Nykredit Center for Database Research Aalborg University
CS 5243: Algorithms Dynamic Programming Dynamic Programming is applicable when sub-problems are dependent! In the case of Divide and Conquer they are.
Dynamic Programming Sequence of decisions. Problem state. Principle of optimality. Dynamic Programming Recurrence Equations. Solution of recurrence equations.
COSC 3101A - Design and Analysis of Algorithms 7 Dynamic Programming Assembly-Line Scheduling Matrix-Chain Multiplication Elements of DP Many of these.
CS 8833 Algorithms Algorithms Dynamic Programming.
Dynamic Programming (Ch. 15) Not a specific algorithm, but a technique (like divide- and-conquer). Developed back in the day when “programming” meant “tabular.
1 Dynamic Programming Andreas Klappenecker [partially based on slides by Prof. Welch]
Dynamic Programming Greed is not always good.. Jaruloj Chongstitvatana Design and Analysis of Algorithm2 Outline Elements of dynamic programming.
Algorithm Design Methods (II) Fall 2003 CSE, POSTECH.
Algorithmics - Lecture 121 LECTURE 11: Dynamic programming - II -
1 Ch20. Dynamic Programming. 2 BIRD’S-EYE VIEW Dynamic programming The most difficult one of the five design methods Has its foundation in the principle.
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.
Computer Sciences Department1.  Property 1: each node can have up to two successor nodes (children)  The predecessor node of a node is called its.
Chapter 7 Dynamic Programming 7.1 Introduction 7.2 The Longest Common Subsequence Problem 7.3 Matrix Chain Multiplication 7.4 The dynamic Programming Paradigm.
Example 2 You are traveling by a canoe down a river and there are n trading posts along the way. Before starting your journey, you are given for each 1
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.
1 Chapter 6 : Graph – Part 2 교수 : 이상환 강의실 : 113,118 호, 324 호 연구실 : 과학관 204 호 Home :
Dynamic Programming Typically applied to optimization problems
Advanced Algorithms Analysis and Design
Dynamic Programming Sequence of decisions. Problem state.
Advanced Algorithms Analysis and Design
Seminar on Dynamic Programming.
Advanced Design and Analysis Techniques
Matrix Chain Multiplication
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Matrix Multiplication Chains
CSCE 411 Design and Analysis of Algorithms
Dynamic Programming Steps.
Dynamic Programming Characterize the structure (problem state) of optimal solution Recursively define the value of optimal solution Compute the value of.
Unit-5 Dynamic Programming
Matrix Chain Multiplication
ICS 353: Design and Analysis of Algorithms
ICS 353: Design and Analysis of Algorithms
Dynamic Programming Characterize the structure (problem state) of optimal solution Recursively define the value of optimal solution Compute the value of.
Dynamic Programming Dynamic Programming 1/18/ :45 AM
Data Structure and Algorithms
Chapter 15: Dynamic Programming II
Lecture 8. Paradigm #6 Dynamic Programming
Algorithms CSCI 235, Spring 2019 Lecture 28 Dynamic Programming III
Algorithm Design Techniques Greedy Approach vs Dynamic Programming
//****************************************
Algorithms and Data Structures Lecture X
Dynamic Programming Sequence of decisions. Problem state.
Dynamic Programming Steps.
Matrix Chain Multiplication
Dynamic Programming Steps.
Algorithms CSCI 235, Spring 2019 Lecture 27 Dynamic Programming II
Chapter 15: Dynamic Programming
Chapter 15: Dynamic Programming
Dynamic Programming Sequence of decisions. Problem state.
All-Pairs Shortest Paths
Seminar on Dynamic Programming.
Data Structures and Algorithms Dynamic Programming
Presentation transcript:

Matrix Multiplication Chains Determine the best way to compute the matrix product M1x M2 x M3 x … x Mq. Let the dimensions of Mi be rix ri+1. q-1 matrix multiplications are to be done. Decide the matrices involved in each of these multiplications.

Decision Sequence M1x M2 x M3 x … x Mq Determine the q-1 matrix products in reverse order. What is the last multiplication? What is the next to last multiplication? And so on.

Problem State M1x M2 x M3 x … x Mq The matrices involved in each multiplication are a contiguous subset of the given q matrices. The problem state is given by a set of pairs of the form (i, j), i <= j. The pair (i,j) denotes a problem in which the matrix product Mix Mi+1 x … x Mj is to be computed. The initial state is (1,q). If the last matrix product is (M1x M2 x … x Mk) x (Mk+1x Mk+2 x … x Mq), the state becomes {(1,k), (k+1,q)}.

Verify Principle Of Optimality Let Mij= Mi x Mi+1 x … x Mj, i <= j. Matrix multiplication is associative. (i.e. ((AB)C)D = ((A(BC))D) = (AB)(CD) = A((BC)D) = A(B(CD)). Suppose that the last multiplication in the best way to compute Mijis Mikx Mk+1,j for some k, i <= k < j. Irrespective of what k is, a best computation of Mijin which the last product is Mikx Mk+1,j has the property that Mikand Mk+1,j are computed in the best possible way. So the principle of optimality holds and dynamic programming may be applied.

Overlapping subproblems Source: http://www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/

Recurrence Equations Let c(i,j) be the cost of an optimal (best) way to compute Mij, i <= j. c(1,q) is the cost of the best way to multiply the given q matrices. Let kay(i,j) = k be such that the last product in the optimal computation of Mij is Mikx Mk+1,j. c(i,i) = 0, 1 <= i <= q. (Mii= Mi) c(i,i+1) = riri+1ri+2, 1 <= i < q. (Mi,i+1= Mix Mi+1) kay(i,i+1) = i.

c(i,i+s) = c(i,k) + c(k+1,i+s) + rirk+1ri+s+1 c(i, i+s), 1 < s < q The last multiplication in the best way to compute Mi,i+s is Mikx Mk+1,i+s for some k, i <= k < i+s. If we knew k, we could claim: c(i,i+s) = c(i,k) + c(k+1,i+s) + rirk+1ri+s+1 Since i <= k < i+s, we can claim c(i,i+s) = min{c(i,k) + c(k+1,i+s) + rirk+1ri+s+1}, where the min is taken over i <= k < i+s. kay(i,i+s) is the k that yields above min.

Recurrence Equations c(i,i+s) = min i <= k < i+s{c(i,k) + c(k+1,i+s) + rirk+1ri+s+1} c(*,*) terms on right side involve fewer matrices than does the c(*,*) term on the left side. So compute in the order s = 2, 3, …, q-1.

Recursive Implementation See text for recursive codes. Code that does not avoid recomputation of already computed c(i,j)s runs in Omega(2q) time. Code that does not recompute already computed c(i,j)s runs in O(q3) time. Implement nonrecursively for best worst-case efficiency.

// Matrix Ai has dimension p[i-1] x p[i] for i = 1..n int MatrixChainOrder(int p[], int i, int j) { if(i == j) return 0; int k; int min = INT_MAX; int count; for (k = i; k <j; k++) count = MatrixChainOrder(p, i, k) + MatrixChainOrder(p, k+1, j) + p[i-1]*p[k]*p[j]; if (count < min) min = count; } return min; } //Thanks geeksforgeeks! Source: http://www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/

Reminder: Memoize Source: http://www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/

Example q = 4, (10 x 1) * (1 x 10) * (10 x 1) * (1 x 10) r = [r1,r2 ,r3,r4,r5] = [10, 1, 10, 1, 10] c(i,j), i <= j kay(i,j), i <= j 1 2 3 4

s = 0 c(i,i) and kay(i,i) , 1 <= i <= 4 are to be computed. c(i,j), i <= j kay(i,j), i <= j

s = 1 c(i,i+1) and kay(i,i+1) , 1 <= i <= 3 are to be computed. c(i,j), i <= j kay(i,j), i <= j

s = 1 c(i,i+1) = riri+1ri+2, 1 <= i < q. (Mii+1= Mix Mi+1) kay(i,i+1) = i. r = [r1,r2 ,r3,r4,r5] = [10, 1, 10, 1, 10] c(i,j), i <= j kay(i,j), i <= j 1 100 2 10 3 100

s = 2 c(i,i+2) = min{c(i,i) + c(i+1,i+2) + riri+1ri+3, r = [r1,r2 ,r3,r4,r5] = [10, 1, 10, 1, 10] c(i,j), i <= j kay(i,j), i <= j 1 100 2 10 3 100

s = 2 c(1,3) = min{c(1,1) + c(2,3) + r1r2r4, c(1,2) + c(3,3) + r1r3r4} r = [r1,r2 ,r3,r4,r5] = [10, 1, 10, 1, 10] c(1,3) = min{0 + 10 + 10, 100 + 0 + 100} c(i,j), i <= j kay(i,j), i <= j 1 1 100 20 2 10 3 100

s = 2 c(2,4) = min{c(2,2) + c(3,4) + r2r3r5, c(2,3) + c(4,4) + r2r4r5} r = [r1,r2 ,r3,r4,r5] = [10, 1, 10, 1, 10] c(2,4) = min{0 + 100 + 100, 10 + 0 + 10} c(i,j), i <= j kay(i,j), i <= j 1 1 100 20 2 3 10 20 3 100

s = 3 c(1,4) = min{c(1,1) + c(2,4) + r1r2r5, c(1,2) + c(3,4) + r1r3r5, c(1,3) + c(4,4) + r1r4r5} r = [r1,r2 ,r3,r4,r5] = [10, 1, 10, 1, 10] c(1,4) = min{0+20+100, 100+100+1000, 20+0+100} c(i,j), i <= j kay(i,j), i <= j 1 1 1 100 20 120 2 3 10 20 3 100

Determine The Best Way To Compute M14 kay(1,4) = 1. So the last multiplication is M14 = M11 x M24. M11 involves a single matrix and no multiply. Find best way to compute M24. c(i,j), i <= j kay(i,j), i <= j 1 1 1 100 20 120 2 3 10 20 3 100

Determine The Best Way To Compute M24 kay(2,4) = 3 . So the last multiplication is M24 = M23 x M44. M23 = M22 x M33. M44 involves a single matrix and no multiply. c(i,j), i <= j kay(i,j), i <= j 1 1 1 100 20 120 2 3 10 20 3 100

The Best Way To Compute M14 The multiplications (in reverse order) are: M14 = M11 x M24 M24 = M23 x M44 M23 = M22 x M33

Time Complexity c(i,j), i <= j 1 2 3 4 100 10 20 120 O(q2) c(i,j) values are to be computed, where q is the number of matrices. c(i,i+s) = min i <= k < i+s{c(i,k) + c(k+1,i+s) + rirk+1ri+s+1}. Each c(i,j) is the min of O(q) terms. Each of these terms is computed in O(1) time. So all c(i,j) are computed in O(q3) time.

Time Complexity kay(i,j), i <= j 1 2 3 4 kay(i,j), i <= j The traceback takes O(1) time to determine each matrix product that is to be done. q-1 products are to be done. Traceback time is O(q).