A reasonable question might be whether there is some easier way to calculate the optimal grouping of matrices (optimal BST structure, etc.) than the Dynamic Programming solution I presented.
In particular, a class of algorithms called greedy algorithms attempt to build up a solution to an optimization question one step at a time, never reconsidering a step once it has been taking (perhaps myopic algorithms would be a better name than greedy algorithms).
For example, in the case of grouping matrices for optimal multiplication, you might wonder whether you would get the optimal grouping by always choosing the top-level parenthesization so as to minimize (or, alternatively, maximize) the number of multiplications in the last step.
In the first case, consider matrices , with dimensions
,
, with dimensions
, and
, with dimensions
. If we group them so that the last matrix multiplication
is minimal, we get:
The opposite approach (maximize the number of multiplications at the
top level) runs into trouble with and
having the same
dimensions as previously, but
having dimensions
.
The greedy criterion leads to
So greediness doesn't work so far for matrix grouping. How about BST structure? A likely choice is to place the most frequent keys as high as possible, but that doesn't seem to work for key 'A' has frequency 3, key 'B' has frequency 2, and key 'C' has frequency 2.
Of course, these counterexamples don't prove that there doesn't exist any greedy algorithm for solving these problems, I've just dealt with the first ones that come to mind.