Chapter 2: Fundamentals of the Analysis of Algorithm Efficiency

Download Report

Transcript Chapter 2: Fundamentals of the Analysis of Algorithm Efficiency

Notes on the analysis of multiplication algorithms.. Dr. M. Sakalli, Marmara University

Integer Multiplication MIT notes and wikipedia

Example.. Classic High school math.. Let

g

= A|B and

h

= C|D where A,B,C and D are n/2 bit integers Simple Method:

gh

= (2 n/2 A+B)(2 n/2 C+D) same as given above. 4 multiplication routines. XY = (2 n )AC+2 n/2 (AD+BC) + BD and carriages c.

Long multiplication

:

r

j =

c

+ Σ k = i-j

g

j

h

k

Running Time Recurrence T(n) < 4T(n/2) + 100n, 100 multiplications.??, In-place??..

T(n) =

q

(n 2 ) Provided that neither c nor the total sum exceed log space, a simple inductive argument shows that the carry

c

indeed, and the total sum for

r

i

can never exceed

n

and 2

n

: <

n

respectively. Space efficiency: S(n)=O(loglog(N)),

(loglog(N)). N=

gh

.

M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes 3-1

Integer Multiplication MIT notes and wikipedia

Pseudo code: Log space multiplication algorithm , multiply( g [0..n-1], h [0..n-1]) // Arrays representing to the binary representations x ← 0 for i= 0 : 2n-1 for j= 0 : i k ← i - j x ← x + ( g [j] × end h [k]) r[i] ← x mod 2 x ← floor(x/2) //I think this is carriage return. Last bit if 1.. end Lattice method,

Muhammad ibn Musa al-Khwarizmi. Gauss's complex multiplication algorithm.

M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes 3-2

Integer Multiplication MIT notes and wikipedia

Karatsuba’s algorithm

: Polynomial extensions..

g = g 1 10

n/2

+ g 2 h = h 1 10

n/2

+ h 2 g h = g 1 h 1 10

n

+ (g 1 h 2 + g 2 h 1 )10

n/2

+ g 2 h 2 (g 1 h 2 + g 2 h 1 ) = (g 1 + h 1 )(g 2 + h 2 ) - (g 2 h 2 + g 1 h 1 ), f(n) = 4sums+1 more final sum = 5n, n>2, suppose it is a constant 100n, and some carriages.

XY

=

(2 n/2 +2 n )AC+2 n/2 (A-B)(C-D) + (2 n/2 +1) BD

A(n) = 3A(n/2)+5n, A(n)

< O(n lg 3 )

≈ q (n 1.6

) Base value 7, when n<2, M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes 3-3

Karatsuba (g, h : n-digit integer; n : integer) // return (2n)-digit integer is a, b, c, d; // (n/2)-digit integer U, V, W; //n-digit integer; begin if n == 1 then return

g(0)*h(0); ????

else g1

g2

g(n-1) ... g(n/2); g(n/2-1) ... g(0); h1

h2

h(n-1) ... h(n/2); h(n/2-1) ... h(0); U

V

W

Karatsuba ( g1, h1, n/2 ); Karatsuba ( g2, h2, n/2 ); Karatsuba ( g1+g2, h1+h2, n/2 ); return

U*10 n + (W-U-V)*10^n/2 + V; end if; end Karatsuba; FFT and Fast Matrix multiplication.

M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes 3-4

Quarter square multiplier 1980, Everett L. Johnson:

gh

= {(

g

+

h

) 2 - (

g

-

h

) 2 }/4= {(

g

2 + 2

hg

+

h

2 ) - (

g

2 - 2

hg

+

h

2 ) }/4 Think hardware implementation, with a lookup table (converter), the difficulty is that summation of the two numbers each 8bits, will require at least 9 bits, when squared, 18 bits wide.. But if divided by 2 before squared, (discarding remainder when n is odd) .

Table lookup

from 0 to .. 9+9, from 0 … to 81. O(3n), working S(n) =

(n).

i.e. 7 by 3, observe that the sum and difference are 10 and 4 respectively. Looking both of those values up on the table yields 25 and 4, the difference of which is 21.

M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes 3-5

Russian (Egyptian) Peasant’s binary multiplication Shift and add.. In-place algorithm, may be implemented and 2n space.. Try complex examples.

11 3, in binary 5 6, 1011 101 110 11 110 011 2 12, 1 24, 10 1100 1 11000 11000.. = 100001 T(n) =

q

(n)+O(n 2 ), think about this?.. Why.. S(n) =

q

(loglog(n)) which is the carriage. If invertible? Division potential question.

M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes 3-6



Matrix multiplication

,

8 multiplications, O(n 3 )  

A

11

A

21

A

12

A

22    

B

11

B

21

B

12

B

22    

C

11

C

21

C

12

C

22  

C C

11 12  

A A

11 11

B B

11 12  

C

21 

A

21

B

11  Pseudo code for MM. MM(A, B) for i ← 1 : N for j ← 1 : N C(i, j) ← 0; for k ← 1 : N end, end, end Time complexity of this algo is Can we do better using divide and conquer?.. Subdividing matrices into four sub-matrices. T(n) = b, n  2, T(n) = 8T(n/2) + c

n 2 n



3

C

22 , n>2, which has T(n) = O(n??) 

A

21

B

12  multiplications and additions.

A

12

B

21

A A A

12 22 22

B B B

22 21 22 3-7 M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes



Strassen’s Algorithm

P

1

P

2   

A

11 

A

21  

A

22  

B

11

A

22 

B

11

P

3

P

4  

A

11 

B

12 

A

22 

B

21 

B

22 

B

11  

B

22 

P

5

P

6

P

7    

A

11  

A

21  

A

12 

A

12 

B

22

A

11  

B

11

A

22  

B

21  

B

12 

B

22 

C C

11 12

C

21

C

22    

P

1 

P

3 

P

4

P

5 

P

2 

P

4

P

1 

P

3 

P

5

P

2  

P

7

P

6 T(n) = b, n  2,  T(n) = 7T(n/2) + (7m+18s) n 2  , n>2, which has T(n) = O(n 2.81

)

7

n 2

(1/4+1/16+…) Strassen-Winograd: 7 multiplies, 15 additions Coppersmith-Winograd, O(n 2.376

) (not easily implementable) In practice faster (not large hidden constants) for relatively smaller n~64, and stable but demonstrated that for some matrices (Strassen and Strassen-Winograd) are too unstable.

M, Sakalli, CS246 Design & Analysis of Algorithms, Lecture Notes 3-8