Lecture 12: Chain Matrix Multiplication

Similar documents
Matrix-Chain Multiplication

Dynamic Programming. Lecture Overview Introduction

Systems of Linear Equations

1.2 Solving a System of Linear Equations

Lecture 13: The Knapsack Problem

DERIVATIVES AS MATRICES; CHAIN RULE

NOTES ON LINEAR TRANSFORMATIONS

Solving Systems of Linear Equations Using Matrices

Similarity and Diagonalization. Similar Matrices

MATH10212 Linear Algebra. Systems of Linear Equations. Definition. An n-dimensional vector is a row or a column of n numbers (or letters): a 1.

Solving Systems of Linear Equations

MATH 304 Linear Algebra Lecture 18: Rank and nullity of a matrix.

Dynamic Programming Problem Set Partial Solution CMPSC 465

160 CHAPTER 4. VECTOR SPACES

Here are some examples of combining elements and the operations used:

a 11 x 1 + a 12 x a 1n x n = b 1 a 21 x 1 + a 22 x a 2n x n = b 2.

Lecture 3: Finding integer solutions to systems of linear equations

Row Echelon Form and Reduced Row Echelon Form

MATH 423 Linear Algebra II Lecture 38: Generalized eigenvectors. Jordan canonical form (continued).

I. GROUPS: BASIC DEFINITIONS AND EXAMPLES

Solutions to Math 51 First Exam January 29, 2015

December 4, 2013 MATH 171 BASIC LINEAR ALGEBRA B. KITCHENS

MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS. + + x 2. x n. a 11 a 12 a 1n b 1 a 21 a 22 a 2n b 2 a 31 a 32 a 3n b 3. a m1 a m2 a mn b m

Warshall s Algorithm: Transitive Closure

2x + y = 3. Since the second equation is precisely the same as the first equation, it is enough to find x and y satisfying the system

Cross product and determinants (Sect. 12.4) Two main ways to introduce the cross product

University of Lille I PC first year list of exercises n 7. Review

Linear Programming. March 14, 2014

Matrix Algebra. Some Basic Matrix Laws. Before reading the text or the following notes glance at the following list of basic matrix algebra laws.

Continued Fractions and the Euclidean Algorithm

Lecture Notes 2: Matrices as Systems of Linear Equations

Methods for Finding Bases

CSE 135: Introduction to Theory of Computation Decidability and Recognizability

Notes on Determinant

SYSTEMS OF EQUATIONS AND MATRICES WITH THE TI-89. by Joseph Collison

Orthogonal Diagonalization of Symmetric Matrices

Lecture 1: Systems of Linear Equations

Linear Algebra Notes for Marsden and Tromba Vector Calculus

Approximation Algorithms

Chapter 19. General Matrices. An n m matrix is an array. a 11 a 12 a 1m a 21 a 22 a 2m A = a n1 a n2 a nm. The matrix A has n row vectors

Rotation Matrices and Homogeneous Transformations

Introduction to Matrix Algebra

MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS

Unit 18 Determinants

Section IV.1: Recursive Algorithms and Recursion Trees

8 Primes and Modular Arithmetic

Controllability and Observability

Solution to Homework 2

Solving Systems of Linear Equations

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Closest Pair Problem

8.2. Solution by Inverse Matrix Method. Introduction. Prerequisites. Learning Outcomes

MATH 304 Linear Algebra Lecture 9: Subspaces of vector spaces (continued). Span. Spanning set.

These axioms must hold for all vectors ū, v, and w in V and all scalars c and d.

5.3 The Cross Product in R 3

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

is in plane V. However, it may be more convenient to introduce a plane coordinate system in V.

Linear Algebra Notes

CS 103X: Discrete Structures Homework Assignment 3 Solutions

1 Solving LPs: The Simplex Algorithm of George Dantzig

Solving simultaneous equations using the inverse matrix

Direct Methods for Solving Linear Systems. Matrix Factorization

Au = = = 3u. Aw = = = 2w. so the action of A on u and w is very easy to picture: it simply amounts to a stretching by 3 and 2, respectively.

7.4. The Inverse of a Matrix. Introduction. Prerequisites. Learning Style. Learning Outcomes

Typical Linear Equation Set and Corresponding Matrices

Near Optimal Solutions

SECOND DERIVATIVE TEST FOR CONSTRAINED EXTREMA

