Algorithms and Data Structures Strassen s Algorithm. ADS (2017/18) Lecture 4 slide 1

Similar documents
Tutorials. Algorithms and Data Structures Strassen s Algorithm. The Master Theorem for solving recurrences. The Master Theorem (cont d)

The Master Theorem for solving recurrences. Algorithms and Data Structures Strassen s Algorithm. Tutorials. The Master Theorem (cont d)

CMPSCI611: Three Divide-and-Conquer Examples Lecture 2

Matrix Multiplication

The Divide-and-Conquer Design Paradigm

Chapter 4 Divide-and-Conquer

Divide and Conquer. Andreas Klappenecker. [based on slides by Prof. Welch]

Class Note #14. In this class, we studied an algorithm for integer multiplication, which. 2 ) to θ(n

Introduction to Algorithms 6.046J/18.401J/SMA5503

Inf 2B: Sorting, MergeSort and Divide-and-Conquer

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

Fast Convolution; Strassen s Method

Complexity of Matrix Multiplication and Bilinear Problems

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Asymptotic Analysis, recurrences Date: 9/7/17

Divide-and-conquer algorithm

CS 577 Introduction to Algorithms: Strassen s Algorithm and the Master Theorem

Chapter 5. Divide and Conquer CLRS 4.3. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

1 Caveats of Parallel Algorithms

Chapter 5. Divide and Conquer CLRS 4.3. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

1.1 Administrative Stuff

Grade 11/12 Math Circles Fall Nov. 5 Recurrences, Part 2

Dynamic Programming: Matrix chain multiplication (CLRS 15.2)

Dynamic Programming (CLRS )

Computational Complexity. This lecture. Notes. Lecture 02 - Basic Complexity Analysis. Tom Kelsey & Susmit Sarkar. Notes

Divide and Conquer. Arash Rafiey. 27 October, 2016

CSE 613: Parallel Programming. Lectures ( Analyzing Divide-and-Conquer Algorithms )

Chapter 2. Recurrence Relations. Divide and Conquer. Divide and Conquer Strategy. Another Example: Merge Sort. Merge Sort Example. Merge Sort Example

Linear Methods (Math 211) - Lecture 2

CMPS 2200 Fall Divide-and-Conquer. Carola Wenk. Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk

COMP 382: Reasoning about algorithms

Divide and Conquer Algorithms

CSE 613: Parallel Programming. Lecture 8 ( Analyzing Divide-and-Conquer Algorithms )

Ma/CS 6b Class 12: Graphs and Matrices

We could express the left side as a sum of vectors and obtain the Vector Form of a Linear System: a 12 a x n. a m2

Computational Complexity - Pseudocode and Recursions

Divide and Conquer. CSE21 Winter 2017, Day 9 (B00), Day 6 (A00) January 30,

Powers of Tensors and Fast Matrix Multiplication

CSE548, AMS542: Analysis of Algorithms, Fall 2017 Date: October 11. In-Class Midterm. ( 7:05 PM 8:20 PM : 75 Minutes )

Matrices: 2.1 Operations with Matrices

CS100: DISCRETE STRUCTURES. Lecture 3 Matrices Ch 3 Pages:

MATH10212 Linear Algebra B Homework Week 5

University of the Virgin Islands, St. Thomas January 14, 2015 Algorithms and Programming for High Schoolers. Lecture 5

Design and Analysis of Algorithms

Dot Products, Transposes, and Orthogonal Projections

Lecture 9: Submatrices and Some Special Matrices

Outline. 1 Introduction. Merging and MergeSort. 3 Analysis. 4 Reference

Solving recurrences. Frequently showing up when analysing divide&conquer algorithms or, more generally, recursive algorithms.

Lecture 6 & 7. Shuanglin Shao. September 16th and 18th, 2013

Divide and Conquer CPE 349. Theresa Migler-VonDollen

Divide and Conquer. Maximum/minimum. Median finding. CS125 Lecture 4 Fall 2016

FFT: Fast Polynomial Multiplications

I. Approaches to bounding the exponent of matrix multiplication

1111: Linear Algebra I

CS 4424 Matrix multiplication

Methods for solving recurrences

Complexity Theory of Polynomial-Time Problems

Lecture 4: Products of Matrices

COE428 Notes Week 4 (Week of Jan 30, 2017)

