Newsgroups: comp.lang.scheme
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!news2.near.net!ll.mit.edu!vdd.VLSI.LL.MIT.EDU!bloom-beacon.mit.edu!panix!news.mathworks.com!news.alpha.net!uwm.edu!math.ohio-state.edu!howland.reston.ans.net!gatech!willis.cis.uab.edu!news.ecn.bgu.edu!siemens!princeton!news.princeton.edu!blume
From: blume@dynamic.cs.princeton.edu (Matthias Blume)
Subject: Re: factorial algorithm
In-Reply-To: rockwell@nova.umd.edu's message of 14 Nov 1994 19:29:06 -0500
Message-ID: <BLUME.94Nov15000356@dynamic.cs.princeton.edu>
Originator: news@hedgehog.Princeton.EDU
Sender: news@Princeton.EDU (USENET News System)
Nntp-Posting-Host: dynamic.cs.princeton.edu
Organization: Princeton University
References: <39l7er$q02@wsiserv.informatik.uni-tuebingen.de>
	<1994Nov8.173643.17118@njitgw.njit.edu> <39peot$4to@larry.rice.edu>
	<ROCKWELL.94Nov9132045@nova.umd.edu>
	<BLUME.94Nov9164643@dynamic.cs.princeton.edu>
	<ROCKWELL.94Nov14192904@nova.umd.edu>
Date: Tue, 15 Nov 1994 05:03:56 GMT
Lines: 62

In article <ROCKWELL.94Nov14192904@nova.umd.edu> rockwell@nova.umd.edu (Raul Deluth Miller) writes:

   Raul Miller:
	 This corresponds to a simple post-order tree walk.  You have a list of
	 dimension pairs to be combined, you arbitrarily pick from the smallest
	 -- this partitions the rest into two pieces.  Then recursively apply
	 this mechanism to each of the pieces.  On the way back, you perform
	 matrix multiplies.

   Matthias Blume:
   > This algorithm does not solve the original poster's problem (optimally).


   In what way is the above algorithm non-optimal?

   48x16 16x94 94x91 91x45 45x31 31x93 93x57 57x85 85x53 53x28 28x36 

   named:
     A     B     C     D     E     F     G     H     I     J     K

   the algorithm I described would perform multiplications in this order:
    A((((((B     C)    D)    E)  ((F     G)   (H     I)))  J)    K)

   Is this organization non-optimal?  If not, can you present an example
   where the algorithm would generate a non-optimal organization?

This is an example already.  Check out the following:

     A(((((((((B    C)    D)    E)    F)    G)    H)    I)    J)   K)

It only requires 572,768 multiplications (the optimum), while your
grouping makes 833,279 multiplications.

To give you some insight on what's going on: When you do the
calculations for computing the total number of multiplications you
will see that the optimal solutions has the `16' in all but one term.
It is obviously of advantage to keep the small 16 in as many products
as possible, even though this means that one has to `give up' on some
of the locally optimal choices, which your algorithm is based on.

To see the same effect on a much smaller example look at the following:

   A   B   C
 3   4   2   1

Grouping this according to your algorithm yields

   (A B) C with 3*4*2 + 3*2*1 = 30 multiplications,

while the optimal solution is

   A (B C) with 4*2*1 + 3*4*1 = 20 of them.

The optimal solution keeps the `1' in both products, while the locally
optimal choice of splitting at `2' doesn't have this nice property.

Convinced?

[This really is something that shouldn't be discussed in this group...]

--
-Matthias
