Divide-and-conquer algorithms

112
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca [email protected] © 2006-2013 by Douglas Wilhelm Harder. Some rights reserved. Divide-and-conquer algorithms

description

Divide-and-conquer algorithms. Divide-and-conquer algorithms. We have seen four divide-and-conquer algorithms: Binary search Depth-first tree traversals Merge sort Quick sort The steps are: A larger problem is broken up into smaller problems The smaller problems are recursively - PowerPoint PPT Presentation

Transcript of Divide-and-conquer algorithms

Page 1: Divide-and-conquer algorithms

ECE 250 Algorithms and Data Structures

Douglas Wilhelm Harder, M.Math. LELDepartment of Electrical and Computer EngineeringUniversity of WaterlooWaterloo, Ontario, Canada

[email protected]

© 2006-2013 by Douglas Wilhelm Harder. Some rights reserved.

Divide-and-conquer algorithms

Page 2: Divide-and-conquer algorithms

2Divide-and-conquer algorithms

Divide-and-conquer algorithms

We have seen four divide-and-conquer algorithms:– Binary search– Depth-first tree traversals– Merge sort– Quick sort

The steps are:– A larger problem is broken up into smaller problems– The smaller problems are recursively– The results are combined together again into a solution

Page 3: Divide-and-conquer algorithms

3Divide-and-conquer algorithms

Divide-and-conquer algorithms

For example, merge sort:– Divide a list of size n into b = 2 sub-lists of size n/2 entries– Each sub-list is sorted recursively– The two sorted lists are merged into a single sorted list

Page 4: Divide-and-conquer algorithms

4Divide-and-conquer algorithms

Divide-and-conquer algorithms

More formally, we will consider only those algorithms which:– Divide a problem into b sub-problems, each approximately of size n/b

• Up to now, b = 2– Solve a ≥ 1 of those sub-problems recursively

• Merge sort and tree traversals solved a = 2 of them• Binary search solves a = 1 of them

– Combine the solutions to the sub-problems to get a solution to the overall problem

Page 5: Divide-and-conquer algorithms

5Divide-and-conquer algorithms

Divide-and-conquer algorithms

With the three problems we have already looked at we have looked at two possible cases for b = 2:

Merge sort b = 2 a = 2Depth-first traversal b = 2 a = 2Binary search b = 2 a = 1

Problem: the first two have different run times:Merge sort Q(n ln(n) )Depth-first traversal Q(n)

Page 6: Divide-and-conquer algorithms

6Divide-and-conquer algorithms

Divide-and-conquer algorithms

Thus, just using a divide-and-conquer algorithm does not solely determine the run time

We must also consider– The effort required to divide the problem into two sub-problems– The effort required to combine the two solutions to the sub-problems

Page 7: Divide-and-conquer algorithms

7Divide-and-conquer algorithms

Divide-and-conquer algorithms

For merge sort:– Division is quick (find the middle): Q(1)– Merging the two sorted lists into a single list is a Q(n) problem

For a depth-first tree traversal:– Division is also quick: Q(1)– A return-from-function is preformed at the end which is Q(1)

For quick sort (assuming division into two):– Dividing is slow: Q(n)– Once both sub-problems are sorted, we are finished: Q(1)

Page 8: Divide-and-conquer algorithms

8Divide-and-conquer algorithms

Divide-and-conquer algorithms

Thus, we are able to write the expression as follows:– Binary search:

Q(ln(n))

– Depth-first traversal:Q(n)

– Merge/quick sort:Q(n ln(n))

In general, we will assume the work done combined work is of the form O(nk)

