Dynamic Programming: Matrix chain multiplication (CLRS 15.2)

Similar documents
Dynamic Programming (CLRS )

Today s Outline. CS 362, Lecture 13. Matrix Chain Multiplication. Paranthesizing Matrices. Matrix Multiplication. Jared Saia University of New Mexico

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Dynamic Programming II Date: 10/12/17

Lecture 10: Powers of Matrices, Difference Equations

Graduate Algorithms CS F-09 Dynamic Programming

CMPSCI611: Three Divide-and-Conquer Examples Lecture 2

CS173 Lecture B, November 3, 2015

Dynamic Programming( Weighted Interval Scheduling)

Vector, Matrix, and Tensor Derivatives

Algorithms Design & Analysis. Dynamic Programming

Algorithmic Game Theory and Applications. Lecture 4: 2-player zero-sum games, and the Minimax Theorem

. =. a i1 x 1 + a i2 x 2 + a in x n = b i. a 11 a 12 a 1n a 21 a 22 a 1n. i1 a i2 a in

CS473 - Algorithms I

Data Structures and Algorithms

Algorithms and Data Structures Strassen s Algorithm. ADS (2017/18) Lecture 4 slide 1

Eigenvalues and eigenvectors

Recitation 9: Probability Matrices and Real Symmetric Matrices. 3 Probability Matrices: Definitions and Examples

Chapter 8 Dynamic Programming

Introduction. I Dynamic programming is a technique for solving optimization problems. I Key element: Decompose a problem into subproblems, solve them

Section 29: What s an Inverse?

Tutorials. Algorithms and Data Structures Strassen s Algorithm. The Master Theorem for solving recurrences. The Master Theorem (cont d)

Testing a Hash Function using Probability

Quick Sort Notes , Spring 2010

22A-2 SUMMER 2014 LECTURE Agenda

Bayesian Updating with Discrete Priors Class 11, Jeremy Orloff and Jonathan Bloom

1 Closest Pair of Points on the Plane

MAT 1302B Mathematical Methods II

4.3 Minimizing & Mixed Constraints

The Master Theorem for solving recurrences. Algorithms and Data Structures Strassen s Algorithm. Tutorials. The Master Theorem (cont d)

Alex s Guide to Word Problems and Linear Equations Following Glencoe Algebra 1

Kevin James. MTHSC 3110 Section 2.1 Matrix Operations

Lecture 13: Dynamic Programming Part 2 10:00 AM, Feb 23, 2018

Chapter 8 Dynamic Programming

MTH5112 Linear Algebra I MTH5212 Applied Linear Algebra (2017/2018)

A = , A 32 = n ( 1) i +j a i j det(a i j). (1) j=1

Matrices: 2.1 Operations with Matrices

Number of solutions of a system

Lecture 7: Dynamic Programming I: Optimal BSTs

Chapter 4 Divide-and-Conquer

CS1210 Lecture 23 March 8, 2019

Row Reduction

Matrices. 1 a a2 1 b b 2 1 c c π

MA 1128: Lecture 08 03/02/2018. Linear Equations from Graphs And Linear Inequalities

INF4130: Dynamic Programming September 2, 2014 DRAFT version

P (E) = P (A 1 )P (A 2 )... P (A n ).

Lesson 3: Solving Equations A Balancing Act

Lecture 3: Big-O and Big-Θ

A primer on matrices

2/3. Activity 3: Adding and Subtracting Unlike Proper Fractions: (Answer all questions using correct spelling and grammar.)

Q520: Answers to the Homework on Hopfield Networks. 1. For each of the following, answer true or false with an explanation:

Lecture 3: Special Matrices

CMPSCI 611 Advanced Algorithms Midterm Exam Fall 2015

CISC 4090: Theory of Computation Chapter 1 Regular Languages. Section 1.1: Finite Automata. What is a computer? Finite automata

The probability of going from one state to another state on the next trial depends only on the present experiment and not on past history.

Inverses and Determinants

