Matrix Multiplication (Dynamic Programming)

Slides:



Advertisements
Similar presentations
Matrix.
Advertisements

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.
Maths for Computer Graphics
Table of Contents Matrices - Multiplication Assume that matrix A is of order m  n and matrix B is of order p  q. To determine whether or not A can be.
1 © 2010 Pearson Education, Inc. All rights reserved © 2010 Pearson Education, Inc. All rights reserved Chapter 9 Matrices and Determinants.
Chapter 4 Matrices By: Matt Raimondi.
Row 1 Row 2 Row 3 Row m Column 1Column 2Column 3 Column 4.
Have we ever seen this phenomenon before? Let’s do some quick multiplication…
AIM: How do we perform basic matrix operations? DO NOW:  Describe the steps for solving a system of Inequalities  How do you know which region is shaded?
If A and B are both m × n matrices then the sum of A and B, denoted A + B, is a matrix obtained by adding corresponding elements of A and B. add these.
Slide Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Relations, Functions, and Matrices Mathematical Structures for Computer Science Chapter 4 Copyright © 2006 W.H. Freeman & Co.MSCS Slides Relations, Functions.
Class 7: Answers 1 (C) Which of the following matrices below is in reduced row echelon form? A B C D. None of them.
How to Multiply Two Matrices. Steps for Matrix Multiplication 1.Determine whether the matrices are compatible. 2.Determine the dimensions of the product.
Matrix Algebra Section 7.2. Review of order of matrices 2 rows, 3 columns Order is determined by: (# of rows) x (# of columns)
8.2 Operations With Matrices
Algebra Matrix Operations. Definition Matrix-A rectangular arrangement of numbers in rows and columns Dimensions- number of rows then columns Entries-
Copyright ©2015 Pearson Education, Inc. All rights reserved.
Matrix Multiplication The Introduction. Look at the matrix sizes.
4.2 Matrix Multiplication Objectives: Multiply 2 matrices. Use matrix multiplication to solve mathematical and real-world problems. Standard:
Section 4.3 – Multiplying Matrices. MATRIX MULTIPLICATION 1. The order makes a difference…AB is different from BA. 2. The number of columns in first matrix.
Notes Over 4.2 Finding the Product of Two Matrices Find the product. If it is not defined, state the reason. To multiply matrices, the number of columns.
Do Now: Perform the indicated operation. 1.). Algebra II Elements 11.1: Matrix Operations HW: HW: p.590 (16-36 even, 37, 44, 46)
Dynamic Programming Examples By Alexandra Stefan and Vassilis Athitsos.
If A and B are both m × n matrices then the sum of A and B, denoted A + B, is a matrix obtained by adding corresponding elements of A and B. add these.
4.3 Multiplying Matrices Dimensions matching Rows times Columns.
Determinants.
Lesson 43: Working with Matrices: Multiplication
12-1 Organizing Data Using Matrices
Multiplying Matrices.
Matrix. Matrix Matrix Matrix (plural matrices) . a collection of numbers Matrix (plural matrices)  a collection of numbers arranged in a rectangle.
Christmas Packets are due on Friday!!!
Matrices Rules & Operations.
Finding the Inverse of a Matrix
Advanced Algorithms Analysis and Design
Matrix Operations Monday, August 06, 2018.
Advanced Design and Analysis Techniques
Matrix Chain Multiplication
Matrix Operations SpringSemester 2017.
Dr. Ameria Eldosoky Discrete mathematics
Multiplying Matrices.
Section 3.1 – Operations with Matrices
Matrix Algebra.
7.3 Matrices.
Dynamic Programming Steps.
Matrix Chain Multiplication
Linear Equations in Linear Algebra
Lial/Hungerford/Holcomb/Mullins: Mathematics with Applications 11e Finite Mathematics with Applications 11e Copyright ©2015 Pearson Education, Inc. All.
Topic 6: Multiplication
Warm-Up 3) 1) 4) Name the dimensions 2).
Multiplying Matrices.
Matrix Algebra.
Bellwork 1) Multiply. 3) Find the determinant. 2) Multiply.
3.5 Perform Basic Matrix Operations
Dimensions matching Rows times Columns
3.6 Multiply Matrices.
Dynamic Programming Steps.
Chapter 4 Matrices & Determinants
1.8 Matrices.
Matrix Operations SpringSemester 2017.
Matrix A matrix is a rectangular arrangement of numbers in rows and columns Each number in a matrix is called an Element. The dimensions of a matrix are.
CSCI 235, Spring 2019, Lecture 25 Dynamic Programming
1.8 Matrices.
Multiplying Matrices.
Algorithms CSCI 235, Spring 2019 Lecture 27 Dynamic Programming II
3.5 Perform Basic Matrix Operations Algebra II.
Multiplying Matrices.
Matrix Multiplication Sec. 4.2
Multiplying Matrices.
Presentation transcript:

Matrix Multiplication (Dynamic Programming)

Matrix Multiplication: Review Suppose that A1 is of size S1 x S2, and A2 is of size S2 x S3. What is the time complexity of computing A1 * A2? What is the size of the result?

Matrix Multiplication: Review Suppose that A1 is of size S1 x S2, and A2 is of size S2 x S3. What is the time complexity of computing A1 * A2? What is the size of the result? S1 x S3. Each number in the result is computed in O(S2) time by: multiplying S2 pairs of numbers. adding S2 numbers. Overall time complexity: O(S1 * S2 * S3).

