Chain Matrix Multiplication

Similar documents
Lecture 13: Chain Matrix Multiplication

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

Dynamic Programming II

Lecture 8. Dynamic Programming

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

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

/463 Algorithms - Fall 2013 Solution to Assignment 3

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

Unit-5 Dynamic Programming 2016

Dynamic Programming (Part #2)

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

Chapter 3 Dynamic programming

Lecture 4: Dynamic programming I

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

Framework for Design of Dynamic Programming Algorithms

Algorithms: Dynamic Programming

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

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

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

Dynamic Programming Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

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

Introduction to Algorithms

Partha Sarathi Manal

Dynamic Programming Group Exercises

We ve done. Now. Next

5.1 The String reconstruction problem

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

Algorithm Design Techniques part I

ECE250: Algorithms and Data Structures Dynamic Programming Part B

Dynamic Programming part 2

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

Lectures 12 and 13 Dynamic programming: weighted interval scheduling

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

CMSC351 - Fall 2014, Homework #4

CS 231: Algorithmic Problem Solving

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

Data Structures and Algorithms Week 8

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

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

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

Homework3: Dynamic Programming - Answers

Optimization II: Dynamic Programming

ECE608 - Chapter 15 answers

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

Parallel Computing: Parallel Algorithm Design Examples Jin, Hai

Algorithms: COMP3121/3821/9101/9801

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

Dynamic Programming. Outline and Reading. Computing Fibonacci

Deterministic and Randomized Quicksort. Andreas Klappenecker

Unit 4: Dynamic Programming

15.Dynamic Programming

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

Data Structures and Algorithms. Dynamic Programming

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

Algorithms for Data Science

Computer Sciences Department 1

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

Dynamic Programming Matrix-chain Multiplication

CS141: Intermediate Data Structures and Algorithms Dynamic Programming

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

1. (a) O(log n) algorithm for finding the logical AND of n bits with n processors

UML CS Algorithms Qualifying Exam Fall, 2003 ALGORITHMS QUALIFYING EXAM

Attendance (2) Performance (3) Oral (5) Total (10) Dated Sign of Subject Teacher

DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2017)

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

Dynamic Programming 1

Advanced Algorithms Class Notes for Monday, November 10, 2014

CS 140 : Numerical Examples on Shared Memory with Cilk++

Dynamic programming II - The Recursion Strikes Back

CSE 101- Winter 18 Discussion Section Week 6

CSE 4/531 Solution 3

DIVIDE & CONQUER. Problem of size n. Solution to sub problem 1

1 Closest Pair Problem

Lecture 4 CS781 February 3, 2011

CS473 - Algorithms I

Problem Set 6. Part A:

Design and Analysis of Algorithms

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

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

CSC 373: Algorithm Design and Analysis Lecture 8

AH Matrices.notebook November 28, 2016

EE/CSCI 451 Midterm 1

Parallelizing The Matrix Multiplication. 6/10/2013 LONI Parallel Programming Workshop

Lecture 6: Parallel Matrix Algorithms (part 3)

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

Lab #10 Multi-dimensional Arrays

CMPS 2200 Fall Dynamic Programming. Carola Wenk. Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk

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

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

IN101: Algorithmic techniques Vladimir-Alexandru Paun ENSTA ParisTech

Analysis of Algorithms, I

Greedy Algorithms. Algorithms

Chapter 1: Number and Operations

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

Longest Common Subsequences and Substrings

Dynamic Programming Algorithms

The Further Mathematics Support Programme

Algorithms Dr. Haim Levkowitz

Week 11: Minimum Spanning trees

Introduction to Algorithms

Transcription:

Chain Matrix Multiplication Version of November 5, 2014 Version of November 5, 2014 Chain Matrix Multiplication 1 / 27

Outline Outline Review of matrix multiplication. The chain matrix multiplication problem. A dynamic programming algorithm for chain matrix multiplication. Version of November 5, 2014 Chain Matrix Multiplication 2 / 27

