Divide and Conquer Part 2: Polynomial Multiplication Algorithm Theory - - PowerPoint PPT Presentation

divide and conquer
SMART_READER_LITE
LIVE PREVIEW

Divide and Conquer Part 2: Polynomial Multiplication Algorithm Theory - - PowerPoint PPT Presentation

Chapter 1 Divide and Conquer Part 2: Polynomial Multiplication Algorithm Theory WS 2012/13 Fabian Kuhn Polynomials Real polynomial in one variable : 1


slide-1
SLIDE 1

Chapter 1

Divide and Conquer

Part 2: Polynomial Multiplication

Algorithm Theory WS 2012/13 Fabian Kuhn

slide-2
SLIDE 2

Algorithm Theory, WS 2012/13 Fabian Kuhn 2

Polynomials

Real polynomial in one variable : … 1 0 Coefficients of : , , … , ∈ Degree of : largest power of in ( in the above case) Example: 33 – 152 18 Set of all real‐valued polynomials in : (polynomial ring)

slide-3
SLIDE 3

Algorithm Theory, WS 2012/13 Fabian Kuhn 3

Operations: Addition

  • Given: Polynomials , ∈ of degree

⋯ ⋯

  • Compute sum :

⋯ ⋯ ⋯

slide-4
SLIDE 4

Algorithm Theory, WS 2012/13 Fabian Kuhn 4

Operations: Multiplication

  • Given: Polynomials , ∈ of degree

⋯ ⋯

  • Product ⋅ :

⋅ ⋯ ⋅ ⋯ ⋯

  • Obtaining : what products of monomials have degree ?

For 0 2:

  • where 0 for .
slide-5
SLIDE 5

Algorithm Theory, WS 2012/13 Fabian Kuhn 5

Operations: Evaluation

  • Given: Polynomial ∈ of degree

  • Horner’s method for evaluation at specific value :

… ⋯

  • Pseudo‐code:

≔ ; ≔ ; while 0 do ≔ 1; ≔ ⋅ end

  • Running time:
slide-6
SLIDE 6

Algorithm Theory, WS 2012/13 Fabian Kuhn 6

Representation of Polynomials

Coefficient representation:

  • Polynomial ∈ of degree is given by its

1 coefficients , … , : ⋯

  • Example:

3 15 18

  • The most typical (and probably most natural) representation of

polynomials

slide-7
SLIDE 7

Algorithm Theory, WS 2012/13 Fabian Kuhn 7

Representation of Polynomials

Product of linear factors:

  • Polynomial ∈ of degree is given by its roots

⋅ ⋅ ⋅ … ⋅

  • Example:

3 2 3

  • Every polynomial has exactly roots ∈ for which 0

– Polynomial is uniquely defined by the roots and

  • We will not use this representation…
slide-8
SLIDE 8

Algorithm Theory, WS 2012/13 Fabian Kuhn 8

Representation of Polynomials

Point‐value representation:

  • Polynomial ∈ of degree is given by

1 point‐value pairs: , , , , … , , where for .

  • Example: The polynomial

3 2 3 is uniquely defined by the four point‐value pairs 0,0 , 1,6 , 2,0 , 3,0 .

slide-9
SLIDE 9

Algorithm Theory, WS 2012/13 Fabian Kuhn 9

Operations: Coefficient Representation

Deg.‐ polynomials ⋯ , ⋯ Addition: ⋯

  • Time:

Multiplication: ⋅ ⋯ , where

  • Naive solution: Need to compute product

for all 0 ,

  • Time:
slide-10
SLIDE 10

Algorithm Theory, WS 2012/13 Fabian Kuhn 10

Operations Point‐Value Representation

Degree‐ polynomials , , … , , , , , … , ,

  • Note: we use the same points , … , for both polynomials

Addition: , , … , ,

  • Time:

Multiplication: ⋅ , ⋅ , … , , ⋅

  • Time:
slide-11
SLIDE 11

