The.Algorithm.Design.Manual.Springer-Verlag.1998
The.Algorithm.Design.Manual.Springer-Verlag.1998 The.Algorithm.Design.Manual.Springer-Verlag.1998
Matrix Multiplication for j = 1 to z In two multiplying bandwidth-b matrices, where all nonzero elements of A and B lie within b elements of the main diagonals, a speedup to O(x b z) is possible, since zero elements will not contribute to the product. Asymptotically faster algorithms for matrix multiplication exist, based on clever divide-and-conquer recurrences. However, these prove difficult to program and require very large matrices to beat the trivial algorithm. In particular, some empirical results show that Strassen's algorithm is unlikely to beat the straightforward algorithm for , and it is less numerically stable to boot. Other studies have been more encouraging, claiming that the crossover point is as low as . Still, I consider it unlikely that you will speed up any serious application by implementing Strassen's algorithm. There is a better way to save computation when you are multiplying a chain of more than two matrices together. Recall that multiplying an matrix by a matrix creates an matrix. Thus multiplying a chain of matrices from left to right might create large intermediate matrices, each taking a lot of time to compute. Matrix multiplication is not commutative, but it is associative, so we can parenthesize the chain in whatever manner we deem best without changing the final product. A standard dynamic programming algorithm can be used to construct the optimal parenthesization. Whether it pays to do this optimization will depend upon whether your matrices are large enough or your chain is multiplied often enough to justify it. Note that we are optimizing over the sizes of the dimensions in the chain, not the actual matrices themselves. If all your matrices are the same dimensions, you are out of luck, since no such optimization is possible. Matrix multiplication has a particularly interesting interpretation in counting the number of paths between two vertices in a graph. Let A be the adjacency matrix of a graph G, meaning A[i,j] = 1 if there is an edge between i and j. Otherwise, A[i,j] = 0. Now consider the square of this matrix, . If , this means that there must be a k such that A[i,k]=A[k,j]=1, so i to k to j is a path of length 2 in G. More generally, counts the number of paths of length exactly k between i and j. This count includes nonsimple paths, where vertices are repeated, such as i to k to i. Implementations: The quick and dirty algorithm will be your best bet unless your matrices are very large. For example, [CLR90] suggests that n>45 before you have a hope of winning. Experimental results suggest that n > 100 is more realistic [CR76], with Bailey [BLS91] finding a crossover point of n=128 for Cray systems. Strassen's algorithm is difficult to implement efficiently because of the data structures required to maintain the array partitions. That said, an implementation of Strassen's algorithm in Mathematica by Stan Wagon is offered ``without promise of efficiency'' on the algorithm repository WWW site. file:///E|/BOOK/BOOK3/NODE138.HTM (2 of 4) [19/1/2003 1:30:18]
Matrix Multiplication The linear algebra library of choice is LAPACK, a descendant of LINPACK [DMBS79], which includes several routines for matrix multiplication. These Fortran codes are available from Netlib as discussed in Section . Algorithm 601 [McN83] of the Collected Algorithms of the ACM is a sparse matrix package written in Fortran that includes routines to multiply any combination of sparse and dense matrices. See Section for details. XTango (see Section ) is an algorithm animation system for UNIX and X-windows that includes an animation of the matrix multiplication algorithm. A C++, implementation of matrix multiplication is embedded in LEDA (see Section ). Notes: Winograd's algorithm for fast matrix multiplication reduces the number of multiplications by a factor of two over the straightforward algorithm. It is implementable, although the additional bookkeeping required makes it doubtful whether it is a win. Expositions on Winograd's algorithm [Win68] include [CLR90, Man89, Win80]. In my opinion, the history of theoretical algorithm design began when Strassen published his - time matrix multiplication algorithm. For the first time, improving an algorithm in the asymptotic sense became a respected goal in its own right. Good expositions on Strassen's algorithm [Str69] include [Baa88, CLR90, Cra94]. Progressive improvements to Strassen's algorithm have gotten progressively less practical. The current best result for matrix multiplication is Coppersmith and Winograd's [CW87] algorithm, while the conjecture is that suffices. The interest in the squares of graphs goes beyond counting paths. Fleischner [Fle74] proved that the square of any biconnected graph has a Hamiltonian cycle. See [LS95] for results on finding the square roots of graphs, i.e. finding A given . The problem of Boolean matrix multiplication can be reduced to that of general matrix multiplication [CLR90]. The four-Russians algorithm for Boolean matrix multiplication [ADKF70] uses preprocessing to construct all subsets of rows for fast retreival in performing the actual multiplication, yielding a complexity of . Additional preprocessing can improve this to [Ryt85]. An exposition on the four-Russians algorithm, including this speedup, appears in [Man89]. Good expositions of the matrix-chain algorithm include [Baa88, CLR90], where it is a standard example of dynamic programming. file:///E|/BOOK/BOOK3/NODE138.HTM (3 of 4) [19/1/2003 1:30:18]
- Page 333 and 334: War Story: Hard Against the Clock I
- Page 335 and 336: Approximation Algorithms Next: Appr
- Page 337 and 338: Approximating Vertex Cover Next: Th
- Page 339 and 340: The Euclidean Traveling Salesman Ne
- Page 341 and 342: The Euclidean Traveling Salesman Ne
- Page 343 and 344: Exercises 1. Prove that the low deg
- Page 345 and 346: Data Structures Next: Dictionaries
- Page 347 and 348: Dictionaries Next: Priority Queues
- Page 349 and 350: Dictionaries use a function that ma
- Page 351 and 352: Dictionaries Implementation-oriente
- Page 353 and 354: Priority Queues ● Besides access
- Page 355 and 356: Priority Queues Fibonacci heaps [FT
- Page 357 and 358: Suffix Trees and Arrays Figure: A t
- Page 359 and 360: Suffix Trees and Arrays [GBY91]. Se
- Page 361 and 362: Graph Data Structures algorithms).
- Page 363 and 364: Graph Data Structures including the
- Page 365 and 366: Set Data Structures Next: Kd-Trees
- Page 367 and 368: Set Data Structures parent pointers
- Page 369 and 370: Kd-Trees Next: Numerical Problems U
- Page 371 and 372: Kd-Trees about p. Say we are lookin
- Page 373 and 374: Numerical Problems Next: Solving Li
- Page 375 and 376: Numerical Problems Mon Jun 2 23:33:
- Page 377 and 378: Solving Linear Equations algorithm
- Page 379 and 380: Solving Linear Equations Matrix inv
- Page 381 and 382: Bandwidth Reduction images near eac
- Page 383: Matrix Multiplication Next: Determi
- Page 387 and 388: Determinants and Permanents Next: C
- Page 389 and 390: Determinants and Permanents exposit
- Page 391 and 392: Constrained and Unconstrained Optim
- Page 393 and 394: Constrained and Unconstrained Optim
- Page 395 and 396: Linear Programming variable assignm
- Page 397 and 398: Linear Programming The book [MW93]
- Page 399 and 400: Random Number Generation Next: Fact
- Page 401 and 402: Random Number Generation is largely
- Page 403 and 404: Random Number Generation Related Pr
- Page 405 and 406: Factoring and Primality Testing The
- Page 407 and 408: Factoring and Primality Testing Alg
- Page 409 and 410: Arbitrary-Precision Arithmetic If y
- Page 411 and 412: Arbitrary-Precision Arithmetic PARI
- Page 413 and 414: Knapsack Problem Next: Discrete Fou
- Page 415 and 416: Knapsack Problem is a subset of S'
- Page 417 and 418: Discrete Fourier Transform Next: Co
- Page 419 and 420: Discrete Fourier Transform an algor
- Page 421 and 422: Combinatorial Problems Next: Sortin
- Page 423 and 424: Sorting Next: Searching Up: Combina
- Page 425 and 426: Sorting The simplest approach to ex
- Page 427 and 428: Sorting operations, implying an sor
- Page 429 and 430: Searching large performance improve
- Page 431 and 432: Searching Notes: Mehlhorn and Tsaka
- Page 433 and 434: Median and Selection followed by fi
Matrix Multiplication<br />
for j = 1 to z<br />
In two multiplying bandwidth-b matrices, where all nonzero elements of A and B lie within b elements of<br />
the main diagonals, a speedup to O(x b z) is possible, since zero elements will not contribute to the<br />
product.<br />
Asymptotically faster algorithms for matrix multiplication exist, based on clever divide-and-conquer<br />
recurrences. However, these prove difficult to program and require very large matrices to beat the trivial<br />
algorithm. In particular, some empirical results show that Strassen's algorithm is unlikely to beat<br />
the straightforward algorithm for , and it is less numerically stable to boot. Other studies have<br />
been more encouraging, claiming that the crossover point is as low as . Still, I consider it unlikely<br />
that you will speed up any serious application by implementing Strassen's algorithm.<br />
<strong>The</strong>re is a better way to save computation when you are multiplying a chain of more than two matrices<br />
together. Recall that multiplying an matrix by a matrix creates an matrix. Thus<br />
multiplying a chain of matrices from left to right might create large intermediate matrices, each taking a<br />
lot of time to compute. Matrix multiplication is not commutative, but it is associative, so we can<br />
parenthesize the chain in whatever manner we deem best without changing the final product. A<br />
standard dynamic programming algorithm can be used to construct the optimal parenthesization.<br />
Whether it pays to do this optimization will depend upon whether your matrices are large enough or your<br />
chain is multiplied often enough to justify it. Note that we are optimizing over the sizes of the<br />
dimensions in the chain, not the actual matrices themselves. If all your matrices are the same dimensions,<br />
you are out of luck, since no such optimization is possible.<br />
Matrix multiplication has a particularly interesting interpretation in counting the number of paths<br />
between two vertices in a graph. Let A be the adjacency matrix of a graph G, meaning A[i,j] = 1 if<br />
there is an edge between i and j. Otherwise, A[i,j] = 0. Now consider the square of this matrix,<br />
. If , this means that there must be a k such that A[i,k]=A[k,j]=1, so i to k to j is a<br />
path of length 2 in G. More generally, counts the number of paths of length exactly k between i<br />
and j. This count includes nonsimple paths, where vertices are repeated, such as i to k to i.<br />
Implementations: <strong>The</strong> quick and dirty algorithm will be your best bet unless your matrices are<br />
very large. For example, [CLR90] suggests that n>45 before you have a hope of winning. Experimental<br />
results suggest that n > 100 is more realistic [CR76], with Bailey [BLS91] finding a crossover point of<br />
n=128 for Cray systems. Strassen's algorithm is difficult to implement efficiently because of the data<br />
structures required to maintain the array partitions. That said, an implementation of Strassen's<br />
algorithm in Mathematica by Stan Wagon is offered ``without promise of efficiency'' on the algorithm<br />
repository WWW site.<br />
file:///E|/BOOK/BOOK3/NODE138.HTM (2 of 4) [19/1/2003 1:30:18]