1)1(

2T

11)T( nn

nn Θ

1)1(

2T2

11)T( nn

nn Θ

1)(

2T2

11)T( nnn

nn Θ

Page 9: Divide-and-conquer algorithms

9Divide-and-conquer algorithms

Divide-and-conquer algorithms

Thus, for a general divide-and-conquer algorithm which:– Divides the problem into b sub-problems– Recursively solves a of those sub-problems– Requires O(nk) work at each step requires

has a run time

Note: we assume a problem of size n = 1 is solved...

1T

11)T( nn

bna

nn kO

Page 10: Divide-and-conquer algorithms

10Divide-and-conquer algorithms

Divide-and-conquer algorithms

Before we solve the general case, let us look at some more complex examples:– Searching an ordered matrix– Integer multiplication (Karatsuba algorithm)– Matrix multiplication

Page 11: Divide-and-conquer algorithms

11Divide-and-conquer algorithms

Searching an ordered matrix

Consider an n × n matrix where each row and column is linearly ordered; for example:– How can we determine if 19 is in the matrix?

Page 12: Divide-and-conquer algorithms

12Divide-and-conquer algorithms

Searching an ordered matrix

Consider the following search for 19:– Search across until ai,j + 1 > 19– Alternate between

• Searching down until ai,j > 19• Searching back until ai,j < 19

This requires us to check at most 3n entries: O(n)

Page 13: Divide-and-conquer algorithms

13Divide-and-conquer algorithms

Searching an ordered matrix

Can we do better than O(n)?Logically, no: any number could appear in up to n positions, each of which must be checked– Never-the-less: let’s generalize checking the middle entry

Page 14: Divide-and-conquer algorithms

14Divide-and-conquer algorithms

Searching an ordered matrix

17 < 19, and therefore, we can only exclude the top-left sub-matrix:

Page 15: Divide-and-conquer algorithms

15Divide-and-conquer algorithms

Searching an ordered matrix

Thus, we must recursively search three of the four sub-matrices– Each sub-matrix is approximately n/2 × n/2

Page 16: Divide-and-conquer algorithms

16Divide-and-conquer algorithms

Searching an ordered matrix

If the number we are searching for was less than the middle element, e.g., 9, we would have to search three different squares

Page 17: Divide-and-conquer algorithms

17Divide-and-conquer algorithms

Searching an ordered matrix

Thus, the recurrence relation must be

because– T(n) is the time to search a matrix of size n × n– The matrix is divided into 4 sub-matrices of size n/2 × n/2– Search 3 of those sub-matrices– At each step, we only need compare the middle element: Q(1)

1)1(

2T3

11)T( nn

nn Θ

Page 18: Divide-and-conquer algorithms

18Divide-and-conquer algorithms

Searching an ordered matrix

We can solve the recurrence relationship

using Maple:> rsolve( {T(n) = 3*T(n/2) + 1, T(1) = 1}, T(n) );

> evalf( log[2]( 3 ) );

1)1(

2T3

11)T( nn

nn Θ

32

n( )log2 3 1

2

1.584962501

Page 19: Divide-and-conquer algorithms

19Divide-and-conquer algorithms

Searching an ordered matrix

Therefore, this search is approximately O(n1.585), which is significantly worse than a linear search:

Page 20: Divide-and-conquer algorithms

20Divide-and-conquer algorithms

Searching an ordered matrix

Note that it isT(n) = 3T(n/2) + Q(1)

and notT(n) = 3T(n/4) + Q(1)

We are breaking the n × n matrix into four (n/2) × (n/2) matrices

If N = n2, then we could writeT(N) = 3T(N/4) + Q(1)

Page 21: Divide-and-conquer algorithms

21Divide-and-conquer algorithms

Searching an ordered matrix

Where is such a search necessary?– Consider a bi-parental heap

– Without proof, most operations are O( ) including searches– Binary heaps: most operations are O(ln(n)) but searches are O(n)

n

Page 22: Divide-and-conquer algorithms

22Divide-and-conquer algorithms

Searching an ordered matrix

For example, consider a search for the value 44:

– The matrix has n entries in

See: http://ece.uwaterloo.ca/~dwharder/aads/Algorithms/Beaps/

n n

Page 23: Divide-and-conquer algorithms

23Divide-and-conquer algorithms

Searching an ordered matrix

Note: the linear searching algorithm is only optimal for square matrices– A binary search would be optimal for a 1 × n or n × 1 matrix– Craig Gidney posts an interesting discussion on such searches when

the matrix is not close to squarehttp://twistedoakstudios.com/blog/Post5365_searching-a-sorted-matrix-faster

Page 24: Divide-and-conquer algorithms

24Divide-and-conquer algorithms

Integer multiplication

Calculate the product of two 16-digit integers3563474256143563 × 8976558458718976

Multiplying two n-digit numbers requires Q(n2) multiplications of two decimal digits:

3563474256143563 × 8976558458718976 21380845536861378 24944319793004941 32071268305292067 28507794049148504 3563474256143563 24944319793004941 28507794049148504 17817371280717815 14253897024574252 28507794049148504 17817371280717815 17817371280717815 21380845536861378 24944319793004941 32071268305292067+ 28507794049148504 . 31987734976412811376690928351488

n

Page 25: Divide-and-conquer algorithms

25Divide-and-conquer algorithms

Integer multiplication

Rewrite the product3563474256143563 × 8976558458718976

as(35634742 × 108 + 56143563) × (89765584×108 + 58718976)

which requires four multiplications of 8-digit integers: (35634742 × 89765584)×1016 + (35634742 × 58718976 + 56143563 × 89765584)×108 + (56143563 × 58718976)

Adding two n-digit integers is a Q(n) operation

Page 26: Divide-and-conquer algorithms

26Divide-and-conquer algorithms

Integer multiplication

Thus, the recurrence relation is:

Again, we solve the recurrence relation using Maple:> rsolve( {T(n) = 4*T(n/2) + n, T(1) = 1}, T(n) );

This is still Q(n2)

(1) 1T( )

( )4T 12

nn n n n

Q Q

n ( )2 n 1

Page 27: Divide-and-conquer algorithms

27Divide-and-conquer algorithms

Integer multiplication

To reduce the run time, the Karatsuba algorithm (1961) reduces the number of multiplications

Letx = 3563474256143563 y = 8976558458718976

and definexMS = 35634742 xLS = 56143563yMS = 89765584 yLS = 58718976

and thusx = xMS×108 + xLS

y = yMS×108 + yLS

Page 28: Divide-and-conquer algorithms

28Divide-and-conquer algorithms

Integer multiplication

The multiplication is now:xy = xMSyMS×1016 + (xLyR + xRyL)×108 + xLSyLS

Rewrite the middle product as xMSyLS + xLSyMS = (xLS – xMS)(yLS – yMS) + xMSyMS + xLSyLS

Two of these are already calculated!

Page 29: Divide-and-conquer algorithms

29Divide-and-conquer algorithms

Integer multiplication

Thus, the revised recurrence relation

may again be solved using Maple:

> rsolve( {T(n) = 3*T(n/2) + n, T(1) = 1}, T(n) );

where log2(3) ≈ 1.585

1)(

2T3

1)1()T( nnn

nn Θ

Θ

3 n( )log2 3

2 n

Page 30: Divide-and-conquer algorithms

30Divide-and-conquer algorithms

Integer multiplication

Plotting the two functions n2 and n1.585, we see that they are significantly different

Page 31: Divide-and-conquer algorithms

31Divide-and-conquer algorithms

Integer multiplication

This is the same asymptotic behaviour we saw for our alternate searching behaviour of an ordered matrix, however, in this case, it is an improvement on the original run time!

Even more interesting is that the recurrence relation are different:– T(n) = 3T(n/2) + Q(n) integer multiplication– T(n) = 3T(n/2) + Q(1) searching an ordered matrix

Page 32: Divide-and-conquer algorithms

32Divide-and-conquer algorithms

Integer multiplication

In reality, you would probably not use this technique: there are others

There are also libraries available for fast integer multiplication

For example, the GNU Image Manipulation Program (GIMP) comes with a complete set of tools for fast integer arithmetic

http://www.gimp.org/

Page 33: Divide-and-conquer algorithms

33Divide-and-conquer algorithms

Integer multiplication

The Toom-Cook algorithm (1963 and 1966) splits the integers into k parts and reduces the k2 multiplicationsto 2k – 1– Complexity is Q(nlogk(2k – 1)) – Karatsuba is a special case when k = 2– Toom-3 (k = 3) results in a run time of Q(nlog3(5)) = Q(n1.465)

The Schönhage-Strassen algorithm runs inQ(n ln(n) ln(ln(n))) time but is only useful for very large integers (greater than 10 000 decimal digits)

Page 34: Divide-and-conquer algorithms

34Divide-and-conquer algorithms

Matrix multiplication

Consider multiplying two n × n matrices, C = ABThis requires the Q(n) dot product of each of the n rows of A with each of the n columns of B

The run time must be Q(n3)– Can we do better?

n

kjkkiji bac

1,,,

ij

ci,j

Page 35: Divide-and-conquer algorithms

35Divide-and-conquer algorithms

In special cases, faster algorithms exist:– If both matrices are diagonal or tri-diagonal Q(n)

– If one matrix is diagonal or tri-diagonal Q(n2)

In general, however, this was not believed to be possible to do better

Matrix multiplication

Page 36: Divide-and-conquer algorithms

36Divide-and-conquer algorithms

Matrix multiplication

Consider this product of two n × n matrices– How can we break this down into smaller sub-problems?

Page 37: Divide-and-conquer algorithms

37Divide-and-conquer algorithms

Matrix multiplication

Break each matrix into four (n/2) × (n/2) sub-matrices– Write each sub-matrix of C as a sum-of-products

A B C

Page 38: Divide-and-conquer algorithms

38Divide-and-conquer algorithms

Matrix multiplication

Justification:cij is the dot product of the ith row of A and the jth column of B

ci,j

Page 39: Divide-and-conquer algorithms

39Divide-and-conquer algorithms

Matrix multiplication

This is equivalent for each of the sub-matrices:

Page 40: Divide-and-conquer algorithms

40Divide-and-conquer algorithms

Matrix multiplication

We must calculate the four sums-of-productsC00 = A00B00 + A01B10

C01 = A00B01 + A01B11

C10 = A10B00 + A11B10

C11 = A10B01 + A11B11

This totals 8 products of (n/2) × (n/2) matrices– This requires four matrix-matrix additions: Q(n2)

Page 41: Divide-and-conquer algorithms

41Divide-and-conquer algorithms

Matrix multiplication

The recurrence relation is:

Using Maple:

> rsolve( {T(n) = 8*T(n/2) + n^2, T(1) = 1}, T(n) );

1)(

2T8

1)1()T( 2 nnn

nn Θ

Θ

n2 ( )2 n 1

Page 42: Divide-and-conquer algorithms

42Divide-and-conquer algorithms

Matrix multiplication

In 1969, Strassen developed a technique for performing matrix-matrix multiplication in Q(nlg(7)) ≈ Q(n2.807) time– Reduce the number of matrix-matrix products

Page 43: Divide-and-conquer algorithms

43Divide-and-conquer algorithms

Matrix multiplication

Consider the following seven matrix productsM1 = (A00 – A10)(B00 + B01)M2 = (A00 + A11)(B00 + B11)M3 = (A01 – A11)(B10 + B11)M4 = A00(B01 – B11)M5 = A11(B10 – B00)M6 = (A10 + A11)B00

M7 = (A00 + A01)B11

The four sub-matrices of C may be written as

C00 = M3 + M2 + M5 – M7

C01 = M4 + M7

C10 = M5 + M6

C11 = M2 – M1 + M4 – M6

Page 44: Divide-and-conquer algorithms

44Divide-and-conquer algorithms

Matrix multiplication

Thus, the new recurrence relation is:

Using Maple:

> rsolve( {T(n) = 7*T(n/2) + n^2, T(1) = 1}, T(n) );

1)(

2T7

1)1()T( 2 nnn

nn Θ

Θ

73

n( )log2 7 4 n2

3

Page 45: Divide-and-conquer algorithms

45Divide-and-conquer algorithms

Matrix multiplication

Note, however, that there is a lot of additional work requiredCounting additions and multiplications:

Classic 2n3 – n2 Strassen 7nlg(7) – 6 n2

Page 46: Divide-and-conquer algorithms

46Divide-and-conquer algorithms

Matrix multiplication

Examining this plot, and then solving explicitly, we find that Strassen’s method only reduces the number of operations for n > 654– Better asymptotic behaviour does not immediately translate into better

run-timesThe Strassen algorithm is not the fastest– the Coppersmith–Winograd algorithm runs in Q(n2.376) time but the

coefficients are too large for any problem

Therefore, better asymptotic behaviour does not immediately translate into better run-times

Page 47: Divide-and-conquer algorithms

47Divide-and-conquer algorithms

Observation

Some literature lists the run-time as O(7lg(n))

Recall that these are equal:

7lg(n) = nlg(7)

Proof:7lg(n)

= (2lg(7)) lg(n) = 2lg(7) lg(n) = 2lg(n) lg(7) = (2lg(n)) lg(7) = n lg(7)

Page 48: Divide-and-conquer algorithms

48Divide-and-conquer algorithms

Fast Fourier transform

The last example is the fast Fourier transform– This takes a vector from the time domain to the frequency domain

The Fourier transform is a linear transform– For finite dimensional vectors, it is a matrix-vector product Fnx

http://xkcd.com/26/

Page 49: Divide-and-conquer algorithms

49Divide-and-conquer algorithms

Fast Fourier transform

To perform a linear transformation, it is necessary to calculate a matrix-vector product:

Page 50: Divide-and-conquer algorithms

50Divide-and-conquer algorithms

Fast Fourier transform

We can apply a divide and conquer algorithm to this problem– Break the matrix-vector product into four matrix-vector products,

each of half the size

Page 51: Divide-and-conquer algorithms

51Divide-and-conquer algorithms

Fast Fourier transform

The recurrence relation is:

Using Maple:

> rsolve( {T(n) = 4*T(n/2) + n, T(1) = 1}, T(n) );

(1) 1T( )

4T ( ) 12

nn n n n

Θ

Θ

n (2 n – 1)

Page 52: Divide-and-conquer algorithms

52Divide-and-conquer algorithms

Discrete Fourier transform

To introduce the Fourier transform, we need a little information about complex numbers:– There are two complex numbers z such that z2 = 1

Page 53: Divide-and-conquer algorithms

53Divide-and-conquer algorithms

Discrete Fourier transform

To introduce the Fourier transform, we need a little information about complex numbers:– There are three complex numbers z such that z3 = 1

Page 54: Divide-and-conquer algorithms

54Divide-and-conquer algorithms

Discrete Fourier transform

To introduce the Fourier transform, we need a little information about complex numbers:– There are four complex numbers z such that z4 = 1

Page 55: Divide-and-conquer algorithms

55Divide-and-conquer algorithms

Discrete Fourier transform

To introduce the Fourier transform, we need a little information about complex numbers:– There are five complex numbers z such that z5 = 1

Page 56: Divide-and-conquer algorithms

56Divide-and-conquer algorithms

Discrete Fourier transform

To introduce the Fourier transform, we need a little information about complex numbers:– There are eight complex numbers z such that z8 = 1– These are also known as the eighth roots of unity

– That root with the smallest non-zero angle issaid to be the eighth principle root of unity

Page 57: Divide-and-conquer algorithms

57Divide-and-conquer algorithms

Discrete Fourier transform

In n dimensions, theFourier transform matrix is

where w = e–2pj/n is the conjugate nth principal root of unity

)1)(1()1(3)1(21

)1(3963

)1(2642

132

1

111

11111

nnnnn

n

n

n

n

wwww

wwwwwwwwwwww

F

Page 58: Divide-and-conquer algorithms

58Divide-and-conquer algorithms

Discrete Fourier transform

For example, the matrix for the Fourier transform for 4-dimensions is

Here, w = –j is the conjugate 4th principal root of unity

Note that:– The matrix is symmetric– All the column/row vectors are orthogonal– These create a orthogonal basis for C4

4

1 1 1 11 11 1 1 11 1

j j

j j

F

Page 59: Divide-and-conquer algorithms

59Divide-and-conquer algorithms

Discrete Fourier transform

Any matrix-vector multiplication is usually Q(n2)– The discrete Fourier transform is a useful tool in all fields of engineering– In general, it is not possible to speed up a matrix-vector multiplication– In this case, however, the matrix has a peculiar shape

• That of a very special Vandermonde matrix

Page 60: Divide-and-conquer algorithms

60Divide-and-conquer algorithms

Fast Fourier transform

We will now look at the Cooley–Tukey algorithm for calculating the discrete Fourier transform– This fast transform can only be applied the dimension is a power of two– We will look at the 8-dimensional transform matrix

– The eighth conjugate root of unity is

– Note that w2 = –j, w4 = –1 and w8 = 1

1 12 2

jw

Page 61: Divide-and-conquer algorithms

61Divide-and-conquer algorithms

Fast Fourier transform

This is the 8 × 8 Fourier transform matrix– We will write w0 instead of 1 so that we can see the pattern– We will number the columns 0, 1, 2, …, 7

0 0 0 0 0 0 0 0

0 1 2 3 4 5 6 7

0 2 4 6 8 10 12 14

0 3 6 9 12 15 18 21

8 0 4 8 12 16 20 24 28

0 5 10 15 20 25 30 35

0 6 12 18 24 30 36 42

0 7 14 21 28 35 42 49

w w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w w

F

0

1

2

3

4

5

6

7

vvvvvvvv

v

Page 62: Divide-and-conquer algorithms

62Divide-and-conquer algorithms

Fast Fourier transform

Now by definition, w8 = 1, so we can make some simplifications– For example, w14 = w8 + 6 = w8w6 = w6

– We may use, wn = wn mod 8

– For example, w49 = w49 mod 8 = w1

0 0 0 0 0 0 0 0

0 1 2 3 4 5 6 7

0 2 4 6 8 10 12 14

0 3 6 9 12 15 18 21

8 0 4 8 12 16 20 24 28

0 5 10 15 20 25 30 35

0 6 12 18 24 30 36 42

0 7 14 21 28 35 42 49

w w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w w

F

0

1

2

3

4

5

6

7

vvvvvvvv

v

Page 63: Divide-and-conquer algorithms

63Divide-and-conquer algorithms

Fast Fourier transform

Now we’ve simplified the matrix with powers on the range 0 to 7

0 0 0 0 0 0 0 0

0 1 2 3 4 5 6 7

0 2 4 6 0 2 4 6

0 3 6 1 4 7 2 5

8 0 4 0 4 0 4 0 4

0 5 2 7 4 1 6 3

0 6 4 2 0 6 4 2

0 7 6 5 4 3 2 1

w w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w w

F

0

1

2

3

4

5

6

7

vvvvvvvv

v

Page 64: Divide-and-conquer algorithms

64Divide-and-conquer algorithms

Fast Fourier transform

As 8 is even, w4 = –1– Thus, we replace w4 = –w0

w5 = –w1

w6 = –w2

w7 = –w3

0 0 0 0 0 0 0 0

0 1 2 3 4 5 6 7

0 2 4 6 0 2 4 6

0 3 6 1 4 7 2 5

8 0 4 0 4 0 4 0 4

0 5 2 7 4 1 6 3

0 6 4 2 0 6 4 2

0 7 6 5 4 3 2 1

w w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w w

F

0

1

2

3

4

5

6

7

vvvvvvvv

v

Page 65: Divide-and-conquer algorithms

65Divide-and-conquer algorithms

Fast Fourier transform

Now we may observe some patterns

0 0 0 0 0 0 0 0

0 1 2 3 0 1 2 3

0 2 0 2 0 2 0 2

0 3 2 1 0 3 2 1

8 0 0 0 0 0 0 0 0

0 1 2 3 0 1 2 3

0 2 0 2 0 2 0 2

0 3 2 1 0 3 2 1

w w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w w

F

0

1

2

3

4

5

6

7

vvvvvvvv

v

Page 66: Divide-and-conquer algorithms

66Divide-and-conquer algorithms

0

1

2

3

4

5

6

7

vvvvvvvv

v

Fast Fourier transform

Note that the even columns (0, 2, 4, 6) are powers of w2

– Note also that

0 0 0 0 0 0 0 0

0 1 2 3 0 1 2 3

0 2 0 2 0 2 0 2

0 3 2 1 0 3 2 1

8 0 0 0 0 0 0 0 0

0 1 2 3 0 1 2 3

0 2 0 2 0 2 0 2

0 3 2 1 0 3 2 1

w w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w w

F

0 0 0 0 0 0 0 0

0 2 4 6 0 2 0 2

4 0 4 8 12 0 0 0 0

0 6 12 18 0 2 0 2

w w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w w

F

If w is the eighth conjugate root of unity, w2is the fourth conjugate root of unity

Page 67: Divide-and-conquer algorithms

67Divide-and-conquer algorithms

Fast Fourier transform

The shape of the odd columns (1, 3, 5, 7) is less obvious

0 0 0 0 0 0 0 0

0 1 2 3 0 1 2 3

0 2 0 2 0 2 0 2

0 3 2 1 0 3 2 1

8 0 0 0 0 0 0 0 0

0 1 2 3 0 1 2 3

0 2 0 2 0 2 0 2

0 3 2 1 0 3 2 1

w w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w w

F

0

1

2

3

4

5

6

7

vvvvvvvv

v

Page 68: Divide-and-conquer algorithms

68Divide-and-conquer algorithms

Fast Fourier transform

Let’s rearrange the columns of the matrix andthe entries of the vector

0 0 0 0 0 0 0 0

0 2 0 2 1 3 1 3

0 0 0 0 2 2 2 2

0 2 0 2 3 1 3 1

8 0 0 0 0 0 0 0 0

0 2 0 2 1 3 1 3

0 0 0 0 2 2 2 2

0 2 0 2 3 1 3 1

w w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w w

F

0 0 0 0 0 0 0 0

0 1 2 3 0 1 2 3

0 2 0 2 0 2 0 2

0 3 2 1 0 3 2 1

8 0 0 0 0 0 0 0 0

0 1 2 3 0 1 2 3

0 2 0 2 0 2 0 2

0 3 2 1 0 3 2 1

w w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w w

F

0

2

4

6

1

3

5

7

vvvvvvvv

v

0 2 4 61 3 5 7

Page 69: Divide-and-conquer algorithms

69Divide-and-conquer algorithms

Fast Fourier transform

Recall that w is the 8th conjugate root of unity– Therefore, w2 is the 4th conjugate root of unity– Both these matrices are the Fourier transform for a 4-dimensional vector

0 0 0 0 0 0 0 0

0 2 0 2 1 3 1 3

0 0 0 0 2 2 2 2

0 2 0 2 3 1 3 1

8 0 0 0 0 0 0 0 0

0 2 0 2 1 3 1 3

0 0 0 0 2 2 2 2

0 2 0 2 3 1 3 1

w w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w w

F

0

2

4

6

1

3

5

7

vvvvvvvv

v

Page 70: Divide-and-conquer algorithms

70Divide-and-conquer algorithms

0 0 0 0 0 0 0 0

0 2 0 2 1 3 1 3

0 0 0 0 2 2 2 2

0 2 0 2 3 1 3 1

8 0 0 0 0 0 0 0 0

0 3 0 2 1 3 1 3

0 0 0 0 2 2 2 2

0 2 0 2 3 1 3 1

w w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w w

F

Fast Fourier transform

We will label these two blocks as F4

F4

F4

0

2

4

6

1

3

5

7

vvvvvvvv

v

Page 71: Divide-and-conquer algorithms

71Divide-and-conquer algorithms

Fast Fourier transform

There is one obvious pattern in the second pair of matrices– The bottom matrix is the negative of the top– The other pattern is more subtle

0 0 0 0 0 0 0 0

0 2 0 2 1 3 1 3

0 0 0 0 2 2 2 2

0 2 0 2 3 1 3 1

8 0 0 0 0 0 0 0 0

0 3 0 2 1 3 1 3

0 0 0 0 2 2 2 2

0 2 0 2 3 1 3 1

w w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w w

F

F4

F4

0

2

4

6

1

3

5

7

vvvvvvvv

v

Page 72: Divide-and-conquer algorithms

72Divide-and-conquer algorithms

Fast Fourier transform

The top matrixis a diagonalmultiplied by F4

0 0 0 0 0 0 0 0

0 2 0 2 1 3 1 3

0 0 0 0 2 2 2 2

0 2 0 2 3 1 3 1

8 0 0 0 0 0 0 0 0

0 3 0 2 1 3 1 3

0 0 0 0 2 2 2 2

0 2 0 2 3 1 3 1

w w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w w

F

F4

F4

0 0 0 0 0 0 0 0 0

1 0 2 0 2 1 3 1 3

2 0 0 0 0 2 2 2 2

3 0 2 0 2 3 1 3 1

0 0 00 0 00 0 00 0 0

w w w w w w w w ww w w w w w w w w

w w w w w w w w ww w w w w w w w w

0

2

4

6

1

3

5

7

vvvvvvvv

v

Page 73: Divide-and-conquer algorithms

73Divide-and-conquer algorithms

Fast Fourier transform

Represent that diagonal matrix by– Recall that multiplying a vector

by a diagonal matrix is Q(n)

24

0

1

3

0 0 00 0 00 0 00 0 0

ww

ww

D

0 0 0 0 0 0 0 0

0 2 0 2 1 3 1 3

0 0 0 0 2 2 2 2

0 2 0 2 3 1 3 1

8 0 0 0 0 0 0 0 0

0 3 0 2 1 3 1 3

0 0 0 0 2 2 2 2

0 2 0 2 3 1 3 1

w w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w w

F

F4

F4

D4F4

0

2

4

6

1

3

5

7

vvvvvvvv

v

Page 74: Divide-and-conquer algorithms

74Divide-and-conquer algorithms

Fast Fourier transform

From our previous observation, the bottom matrix is –D4F4

0 0 0 0 0 0 0 0

0 2 0 2 1 3 1 3

0 0 0 0 2 2 2 2

0 2 0 2 3 1 3 1

8 0 0 0 0 0 0 0 0

0 3 0 2 1 3 1 3

0 0 0 0 2 2 2 2

0 2 0 2 3 1 3 1

w w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w w

F

F4

F4

D4F4

–D4F4

0

2

4

6

1

3

5

7

vvvvvvvv

v

Page 75: Divide-and-conquer algorithms

75Divide-and-conquer algorithms

Fast Fourier transform

Thus, our adjusted Fourier transform matrix is a block matrix consisting of four 4 × 4 matrices

0 0 0 0 0 0 0 0

0 2 0 2 1 3 1 3

0 0 0 0 2 2 2 2

0 2 0 2 3 1 3 1

8 0 0 0 0 0 0 0 0

0 3 0 2 1 3 1 3

0 0 0 0 2 2 2 2

0 2 0 2 3 1 3 1

w w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w w

F

F4

F4

D4F4

–D4F4

0

2

4

6

1

3

5

7

vvvvvvvv

v

Page 76: Divide-and-conquer algorithms

76Divide-and-conquer algorithms

Fast Fourier transform

Let’s split up the vector into two vectors

0 0 0 0 0 0 0 0

0 2 0 2 1 3 1 3

0 0 0 0 2 2 2 2

0 2 0 2 3 1 3 1

8 0 0 0 0 0 0 0 0

0 3 0 2 1 3 1 3

0 0 0 0 2 2 2 2

0 2 0 2 3 1 3 1

w w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w ww w w w w w w w

F

F4

F4

D4F4

–D4F4

0

20

4

6

vvvv

v

1

31

5

7

vvvv

v

Page 77: Divide-and-conquer algorithms

77Divide-and-conquer algorithms

Fast Fourier transform

Thus, we have the relation:

Thus,

Note that we want to calculate and not – The first is Q(n + TF(n)), the second Q(n2 + TF(n))– That is, calculate the FFT of (a vector) and then multiply the entries

of that vector by the diagonal entries

4

48

4

4

4

4

FD FD F

FF

0

1

vv

v

0 14 4

4

4 4 4 4

4

08

04 4 4 114

D F D FFD F D F

v vvF v

v vvF

F F

1n nD F v 1n nD F v

4 × 4 matrices

4-dimensional vectors

1v

Page 78: Divide-and-conquer algorithms

78Divide-and-conquer algorithms

Fast Fourier transform

Now we must calculate and recursively– Calculating and are both Q(n) operations– Adding and are also both Q(n) – Rearranging the results is also Q(n)

4 0F v 4 1F v 4 4 1D F v 4 4 1 D F v

4 0 4 4 1F v D F v 4 0 4 4 1F v D F v

4 4 4 4 0 4 4 108

4 4 4 4 0 4 4 11

F D F F v D F vvF v F D F F v D F vv

0 0

1 4

2 1

3 58

4 2

5 6

6 3

7 7

r rr rr rr rr rr rr rr r

F v

Page 79: Divide-and-conquer algorithms

79Divide-and-conquer algorithms

Fast Fourier transform

Thus, we have that the time T(n) requires that we– Divide the vector v into the vectors and– Calculate and recursively– Calculate and – Add and – Reorder the results to get the result F8v

The recurrence relation now

– This is the run-time of merge sort: Q(n ln(n))

4 0F v 4 1F v

4 4 1D F v 4 4 1 D F v4 0 4 4 1F v D F v 4 0 4 4 1F v D F v

0v 1v Q(n) 2 T(n/2) Q(n) Q(n) Q(n)

(1) 1T( )

2T ( ) 12

nn n n n

Q Q

Page 80: Divide-and-conquer algorithms

80Divide-and-conquer algorithms

Fast Fourier transform

An argument that this generalizes for other powers of two:– The even columns of are powers of w2

w0 w2 w4 w6 w8 ··· w0 w4 w8 w12 w16 ···

– The normalized bottom halves equal the top halves – The odd columns are of the form

w0 w3 w6 w9 w12 ···w0 w5 w10 w15 w20 ···

– These can be written asw0·w0 w1·w2 w2·w4 w3· w6 w4· w8 ···w0·w0 w1·w4 w2·w8 w3·w12 w4·w16 ···

– The normalized bottom halves are the top halves multiplied by

2nF

12 1n

w

Page 81: Divide-and-conquer algorithms

81Divide-and-conquer algorithms

Fast Fourier transform

void FFT( std::complex<double> *array, int n ) { if ( n == 1 ) return;

std::complex<double> even[n/2]; std::complex<double> odd[n/2];

for ( int k = 0; k < n/2; ++k ) { even[k] = array[2*k]; odd[k] = array[2*k + 1]; }

FFT( even, n/2 ); FFT( odd, n/2 );

double const PI = 4.0*std::atan( 1.0 ); std::complex<double> w = 1.0; std::complex<double> wn = std::exp( std::complex<double>( 0.0, -2.0*PI/n ) );

for ( int k = 0; k < n/2; ++k ) { array[k] = even[k] + w*odd[k]; array[n/2 + k] = even[k] - w*odd[k]; w = w * wn; }}

( )n Q

( )n Q

(1)Q

2T2 n

(1)Q

(1)Q

Page 82: Divide-and-conquer algorithms

82Divide-and-conquer algorithms

Divide and Conquer

We have now looked at a number of divide-and-conquer algorithms, and come up with a number of different run times:

Binary search T(n) = T(n/2) + Q(1) O(ln(n))Tree traversals T(n) = 2T(n/2) + Q(1) Q(n)Merge sort T(n) = 2T(n/2) + Q(n) Q(n ln(n))Ordered-matrix search T(n) = 3T(n/2) + Q(1) O(nlg(3))Integer multiplication T(n) = 3T(n/2) + Q(n) Q(nlg(3))Toom-3 integer multiplication T(n) = 5T(n/3) + Q(n) Q(nlog3(5))Matrix multiplication T(n) = 7T(n/2) + Q(n2) Q(nlg(7))Fast Fourier transform T(n) = 2T(n/2) + Q(n) Q(n ln(n))

Page 83: Divide-and-conquer algorithms

83Divide-and-conquer algorithms

The master theorem

We used Maple to solve the recurrence relationships

We will now solve the general problem

1)(T

11)T( nn

bna

nn kO

Page 84: Divide-and-conquer algorithms

84Divide-and-conquer algorithms

The master theorem

In all cases when b = 2, we assumed n = 2m

That is, n = 1, 2, 4, 8, 16, 32, 64, ....and interpolated the intermediate results

Page 85: Divide-and-conquer algorithms

85Divide-and-conquer algorithms

The master theorem

In this case, we will assume n = bm, as we are dividing each interval into b equal parts

n = 1, b, b2, b3, ...

As before, we will interpolate intermediate behaviour– Thus, we will solve T(bm) and use this to approximate T(n)

Page 86: Divide-and-conquer algorithms

86Divide-and-conquer algorithms

The master theorem

Thus, given the recurrence relation

we have that

We can rewrite this as:

1)(T

11)T( nn

bna

nn kO

kmm

m bb

babn OTT)T(

mkmm bbab O1TT

bk is a constant

Page 87: Divide-and-conquer algorithms

87Divide-and-conquer algorithms

The master theorem

Therefore, we may iterate:

mkmkmkmkm

mkmkmkm

mkmkm

mkmkm

mkmm

bbabababa

bbababa

bbaba

bbbaa

bbab

1223344

12233

122

12

1

T

T

T

T

TT

Page 88: Divide-and-conquer algorithms

88Divide-and-conquer algorithms

The master theorem

Determining a pattern is possible, however, we can determine the pattern more easily if we divide both sides by am:

We can simplify this to:

m

mk

m

m

m

m

ab

aba

ab

1TT

mk

m

m

m

m

ab

ab

ab

1

1TT

Page 89: Divide-and-conquer algorithms

89Divide-and-conquer algorithms

The master theorem

We can repeatedly calculate this formula for smaller and smaller values of m: mk

m

m

m

m

ab

ab

ab

1

1TT

1

2

2

1

1 TT

mk

m

m

m

m

ab

ab

ab

2

3

3

2

2 TT

mk

m

m

m

m

ab

ab

ab

3

4

4

3

3 TT

mk

m

m

m

m

ab

ab

ab

Page 90: Divide-and-conquer algorithms

90Divide-and-conquer algorithms

The master theorem

Thus, we may carry on mk

m

m

m

m

ab

ab

ab

1

1TT

1

2

2

1

1 TT

mk

m

m

m

m

ab

ab

ab

2

3

3

2

2 TT

mk

m

m

m

m

ab

ab

ab

1

0

0

1

1 TT

ab

ab

ab k

2

1

1

2

2 TT

ab

ab

ab k

Page 91: Divide-and-conquer algorithms

91Divide-and-conquer algorithms

The master theorem

A telescoping series is any series of the form

Alternatively, if , it follows that

More generally, we have:

1 01

n

k k nk

a a a a

1 01 1

n n

k k k n kk k

a a b a a b

1

1 0

n n

k kk k

a a

0na a

Page 92: Divide-and-conquer algorithms

92Divide-and-conquer algorithms

The master theorem

Thus, we find:

+

mk

m

m

m

m

ab

ab

ab

1

1TT

1

2

2

1

1 TT

mk

m

m

m

m

ab

ab

ab

2

3

3

2

2 TT

mk

m

m

m

m

ab

ab

ab

1

0

0

1

1 TT

ab

ab

ab k

2

1

1

2

2 TT

ab

ab

ab k

0

01

T Tm km

m

b b ba a a

Page 93: Divide-and-conquer algorithms

93Divide-and-conquer algorithms

The master theorem

We can sum these:

and simplify:

m k

m

m

ab

ab

ab

10

0TT

m km k

m k

m

m

ab

ab

ab

ab

01

1

1

1TT

Page 94: Divide-and-conquer algorithms

94Divide-and-conquer algorithms

The master theorem

We multiply by am to get

0

Tkm

m m bb aa

Page 95: Divide-and-conquer algorithms

95Divide-and-conquer algorithms

The master theorem

The sum is a geometric series, and the actual value will depend on the ratio

Recall that for a geometric series, if r < 1 then the series converges:

rr

11

1

Page 96: Divide-and-conquer algorithms

96Divide-and-conquer algorithms

The master theorem

Also, if r = 1, we have:

If r > 1, we can only determine a finite sum:

11100

mmm

11

11 11

0

r

rr

rrmmm

Page 97: Divide-and-conquer algorithms

97Divide-and-conquer algorithms

The master theorem

Thus, we consider three possible cases

1abk

1abk

1abk

abk

abk

abk

Page 98: Divide-and-conquer algorithms

98Divide-and-conquer algorithms

The master theorem

These may be roughly translated into:– The number of recursions at each step is more significant than the

amount of work at each step (bk < a)– The contributions are equal (bk = a)– The amount of work at each step is more significant than the additional

work contributed by the recursion (bk > a)

Page 99: Divide-and-conquer algorithms

99Divide-and-conquer algorithms

bk < a

Which examples fall in this case?

a b k bk

Traversal 2 2 0 1

Quaternary search of an ordered matrix 3 2 0 1

Karatsuba’s integer-multiplication algorithm 3 2 1 2

Toom-3 integer-multiplication algorithm 5 3 1 3

Strassen’s matrix-multiplication algorithm 7 2 2 4

Page 100: Divide-and-conquer algorithms

100Divide-and-conquer algorithms

bk < a

In this case,

where is a constant

which we may asymptotically ignore

mk

mm k

m caaba

aban

00

T

0 1

1

aba

bc k

k

Page 101: Divide-and-conquer algorithms

101Divide-and-conquer algorithms

bk < a

Therefore, T(n) = O(am)

By assumption, n = bm, hence m =logbn and thereforeT(n) = O(alogbn) = O(nlogba)

Page 102: Divide-and-conquer algorithms

102Divide-and-conquer algorithms

bk < a

Going back to our examples:

a b k bk logb(a) Run time

Traversal 2 2 0 1 1.000 O(n)

Quaternary search of an ordered matrix 3 2 0 1 1.585 O(n1.585)

Karatsuba’s integer-multiplication algorithm 3 2 1 2 1.465 O(n1.585)

Toom-3 integer-multiplication algorithm 5 3 1 3 1.465 O(n1.465)

Strassen’s matrix-multiplication algorithm 7 2 2 4 2.807 O(n2.807)

Page 103: Divide-and-conquer algorithms

103Divide-and-conquer algorithms

bk = a

Which examples fall in this case?

a b k bk

Binary search 1 2 0 1

Merge sort 2 2 1 2

Fast Fourier transform 2 2 1 2

Page 104: Divide-and-conquer algorithms

104Divide-and-conquer algorithms

bk = a

In this case,

Therefore, T(n) = O(mam)

By assumption, n = bm and a = bk ∴ m = logbn and k = logba Hence

mm

mm k

m amaaban )1(1T

00

log

T O

O log

O log

O log

O log b

m

mkb

kmb

kb

ab

n ma

n b

n b

n n

n n

Page 105: Divide-and-conquer algorithms

105Divide-and-conquer algorithms

bk = a

Going back to our examples:

a b k bk Run time

Binary search 1 2 0 1 O(1·ln(n))

Merge sort 2 2 1 1 O(n ln(n))

Fast Fourier transform 2 2 1 2 O(n ln(n))

Page 106: Divide-and-conquer algorithms

106Divide-and-conquer algorithms

bk > a

We haven’t seen any examples that fall into this case– Suppose we divide the problem into two, but we must perform a linear

operation to determine which half to recursively call

a b k bk

Sample 1 2 1 2

Page 107: Divide-and-conquer algorithms

107Divide-and-conquer algorithms

bk > a

In this case,

Factor out the constant term and simplify to get:

1

1T

1

0

ab

ab

aa

ban k

mk

mm k

m

mk

km

k

mmk

mk a

ab

b

baa

aba

ab

n1

1

1

1

1

1T1

Both positive constants (see assumption)

Page 108: Divide-and-conquer algorithms

108Divide-and-conquer algorithms

bk > a

Recall that if p < q then pm = o(qm), hence am = o((bk)m)

Thus, we can ignore the second term:T(n) = O(bkm – am) = O(bkm)

Again, by assumption, n = bm, hence T(n) = O((bm)k) = O(nk)

Page 109: Divide-and-conquer algorithms

109Divide-and-conquer algorithms

bk > a

Going back to our example:

The linear operation contributes more than the divide-and-conquer component

a b k bk Run time

Sample 1 2 1 2 O(n1)

Page 110: Divide-and-conquer algorithms

110Divide-and-conquer algorithms

Summary of cases

To summarize these run times:

1abk

1abk

1abk

)( log abnO

))((log))((log log kb

ab nnnn b OO

)( knO

)(log.,. akei b

Page 111: Divide-and-conquer algorithms

111Divide-and-conquer algorithms

Summary

Therefore:– If the amount of work being done at each step to either sub-divide the

problem or to recombine the solutions dominates, then this is the run time of the algorithm: O(nk)

– If the problem is being divided into many small sub-problems (a > bk) then the number of sub-problems dominates: O(nlogb(a))

– In between, a little more (logarithmically more) work must be done

Page 112: Divide-and-conquer algorithms

112Divide-and-conquer algorithms

References

Wikipedia, http://en.wikipedia.org/wiki/Divide_and_conquer

These slides are provided for the ECE 250 Algorithms and Data Structures course. The material in it reflects Douglas W. Harder’s best judgment in light of the information available to him at the time of preparation. Any reliance on these course slides by any party for any other purpose are the responsibility of such parties. Douglas W. Harder accepts no responsibility for damages, if any, suffered by any party as a result of decisions made or actions based on these course slides for any other purpose than that for which it was intended.