Lecture 13: Chain Matrix Multiplication

Similar documents
Chain Matrix Multiplication

12 Dynamic Programming (2) Matrix-chain Multiplication Segmented Least Squares

Lecture 8. Dynamic Programming

Dynamic Programming II

14 Dynamic. Matrix-chain multiplication. P.D. Dr. Alexander Souza. Winter term 11/12

CS473-Algorithms I. Lecture 10. Dynamic Programming. Cevdet Aykanat - Bilkent University Computer Engineering Department

Unit-5 Dynamic Programming 2016

/463 Algorithms - Fall 2013 Solution to Assignment 3

Dynamic Programming. Design and Analysis of Algorithms. Entwurf und Analyse von Algorithmen. Irene Parada. Design and Analysis of Algorithms

Dynamic Programming (Part #2)

Dynamic Programming. December 15, CMPE 250 Dynamic Programming December 15, / 60

Efficient Sequential Algorithms, Comp309. Problems. Part 1: Algorithmic Paradigms

Lecture 4: Dynamic programming I

Lecture 57 Dynamic Programming. (Refer Slide Time: 00:31)

Chapter 3 Dynamic programming

Framework for Design of Dynamic Programming Algorithms

ECE250: Algorithms and Data Structures Dynamic Programming Part B

We ve done. Now. Next

Elements of Dynamic Programming. COSC 3101A - Design and Analysis of Algorithms 8. Discovering Optimal Substructure. Optimal Substructure - Examples

CS 231: Algorithmic Problem Solving

Partha Sarathi Manal

Algorithms: Dynamic Programming

Write an algorithm to find the maximum value that can be obtained by an appropriate placement of parentheses in the expression

Outline. CS38 Introduction to Algorithms. Fast Fourier Transform (FFT) Fast Fourier Transform (FFT) Fast Fourier Transform (FFT)

Algorithm Design Techniques part I

Homework3: Dynamic Programming - Answers

Lectures 12 and 13 Dynamic programming: weighted interval scheduling

Lecture 5: Matrices. Dheeraj Kumar Singh 07CS1004 Teacher: Prof. Niloy Ganguly Department of Computer Science and Engineering IIT Kharagpur

Dynamic Programming Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Introduction to Algorithms

Data Structures and Algorithms Week 8

y j LCS-Length(X,Y) Running time: O(st) set c[i,0] s and c[0,j] s to 0 for i=1 to s for j=1 to t if x i =y j then else if

Module 27: Chained Matrix Multiplication and Bellman-Ford Shortest Path Algorithm

So far... Finished looking at lower bounds and linear sorts.

Dynamic Programming Group Exercises

Greedy Algorithms. Algorithms

Dynamic Programming part 2

Algorithms for Data Science

15.Dynamic Programming

We augment RBTs to support operations on dynamic sets of intervals A closed interval is an ordered pair of real

Dynamic Programming. An Enumeration Approach. Matrix Chain-Products. Matrix Chain-Products (not in book)

Dynamic Programming Matrix-chain Multiplication

CMSC351 - Fall 2014, Homework #4

5.1 The String reconstruction problem

CSE 373 Analysis of Algorithms, Fall Homework #3 Solutions Due Monday, October 18, 2003

Computer Sciences Department 1

CSE 101- Winter 18 Discussion Section Week 6

Memoization/Dynamic Programming. The String reconstruction problem. CS124 Lecture 11 Spring 2018

1 Non greedy algorithms (which we should have covered

ECE608 - Chapter 15 answers

Data Structure and Algorithm II Homework #2 Due: 13pm, Monday, October 31, === Homework submission instructions ===

Artificial Intelligence

Chapter 18 out of 37 from Discrete Mathematics for Neophytes: Number Theory, Probability, Algorithms, and Other Stuff by J. M. Cargal.

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Dynamic Programming

F(0)=0 F(1)=1 F(n)=F(n-1)+F(n-2)

Design and Analysis of Algorithms 演算法設計與分析. Lecture 7 April 15, 2015 洪國寶

CSE 101, Winter Design and Analysis of Algorithms. Lecture 11: Dynamic Programming, Part 2

DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2017)

Optimization II: Dynamic Programming

Dynamic Programming. Nothing to do with dynamic and nothing to do with programming.

Algorithms Dr. Haim Levkowitz

Design and Analysis of Algorithms

Dynamic Programming. Outline and Reading. Computing Fibonacci

Algorithms: COMP3121/3821/9101/9801

Unit 4: Dynamic Programming

CS141: Intermediate Data Structures and Algorithms Dynamic Programming

Parallel Computing: Parallel Algorithm Design Examples Jin, Hai

CSED233: Data Structures (2017F) Lecture12: Strings and Dynamic Programming

Greedy Algorithms. CLRS Chapters Introduction to greedy algorithms. Design of data-compression (Huffman) codes

Design and Analysis of Algorithms 演算法設計與分析. Lecture 7 April 6, 2016 洪國寶

(Feodor F. Dragan) Department of Computer Science Kent State University. Advanced Algorithms, Feodor F. Dragan, Kent State University 1

D-BAUG Informatik I. Exercise session: week 5 HS 2018

Lecturers: Sanjam Garg and Prasad Raghavendra March 20, Midterm 2 Solutions

Advanced Algorithms. Problem solving Techniques. Divide and Conquer הפרד ומשול

Dynamic Programming. Lecture Overview Introduction

Divide and Conquer Algorithms

Lecture 13: The Knapsack Problem

Writing Parallel Programs; Cost Model.

Dynamic programming II - The Recursion Strikes Back

Greedy algorithms is another useful way for solving optimization problems.

Parallel Algorithms CSE /22/2015. Outline of this lecture: 1 Implementation of cilk for. 2. Parallel Matrix Multiplication

Problem Set 6. Part A:

Data Structures and Algorithms. Dynamic Programming

UML CS Algorithms Qualifying Exam Fall, 2003 ALGORITHMS QUALIFYING EXAM

Dynamic Programming 1

Tutorial 6-7. Dynamic Programming and Greedy

memoization or iteration over subproblems the direct iterative algorithm a basic outline of dynamic programming

Analysis of Algorithms, I

CMPS 102 Solutions to Homework 6

CSC 373: Algorithm Design and Analysis Lecture 8

Lecture 4 CS781 February 3, 2011

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING QUESTION BANK UNIT-III. SUB NAME: DESIGN AND ANALYSIS OF ALGORITHMS SEM/YEAR: III/ II PART A (2 Marks)

15-451/651: Design & Analysis of Algorithms January 26, 2015 Dynamic Programming I last changed: January 28, 2015

CSC 505, Spring 2005 Week 6 Lectures page 1 of 9

CS473 - Algorithms I

Dynamic Programming: 1D Optimization. Dynamic Programming: 2D Optimization. Fibonacci Sequence. Crazy 8 s. Edit Distance

Dynamic Programming Assembly-Line Scheduling. Greedy Algorithms

Least Squares; Sequence Alignment

The resulting array is written as row i + 1 in our matrix:

Graph Contraction. Graph Contraction CSE341T/CSE549T 10/20/2014. Lecture 14

Transcription:

Lecture 3: Chain Matrix Multiplication CLRS Section 5.2 Revised April 7, 2003 Outline of this Lecture Recalling matrix multiplication. The chain matrix multiplication problem. A dynamic programming algorithm for chain matrix multiplication.

Recalling Matrix Multiplication Matrix: An n m matrix A = [a[i, j]] is a twodimensional array A = a[, ] a[, 2] a[, m ] a[, m] a[2, ]. a[2, 2]. a[2, m ]. a[2, m]. a[n, ] a[n, 2] a[n, m ] a[n, m] which has n rows and m columns., Example: The following is a 4 5 matrix: 2 8 9 7 6 7 6 89 56 2 5 5 6 9 0 8 6 0 8. 2

Recalling Matrix Multiplication The product C = AB of a p q matrix A and a q r matrix B is a p r matrix given by c[i, j] = q k= for i p and j r. a[i, k]b[k, j] Example: If then A = 8 9 7 6 5 5 6 C = AB =, B = 02 0 44 87 70 00 8 7 6 5 5., 3

Remarks on Matrix Multiplication If AB is defined, BA may not be defined. Quite possible that AB BA. Multiplication is recursively defined by A A 2 A 3 A s A s = A (A 2 (A 3 (A s A s ))). Matrix multiplication is associative, e.g., A A 2 A 3 = (A A 2 )A 3 = A (A 2 A 3 ), so parenthenization does not change result. 4

Direct Matrix multiplication AB Given a p q matrix A and a q r matrix B, the direct way of multiplying C = AB is to compute each c[i, j] = q k= for i p and j r. a[i, k]b[k, j] Complexity of Direct Matrix multiplication: Note that C has pr entries and each entry takes Θ(q) time to compute so the total procedure takes Θ(pqr) time. 5

Direct Matrix multiplication of ABC Given a p q matrix A, a q r matrix B and a r s matrix C, then ABC can be computed in two ways (AB)C and A(BC): The number of multiplications needed are: mult[(ab)c] = pqr + prs, mult[a(bc)] = qrs + pqs. When p = 5, q = 4, r = 6 and s = 2, then mult[(ab)c] = 80, mult[a(bc)] = 88. A big difference! Implication: The multiplication sequence (parenthesization) is important!! 6

The Chain Matrix Multiplication Problem Given dimensions p 0, p,..., p n corresponding to matrix sequence A, A 2,..., A n where A i has dimension p i p i, determine the multiplication sequence that minimizes the number of scalar multiplications in computing A A 2 A n. That is, determine how to parenthisize the multiplications. A A 2 A 3 A 4 = (A A 2 )(A 3 A 4 ) = A (A 2 (A 3 A 4 )) = A ((A 2 A 3 )A 4 ) = ((A A 2 )A 3 )(A 4 ) = (A (A 2 A 3 ))(A 4 ) Exhaustive search: Ω(4 n /n 3/2 ). Question: Any better approach? Yes DP 7

Developing a Dynamic Programming Algorithm Step : Determine the structure of an optimal solution (in this case, a parenthesization). Decompose the problem into subproblems: For each pair i j n, determine the multiplication sequence for A i..j = A i A i+ A j that minimizes the number of multiplications. Clearly, A i..j is a p i p j matrix. Original Problem: determine sequence of multiplication for A..n. 8

Developing a Dynamic Programming Algorithm Step : Determine the structure of an optimal solution (in this case, a parenthesization). High-Level Parenthesization for A i..j For any optimal multiplication sequence, at the last step you are multiplying two matrices A i..k and A k+..j for some k. That is, A i..j = (A i A k )(A k+ A j ) = A i..k A k+..j. Example A 3..6 = (A 3 (A 4 A 5 ))(A 6 ) = A 3..5 A 6..6. Here k = 5. 9

Developing a Dynamic Programming Algorithm Step Continued: Thus the problem of determining the optimal sequence of multiplications is broken down into 2 questions: How do we decide where to split the chain (what is k)? (Search all possible values of k) How do we parenthesize the subchains A i..k and A k+..j? (Problem has optimal substructure property that A i..k and A k+..j must be optimal so we can apply the same procedure recursively) 0

Developing a Dynamic Programming Algorithm Step Continued: Optimal Substructure Property: If final optimal solution of A i..j involves splitting into A i..k and A k+..j at final step then parenthesization of A i..k and A k+..j in final optimal solution must also be optimal for the subproblems standing alone : If parenthisization of A i..k was not optimal we could replace it by a better parenthesization and get a cheaper final solution, leading to a contradiction. Similarly, if parenthisization of A k+..j was not optimal we could replace it by a better parenthesization and get a cheaper final solution, also leading to a contradiction.

Developing a Dynamic Programming Algorithm Step 2: Recursively define the value of an optimal solution. As with the 0- knapsack problem, we will store the solutions to the subproblems in an array. For i j n, let m[i, j] denote the minimum number of multiplications needed to compute A i..j. The optimum cost can be described by the following recursive definition. 2

Developing a Dynamic Programming Algorithm Step 2: Recursively define the value of an optimal solution. m[i, j] = { 0 i = j, min i k<j (m[i, k] + m[k +, j] + p i p k p j ) i < j Proof: Any optimal sequence of multiplication for A i..j is equivalent to some choice of splitting A i..j = A i..k A k+..j for some k, where the sequences of multiplications for A i..k and A k+..j also are optimal. Hence m[i, j] = m[i, k] + m[k +, j] + p i p k p j. 3

Developing a Dynamic Programming Algorithm Step 2 Continued: We know that, for some k m[i, j] = m[i, k] + m[k +, j] + p i p k p j. We don t know what k is, though But, there are only j i possible values of k so we can check them all and find the one which returns a smallest cost. Therefore m[i, j] = { 0 i = j, min i k<j (m[i, k] + m[k +, j] + p i p k p j ) i < j 4

Developing a Dynamic Programming Algorithm Step 3: Compute the value of an optimal solution in a bottom-up fashion. Our Table: m[..n,..n]. m[i, j] only defined for i j. The important point is that when we use the equation m[i, j] = min i k<j (m[i, k] + m[k +, j] + p i p k p j ) to calculate m[i, j] we must have already evaluated m[i, k] and m[k +, j]. Note that k i < j i and j (k + ) < j i so, to ensure that m[i, j] is evaluated after m[i, k] and m[k +, j] we simply let l =, 2,..., n and calculate all the terms of the form m[i, i + l], i =,... n l before we calculate the terms of the form m[i, i + l + ], i =,... n l. That is, we calculate in the order m[, 2], m[2, 3], m[3, 4],..., m[n 3, n 2], m[n 2, n ], m[n, n] m[, 3], m[2, 4], m[3, 5],..., m[n 3, n ], m[n 2, n] m[, 4], m[2, 5], m[3, 6],..., m[n 3, n]. m[, n ], m[2, n] m[, n] 5

Dynamic Programming Design Warning!! When designing a dynamic programming algorithm there are two parts:. Finding an appropriate optimal substructure property and corresponding recurrence relation on table items. Example: m[i, j] = min i k<j ( m[i, k] + m[k +, j] + pi p k p j ) 2. Filling in the table properly. This requires finding an ordering of the table elements so that when a table item is calculated using the recurrence relation, all the table values needed by the recurrence relation have already been calculated. In our example this means that by the time m[i, j] is calculated all of the values m[i, k] and m[k +, j] were already calculated. 6

Example for the Bottom-Up Computation Example: Given a chain of four matrices A, A 2, A 3 and A 4, with p 0 = 5, p = 4, p 2 = 6, p 3 = 2 and p 4 = 7. Find m[, 4]. S0: Initialization 4 m[i,j] 3 2 j i 2 3 4 0 0 0 0 5 4 6 2 7 A A2 A3 A4 p0 p p2 p3 p4 7

Example Continued Stp : Computing m[, 2] By definition m[, 2] = min 0p k p 2 ) k<2 = m[, ] + m[2, 2] + p 0 p p 2 = 20. 4 m[i,j] 3 2 j i 2 3 20 4 0 0 0 0 5 4 6 2 7 A A2 A3 A4 p0 p p2 p3 p4 8

Example Continued Stp 2: Computing m[2, 3] By definition m[2, 3] = min p k p 3 ) 2 k<3 = m[2, 2] + m[3, 3] + p p 2 p 3 = 48. 4 m[i,j] 3 2 j i 2 3 20 48 4 0 0 0 0 5 4 6 2 7 A A2 A3 A4 p0 p p2 p3 p4 9

Example Continued Stp3: Computing m[3, 4] By definition m[3, 4] = min 2p k p 4 ) 3 k<4 = m[3, 3] + m[4, 4] + p 2 p 3 p 4 = 84. 4 m[i,j] 3 2 j i 2 3 20 48 84 4 0 0 0 0 5 4 6 2 7 A A2 A3 A4 p0 p p2 p3 p4 20