A = , A 32 = n ( 1) i +j a i j det(a i j). (1) j=1

Recommended readings: Description of Quicksort in my notes, Ch7 of your CLRS text.

What if the characteristic equation has complex roots?

Introduction to Algorithms

Data Structures and Algorithms CMPSC 465

Analysis of Algorithms I: Asymptotic Notation, Induction, and MergeSort

! Break up problem into several parts. ! Solve each part recursively. ! Combine solutions to sub-problems into overall solution.

Divide-and-Conquer. Reading: CLRS Sections 2.3, 4.1, 4.2, 4.3, 28.2, CSE 6331 Algorithms Steve Lai

282 Math Preview. Chris Brown. September 8, Why This? 2. 2 Logarithms Basic Identities Basic Consequences...

Divide and Conquer: Polynomial Multiplication Version of October 1 / 7, 24201

Algorithmic Game Theory and Applications. Lecture 4: 2-player zero-sum games, and the Minimax Theorem

ICS141: Discrete Mathematics for Computer Science I

CS483 Design and Analysis of Algorithms

Vector, Matrix, and Tensor Derivatives

The divide-and-conquer strategy solves a problem by: 1. Breaking it into subproblems that are themselves smaller instances of the same type of problem

MATH 320, WEEK 7: Matrices, Matrix Operations

CSC236 Week 4. Larry Zhang

Mathematics 13: Lecture 10

cse547, math547 DISCRETE MATHEMATICS Professor Anita Wasilewska

CSI2101-W08- Recurrence Relations

Divide and Conquer Strategy

Asymptotic Analysis and Recurrences

Lecture 10: Powers of Matrices, Difference Equations

Divide-and-conquer: Order Statistics. Curs: Fall 2017

Copyright 2000, Kevin Wayne 1

1300 Linear Algebra and Vector Geometry

22A-2 SUMMER 2014 LECTURE Agenda

Finite Math - J-term Section Systems of Linear Equations in Two Variables Example 1. Solve the system

Chapter 2. Divide-and-conquer. 2.1 Strassen s algorithm

Divide and Conquer algorithms

On the Exponent of the All Pairs Shortest Path Problem

How to find good starting tensors for matrix multiplication

A design paradigm. Divide and conquer: (When) does decomposing a problem into smaller parts help? 09/09/ EECS 3101

Lecture 1b: The Maximum Contiguous Subarray Problem

. =. a i1 x 1 + a i2 x 2 + a in x n = b i. a 11 a 12 a 1n a 21 a 22 a 1n. i1 a i2 a in

Lecture 6: Lies, Inner Product Spaces, and Symmetric Matrices

Divide and Conquer. Chapter Overview. 1.2 Divide and Conquer

Design and Analysis of Algorithms

Determinants of 2 2 Matrices

CS483 Design and Analysis of Algorithms

Math.3336: Discrete Mathematics. Advanced Counting Techniques

Evaluating Determinants by Row Reduction

Transcription:

Algorithms and Data Structures Strassen s Algorithm ADS (2017/18) Lecture 4 slide 1

Tutorials Start in week (week 3) Tutorial allocations are linked from the course webpage http://www.inf.ed.ac.uk/teaching/courses/ads/ ADS (2017/18) Lecture 4 slide 2

