12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

548 Chap. 16 Patterns of <strong>Algorithm</strong>s16.3.3 Matrix MultiplicationThe st<strong>and</strong>ard algorithm for multiplying two n × n matrices requires Θ(n 3 ) time.It is possible <strong>to</strong> do better than this by rearranging <strong>and</strong> grouping the multiplicationsin various ways. One example of this is known as Strassen’s matrix multiplicationalgorithm.For simplicity, we will assume that n is a power of two. In the following, A<strong>and</strong> B are n × n arrays, while A ij <strong>and</strong> B ij refer <strong>to</strong> arrays of size n/2 × n/2. Usingthis notation, we can think of matrix multiplication using divide <strong>and</strong> conquer in thefollowing way:[ ][ ] [ ]A11 A 12 B11 B 12 A11 B= 11 + A 12 B 21 A 11 B 12 + A 12 B 22.A 21 A 22 B 21 B 22 A 21 B 11 + A 22 B 21 A 21 B 12 + A 22 B 22Of course, each of the multiplications <strong>and</strong> additions on the right side of thisequation are recursive calls on arrays of half size, <strong>and</strong> additions of arrays of halfsize, respectively. The recurrence relation for this algorithm isT (n) = 8T (n/2) + 4(n/2) 2 = Θ(n 3 ).This closed form solution can easily be obtained by applying the Master Theorem14.1.Strassen’s algorithm carefully rearranges the way that the various terms aremultiplied <strong>and</strong> added <strong>to</strong>gether. It does so in a particular order, as expressed by thefollowing equation:[ ][ ] [ ]A11 A 12 B11 B 12 s1 + s=2 − s 4 + s 6 s 4 + s 5.A 21 A 22 B 21 B 22 s 6 + s 7 s 2 − s 3 + s 5 − s 7In other words, the result of the multiplication for an n × n array is obtained bya different series of matrix multiplications <strong>and</strong> additions for n/2 × n/2 arrays.Multiplications between subarrays also use Strassen’s algorithm, <strong>and</strong> the additionof two subarrays requires Θ(n 2 ) time. The subfac<strong>to</strong>rs are defined as follows:s 1 = (A 12 − A 22 ) · (B 21 + B 22 )s 2 = (A 11 + A 22 ) · (B 11 + B 22 )s 3 = (A 11 − A 21 ) · (B 11 + B 12 )s 4 = (A 11 + A 12 ) · B 22s 5 = A 11 · (B 12 − B 22 )s 6 = A 22 · (B 21 − B 11 )s 7 = (A 21 + A 22 ) · B 11 .

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!