Next: Optimal structure of a
Up: The problems
Previous: Minimal triangulation of a
Optimal grouping of a matrix product
A matrix is a rectangular array of numbers. The product of two
matrices,
is defined, so long as
has the same number
of columns as
has rows. Suppose
has
rows and
columns,
has
rows and
columns (
must equal
), then the product
requires
multiplications of array elements. Matrix multiplication is
associative (although not commutative), so three or more matrices can
be multiplied (so long as the appropriate adjacent dimensions match):
Although the value of
is the same whichever way you
group them, the amount of work (multiplications of array elements) is
not. For example, suppose
is a
matrix (2 rows, 3
columns),
is
, and
is
. Then the
grouping
costs:
multiplications. The grouping
costs:
multiplications. This disparity gets worse for longer chains of
matrices. What is the best way to group
Here is a first attempt at finding a minimum-cost grouping. In
analogy with triangulation, denote the minimum cost of grouping
matrices
as
, and the product
as
. Then for each choice of
with
we
check the value of
, and take the
minimum. This formula looks close to that for triangulation except
for two things: the first part of the sum is
rather than
(since the matrices don't overlap), and the function
is different.
You can fix the first problem by changing notation slightly.
Define
to be the minimum cost of grouping matrices
(instead of
), so
, and our recursive formula is now the same as
Equation 1, except for
being
different (and aren't there C++ features that might help with that?).
You can use the same algorithm for both problems. The cost of a
minimal grouping of matrices
is
.
Next: Optimal structure of a
Up: The problems
Previous: Minimal triangulation of a
Danny Heap
2002-11-27