Lecture 11 22 Oct 2013
Chain matrix multiplication. all pairs shortest paths.
Optimizing matrix multiplication.
A x B x C x D
examples from book:
Given m x n and n x p matrices (draw)
How many multiplications?
mnp multiplications.
Is matrix multiplication commutative? NO
Is it associative? YES
Compare costs:
A x ((B x C) x D) = 20.1.10 + 20.10.100 + 50.20.100 = 120,000
(A x (B x C)) x D) = 20.1.10 x 50.20.10 + 50.10.100 = 60,200
(A x B) x (C x D) = 50.20.1 + 1.10.100 + 50.1.100 = 7,000
Can we find optimal association greedily?
No, because cheapest pair available is (B,C), so would start with (B x C)
Can represent each solution as binary tree:
((A x B) x C) x D)
(A x B) x (C x D)
For a tree to be optimal, each subtree must be optimal.
Why? Because the root of a subtree is a particular sequence of mults, e.g. A x B x C
So the subtree is just how to ASSOCIATE that sequence
Let input be A1 x A2 x ... An
where size of arrays is
m_0 x m_1, m_1 x m_2, ..., m_{n-1} x m_n
C(i,j) = minimum cost of multiplying Ai x A_{i+1} x .... Aj
C(i,i) = ? Zero!
Consider tree rooted at C(i,j). It's children are C(i,k) and C(k,j) for some i < k < j
root multiplies
Ai x ... x Ak by A_{k+1} x ... Aj
what are the dimensions of these intermediate results?
m_{i-1} x m_k m_k x m_j
It's cost is C(i,k) + C(k,j) + what?
C(i,k) + C(k,j) + m_{i-1} x m_k x m_j
Algorithm: find the k that minimizes overall cost
Solve for increasing size of subproblems
for i = 1 to n: C(i,i) = 0
for s = 1 to n-1 // subproblem size
for i = 1 to n-s
j = i + s
C(i,j) = min{ C(i,k) + C(k+1,j) + m_{i-1} * m_k * m_j : i <= k < j }
return C(l,n)
This returns the cost.
One of the problems on the next problem set (the one after the one due this week) will
be to modify it so that it returns the tree.
Once you have you have tree, then you carry out the actual multiplications by traversing the tree.
**************************
One last dynamic algorithm, that you already know: all pairs shortest paths.
Floyd Washall.
dist(i,j,k) = distance from i to j using no intermediate node numbered higher than k
dist(i,j,0) = length(i,j) if (i,j) in E else infinity
for k = 1 to n
for i = 1 to n
for j = 1 to n
dist(i,j,k) = min { dist(i,k,k-1) + dist(k,j,k-1), dist(i,j,k-1) }
Next class: everything you know about dynamic programming is wrong: MEMOIZATION.