Before this course is over we will see the need to split up a fraction in a couple of ways, one using multiplication and the other using addition.

Dynamic Programming: Shortest Paths and DFA to Reg Exps

Aditya Bhaskara CS 5968/6968, Lecture 1: Introduction and Review 12 January 2016

Construction of latin squares of prime order

30.4. Matrix Norms. Introduction. Prerequisites. Learning Outcomes

9/4/2017. Motion: Acceleration

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Matroids and Greedy Algorithms Date: 10/31/16

Strauss PDEs 2e: Section Exercise 4 Page 1 of 5

Determinants: Uniqueness and more

Unit 3 Day 4. Solving Equations with Rational Exponents and Radicals

Lecture 13. More dynamic programming! Longest Common Subsequences, Knapsack, and (if time) independent sets in trees.

Arithmetic: Decimals, Fractions and Percentages

Dot Products, Transposes, and Orthogonal Projections

Lecture 3. Big-O notation, more recurrences!!

Notes on vectors and matrices

= main diagonal, in the order in which their corresponding eigenvectors appear as columns of E.

Lecture 17: Trees and Merge Sort 10:00 AM, Oct 15, 2018

Mergesort and Recurrences (CLRS 2.3, 4.4)

Math 4310 Solutions to homework 1 Due 9/1/16

Week 7 Solution. The two implementations are 1. Approach 1. int fib(int n) { if (n <= 1) return n; return fib(n 1) + fib(n 2); } 2.

MATH 320, WEEK 7: Matrices, Matrix Operations

Math 304 Handout: Linear algebra, graphs, and networks.

CSE373: Data Structures and Algorithms Lecture 2: Proof by Induction. Linda Shapiro Spring 2016

22A-2 SUMMER 2014 LECTURE 5

LESSON 21: DIFFERENTIALS OF MULTIVARIABLE FUNCTIONS MATH FALL 2018

Dynamic Programming: Shortest Paths and DFA to Reg Exps

Reading 10 : Asymptotic Analysis

MAT1302F Mathematical Methods II Lecture 19

Modular Arithmetic Instructor: Marizza Bailey Name:

Matrix Inverses. November 19, 2014

Math 103, Summer 2006 Determinants July 25, 2006 DETERMINANTS. 1. Some Motivation

1 Assembly Line Scheduling in Manufacturing Sector

Elementary maths for GMT

University of the Virgin Islands, St. Thomas January 14, 2015 Algorithms and Programming for High Schoolers. Lecture 5

Section 20: Arrow Diagrams on the Integers

CS2223 Algorithms D Term 2009 Exam 3 Solutions

M. Matrices and Linear Algebra

Section Summary. Sequences. Recurrence Relations. Summations Special Integer Sequences (optional)

Linear Algebra, Summer 2011, pt. 2

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Asymptotic Analysis, recurrences Date: 9/7/17

Dynamic Programming. Prof. S.J. Soni

Determinants and Scalar Multiplication

Math 308 Midterm Answers and Comments July 18, Part A. Short answer questions

Transcription:

Dynamic Programming: Matrix chain multiplication (CLRS.) The problem Given a sequence of matrices A, A, A,..., A n, find the best way (using the minimal number of multiplications) to compute their product. Isn t there only one way? (( ((A A ) A ) ) A n ) No, matrix multiplication is associative. e.g. A (A (A ( (A n A n ) ))) yields the same matrix. Different multiplication orders do not cost the same: Multiplying p q matrix A and q r matrix B takes p q r multiplications; result is a p r matrix. Consider multiplying 0 00 matrix A with 00 matrix A and 0 matrix A. Notation (A A ) A takes 0 00 + 0 0 = 700 multiplications. A (A A ) takes 00 0 + 0 0 00 = 7000 multiplications. In general, let A i be p i p i matrix. Let m(i, j) denote minimal number of multiplications needed to compute A i A i+ A j We want to compute m(, n). Recursive algorithm Assume that someone tells us the position of the last product, say k. Then we have to compute recursively the best way to multiply the chain from i to k, and from k + to j, and add the cost of the final product. This means that m(i, j) = m(i, k) + m(k +, j) + p i p k p j If noone tells us k, then we have to try all possible values of k and pick the best solution.

