Lab 2 Monday September 10 Comp Sci 531
Lab 2 Monday, September 10 Comp. Sci 531, Fall 2018
Outline • Review Strassen’s Algorithm • Implementing Strassen’s Algorithm 2
Review Strassen’s Algorithm • Divide and Conquer at a High Level: • • Check for your base case. Divide your problem into multiple identical subproblems. Recursively solve each subproblem. Merge the solutions to your subproblems.
Review Strassen’s Algorithm • Recall the matrix multiplication problem: we have two n by n matrices X and Y, and we want to compute M = XY M = X Y
Review Strassen’s Algorithm • Break each matrix up into four (n/2) by (n/2) sub-matrices as follows: MA MB = MC MD XA XB YA YB XC XD YC YD • Note • • M A = X A Y A + XB Y C M B = X A Y B + XB Y D M C = X CY A + XDY C M D = X CY B + XDY D There are 8 recursive subproblems to solve!
Review Strassen’s Algorithm • Yields the recurrence T(n) = 8 T(n/2) + O(n 2). So T(n) = O(n 3). No better than the iterative algorithm! • Strassen’s insight: The run time is dominated by the branching factor of 8. What if we could reduce that? Let: S 1 = (XB - XD) (YC + YD) S 2 = (XA + XD) (YA + YD) S 3 = (XA - XC) (YA + YB) S 4 = (XA + XB) (YD) S 5 = (XA) (YB - YD) S 6 = (XD) (YC - YB) S 7 = (XC + XD) (YA) M A = S 1 + S 2 – S 4 + S 6 M B = S 4 + S 5 M C = S 6 + S 7 M D = S 2 + S 3 + S 5 – S 7
Review Strassen’s Algorithm • We went from 8 matrix multiplications (recursive calls) and 4 matrix additions (merge steps) to 7 matrix multiplications and 18 matrix additions. • T(n) = 7 T(n/2) + O(n 2). So T(n) = O(nlg(7)) ~ O(n 2. 81). • Does this matter? Let’s find out!
Outline • Review Strassen’s Algorithm • Implementing Strassen’s Algorithm 8
Implementing Strassen’s Algorithm • Break into groups of 3 or 4. • Code up 3 matrix multiplication algorithms: • Iterative algorithm by definition • Naïve recursive algorithm • Strassen’s recursive algorithm • To test, generate random 128 x 128, 256 x 256, 512 x 512, and 1024 x 1024 matrices (in whatever way is convenient) • Time all of your algorithms, and try to explain your results. • (Tip – you may be able to improve your recursive algorithms by using the iterative algorithm once you get to small matrices, maybe 8 x 8 or 16 x 16).
Conclusion • Many recursive divide and conquer algorithms can be sped up if you can reduce the number of recursive calls, maybe at the expense of a larger merge step. • (But this improvement might not be large until you work with larger problem sizes) • There are tricks that matter in practice but not in theory. Examples: • In many languages, basic operations like matrix multiplication, summing vectors, etc. , are heavily optimized, and you shouldn’t reinvent the wheel (outside of this exercise). • Combining recursive and iterative methods rather than recursing all the way to the trivial base case often helps.
- Slides: 10