<s>
Matrix	B-Algorithm
chain	I-Algorithm
multiplication	I-Algorithm
(	O
or	O
the	O
matrix	O
chain	O
ordering	O
problem	O
)	O
is	O
an	O
optimization	O
problem	O
concerning	O
the	O
most	O
efficient	O
way	O
to	O
multiply	O
a	O
given	O
sequence	O
of	O
matrices	B-Architecture
.	O
</s>
<s>
The	O
problem	O
may	O
be	O
solved	O
using	O
dynamic	B-Algorithm
programming	I-Algorithm
.	O
</s>
<s>
For	O
example	O
,	O
for	O
four	O
matrices	B-Architecture
A	O
,	O
B	O
,	O
C	B-Language
,	O
and	O
D	O
,	O
there	O
are	O
five	O
possible	O
options	O
:	O
</s>
<s>
((AB )	O
C	B-Language
)	O
D	O
=	O
(A(BC )	O
)	O
D	O
=	O
(	O
AB	O
)	O
(	O
CD	O
)	O
=	O
A((BC )	O
D	O
)	O
=	O
A(B(CD )	O
)	O
.	O
</s>
<s>
With	O
this	O
information	O
,	O
the	O
problem	O
statement	O
can	O
be	O
refined	O
as	O
"	O
how	O
to	O
determine	O
the	O
optimal	O
parenthesization	O
of	O
a	O
product	O
of	O
n	O
matrices	B-Architecture
?	O
"	O
</s>
<s>
Checking	O
each	O
possible	O
parenthesization	O
(	O
brute	B-Algorithm
force	I-Algorithm
)	O
would	O
require	O
a	O
run-time	O
that	O
is	O
exponential	O
in	O
the	O
number	O
of	O
matrices	B-Architecture
,	O
which	O
is	O
very	O
slow	O
and	O
impractical	O
for	O
large	O
n	O
.	O
A	O
quicker	O
solution	O
to	O
this	O
problem	O
can	O
be	O
achieved	O
by	O
breaking	O
up	O
the	O
problem	O
into	O
a	O
set	O
of	O
related	O
subproblems	O
.	O
</s>
<s>
To	O
begin	O
,	O
let	O
us	O
assume	O
that	O
all	O
we	O
really	O
want	O
to	O
know	O
is	O
the	O
minimum	O
cost	O
,	O
or	O
minimum	O
number	O
of	O
arithmetic	O
operations	O
needed	O
to	O
multiply	O
out	O
the	O
matrices	B-Architecture
.	O
</s>
<s>
If	O
we	O
are	O
only	O
multiplying	O
two	O
matrices	B-Architecture
,	O
there	O
is	O
only	O
one	O
way	O
to	O
multiply	O
them	O
,	O
so	O
the	O
minimum	O
cost	O
is	O
the	O
cost	O
of	O
doing	O
this	O
.	O
</s>
<s>
Take	O
the	O
sequence	O
of	O
matrices	B-Architecture
and	O
separate	O
it	O
into	O
two	O
subsequences	O
.	O
</s>
<s>
Add	O
these	O
costs	O
together	O
,	O
and	O
add	O
in	O
the	O
cost	O
of	O
multiplying	O
the	O
two	O
result	O
matrices	B-Architecture
.	O
</s>
<s>
Do	O
this	O
for	O
each	O
possible	O
position	O
at	O
which	O
the	O
sequence	O
of	O
matrices	B-Architecture
can	O
be	O
split	O
,	O
and	O
take	O
the	O
minimum	O
over	O
all	O
of	O
them	O
.	O
</s>
<s>
For	O
example	O
,	O
if	O
we	O
have	O
four	O
matrices	B-Architecture
ABCD	O
,	O
we	O
compute	O
the	O
cost	O
required	O
to	O
find	O
each	O
of	O
(	O
A	O
)	O
(	O
BCD	O
)	O
,	O
(	O
AB	O
)	O
(	O
CD	O
)	O
,	O
and	O
(	O
ABC	O
)	O
(	O
D	O
)	O
,	O
making	O
recursive	O
calls	O
to	O
find	O
the	O
minimum	O
cost	O
to	O
compute	O
ABC	O
,	O
AB	O
,	O
CD	O
,	O
and	O
BCD	O
.	O
</s>
<s>
Since	O
there	O
are	O
about	O
n2/2	O
different	O
subsequences	O
,	O
where	O
n	O
is	O
the	O
number	O
of	O
matrices	B-Architecture
,	O
the	O
space	O
required	O
to	O
do	O
this	O
is	O
reasonable	O
.	O
</s>
<s>
This	O
is	O
top-down	O
dynamic	B-Algorithm
programming	I-Algorithm
.	O
</s>
<s>
A	O
Python	B-Language
implementation	O
using	O
the	O
memoization	O
decorator	O
from	O
the	O
standard	O
library	O
:	O
</s>
<s>
A	O
Java	B-Language
implementation	O
using	O
zero-based	O
array	O
indexes	O
along	O
with	O
a	O
convenience	O
method	O
for	O
printing	O
the	O
solved	O
order	O
of	O
operations	O
:	O
</s>
<s>
There	O
are	O
algorithms	O
that	O
are	O
more	O
efficient	O
than	O
the	O
O(n3 )	O
dynamic	B-Algorithm
programming	I-Algorithm
algorithm	O
,	O
though	O
they	O
are	O
more	O
complex	O
.	O
</s>
<s>
They	O
showed	O
how	O
the	O
matrix	B-Algorithm
chain	I-Algorithm
multiplication	I-Algorithm
problem	O
can	O
be	O
transformed	O
(	O
or	O
reduced	B-Algorithm
)	O
into	O
the	O
problem	O
of	O
triangulation	B-Algorithm
of	O
a	O
regular	O
polygon	O
.	O
</s>
<s>
The	O
other	O
n	O
sides	O
of	O
the	O
polygon	O
,	O
in	O
the	O
clockwise	O
direction	O
,	O
represent	O
the	O
matrices	B-Architecture
.	O
</s>
<s>
With	O
n	O
matrices	B-Architecture
in	O
the	O
multiplication	O
chain	O
there	O
are	O
n−1	O
binary	O
operations	O
and	O
Cn−1	O
ways	O
of	O
placing	O
parentheses	O
,	O
where	O
Cn−1	O
is	O
the	O
(	O
n−1	O
)	O
-th	O
Catalan	O
number	O
.	O
</s>
<s>
This	O
image	O
illustrates	O
possible	O
triangulations	O
of	O
a	O
regular	O
hexagon	B-Application
.	O
</s>
<s>
These	O
correspond	O
to	O
the	O
different	O
ways	O
that	O
parentheses	O
can	O
be	O
placed	O
to	O
order	O
the	O
multiplications	O
for	O
a	O
product	O
of	O
5	O
matrices	B-Architecture
.	O
</s>
<s>
For	O
the	O
example	O
below	O
,	O
there	O
are	O
four	O
sides	O
:	O
A	O
,	O
B	O
,	O
C	B-Language
and	O
the	O
final	O
result	O
ABC	O
.	O
</s>
<s>
A	O
is	O
a	O
10×30	O
matrix	O
,	O
B	O
is	O
a	O
30×5	O
matrix	O
,	O
C	B-Language
is	O
a	O
5×60	O
matrix	O
,	O
and	O
the	O
final	O
result	O
is	O
a	O
10×60	O
matrix	O
.	O
</s>
<s>
The	O
total	O
cost	O
of	O
a	O
particular	O
triangulation	B-Algorithm
of	O
the	O
polygon	O
is	O
the	O
sum	O
of	O
the	O
costs	O
of	O
all	O
its	O
triangles	O
:	O
</s>
<s>
Wang	O
,	O
Zhu	O
and	O
Tian	O
have	O
published	O
a	O
simplified	O
O(n log m )	O
algorithm	O
,	O
where	O
n	O
is	O
the	O
number	O
of	O
matrices	B-Architecture
in	O
the	O
chain	O
and	O
m	O
is	O
the	O
number	O
of	O
local	O
minimums	O
in	O
the	O
dimension	O
sequence	O
of	O
the	O
given	O
matrix	O
chain	O
.	O
</s>
<s>
we	O
remove	O
the	O
vertex	O
from	O
the	O
polygon	O
and	O
add	O
the	O
side	O
to	O
the	O
triangulation	B-Algorithm
.	O
</s>
<s>
For	O
all	O
the	O
remaining	O
vertices	O
,	O
we	O
add	O
the	O
side	O
to	O
the	O
triangulation	B-Algorithm
.	O
</s>
<s>
This	O
gives	O
us	O
a	O
nearly	O
optimal	O
triangulation	B-Algorithm
.	O
</s>
<s>
The	O
matrix	B-Algorithm
chain	I-Algorithm
multiplication	I-Algorithm
problem	O
generalizes	O
to	O
solving	O
a	O
more	O
abstract	O
problem	O
:	O
given	O
a	O
linear	O
sequence	O
of	O
objects	O
,	O
an	O
associative	O
binary	O
operation	O
on	O
those	O
objects	O
,	O
and	O
a	O
way	O
to	O
compute	O
the	O
cost	O
of	O
performing	O
that	O
operation	O
on	O
any	O
two	O
given	O
objects	O
(	O
as	O
well	O
as	O
all	O
partial	O
results	O
)	O
,	O
compute	O
the	O
minimum	O
cost	O
way	O
to	O
group	O
the	O
objects	O
to	O
apply	O
the	O
operation	O
over	O
the	O
sequence.G.	O
</s>
<s>
In	O
C	B-Language
,	O
for	O
example	O
,	O
the	O
cost	O
of	O
concatenating	O
two	O
strings	O
of	O
length	O
m	O
and	O
n	O
using	O
strcat	O
is	O
O( m	O
+	O
n	O
)	O
,	O
since	O
we	O
need	O
O(m )	O
time	O
to	O
find	O
the	O
end	O
of	O
the	O
first	O
string	O
and	O
O(n'' )	O
time	O
to	O
copy	O
the	O
second	O
string	O
onto	O
the	O
end	O
of	O
it	O
.	O
</s>
<s>
Using	O
this	O
cost	O
function	O
,	O
we	O
can	O
write	O
a	O
dynamic	B-Algorithm
programming	I-Algorithm
algorithm	O
to	O
find	O
the	O
fastest	O
way	O
to	O
concatenate	O
a	O
sequence	O
of	O
strings	O
.	O
</s>
<s>
A	O
similar	O
problem	O
exists	O
for	O
singly	O
linked	B-Data_Structure
lists	I-Data_Structure
.	O
</s>