ADS (2017/18) Lecture 4 slide 3 The Master Theorem for solving recurrences Theorem Let n 0 N, k N 0 and a, b R with a > 0 and b > 1, and let T : N R satisfy the following recurrence: { Θ(1) if n < n 0, T (n) = a T (n/b) + Θ(n k ) if n n 0. Let c = log b (a); we call c the critical exponent. Then Θ(n c ) if k < c (I), T (n) = Θ(n c lg(n)) if k = c (II), Θ(n k ) if k > c (III). Theorem also holds if we replace a T (n/b) above by a 1 T ( n/b ) + a 2 T ( n/b ) for any a 1, a 2 0 with a 1 + a 2 = a.

The Master Theorem (cont d) We don t have time to prove the Master Theorem in class. You can find the proof in Section 4.6 of [CLRS]. Section 4.4 of [CLRS], 2nd ed. Their version of the M.T. is a bit more general than ours. Consider the following examples: T (n) = 4T (n/2) + n, T (n) = 4T ( n/2 ) + n 2, T (n) = 4T (n/2) + n 3. Could alternatively unfold-and-sum to prove the first and third of these (and to get an estimate for the second). CLASS EXERCISE ADS (2017/18) Lecture 4 slide 4

ADS (2017/18) Lecture 4 slide 5 Matrix Multiplication Recall The product of two (n n)-matrices A = (a ij ) 1 i,j n and B = (b ij ) 1 i,j n is the (n n)-matrix C = AB where C = (c ij ) 1 i,j n with entries n c ij = a ik b kj. k=1 The Matrix Multiplication Problem Input: (n n)-matrices A and B Output: the (n n)-matrix AB

ADS (2017/18) Lecture 4 slide 6 Matrix Multiplication row i c ij = b 1j b2j a i1 a i2 a in b nj column j

ADS (2017/18) Lecture 4 slide 6 Matrix Multiplication row i c ij = b 1j b2j a i1 a i2 a in b nj column j - n multiplications and n additions for each c ij. - there are n 2 different c ij entries.

ADS (2017/18) Lecture 4 slide 7 A straightforward algorithm Algorithm MatMult(A, B) 1. n number of rows of A 2. for i 1 to n do 3. for j 1 to n do 4. c ij 0 5. for k 1 to n do 6. c ij c ij + a ik b kj 7. return C = (c ij ) 1 i,j n Requires Θ(n 3 ) arithmetic operations (additions and multiplications).

A näive divide-and-conquer algorithm Observe If ) ) A = ( A11 A 12 A 21 A 22 and B = ( B11 B 12 B 21 B 22 for (n/2 n/2)-submatrices A ij and B ij then ( A11 B 11 + A 12 B 21 A 11 B 12 + A 12 B 22 ) AB = A 21 B 11 + A 22 B 21 A 21 B 12 + A 22 B 22 note: We are assuming n is a power of 2. ADS (2017/18) Lecture 4 slide 8

ADS (2017/18) Lecture 4 slide 9 A näive divide-and-conquer algorithm row i c ij = A A 11 12 a i1 a i2 a in B b b 1j 2j 11 B12 A A 21 22 B B 21 22 b nj column j

ADS (2017/18) Lecture 4 slide 9 A näive divide-and-conquer algorithm row i c ij = A A 11 12 a i1 a i2 a in B b b 1j 2j 11 B12 A A 21 22 B B 21 22 b nj column j Suppose i n/2 and j > n/2. Then c ij = n/2 n a ik b kj = a ik b kj + k=1 k=1 n k=n/2+1 a ik b kj A 11 B 12 A 12 B 22

ADS (2017/18) Lecture 4 slide 10 A näive divide-and-conquer algorithm (cont d) Assume n is a power of 2. Algorithm D&C-MatMult(A, B) 1. n number of rows of A 2. if n = 1 then return (a 11b 11) 3. else 4. Let A ( ij, B ij (for i, j ) = 1, 2) be (n/2 ( n/2)-submatrices ) s.th. A11 A 12 B11 B 12 A = and B = A 21 A 22 B 21 B 22 5. Recursively compute A 11B 11, A 12B 21, A 11B 12, A 12B 22, A 21B 11, A 22B 21, A 21B 12, A 22B 22 6. Compute C 11 = A 11B 11 + A 12B 21, C 12 = A 11B 12 + A 12B 22, 7. return C 21 = A 21B 11 + A 22B 21, C 22 = A 21B 12 + A 22B 22 ( ) C11 C 12 C 21 C 22

Analysis of D&C-MatMult T (n) is the number of operations done by D&C-MatMult. Lines 1, 2, 3, 4, 7 require Θ(1) arithmetic operations Line 5 requires 8T (n/2) arithmetic operations Line 6 requires 4(n/2) 2 = Θ(n 2 ) arithmetic operations. Remember! Size of matrices is Θ(n 2 ), NOT Θ(n) We get the recurrence T (n) = 8T (n/2) + Θ(n 2 ). Since log 2 (8) = 3, the Master Theorem yields T (n) = Θ(n 3 ). ADS (2017/18) Lecture 4 slide 11

Analysis of D&C-MatMult T (n) is the number of operations done by D&C-MatMult. Lines 1, 2, 3, 4, 7 require Θ(1) arithmetic operations Line 5 requires 8T (n/2) arithmetic operations Line 6 requires 4(n/2) 2 = Θ(n 2 ) arithmetic operations. Remember! Size of matrices is Θ(n 2 ), NOT Θ(n) We get the recurrence T (n) = 8T (n/2) + Θ(n 2 ). Since log 2 (8) = 3, the Master Theorem yields T (n) = Θ(n 3 ). (No improvement over MatMult... why? CLASS?...) ADS (2017/18) Lecture 4 slide 11

ADS (2017/18) Lecture 4 slide 12 Strassen s algorithm (1969) Assume n is a power of 2. Let ( ) ( ) A11 A 12 B11 B 12 A = and B =. A 21 A 22 B 21 B 22 We want to compute ( ) A11 B 11 + A 12 B 21 A 11 B 12 + A 12 B 22 AB = A 21 B 11 + A 22 B 21 A 21 B 12 + A 22 B 22 ( ) C11 C 12 =. C 21 C 22 Strassen s algorithm uses a trick in applying Divide-and-Conquer.

Strassen s algorithm (cont d) Let P 1 = (A 11 + A 22 )(B 11 + B 22 ) P 2 = (A 21 + A 22 )B 11 P 3 = A 11 (B 12 B 22 ) P 4 = A 22 ( B 11 + B 21 ) ( ) P 5 = (A 11 + A 12 )B 22 P 6 = ( A 11 + A 21 )(B 11 + B 12 ) P 7 = (A 12 A 22 )(B 21 + B 22 ) ADS (2017/18) Lecture 4 slide 13

Strassen s algorithm (cont d) Let P 1 = (A 11 + A 22 )(B 11 + B 22 ) P 2 = (A 21 + A 22 )B 11 P 3 = A 11 (B 12 B 22 ) P 4 = A 22 ( B 11 + B 21 ) ( ) P 5 = (A 11 + A 12 )B 22 P 6 = ( A 11 + A 21 )(B 11 + B 12 ) P 7 = (A 12 A 22 )(B 21 + B 22 ) Then C 11 = P 1 + P 4 P 5 + P 7 C 12 = P 3 + P 5 C 21 = P 2 + P 4 C 22 = P 1 + P 3 P 2 + P 6 ( ) ADS (2017/18) Lecture 4 slide 13

Checking Strassen s algorithm - C 11 We will check the equation for C 11 is correct. Strassen s algorithm computes C 11 = P1 + P4 P5 + P7. We have P1 = (A11 + A22)(B11 + B22) = A11B11 + A11B22 + A22B11 + A22B22. P4 = A22( B11 + B21) = A22B21 A22B11. P5 = (A11 + A12)B22 = A11B22 + A12B22. P7 = (A12 A22)(B21 + B22) = A12B21 + A12B22 A22B21 A22B22. ADS (2017/18) Lecture 4 slide 14

Checking Strassen s algorithm - C 11 We will check the equation for C 11 is correct. Strassen s algorithm computes C 11 = P1 + P4 P5 + P7. We have P1 = (A11 + A22)(B11 + B22) = A11B11 + A11B22 + A22B11 + A22B22. P4 = A22( B11 + B21) = A22B21 A22B11. P5 = (A11 + A12)B22 = A11B22 + A12B22. P7 = (A12 A22)(B21 + B22) = A12B21 + A12B22 A22B21 A22B22. Then P1 + P4 = A11B11 + A11B22 + A22B22 + A22B21. ADS (2017/18) Lecture 4 slide 14

Checking Strassen s algorithm - C 11 We will check the equation for C 11 is correct. Strassen s algorithm computes C 11 = P1 + P4 P5 + P7. We have P1 = (A11 + A22)(B11 + B22) = A11B11 + A11B22 + A22B11 + A22B22. P4 = A22( B11 + B21) = A22B21 A22B11. P5 = (A11 + A12)B22 = A11B22 + A12B22. P7 = (A12 A22)(B21 + B22) = A12B21 + A12B22 A22B21 A22B22. Then P1 + P4 = A11B11 + A11B22 + A22B22 + A22B21. Then P1 + P4 P5 = A11B11 + A22B22 + A22B21 A12B22. ADS (2017/18) Lecture 4 slide 14

Checking Strassen s algorithm - C 11 We will check the equation for C 11 is correct. Strassen s algorithm computes C 11 = P1 + P4 P5 + P7. We have P1 = (A11 + A22)(B11 + B22) = A11B11 + A11B22 + A22B11 + A22B22. P4 = A22( B11 + B21) = A22B21 A22B11. P5 = (A11 + A12)B22 = A11B22 + A12B22. P7 = (A12 A22)(B21 + B22) = A12B21 + A12B22 A22B21 A22B22. Then P1 + P4 = A11B11 + A11B22 + A22B22 + A22B21. Then P1 + P4 P5 = A11B11 + A22B22 + A22B21 A12B22. Then P1 + P4 P5 + P7 = A11B11 + A12B21, which is C11. ADS (2017/18) Lecture 4 slide 14

Checking Strassen s algorithm - C 11 We will check the equation for C 11 is correct. Strassen s algorithm computes C 11 = P1 + P4 P5 + P7. We have P1 = (A11 + A22)(B11 + B22) = A11B11 + A11B22 + A22B11 + A22B22. P4 = A22( B11 + B21) = A22B21 A22B11. P5 = (A11 + A12)B22 = A11B22 + A12B22. P7 = (A12 A22)(B21 + B22) = A12B21 + A12B22 A22B21 A22B22. Then P1 + P4 = A11B11 + A11B22 + A22B22 + A22B21. Then P1 + P4 P5 = A11B11 + A22B22 + A22B21 A12B22. Then P1 + P4 P5 + P7 = A11B11 + A12B21, which is C11. homework: check other 3 equations. ADS (2017/18) Lecture 4 slide 14

ADS (2017/18) Lecture 4 slide 15 Strassen s algorithm (cont d) Crucial Observation Only 7 multiplications of (n/2 n/2)-matrices are needed to compute AB. Algorithm Strassen(A, B) 1. n number of rows of A 2. if n = 1 then return (a 11 b 11 ) 3. else 4. Determine A ij and B ij for i, j = 1, 2 (as before) 5. Compute P 1,..., P 7 as in ( ) 6. Compute C 11, C 12, C 21, C 22 as in ( ) ( ) C11 C 12 7. return C 21 C 22

Analysis of Strassen s algorithm Let T (n) be the number of arithmetic operations performed by Strassen. Lines 1 4 and 7 require Θ(1) arithmetic operations Line 5 requires 7T (n/2) + Θ(n 2 ) arithmetic operations Line 6 requires Θ(n 2 ) arithmetic operations. remember. We get the recurrence T (n) = 7T (n/2) + Θ(n 2 ). Since log 2 (7) 2.807 > 2, the Master Theorem yields T (n) = Θ(n log 2 (7) ). ADS (2017/18) Lecture 4 slide 16

ADS (2017/18) Lecture 4 slide 17 Breakthroughs on matrix multiplication Coppersmith & Winograd (1987) came up with an improved algorithm with running time of Θ(n 2.376 ).... many years of silence... Then in his 2010 PhD thesis, Andrew Stothers from the School of Maths, at the University of Edinburgh got an algorithm with Θ(n c ) for c < 2.3737... Coppersmith/Winograd not optimal. But Stothers didn t publish. In December 2011, Virginia Vassilevska Williams of Stanford, came up with a Θ(n c ) algoithm, for c < 2.3727 (partly, but not only, making use of some of Stothers ideas)

Remarks on Matrix Multiplication In practice, the school MatMult algorithm tends to outperform Strassen s algorithm, unless the matrices are huge. The best known lower bound for matrix multiplication is Ω(n 2 ). This is a trivial lower bound (need to look at all entries of each matrix). Amazingly, Ω(n 2 ) is believed to be the truth! Open problem: Can we find a O(n 2+o(1) )-algorithm for Matrix Multiplication of n n matrices? ADS (2017/18) Lecture 4 slide 18

Reading Assignment [CLRS] (3rd ed) Section 4.5 The Master method for solving recurrences (Section 4.3 Using the Master method of [CLRS], 2nd ed) [CLRS] (3rd ed) Section 4.2 (Section 28.2 of [CLRS], 2nd ed) Problems 1. Exercise 4.5-2 of [CLRS] (3rd ed) Exercise 4.3-2 of [CLRS], 2nd ed. 2. Exercise 4.2-1 of [CLRS], 3rd ed. Exercise 28.2-1 [CLRS], 2nd ed. 3. Week 3 tutorial sheet :-) ADS (2017/18) Lecture 4 slide 19