11-1 Matrix-chain Multiplication Suppose we have a sequence or chain A 1, A 2, …, A n of n matrices to be multiplied –That is, we want to compute the product.

Slides:



Advertisements
Similar presentations
Welcome to our presentation
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.
Algorithm Design Methodologies Divide & Conquer Dynamic Programming Backtracking.
1 Dynamic Programming Jose Rolim University of Geneva.
Dynamic Programming II Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
CS 691 Computational Photography Instructor: Gianfranco Doretto Cutting Images.
Chapter 15 Dynamic Programming Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from.
1 Dynamic Programming (DP) Like divide-and-conquer, solve problem by combining the solutions to sub-problems. Differences between divide-and-conquer and.
Dynamic Programming (pro-gram)
Dynamic Programming Lets begin by looking at the Fibonacci sequence.
Dynamic Programming Lecture 9 Asst. Prof. Dr. İlker Kocabaş 1.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2006 Lecture 1 (Part 3) Design Patterns for Optimization Problems.
Dynamic Programming Reading Material: Chapter 7..
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2009 Design Patterns for Optimization Problems Dynamic Programming.
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
Dynamic Programming Code
CSC401 – Analysis of Algorithms Lecture Notes 12 Dynamic Programming
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
1 Dynamic Programming Andreas Klappenecker [based on slides by Prof. Welch]
UNC Chapel Hill Lin/Manocha/Foskey Optimization Problems In which a set of choices must be made in order to arrive at an optimal (min/max) solution, subject.
Analysis of Algorithms CS 477/677
Dynamic Programming Reading Material: Chapter 7 Sections and 6.
Optimal binary search trees
Analysis of Algorithms
Dynamic Programming Introduction to Algorithms Dynamic Programming CSE 680 Prof. Roger Crawfis.
1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems.
Algorithms and Data Structures Lecture X
Dynamic Programming Dynamic programming is a technique for solving problems with a recursive structure with the following characteristics: 1.optimal substructure.
Dynamic Programming UNC Chapel Hill Z. Guo.
October 21, Algorithms and Data Structures Lecture X Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Dynamic Programming. Well known algorithm design techniques:. –Divide-and-conquer algorithms Another strategy for designing algorithms is dynamic programming.
CS 5243: Algorithms Dynamic Programming Dynamic Programming is applicable when sub-problems are dependent! In the case of Divide and Conquer they are.
COSC 3101A - Design and Analysis of Algorithms 7 Dynamic Programming Assembly-Line Scheduling Matrix-Chain Multiplication Elements of DP Many of these.
CSC401: Analysis of Algorithms CSC401 – Analysis of Algorithms Chapter Dynamic Programming Objectives: Present the Dynamic Programming paradigm.
CS 8833 Algorithms Algorithms Dynamic Programming.
1 Dynamic Programming Andreas Klappenecker [partially based on slides by Prof. Welch]
Dynamic Programming Greed is not always good.. Jaruloj Chongstitvatana Design and Analysis of Algorithm2 Outline Elements of dynamic programming.
Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 4: Dynamic Programming Phan Th ị Hà D ươ ng 1.
Algorithmics - Lecture 121 LECTURE 11: Dynamic programming - II -
1 Ch20. Dynamic Programming. 2 BIRD’S-EYE VIEW Dynamic programming The most difficult one of the five design methods Has its foundation in the principle.
Optimization Problems In which a set of choices must be made in order to arrive at an optimal (min/max) solution, subject to some constraints. (There may.
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
15.Dynamic Programming. Computer Theory Lab. Chapter 15P.2 Dynamic programming Dynamic programming is typically applied to optimization problems. In such.
Chapter 15 Dynamic Programming Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from.
TU/e Algorithms (2IL15) – Lecture 3 1 DYNAMIC PROGRAMMING
1 Chapter 15-2: Dynamic Programming II. 2 Matrix Multiplication Let A be a matrix of dimension p x q and B be a matrix of dimension q x r Then, if we.
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
Seminar on Dynamic Programming.
Matrix Chain Multiplication
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Dynamic programming techniques
Dynamic programming techniques
Dynamic Programming Several problems Principle of dynamic programming
Chapter 8 Dynamic Programming.
Dynamic Programming Comp 122, Fall 2004.
Unit-5 Dynamic Programming
Matrix Chain Multiplication
Dynamic Programming.
Data Structure and Algorithms
Chapter 15: Dynamic Programming II
Dynamic Programming Comp 122, Fall 2004.
Algorithms CSCI 235, Spring 2019 Lecture 28 Dynamic Programming III
Dynamic Programming-- Longest Common Subsequence
Matrix Chain Multiplication
CSCI 235, Spring 2019, Lecture 25 Dynamic Programming
Algorithms CSCI 235, Spring 2019 Lecture 27 Dynamic Programming II
Chapter 15: Dynamic Programming
Chapter 15: Dynamic Programming
Seminar on Dynamic Programming.
Presentation transcript:

11-1 Matrix-chain Multiplication Suppose we have a sequence or chain A 1, A 2, …, A n of n matrices to be multiplied –That is, we want to compute the product A 1 A 2 …A n There are many possible ways (parenthesizations) to compute the product

11-2 Matrix-chain Multiplication …contd Example: consider the chain A 1, A 2, A 3, A 4 of 4 matrices –Let us compute the product A 1 A 2 A 3 A 4 There are 5 possible ways: 1.(A 1 (A 2 (A 3 A 4 ))) 2.(A 1 ((A 2 A 3 )A 4 )) 3.((A 1 A 2 )(A 3 A 4 )) 4.((A 1 (A 2 A 3 ))A 4 ) 5.(((A 1 A 2 )A 3 )A 4 )

11-3 Matrix-chain Multiplication …contd To compute the number of scalar multiplications necessary, we must know: –Algorithm to multiply two matrices –Matrix dimensions Can you write the algorithm to multiply two matrices?

11-4 Algorithm to Multiply 2 Matrices Input: Matrices A p×q and B q×r (with dimensions p×q and q×r) Result: Matrix C p×r resulting from the product A·B MATRIX-MULTIPLY(A p×q, B q×r ) 1.for i ← 1 to p 2.for j ← 1 to r 3.C[i, j] ← 0 4.for k ← 1 to q 5.C[i, j] ← C[i, j] + A[i, k] · B[k, j] 6.return C Scalar multiplication in line 5 dominates time to compute CNumber of scalar multiplications = pqr

11-5 Matrix-chain Multiplication …contd Example: Consider three matrices A 10  100, B 100  5, and C 5  50 There are 2 ways to parenthesize –((AB)C) = D 10  5 · C 5  50 AB  10 · 100 · 5=5,000 scalar multiplications DC  10 · 5 · 50 =2,500 scalar multiplications –(A(BC)) = A 10  100 · E 100  50 BC  100 · 5 · 50=25,000 scalar multiplications AE  10 · 100 · 50 =50,000 scalar multiplications Total: 7,500 Total: 75,000

11-6 Matrix-chain Multiplication …contd Matrix-chain multiplication problem –Given a chain A 1, A 2, …, A n of n matrices, where for i=1, 2, …, n, matrix A i has dimension p i-1  p i –Parenthesize the product A 1 A 2 …A n such that the total number of scalar multiplications is minimized Brute force method of exhaustive search takes time exponential in n

11-7 Dynamic Programming Approach The structure of an optimal solution –Let us use the notation A i..j for the matrix that results from the product A i A i+1 … A j –An optimal parenthesization of the product A 1 A 2 …A n splits the product between A k and A k+1 for some integer k where1 ≤ k < n –First compute matrices A 1..k and A k+1..n ; then multiply them to get the final matrix A 1..n

11-8 Dynamic Programming Approach …contd –Key observation: parenthesizations of the subchains A 1 A 2 …A k and A k+1 A k+2 …A n must also be optimal if the parenthesization of the chain A 1 A 2 …A n is optimal (why?) –That is, the optimal solution to the problem contains within it the optimal solution to subproblems

11-9 Dynamic Programming Approach …contd Recursive definition of the value of an optimal solution –Let m[i, j] be the minimum number of scalar multiplications necessary to compute A i..j –Minimum cost to compute A 1..n is m[1, n] –Suppose the optimal parenthesization of A i..j splits the product between A k and A k+1 for some integer k where i ≤ k < j

11-10 Dynamic Programming Approach …contd –A i..j = (A i A i+1 …A k )·(A k+1 A k+2 …A j )= A i..k · A k+1..j –Cost of computing A i..j = cost of computing A i..k + cost of computing A k+1..j + cost of multiplying A i..k and A k+1..j –Cost of multiplying A i..k and A k+1..j is p i-1 p k p j –m[i, j ] = m[i, k] + m[k+1, j ] + p i-1 p k p j for i ≤ k < j –m[i, i ] = 0 for i=1,2,…,n

11-11 Dynamic Programming Approach …contd –But … optimal parenthesization occurs at one value of k among all possible i ≤ k < j –Check all these and select the best one m[i, j ] = 0 if i=j min {m[i, k] + m[k+1, j ] + p i-1 p k p j } if i<j i ≤ k< j

11-12 Dynamic Programming Approach …contd To keep track of how to construct an optimal solution, we use a table s s[i, j ] = value of k at which A i A i+1 … A j is split for optimal parenthesization Algorithm: next slide –First computes costs for chains of length l =1 –Then for chains of length l =2,3, … and so on –Computes the optimal cost bottom-up

11-13 Algorithm to Compute Optimal Cost Input: Array p[0…n] containing matrix dimensions and n Result: Minimum-cost table m and split table s MATRIX-CHAIN-ORDER(p[ ], n) for i ← 1 to n m[i, i] ← 0 for l ← 2 to n for i ← 1 to n-l+1 j ← i+l-1 m[i, j] ←  for k ← i to j-1 q ← m[i, k] + m[k+1, j] + p[i-1] p[k] p[j] if q < m[i, j] m[i, j] ← q s[i, j] ← k return m and s Takes O(n 3 ) time Requires O(n 2 ) space

11-14 Constructing Optimal Solution Our algorithm computes the minimum- cost table m and the split table s The optimal solution can be constructed from the split table s –Each entry s[i, j ]=k shows where to split the product A i A i+1 … A j for the minimum cost

11-15 Example Show how to multiply this matrix chain optimally Solution on the board –Minimum cost 15,125 –Optimal parenthesization ((A 1 (A 2 A 3 ))((A 4 A 5 )A 6 )) MatrixDimension A1A1 30×35 A2A2 35×15 A3A3 15×5 A4A4 5×10 A5A5 10×20 A6A6 20×25