Algorithm Theory, WS 2012/13 Fabian Kuhn 11

Faster Multiplication?

  • Multiplication is slow Θ

when using the standard coefficient representation

  • Try divide‐and‐conquer to get a faster algorithm
  • Assume: degree is 1, is even
  • Divide polynomial ⋯ into 2

polynomials of degree ⁄ 1:

⋯ ⋯

  • Similarly: ⋅

slide-12
SLIDE 12

Algorithm Theory, WS 2012/13 Fabian Kuhn 12

Use Divide‐And‐Conquer

  • Divide:

,

  • Multiplication:

⋅ ⋅

  • 4 multiplications of degree

⁄ 1 polynomials: 4 2

  • Leads to Θ like the naive algorithm… (see exercises)
slide-13
SLIDE 13

Algorithm Theory, WS 2012/13 Fabian Kuhn 13

More Clever Recursive Solution

  • Recall that

⋅ ⋅

  • Compute

⋅ :

slide-14
SLIDE 14

Algorithm Theory, WS 2012/13 Fabian Kuhn 14

Karatsuba Algorithm

  • Recursive multiplication:

⋅ ⋅ ⋅

  • Recursively do 3 multiplications of degr.

⁄ 1 ‐polynomials 3 2

  • Gives: .

(see exercises)

slide-15
SLIDE 15

Algorithm Theory, WS 2012/13 Fabian Kuhn 15

Faster Polynomial Multiplication?

Multiplication is fast when using the point‐value representation Idea to compute ⋅ (for polynomials of degree ): , of degree 1, coefficients 2 2 point‐value pairs , and , 2 point‐value pairs ,

  • f degree 2 2, 2 1 coefficients

Evaluation at points , , … , Point‐wise multiplication Interpolation

slide-16
SLIDE 16

Algorithm Theory, WS 2012/13 Fabian Kuhn 16

Point‐Value Representation of ,

  • Select points , , … , to evaluate and in a clever way

Consider the powers of the principle th root of unity:

  • Principle root of unity:
  • 1,
  • Powers of (roots of unity):

1

, , … ,

  • Note:

cos

  • ⋅ sin
slide-17
SLIDE 17

Algorithm Theory, WS 2012/13 Fabian Kuhn 17

Discrete Fourier Transform

  • The values
  • for 0, … , 1 uniquely define a

polynomial of degree . Discrete Fourier Transform (DFT):

  • Assume , … , is the coefficient vector of poly.

⋯ DFT ≔

, , … ,

slide-18
SLIDE 18

Algorithm Theory, WS 2012/13 Fabian Kuhn 18

Example

  • Consider polynomial 3 15 18
  • Choose 4
  • Roots of unity:
slide-19
SLIDE 19

Algorithm Theory, WS 2012/13 Fabian Kuhn 19

Example

  • Consider polynomial 3 15 18
  • 4, roots of unity:

1, , 1,

  • Evaluate at

:

  • ,
  • 1, 1

1,6

  • ,
  • ,

, 15 15

  • ,
  • 1, 1

1, 36

  • ,
  • ,

, 15 15

  • For 3, 15,18,0 :

, , ,

slide-20
SLIDE 20

Algorithm Theory, WS 2012/13 Fabian Kuhn 20

Faster Polynomial Multiplication?

Idea to compute ⋅ (for polynomials of degree ): , of degree 1, coefficients 2 2 point‐value pairs

,

  • and

,

  • 2 point‐value pairs

,

  • f degree 2 2, 2 1 coefficients

Evaluation at points

, , … ,

  • Point‐wise multiplication

Interpolation

slide-21
SLIDE 21

Algorithm Theory, WS 2012/13 Fabian Kuhn 21

Properties of the Roots of Unity

  • Cancellation Lemma:

For all integers 0, 0, and 0, we have:

  • Proof:
slide-22
SLIDE 22

Algorithm Theory, WS 2012/13 Fabian Kuhn 22