Example Continued Stp4: Computing m[, 3] By definition m[, 3] = min (m[, k] + m[k +, 3] + p 0p k p 3 ) k<3 { m[, ] + m[2, 3] + p0 p = min p 3 m[, 2] + m[3, 3] + p 0 p 2 p 3 = 88. } 4 m[i,j] 3 2 j 2 88 i 3 20 48 84 4 0 0 0 0 5 4 6 2 7 A A2 A3 A4 p0 p p2 p3 p4 2

Example Continued Stp5: Computing m[2, 4] By definition m[2, 4] = min (m[2, k] + m[k +, 4] + p p k p 4 ) 2 k<4 { m[2, 2] + m[3, 4] + p p = min 2 p 4 m[2, 3] + m[4, 4] + p p 3 p 4 = 04. } 4 m[i,j] 3 2 j 2 88 04 i 3 20 48 84 4 0 0 0 0 5 4 6 2 7 A A2 A3 A4 p0 p p2 p3 p4 22

Example Continued St6: Computing m[, 4] By definition m[, 4] = min (m[, k] + m[k +, 4] + p 0p k p 4 ) k<4 = min = 58. m[, ] + m[2, 4] + p 0 p p 4 m[, 2] + m[3, 4] + p 0 p 2 p 4 m[, 3] + m[4, 4] + p 0 p 3 p 4 4 m[i,j] 3 58 2 j 2 88 04 i 3 20 48 84 4 0 0 0 0 5 4 6 2 7 A A2 A3 A4 p0 p p2 p3 p4 We are done! 23

