Docsity

Divide and Conquer Algorithms: Integer and Matrix Multiplication, Study notes of Computer Science

Toggle sidebar
bg1
1
10/1/2007
S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne
Sofya Raskhodnikova
Algorithm Design and Analysis
L
ECTURE 15
Divide and Conquer
•Integer Multiplication
•Karatsuba’s algorithm
•Matrix Multiplication
•Strassen’s algorithm
10/1/2007
S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne
Review: use asymptotic notation
•The statement “my alogirhtm takes time 2n” is
not accurate.
– Do you mean 2nseconds? 2nminutes?
– The exact running time depends on the processor!
•Use asymptotic notation: “my alogirhtm takes
time Θ(n)”. It means that the running time is
proportional to n.
•The same applies to space complexity.
10/1/2007
S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne
Last time: order statistics
•Deterministic O(n) time algorithm
•There is a randomized algorithm that runs
in expected linear time.
•The randomized algorithm is far more
practical.
Exercise: Why not divide into groups of 3?
10/1/2007
S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne
Multiplying large integers
•Given n-bit integers a, b (in binary), compute
c=ab
•Naive (grade-school) algorithm:
– Write a,b in binary
– Compute nintermediate
products
– Do nadditions
– Total work: Θ(n
2
)
a
n-1
a
n-2
… a
0
b
n-1
b
n-2
… b
0
nbits
nbits
nbits
2n bit output
…
0
0 0 0
10/1/2007
S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne
Multiplying large integers
•Divide and Conquer (Attempt #1):
– Write a= A
1
2
n
/2
+ A
0
b= B
1
2
n
/2
+ B
0
– We want ab = A
1
B
1
2
n
+(A
1
B
0
+ B
1
A
0
) 2
n
/2
+A
0
B
0
– Multiply n/2 –bit integers recursively
–T(n) = 4T(n/2) + Θ(n)
– Alas! this is still Θ(n
2
)(Master Theorem, Case 1)
10/1/2007
S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne
•Divide and Conquer (Attempt #1):
– Write a= A
1
2
n
/2
+ A
0
b= B
1
2
n
/2
+ B
0
– We want ab = A
1
B
1
2
n
+(A
1
B
0
+ B
1
A
0
) 2
n
/2
+A
0
B
0
– Multiply n/2 –bit integers recursively
–T(n) = 4T(n/2) + Θ(n)
– Alas! this is still Θ(n
2
).
(Exercise: write out the recursion tree.)
Multiplying large integers
– Karatsuba’s idea:
(A
0
+A
1
) (B
0
+ B
1
)= A
0
B
0
+ A
1
B
1
+ (A
0
B
1
+ B
1
A
0
)
– We can get away with 3 multiplications! (in yellow)
x
= A
1
B
1
y
= A
0
B
0
z
= (A
0
+A
1
)(B
0
+B
1
)
– Now we use ab = A
1
B
1
2
n
+(A
1
B
0
+ B
1
A
0
)2
n
/2
+ A
0
B
0
=
x
2
n
+ (z–x–y)
2
n
/2
+
y
pf3

Partial preview of the text

Download Divide and Conquer Algorithms: Integer and Matrix Multiplication and more Study notes Computer Science in PDF only on Docsity!

10/1/2007 (^) S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne

Sofya Raskhodnikova

Algorithm Design and Analysis

LECTURE 15

Divide and Conquer

  • Integer Multiplication
    • Karatsuba’s algorithm
  • Matrix Multiplication
    • Strassen’s algorithm

10/1/2007 (^) S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne

Review: use asymptotic notation

  • The statement “my alogirhtm takes time 2 n ” is not accurate. - Do you mean 2 n seconds? 2 n minutes? - The exact running time depends on the processor!
  • Use asymptotic notation: “my alogirhtm takes time Θ( n )”. It means that the running time is proportional to n.
  • The same applies to space complexity.

10/1/2007 (^) S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne

Last time: order statistics

  • Deterministic O(n) time algorithm
  • There is a randomized algorithm that runs in expected linear time.
  • The randomized algorithm is far more practical.

Exercise: Why not divide into groups of 3? 10/1/2007 (^) S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne

Multiplying large integers

  • Given n -bit integers a, b (in binary), compute c=ab
  • Naive (grade-school) algorithm:
    • Write a,b in binary
    • Compute n intermediate products
    • Do n additions
    • Total work: Θ( n^2 )

an-1 an-2 … a 0  bn-1 bn-2 … b 0 n bits n bits

n bits 2n bit output

0

0 0  0

10/1/2007 (^) S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne

Multiplying large integers

  • Divide and Conquer (Attempt #1):
    • Write a = A 1 2 n /2^ + A 0 b = B 1 2 n /2^ + B 0
    • We want ab = A 1 B 1 2 n^ + ( A 1 B 0 + B 1 A 0 ) 2 n /2^ + A 0 B 0
    • Multiply n /2 –bit integers recursively
    • T ( n ) = 4 T ( n /2) + Θ( n )
    • Alas! this is still Θ( n^2 ) (Master Theorem, Case 1)

10/1/2007 (^) S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne

  • Divide and Conquer (Attempt #1):
    • Write a = A 1 2 n /2^ + A 0 b = B 1 2 n /2^ + B 0
    • We want ab = A 1 B 1 2 n^ + ( A 1 B 0 + B 1 A 0 ) 2 n /2^ + A 0 B 0
    • Multiply n /2 –bit integers recursively
    • T ( n ) = 4 T ( n /2) + Θ( n )
    • Alas! this is still Θ( n^2 ). (Exercise: write out the recursion tree.)

Multiplying large integers

  • Karatsuba’s idea: ( A 0 + A 1 ) ( B 0 + B 1 ) = A 0 B 0 + A 1 B 1 + ( A 0 B 1 + B 1 A 0 )
  • We can get away with 3 multiplications! (in yellow) x = A 1 B 1 y = A 0 B 0 z = ( A 0 + A 1 )( B 0 + B 1 )
  • Now we use ab = A 1 B 1 2 n^ + ( A 1 B 0 + B 1 A 0 ) 2 n /2^ + A 0 B 0 = x 2 n^ + ( z–x–y ) 2 n /2^ + y

10/1/2007 (^) S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne

Multiplying large integers

MULTIPLY ( n , a, b )  a and b are n -bit integers  Assume n is a power of 2 for simplicity

1. If n  2 then use grade-school algorithm else 2. A 1  a div 2 n /2^ ; B 1  b div 2 n /2^ ; 3. A 0  a mod 2 n /2^ ; B 0  b mod 2 n /^. 4. x  MULTIPLY( n /2 , A 1 , B 1 ) 5. y  MULTIPLY( n /2 , A 0 , B 0 ) 6. z  MULTIPLY( n /2 , A 1 + A 0 , B 1 + B 0 )

  1. Output x 2 n^ + ( z–x–y )2 n /2^ + y 10/1/2007 (^) S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne

Multiplying large integers

  • The resulting recurrence T ( n ) = 3 T ( n /2) + Θ( n )
  • Master Theorem case 1: T ( n ) = Θ ( n log^23 ) = Θ( n 1.59…)
  • Note: There is a Θ( n log n ) algorithm for multiplication (more on it later in the course).

10/1/2007 (^) S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne

Matrix multiplication























 ⋅ 















































n n nn

n

n

n n nn

n

n

n n nn

n

n

b b b

b b b

b b b

a a a

a a a

a a a

c c c

c c c

c c c



   







   







   





1 2

21 22 2

11 12 1

1 2

21 22 2

11 12 1

1 2

21 22 2

11 12 1

=

n

k

cij aik b kj 1

Input: A = [ aij ], B = [ bij ]. Output: C = [ cij ] = A× B. i , j = 1, 2,… , n.

10/1/2007 (^) S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne

Standard algorithm

for i ← 1 to n do for j ← 1 to n do cij ← 0 for k ← 1 to n do cij ← cij + aik× bkj

Running time = Θ( n^3 )

10/1/2007 (^) S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne

Divide-and-conquer algorithm

n  n matrix = 2 2 matrix of ( n /2)( n /2) submatrices:

IDEA:

^ ⋅

^ =

g h

e f c d

a b t u

r s

C = A ×^ B

r = ae + bg s = af + bh t = ce + dh u = cf + dg

8 mults of ( n /2) ( n /2) submatrices 4 adds of ( n /2) ( n /2) submatrices

^

recursive

10/1/2007 (^) S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith, K. Wayne

Analysis of D&C algorithm

n log ba^ = n log^28 = n^3  CASE 1  T ( n ) = Θ( n^3 ).

No better than the ordinary algorithm.

# submatrices submatrix size

work adding submatrices

T ( n ) = 8 T ( n /2) + Θ( n^2 )