Recursive formulation of m(i, j): m(i, j) = { 0 If i = j min i k<j {m(i, k) + m(k +, j) + p i p k p j } If i < j To go from the recursive formulation above to a program is pretty straightforward: Matrix-chain(i, j) IF i = j THEN return 0 m = FOR k = i TO j DO q = Matrix-chain(i, k) + Matrix-chain(k +, j) +p i p k p j IF q < m THEN m = q Return m END Matrix-chain Return Matrix-chain(, n) Running time: Exponential is... SLOW! T (n) = n = k= n (T (k) + T (n k) + O()) k= T (n ) T (k) + O(n) T (n )... = n Problem is that we compute the same result over and over again. Example: Recursion tree for Matrix-chain(, ),,,,,,,,,,,,,,,,,,,,,,,,,,,

For example, we compute Matrix-chain(, ) twice. Dynamic programming with a table and recursion Solution is to remember the values we have already computed in a table. This is called memoization. We ll have a table T[..n][..n] such that T[i][j] stores the solution to problem Matrix-CHAIN(i,j). Initially all entries will be set to. FOR i = to n DO FOR j = i to n DO T [i][j] = The code for MATRIX-CHAIN(i,j) stays the same, except that it now uses the table. The first thing MATRIX-CHAIN(i,j) does is to check the table to see if T [i][j] is already computed. Is so, it returns it, otherwise, it computes it and writes it in the table. Below is the updated code. Matrix-chain(i, j) IF T [i][j] < THEN return T [i][j] IF i = j THEN T [i][j] = 0, return 0 m = FOR k = i to j DO q = Matrix-chain(i, k) + Matrix-chain(k +, j)+p i p k p j IF q < m THEN m = q T [i][j] = m return m END Matrix-chain return Matrix-chain(, n) The table will prevent a subproblem MATRIX-CHAIN(i,j) to be computed more than once. Running time: Θ(n ) different calls to matrix-chain(i, j). The first time a call is made it takes O(n) time, not counting recursive calls. When a call has been made once it costs O() time to make it again. O(n ) time Another way of thinking about it: Θ(n ) total entries to fill, it takes O(n) to fill one.

Dynamic Programming without recursion Often dynamic programming is presented as filling up a table from the bottom, in such a way that makes recursion unnecessary. Avoiding recursion is perhaps elegant, and necessary 0-0 years ago when programming languages did not include support for recursion. Personally, I like top-down recursive thinking, and I think compilers are pretty effective at optimizing recursion. Solution for Matrix-chain without recursion: Key is that m(i, j) only depends on m(i, k) and m(k +, j) where i k < j if we have computed them, we can compute m(i, j) We can easily compute m(i, i) for all i n (m(i, i) = 0) Then we can easily compute m(i, i + ) for all i n m(i, i + ) = m(i, i) + m(i +, i + ) + p i p i p i+ Then we can compute m(i, i + ) for all i n m(i, i + ) = min{m(i, i) + m(i +, i + ) + p i p i p i+, m(i, i + ) + m(i +, i + ) + p i p i+ p i+ }. Until we compute m(, n) Computation order: j 7 7 Computation order i 7 Program:

FOR i = to n DO T [i][i] = 0 FOR l = to n DO FOR i = to n l DO j = i + l T [i][j] = FOR k = to j DO q = T [i][k] + T [k + ][j] + p i p k p j IF q < T [i[j] THEN T [i][j] = q Analysis: O(n ) entries, O(n) time to compute each O(n ). Extensions Above we only computed the best way to multiply the chain (with the smallest number of operations). The algorithm can be extended to compute the actual order of multiplications corresponding to this optimal cost (we ll do this as homework or in-class exercise). This is a simplification that is often done: focus on computing the optimal cost, and leave the details of computing the solution corresponding to that optimal cost for later.