Review of Matrix Multiplication Matrix: An n m matrix A = [a[i, j]] is a two-dimensional array a[1, 1] a[1, 2] a[1, m 1] a[1, m] a[2, 1] a[2, 2] a[2, m 1] a[2, m] A =...., a[n, 1] a[n, 2] a[n, m 1] a[n, m] which has n rows and m columns. Example A 4 5 matrix: 12 8 9 7 6 7 6 89 56 2 5 5 6 9 10 8 6 0 8 1. Version of November 5, 2014 Chain Matrix Multiplication 3 / 27

Review of Matrix Multiplication The product C = AB of a p q matrix A and a q r matrix B is a p r matrix C given by q c[i, j] = a[i, k]b[k, j], k=1 for 1 i p and 1 j r Complexity of Matrix multiplication: Note that C has pr entries and each entry takes Θ(q) time to compute so the total procedure takes Θ(pqr) time. Example A = 1 8 9 7 6 1 5 5 6, B = 1 8 7 6 5 5, C = AB = 102 101 44 87 70 100. Version of November 5, 2014 Chain Matrix Multiplication 4 / 27

Remarks on Matrix Multiplication Matrix multiplication is associative, e.g., A 1 A 2 A 3 = (A 1 A 2 )A 3 = A 1 (A 2 A 3 ), so parenthesization does not change result. Matrix multiplication is NOT commutative, e.g., A 1 A 2 A 2 A 1 Version of November 5, 2014 Chain Matrix Multiplication 5 / 27

Matrix Multiplication of ABC Example Given p q matrix A, q r matrix B and r s matrix C, ABC can be computed in two ways: (AB)C and A(BC) The number of multiplications needed are: For p = 5, q = 4, r = 6 and s = 2, A big difference! mult[(ab)c] = pqr + prs, mult[a(bc)] = qrs + pqs. mult[(ab)c] = 180, mult[a(bc)] = 88. Implication: Multiplication sequence (parenthesization) is important!! Version of November 5, 2014 Chain Matrix Multiplication 6 / 27

Outline Review of matrix multiplication. The chain matrix multiplication problem. A dynamic programming algorithm for chain matrix multiplication. Version of November 5, 2014 Chain Matrix Multiplication 7 / 27

The Chain Matrix Multiplication Problem Definition (Chain matrix multiplication problem) Given dimensions p 0, p 1,..., p n, corresponding to matrix sequence A 1, A 2,..., A n in which A i has dimension p i 1 p i, determine the multiplication sequence that minimizes the number of scalar multiplications in computing A 1 A 2 A n. Example i.e.,, determine how to parenthesize the multiplications. A 1 A 2 A 3 A 4 = (A 1 A 2 )(A 3 A 4 ) = A 1 (A 2 (A 3 A 4 )) = A 1 ((A 2 A 3 )A 4 ) = ((A 1 A 2 )A 3 )(A 4 ) = (A 1 (A 2 A 3 ))(A 4 ) Exhaustive search: Ω(4 n /n 3/2 ). Question Is there a better approach? Yes DP Version of November 5, 2014 Chain Matrix Multiplication 8 / 27

Outline Review of matrix multiplication. The chain matrix multiplication problem. A dynamic programming algorithm. Version of November 5, 2014 Chain Matrix Multiplication 9 / 27

Developing a Dynamic Programming Algorithm Step 1: Define Space of Subproblems Original Problem: Determine minimal cost multiplication sequence for A 1..n. Subproblems: For every pair 1 i j n: Determine minimal cost multiplication sequence for A i..j = A i A i+1 A j. Note that A i..j is a p i 1 p j matrix. There are ( n 2) = Θ(n 2 ) such subproblems. (Why?) How can we solve larger problems using subproblem solutions? Version of November 5, 2014 Chain Matrix Multiplication 10 / 27

Relationships among subproblems At the last step of any optimal multiplication sequence (for a subbroblem), there is some k such that the two matrices A i..k and A k+1..j are multipled together. That is, A i..j = (A i A k ) (A k+1 A j ) = A i..k A k+1..j. Question How do we decide where to split the chain (what is k)? ANS: Can be any k. Need to check all possible values. Question How do we parenthesize the two subchains A i..k and A k+1..j? ANS: A i..k and A k+1..j must be computed optimally, so we can apply the same procedure recursively. Version of November 5, 2014 Chain Matrix Multiplication 11 / 27