Developing a Dynamic Programming Algorithm Step 4: Construct an optimal solution from computed information extract the actual sequence. Idea: Maintain an array s[..n,..n], where s[i, j] denotes k for the optimal splitting in computing A i..j = A i..k A k+..j. The array s[..n,..n] can be used recursively to recover the multiplication sequence. How to Recover the Multiplication Sequence? s[, n] (A A s[,n] )(A s[,n]+ A n ) s[, s[, n]] (A A s[,s[,n]] )(A s[,s[,n]]+ A s[,n] ) s[s[, n] +, n] (A s[,n]+ A s[s[,n]+,n] ) (A s[s[,n]+,n]+ A n ).. Do this recursively until the multiplication sequence is determined. 24

Developing a Dynamic Programming Algorithm Step 4: Construct an optimal solution from computed information extract the actual sequence. Example of Finding the Multiplication Sequence: Consider n = 6. Assume that the array s[..6,..6] has been computed. The multiplication sequence is recovered as follows. s[, 6] = 3 (A A 2 A 3 )(A 4 A 5 A 6 ) s[, 3] = (A (A 2 A 3 )) s[4, 6] = 5 ((A 4 A 5 )A 6 ) Hence the final multiplication sequence is (A (A 2 A 3 ))((A 4 A 5 )A 6 ). 25