Divide‐and‐Conquer Approach

  • Divide of degree 1 ( is even) into 2 polynomials of

degree ⁄ 1 differently than in Karatsuba’s algorithm

(even coeff.) ⋯

(odd coeff.)

slide-23
SLIDE 23

Algorithm Theory, WS 2012/13 Fabian Kuhn 23

Discrete Fourier Transform

Evaluation for 0, … , 1:

  • if 2

⁄ ⋅ ⁄ ⁄

if 2

  • For the coefficient vector of :

DFT

  • , … ,

  • ,

  • , … ,

⁄ ⁄ ⁄

  • , … ,

⁄ ⁄

  • ,

⁄ ⁄

  • , … ,

⁄ ⁄

slide-24
SLIDE 24

Algorithm Theory, WS 2012/13 Fabian Kuhn 24

Example

For the coefficient vector of :

DFT

  • , … ,

  • ,

  • , … ,

⁄ ⁄ ⁄

  • , … ,

⁄ ⁄

  • ,

⁄ ⁄

  • , … ,

⁄ ⁄

4:

  • Need:

,

  • and

,

  • (DFTs of coefficient vectors of and )
slide-25
SLIDE 25

Algorithm Theory, WS 2012/13 Fabian Kuhn 25

Recursive Structure

For simplicity, we abuse notation in the following:

  • Poly. ⋯ with coefficient vector

Let DFT ≔ DFT Recursive structure:

  • For 4:

DFT

  • DFT

⋅ DFT

  • General (assume is even):

DFT

  • DFT/

⋅ DFT/

slide-26
SLIDE 26

Algorithm Theory, WS 2012/13 Fabian Kuhn 26

Computation of

  • Divide‐and‐conquer algorithm for DFT:
  • 1. Divide

1: DFT 1: Divide into (even coeff.) and (odd coeff).

  • 2. Conquer

Solve DFT

⁄ and DFT ⁄ recursively

  • 3. Combine

Compute DFT based on DFT

⁄ and DFT ⁄

slide-27
SLIDE 27

Algorithm Theory, WS 2012/13 Fabian Kuhn 27

Analysis

  • : max. time to compute DFT:

2 2

  • ,

1 1

  • As for mergesort, comparing orders, closest pair of points:

⋅ log

slide-28
SLIDE 28

Algorithm Theory, WS 2012/13 Fabian Kuhn 28

Small Improvement

Polynomial of degree 1:

  • if 2

⁄ ⋅ ⁄ ⁄

if 2

  • if 2

⁄ ⁄ ⋅ ⁄ ⁄

if 2

  • Need to compute

  • and

⋅ ⁄

  • for 0

⁄ .

slide-29
SLIDE 29

Algorithm Theory, WS 2012/13 Fabian Kuhn 29

Example

slide-30
SLIDE 30

Algorithm Theory, WS 2012/13 Fabian Kuhn 30

Fast Fourier Transform (FFT) Algorithm

Algorithm FFT(a)

  • Input: Array of length , where is a power of 2
  • Output: DFT

if 1 then return ; // ≔ FFT , , … , ; ≔ FFT , , … , ; ≔

; ≔ 1;

for 0 to ⁄ 1 do //

  • ≔ ⋅

;

; ⁄ ≔ ;

≔ ⋅ end; return , , … , ;

slide-31
SLIDE 31

Algorithm Theory, WS 2012/13 Fabian Kuhn 31

Example

  • 3 15 18 0, 0,18, 15,3
slide-32
SLIDE 32

Algorithm Theory, WS 2012/13 Fabian Kuhn 32

Faster Polynomial Multiplication?

Idea to compute ⋅ (for polynomials of degree ): , of degree 1, coefficients 2 2 point‐value pairs

,

  • and

,

  • 2 point‐value pairs

,

  • f degree 2 2, 2 1 coefficients

Evaluation at

, , … , using FFT

Point‐wise multiplication Interpolation