Optimal Structure Property If the optimal solution of A i..j involves splitting into A i..k and A k+1..j at the final step, then parenthesization of A i..k and A k+1..j in the optimal solution must also be optimal If parenthesization of A i..k was not optimal, it could be replaced by a cheaper parenthesization, yielding a cheaper final solution, constradicting optimality Similarly, if parenthesization of A k+1..j was not optimal, it could be replaced by a cheaper parenthesization, again yielding contradiction of cheaper final solution. Version of November 5, 2014 Chain Matrix Multiplication 12 / 27

Relationships among subproblems Step 2: Constructing optimal solutions from optimal subproblem solution For 1 i j n, let m[i, j] denote the minimum number of multiplications needed to compute A i..j. This optimum cost must satisify the following recursive definition. { 0 i = j, m[i, j] = min i k<j (m[i, k] + m[k + 1, j] + p i 1 p k p j ) i < j A i..j = A i..k A k+1..j Version of November 5, 2014 Chain Matrix Multiplication 13 / 27

Proof of Recurrence Proof. If j = i, then m[i, j] = 0 because, no mutiplications are required. If i < j, note that, for every k, calculating A i..k and A k+1..j optimally and then finishing by multiplying A i..k A k+1..j to get A i..j uses (m[i, k] + m[k + 1, j] + p i 1 p k p j multiplications. The optimal way of calculating A i..j uses no more than the worst of these j i ways so m[i, j] min i k<j (m[i, k] + m[k + 1, j] + p i 1p k p j ). A i..j = A i..k A k+1..j Version of November 5, 2014 Chain Matrix Multiplication 14 / 27

Proof of Recurrence (II) Proof. For the other direction, note that an optimal sequence of multiplications for A i..j is equivalent to splitting A i..j = A i..k A k+1..j for some k, where the sequences of multiplications to calculate A i..k and A k+1..j are also optimal. Hence, for that special k, m[i, j] = m[i, k] + m[k + 1, j] + p i 1 p k p j. Combining with the previous page, we have just proven { 0 i = j, m[i, j] = min i k<j (m[i, k] + m[k + 1, j] + p i 1 p k p j ) i < j. Version of November 5, 2014 Chain Matrix Multiplication 15 / 27

Developing a Dynamic Programming Algorithm Recurrence: Step 3: Bottom-up computation of m[i, j]. m[i, j] = min i k<j (m[i, k] + m[k + 1, j] + p i 1p k p j ) Fill in the m[i, j] table in an order, such that when it is time to calculate m[i, j], the values of m[i, k] and m[k + 1, j] for all k are already available. An easy way to ensure this is to compute them in increasing order of the size (j i) of the matrix-chain A i..j : m[1, 2], m[2, 3], m[3, 4],..., m[n 3, n 2], m[n 2, n 1], m[n 1, n] m[1, 3], m[2, 4], m[3, 5],..., m[n 3, n 1], m[n 2, n] m[1, 4], m[2, 5], m[3, 6],..., m[n 3, n]... m[1, n 1], m[2, n] m[1, n] Version of November 5, 2014 Chain Matrix Multiplication 16 / 27

Example for the Bottom-Up Computation Example A chain of four matrices A 1, A 2, A 3 and A 4, with p 0 = 5, p 1 = 4, p 2 = 6, p 3 = 2 and p 4 = 7. Find m[1, 4]. S0: Initialization Version of November 5, 2014 Chain Matrix Multiplication 17 / 27

Example Continued Step 1: Computing m[1, 2] By definition m[1, 2] = min 1 k<2 (m[1, k] + m[k + 1, 2] + p 0p k p 2 ) = m[1, 1] + m[2, 2] + p 0 p 1 p 2 = 120. Version of November 5, 2014 Chain Matrix Multiplication 18 / 27

Example Continued Step 2: Computing m[2, 3] By definition m[2, 3] = min 2 k<3 (m[2, k] + pm[k + 1, 3] + p 1p k p 3 ) = m[2, 2] + m[3, 3] + p 1 p 2 p 3 = 48. Version of November 5, 2014 Chain Matrix Multiplication 19 / 27

Example Continued Step 3: Computing m[3, 4] By definition m[3, 4] = min 3 k<4 (m[3, k] + m[k + 1, 4] + p 2p k p 4 ) = m[3, 3] + m[4, 4] + p 2 p 3 p 4 = 84. Version of November 5, 2014 Chain Matrix Multiplication 20 / 27

Example Continued Step 4: Computing m[1, 3] By definition m[1, 3] = min (m[1, k] + m[k + 1, 3] + p 0p k p 3 ) 1 k<3 { } m[1, 1] + m[2, 3] + p0 p = min 1 p 3 m[1, 2] + m[3, 3] + p 0 p 2 p 3 = 88. Version of November 5, 2014 Chain Matrix Multiplication 21 / 27

Example Continued Step 5: Computing m[2, 4] By definition m[2, 4] = min (m[2, k] + m[k + 1, 4] + p 1p k p 4 ) 2 k<4 { } m[2, 2] + m[3, 4] + p1 p = min 2 p 4 m[2, 3] + m[4, 4] + p 1 p 3 p 4 = 104. Version of November 5, 2014 Chain Matrix Multiplication 22 / 27

Example Continued Step 6: Computing m[1, 4] By definition m[1, 4] = min (m[1, k] + m[k + 1, 4] + p 0p k p 4 ) 1 k<4 m[1, 1] + m[2, 4] + p 0 p 1 p 4 = min m[1, 2] + m[3, 4] + p 0 p 2 p 4 m[1, 3] + m[4, 4] + p 0 p 3 p 4 = 158. Version of November 5, 2014 Chain Matrix Multiplication 23 / 27

Constructing a Solution m[i, j] only keeps costs but not actual multiplication sequence. To solve problem, need to reconstruct multiplication sequence that yields m[1, n]. Solution: similar to previous DP algorithm(s) keep an auxillary array s[, ]. s[i, j] = k where k is the index that achieves minimum in m[i, j] = min i k<j (m[i, k] + m[k + 1, j] + p i 1p k p j ). Version of November 5, 2014 Chain Matrix Multiplication 24 / 27

Developing a Dynamic Programming Algorithm Step 4: Constructing optimal solution Idea: Maintain an array s[1..n, 1..n], where s[i, j] denotes k for the optimal splitting in computing A i..j = A i..k A k+1..j. Question How to Recover the Multiplication Sequence using s[i, j]? s[1, n] (A 1 A s[1,n] ) (A s[1,n]+1 A n ) s[1, s[1, n]] (A 1 A s[1,s[1,n]] ) (A s[1,s[1,n]]+1 A s[1,n] ) s[s[1, n] + 1, n] (A s[1,n]+1 A s[s[1,n]+1,n] ) (A s[s[1,n]+1,n]+1 A n ).. Apply recursively until multiplication sequence is completely determined. Version of November 5, 2014 Chain Matrix Multiplication 25 / 27

Step 4... Example (Finding the Multiplication Sequence) Consider n = 6. Assume array s[1..6, 1..6] has been properly constructed. The multiplication sequence is recovered as follows. s[1, 6] = 3 (A 1 A 2 A 3 )(A 4 A 5 A 6 ) s[1, 3] = 1 (A 1 (A 2 A 3 )) s[4, 6] = 5 ((A 4 A 5 )A 6 ) Hence the final multiplication sequence is (A 1 (A 2 A 3 ))((A 4 A 5 )A 6 ). Version of November 5, 2014 Chain Matrix Multiplication 26 / 27

The Dynamic Programming Algorithm Matrix-Chain(p, n): // l is length of sub-chain for i = 1 to n do m[i, i] = 0; ; for l = 2 to n do for i = 1 to n l + 1 do j = i + l 1; m[i, j] = ; for k = i to j 1 do q = m[i, k] + m[k + 1, j] + p[i 1] p[k] p[j]; if q < m[i, j] then m[i, j] = q; s[i, j] = k; end end end end return m and s; (Optimum in m[1, n]) Complexity: The loops are nested three levels deep. Each loop index takes on n values. Hence the time complexity is O(n 3 ). Space complexity is Θ(n 2 ). Version of November 5, 2014 Chain Matrix Multiplication 27 / 27