MATH APPLIED MATRIX THEORY

Torgerson s Classical MDS derivation: 1: Determining Coordinates from Euclidean Distances


Chapter 6. Orthogonality

Just the Factors, Ma am

Why Query Optimization? Access Path Selection in a Relational Database Management System. How to come up with the right query plan?

6. LECTURE 6. Objectives

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

Recall that two vectors in are perpendicular or orthogonal provided that their dot

Solutions to Homework 6

MAT 242 Test 2 SOLUTIONS, FORM T

13 MATH FACTS a = The elements of a vector have a graphical interpretation, which is particularly easy to see in two or three dimensions.

Matrix Representations of Linear Transformations and Changes of Coordinates

( ) which must be a vector

Mathematics Course 111: Algebra I Part IV: Vector Spaces

Eigenvalues, Eigenvectors, Matrix Factoring, and Principal Components

5 Homogeneous systems

Beginner s Matlab Tutorial

Lecture L3 - Vectors, Matrices and Coordinate Transformations

v w is orthogonal to both v and w. the three vectors v, w and v w form a right-handed set of vectors.

EXCEL SOLVER TUTORIAL

Matrix Differentiation

1 Sets and Set Notation.

Lecture 18: Applications of Dynamic Programming Steven Skiena. Department of Computer Science State University of New York Stony Brook, NY

1 Review of Least Squares Solutions to Overdetermined Systems

7. LU factorization. factor-solve method. LU factorization. solving Ax = b with A nonsingular. the inverse of a nonsingular matrix

Breaking The Code. Ryan Lowe. Ryan Lowe is currently a Ball State senior with a double major in Computer Science and Mathematics and

1.3. DOT PRODUCT If θ is the angle (between 0 and π) between two non-zero vectors u and v,

FUZZY CLUSTERING ANALYSIS OF DATA MINING: APPLICATION TO AN ACCIDENT MINING SYSTEM

Math 115 Spring 2011 Written Homework 5 Solutions

1 Definition of a Turing machine

A linear combination is a sum of scalars times quantities. Such expressions arise quite frequently and have the form

DATA ANALYSIS II. Matrix Algorithms

Transcription:

Lecture 12: Chain Matrix Multiplication CLRS Section 15.2 Outline of this Lecture Recalling matrix multiplication. The chain matrix multiplication problem. A dynamic programming algorithm for chain matrix multiplication. 1

Recalling Matrix Multiplication Matrix: An matrix is a twodimensional array! " ')((( # # $#! " $# %" &...! ". * which has rows and columns. Example: The - a+, following. / is 0 1 matrix: '((( 0 1.2/, 1.,, 1 31 / 43!.! * 5 2

Recalling Matrix Multiplication The product of a matrix and a matrix is a matrix given by for and. Example: If. / 0 1!,, 1 '( * 0. 1,, '( * then 43 43 + +. 0 0 3 43 3 ')( * 5 3

5 Remarks on Matrix Multiplication If is defined, may not be defined. Quite possible that. Multiplication is recursively defined by Matrix multiplication is associative, e.g., so parenthenization does not change result. 4

Direct Matrix multiplication Given a matrix and a matrix, the direct way of multiplying is to compute each for and. Complexity of Direct Matrix multiplication: Note that has entries and each entry takes time to compute so the total procedure takes time. 5

5 Direct Matrix multiplication of Given a matrix, a matrix and a matrix, then and : can be computed in two ways The number of multiplications needed are: 5, + 1 When,, and, then. 3.. A big difference! Implication: The multiplication sequence (parenthesization) is important!! 6

The Chain Matrix Multiplication Problem dimensions 555 corresponding to matrix sequence, Given,555, where has dimension, determine the multiplication sequence that minimizes the number of scalar multiplications in computing. That is, determine how to parenthisize - the multiplications. Exhaustive search: +. Question: Any better approach? Yes DP 7

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

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

Developing a Dynamic Programming Algorithm Step 1 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 )? (Search all possible values of ) How do we parenthesize the subchains and? (Problem has optimal substructure property that and must be optimal so we can apply the same procedure recursively) 10