Optimal Ordering for Matrix Multiplication Suppose that we need to do a sequence of matrix multiplications: result = A1 * A2 * A3 * ... * AK The number of columns for Ai must equal the number of rows for Ai+1. What is the time complexity for performing this sequence of multiplications?

Optimal Ordering for Matrix Multiplication Suppose that we need to do a sequence of matrix multiplications: result = A1 * A2 * A3 * ... * AK The number of columns for Ai must equal the number of rows for Ai+1. What is the time complexity for performing this sequence of multiplications? The answer is: it depends on the order in which we perform the multiplications.

An Example Suppose: (A1 * A2) * A3: A1 * (A2 * A3): A1 is17x2.

An Example Suppose: (A1 * A2) * A3: A1 * (A2 * A3): A1 is17x2. 17*2*35 = 1190 multiplications and additions to compute A1 * A2. 17*35*4 = 2380 multiplications and additions to compute multiplying the result of (A1 * A2) with A3. Total: 3570 multiplications and additions. A1 * (A2 * A3): 2*35*4 = 280 multiplications and additions to compute A2 * A3. 17*2*4 = 136 multiplications and additions to compute multiplying A1 with the result of (A2 * A3). Total: 416 multiplications and additions.

Adaptation to Dynamic Programming Suppose that we need to do a sequence of matrix multiplications: result = A1 * A2 * A3 * ... * AK To figure out if and how we can use dynamic programming, we must address the standard two questions we always need to address for dynamic programming: Can we define a set of smaller problems, such that the solutions to those problems make it easy to solve the original problem? Can we arrange those smaller problems in a sequence of reasonable size, so that each problem in that sequence only depends on problems that come earlier in the sequence?

Defining Smaller Problems Can we define a set of smaller problems, whose solutions make it easy to solve the original problem? Original problem: optimal ordering for A1 * A2 * A3 * ... * AK Yes! Suppose that, for every i between 1 and K-1 we know: The best order (and best cost) for multiplying matrices A1, ..., Ai. The best order (and best cost) for multiplying matrices Ai+1, ..., AK. Then, for every such i, we obtain a possible solution for our original problem: Multiply matrices A1, ..., Ai in the best order. Let C1 be the cost of that. Multiply matrices Ai+1, ..., AK in the best order. Let C2 be the cost of that. Compute (A1 * ... * Ai) * (Ai+1 * ... * AK). Let C3 be the cost of that. C3 = rows of (A1 * ... * Ai) * cols of (A1 * ... * Ai) * cols of (Ai+1 * ... * AK). = rows of A1 * cols of Ai * cols of AK Total cost of this solution = C1 + C2 + C3.

Defining Smaller Problems Can we define a set of smaller problems, whose solutions make it easy to solve the original problem? Original problem: optimal ordering for A1 * A2 * A3 * ... * AK Yes! Suppose that, for every i between 1 and K-1 we know: The best order (and best cost) for multiplying matrices A1, ..., Ai. The best order (and best cost) for multiplying matrices Ai+1, ..., AK. Then, for every such i, we obtain a possible solution. We just need to compute the cost of each of those solutions, and choose the smallest cost. Next question: Can we arrange those smaller problems in a sequence of reasonable size, so that each problem in that sequence only depends on problems that come earlier in the sequence?

Defining Smaller Problems Can we arrange those smaller problems in a sequence of reasonable size, so that each problem in that sequence only depends on problems that come earlier in the sequence? To compute answer for A1 * A2 * A3 * ... * AK : For i = 1, …, K-1, we had to consider solutions for: A1, ..., Ai. Ai+1, ..., AK. So, what is the set of all problems we must solve?

Defining Smaller Problems Can we arrange those smaller problems in a sequence of reasonable size, so that each problem in that sequence only depends on problems that come earlier in the sequence? To compute answer for A1 * A2 * A3 * ... * AK : For i = 1, …, K-1, we had to consider solutions for: A1, ..., Ai. Ai+1, ..., AK. So, what is the set of all problems we must solve? For M = 1, ..., K. For N = 1, ..., M. Compute the best ordering for AN * ... * AM. What this the number of problems we need to solve? Is the size reasonable? We must solve Θ(K2) problems. We consider this a reasonable number.

Defining Smaller Problems The set of all problems we must solve: For M = 1, ..., K. For N = 1, ..., M. Compute the best ordering for AN * ... * AM. What is the order in which we must solve these problems?

Defining Smaller Problems The set of all problems we must solve, in the correct order: For M = 1, ..., K. For N = M, ..., 1. Compute the best ordering for AN * ... * AM. N must go from M to 1, NOT the other way around. Why? Because, given M, the larger the N is, the smaller the problem is of computing the best ordering for AN * ... * AM.

Solving These Problems For M = 1, ..., K. For N = M, ..., 1. Compute the best ordering for AN * ... * AM. What are the base cases? N = M. costs[N][M] = 0. N = M - 1. costs[N][M] = rows(AN) * cols(AN) * cols(AM). Solution for the recursive case:

Solving These Problems For M = 1, ..., K. For N = M, ..., 1. Compute the best ordering for AN * ... * AM. Solution for the recursive case: minimum_cost = 0 For R = N, ..., M-1: cost1 = costs[N][R] cost2 = costs[R+1][M] cost3 = rows(AN) * cols(AR) * cols(AM) cost = cost1 + cost2 + cost3 if (cost < minimum_cost) minimum_cost = cost costs[N][M] = minimum_cost