Lec12.1-Dynamic Programming 2
Lec12.1-Dynamic Programming 2
Lec12.1-Dynamic Programming 2
1
Multiplying Matrices
2
Ex2:Matrix-chain multiplication
n We are given a sequence (chain) <A1, A2, ..., An> of n
matrices to be multiplied, and we wish to compute the
product. A1.A2…An.
3
Matrix-chain multiplication – cont.
n We can multiply two matrices A and B only if they are
compatible the number of columns of A must equal the
number of rows of B. If A is a p × q matrix and B is a q ×
r matrix, the resulting matrix C is a p × r matrix.
4
Matrix-chain multiplication – cont.
n Ex: consider the problem of a chain <A1, A2, A3> of
three matrices, with the dimensions of 10 × 100, 100
× 5, and 5 × 50, respectively.
n Costs:
q ((AB)C)D = 1200 + 12000 + 7500 = 20700
q (AB)(CD) = 1200 + 10000 + 30000 = 41200
q A((BC)D) = 400 + 250 + 750 = 1400
6
Matrix Chain Multiplication (MCM)
Problem
n Input:
q Matrices A1, A2, …, An, each Ai of size pi-1 x pi,
n Output:
q Fully parenthesised product A1 x A2 x … x An
that minimizes the number of scalar
multiplications.
n Note:
q In MCM problem, we are not actually multiplying
matrices
q Our goal is only to determine an order for
multiplying matrices that has the lowest cost
7
Matrix Chain Multiplication (MCM)
Problem
n Typically, the time invested in determining
this optimal order is more than paid for by the
time saved later on when actually performing
the matrix multiplications
8
Counting the Number of Parenthesizations
n Denote the number of alternative parenthesizations of a
sequence of (n) matrices by P(n) then a fully
parenthesized matrix product is given by:
9
Counting the Number of Parenthesizations
10
Step 1: Optimal Sub-structure
11
Step 1: Optimal Sub-structure
12
Step 1: Optimal Sub-structure
13
Step 1: Optimal Sub-structure
14
Step 2: Recursive Solution
n Next we define the cost of optimal solution
recursively in terms of the optimal solutions to
sub-problems
15
Step 2: Recursive Solution
n Since Ai..j can be obtained by breaking it into
Ai..k Ak+1..j, We have (each Ai of size pi-1 x
pi)
q m[i, j] = m[i, k] + m[k+1, j] + pi-1pkpj
16
Step 2: Recursive Solution
17
Step 3: Computing the Optimal Costs
18
Elements of Dynamic Programming
19
Overlapping Sub-problems
20
Step 3: Computing the Optimal Costs
The following code assumes that matrix Ai has
dimensions pi-1 x pi for i =1,2, …, n
21
Step 3: Computing the Optimal Costs
22
Step 3: Computing the Optimal Costs
23
Step 3: Computing the Optimal Costs
24
Step 3: Computing the Optimal Costs
25
Step 3: Computing the Optimal Costs
26
Step 3: Computing the Optimal Costs
27
Step 4: Constructing an Optimal Solution
n Although Matrix-Chain-Order determines the
optimal number of scalar multiplications
needed to compute a matrix-chain product, it
does not directly show how to multiply the
matrices
28
Step 4: Constructing an Optimal Solution
29
Step 4: Constructing an Optimal Solution
30
Step 4: Constructing an Optimal Solution
31
Step 4: Constructing an Optimal Solution
32