The Dynamic Programming Algorithm Matrix-Chain(p, n) { for (i = to n) m[i, i] = 0; for (l = 2 to n) { for (i = to n l + ) { j = i + l ; m[i, j] = ; for (k = i to j ) { q = m[i, k] + m[k +, j] + p[i ] p[k] p[j]; if (q < m[i, j]) { m[i, j] = q; s[i, j] = k; } } } } return m and s; (Optimum in m[, n]) } Complexity: The loops are nested three deep. Each loop index takes on n values. Hence the time complexity is O(n 3 ). Space complexity Θ(n 2 ). 26

Constructing an Optimal Solution: Compute A..n The actual multiplication code uses the s[i, j] value to determine how to split the current sequence. Assume that the matrices are stored in an array of matrices A[..n], and that s[i, j] is global to this recursive procedure. The procedure returns a matrix. Mult(A, s, i, j) { if (i < j) { X = Mult(A, s, i, s[i, j]); X is now A i A k, where k is s[i, j] Y = Mult(A, s, s[i, j] +, j); } Y is now A k+ A j return X Y ; multiply matrices X and Y } else return A[i]; To compute A A 2 A n, call Mult(A, s,, n). 27

Constructing an Optimal Solution: Compute A..n Example of Constructing an Optimal Solution: Compute A..6. Consider the example earlier, where n = 6. Assume that the array s[..6,..6] has been computed. The multiplication sequence is recovered as follows. Mult(A, s,, 6), s[, 6] = 3, (A A 2 A 3 )(A 4 A 5 A 6 ) Mult(A, s,, 3), s[, 3] =, ((A )(A 2 A 3 ))(A 4 A 5 A 6 ) Mult(A, s, 4, 6), s[4, 6] = 5, ((A )(A 2 A 3 ))((A 4 A 5 )(A 6 )) Mult(A, s, 2, 3), s[2, 3] = 2, ((A )((A 2 )(A 3 )))((A 4 A 5 )(A 6 )) Mult(A, s, 4, 5), s[4, 5] = 4, ((A )((A 2 )(A 3 )))(((A 4 )(A 5 ))(A 6 )) Hence the product is computed as follows (A (A 2 A 3 ))((A 4 A 5 )A 6 ). 28