Developing a Dynamic Programming Algorithm Step 1 Continued: Optimal Substructure Property: If final optimal solution of involves splitting into and at final step then parenthesization of and in final optimal solution must also be optimal for the subproblems standing alone : If parenthisization of 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 was not optimal we could replace it by a better parenthesization and get a cheaper final solution, also leading to a contradiction. 11

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

Developing a Dynamic Programming Algorithm Step 2: Recursively define the value of an optimal solution. Proof: Any optimal sequence of multiplication for is equivalent to some choice of splitting and also are optimal. Hence for some, where the sequences of multiplications for 5 13

Developing a Dynamic Programming Algorithm Step 2 Continued: We know that, for some 5 We don t know what is, though But, there are only! possible values of so we can check them all and find the one which returns a smallest cost. Therefore 14

Developing a Dynamic Programming Algorithm Step 3: Compute the value of an optimal solution in a bottom-up fashion. Our Table:. only defined for. The important point is that when we use the equation to calculate we must have already evaluated and For both cases, the corresponding length of the matrix-chain are both less than. Hence, the algorithm should fill the table in increasing order of the length of the matrixchain. That is, we calculate in the order!! "# $ %& ' ( ) * *+, - & ) /.0 * 1. *2# & & 15

Dynamic Programming Design Warning!! When designing a dynamic programming algorithm there are two parts: 1. Finding an appropriate optimal substructure property and corresponding recurrence relation on table items. Example: 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 " is calculated all of the values and were already calculated. 16

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

5 Example Continued Stp 1: Computing By definition & " $ 3 4 1 m[i,j] 3 2 j i 2 3 1 120 4 0 0 0 0 5 4 6 2 7 A1 A2 A3 A4 p0 p1 p2 p3 p4 18

5 Example Continued Stp 2: Computing # By definition # $ $ +. 4 1 m[i,j] 3 2 j i 2 3 1 120 48 4 0 0 0 0 5 4 6 2 7 A1 A2 A3 A4 p0 p1 p2 p3 p4 19

Example Continued Stp3: Computing # + By definition # + + + +. + 5 4 1 m[i,j] 3 2 j i 2 3 1 120 48 84 4 0 0 0 0 5 4 6 2 7 A1 A2 A3 A4 p0 p1 p2 p3 p4 20

5 Example Continued Stp4: Computing By definition " $# & #.. 4 1 m[i,j] 3 2 j 2 88 i 3 1 120 48 84 4 0 0 0 0 5 4 6 2 7 A1 A2 A3 A4 p0 p1 p2 p3 p4 21

Example Continued Stp5: Computing # + By definition # + $ + $#& + $# + + 43 + 5 # 4 1 m[i,j] 3 2 j 2 88 104 i 3 1 120 48 84 4 0 0 0 0 5 4 6 2 7 A1 A2 A3 A4 p0 p1 p2 p3 p4 22

5 Example Continued St6: Computing + By definition + + # + # + + +,. 4 1 m[i,j] 3 158 2 j 2 88 104 i 3 1 120 48 84 4 0 0 0 0 5 4 6 2 7 A1 A2 A3 A4 p0 p1 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 % " 55 55, where denotes for the optimal splitting in computing 55 55. The array can be used recursively to recover the multiplication sequence. How to Recover the Multiplication Sequence?.. Do this recursively until the multiplication sequence is determined. 24

5 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 1. Assume that the array has been computed. The multiplication sequence is recovered as follows. 1 + 1, Hence the final multiplication sequence is 55 1 % 55 1 25

Hence the time complexity is. Space complexity. The Dynamic Programming Algorithm Matrix-Chain( ) for ( to ) ; for ( to ) for ( to ; ; for ( return and ) to ) if ( ) ; ; ; (Optimum in ) ; Complexity: The loops are nested three deep. Each loop index takes on values. 26

Constructing an Optimal Solution: Compute The actual multiplication code uses the " value to determine how to split the current sequence. Assume that the matrices are stored in an array of matrices 55, and that is global to this recursive procedure. The procedure returns a matrix. Mult( ) if ( ) ; is now, where is ; is now return ; multiply matrices and else return ; To compute, call Mult( % ). 27

5 Constructing an Optimal Solution: Compute Example of Constructing an Optimal Solution: Compute. 1 1 1 Consider the example 55 earlier, 55 where. Assume that the array has been computed. The multiplication sequence is recovered as follows. Mult Mult Mult Mult Mult Hence the product is computed as follows 28