28
Solving recurrences The analysis of divide and conquer algorithms require us to solve a recurrence. Recurrences are a major tool for CS 4407, Algorithms University College Cork, Gregory M. Provan Recurrences are a major tool for analysis of algorithms

The analysis of divide and conquer algorithms require us to solve …gprovan/CS4407/Lyz-MasterTheorem.pdf · The master method The master method applies to recurrences of the form

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: The analysis of divide and conquer algorithms require us to solve …gprovan/CS4407/Lyz-MasterTheorem.pdf · The master method The master method applies to recurrences of the form

Solving recurrences

• The analysis of divide and conquer algorithms require us to solve a recurrence.

• Recurrences are a major tool for

CS 4407, Algorithms University College Cork, Gregory M. Provan

• Recurrences are a major tool for analysis of algorithms

Page 2: The analysis of divide and conquer algorithms require us to solve …gprovan/CS4407/Lyz-MasterTheorem.pdf · The master method The master method applies to recurrences of the form

MergeSort

cn

A L G O R I T H M S

divideA L G O R I T H M S

CS 4407, Algorithms University College Cork, Gregory M. Provan

T(n/2) T(n/2)

Page 3: The analysis of divide and conquer algorithms require us to solve …gprovan/CS4407/Lyz-MasterTheorem.pdf · The master method The master method applies to recurrences of the form

MergeSort

A L G O R I T H M S

Divide #1A L G O R I T H M S

Divide #2A L G O R I T H M S

CS 4407, Algorithms University College Cork, Gregory M. Provan

T(n/2) T(n/2)

cn

T(n/4) T(n/4) T(n/4) T(n/4)

Page 4: The analysis of divide and conquer algorithms require us to solve …gprovan/CS4407/Lyz-MasterTheorem.pdf · The master method The master method applies to recurrences of the form

MergeSort

(n/2) +c (n/2) +c

cn

Solve T(n) = T(n/2) + T(n/2) + cn

CS 4407, Algorithms University College Cork, Gregory M. Provan

>+

=

=1

22

1

)(ncn

nT

nc

nT

T(n/4) T(n/4) T(n/4) T(n/4)

Recurrence

Page 5: The analysis of divide and conquer algorithms require us to solve …gprovan/CS4407/Lyz-MasterTheorem.pdf · The master method The master method applies to recurrences of the form

Integer Multiplication

Let X = A B and Y = C D where A,B,C and D

are n/2 bit integers

Simple Method: XY = (2n/2A+B)(2n/2C+D)

Running Time Recurrence

T(n) < 4T(n/2) + 100n

CS 4407, Algorithms University College Cork, Gregory M. Provan

T(n) < 4T(n/2) + 100n

How do we solve it?

Page 6: The analysis of divide and conquer algorithms require us to solve …gprovan/CS4407/Lyz-MasterTheorem.pdf · The master method The master method applies to recurrences of the form

Substitution method

1. Guess the form of the solution.2. Verify by induction.3. Solve for constants.

The most general method:

Example: T(n) = 4T(n/2) + 100n

CS 4407, Algorithms University College Cork, Gregory M. Provan

Example: T(n) = 4T(n/2) + 100n

• [Assume that T(1) = Θ(1).]• Guess O(n3) . (Prove O and Ω separately.)• Assume that T(k) ≤ ck3 for k < n .• Prove T(n) ≤ cn3 by induction.

Page 7: The analysis of divide and conquer algorithms require us to solve …gprovan/CS4407/Lyz-MasterTheorem.pdf · The master method The master method applies to recurrences of the form

Example of substitution

desired – residualdesired3

33

3

3

))2/(()2/(

)2/(4)2/(4)(

cn100nnccn

100nnc100nnc

100nnTnT

≤−−=

+=+≤

+=

CS 4407, Algorithms University College Cork, Gregory M. Provan

whenever (c/2)n3 – 100n ≥ 0, for example, if c ≥ 200and n ≥ 1.

desired

residual

3cn≤

Page 8: The analysis of divide and conquer algorithms require us to solve …gprovan/CS4407/Lyz-MasterTheorem.pdf · The master method The master method applies to recurrences of the form

Recursion-tree method

• A recursion tree models the costs (time) of a recursive execution of an algorithm.

• The recursion tree method is good for generating guesses for the substitution method.

CS 4407, Algorithms University College Cork, Gregory M. Provan

generating guesses for the substitution method.• The recursion-tree method can be unreliable,

just like any method that uses ellipses (…).• The recursion-tree method promotes intuition,

however.

Page 9: The analysis of divide and conquer algorithms require us to solve …gprovan/CS4407/Lyz-MasterTheorem.pdf · The master method The master method applies to recurrences of the form

Example of recursion tree

Solve T(n) = T(n/4) + T(n/2) + n2:

CS 4407, Algorithms University College Cork, Gregory M. Provan

Page 10: The analysis of divide and conquer algorithms require us to solve …gprovan/CS4407/Lyz-MasterTheorem.pdf · The master method The master method applies to recurrences of the form

Example of recursion tree

T(n)

Solve T(n) = T(n/4) + T(n/2) + n2:

CS 4407, Algorithms University College Cork, Gregory M. Provan

Page 11: The analysis of divide and conquer algorithms require us to solve …gprovan/CS4407/Lyz-MasterTheorem.pdf · The master method The master method applies to recurrences of the form

Example of recursion tree

T(n/4) T(n/2)

n2

Solve T(n) = T(n/4) + T(n/2) + n2:

CS 4407, Algorithms University College Cork, Gregory M. Provan

T(n/4)

Page 12: The analysis of divide and conquer algorithms require us to solve …gprovan/CS4407/Lyz-MasterTheorem.pdf · The master method The master method applies to recurrences of the form

Example of recursion tree

Solve T(n) = T(n/4) + T(n/2) + n2:

n2

(n/4)2 (n/2)2

CS 4407, Algorithms University College Cork, Gregory M. Provan

(n/4)

T(n/16) T(n/8) T(n/8) T(n/4)

Page 13: The analysis of divide and conquer algorithms require us to solve …gprovan/CS4407/Lyz-MasterTheorem.pdf · The master method The master method applies to recurrences of the form

Example of recursion tree

(n/4)2 (n/2)2

Solve T(n) = T(n/4) + T(n/2) + n2:

n2

CS 4407, Algorithms University College Cork, Gregory M. Provan

(n/16)2 (n/8)2 (n/8)2 (n/4)2

(n/4)

Θ(1)

Page 14: The analysis of divide and conquer algorithms require us to solve …gprovan/CS4407/Lyz-MasterTheorem.pdf · The master method The master method applies to recurrences of the form

Example of recursion tree

Solve T(n) = T(n/4) + T(n/2) + n2:

(n/4)2 (n/2)2

2nn2

CS 4407, Algorithms University College Cork, Gregory M. Provan

(n/16)2 (n/8)2 (n/8)2 (n/4)2

(n/4)

Θ(1)

Page 15: The analysis of divide and conquer algorithms require us to solve …gprovan/CS4407/Lyz-MasterTheorem.pdf · The master method The master method applies to recurrences of the form

Example of recursion tree

Solve T(n) = T(n/4) + T(n/2) + n2:

(n/4)2 (n/2)2 2165 n

2nn2

CS 4407, Algorithms University College Cork, Gregory M. Provan

(n/16)2 (n/8)2 (n/8)2 (n/4)2

(n/4)

Θ(1)

16

Page 16: The analysis of divide and conquer algorithms require us to solve …gprovan/CS4407/Lyz-MasterTheorem.pdf · The master method The master method applies to recurrences of the form

Example of recursion tree

Solve T(n) = T(n/4) + T(n/2) + n2:

(n/4)2 2165 n

2nn2

(n/2)2

CS 4407, Algorithms University College Cork, Gregory M. Provan

(n/16)2 (n/8)2 (n/8)2 (n/4)2

(n/4)

Θ(1)

162

25625 n

Page 17: The analysis of divide and conquer algorithms require us to solve …gprovan/CS4407/Lyz-MasterTheorem.pdf · The master method The master method applies to recurrences of the form

Example of recursion tree

Solve T(n) = T(n/4) + T(n/2) + n2:

(n/16)2 (n/8)2 (n/8)2 (n/4)2

(n/4)2 2165 n

2n

225 n

n2

(n/2)2

CS 4407, Algorithms University College Cork, Gregory M. Provan

(n/16)2 (n/8)2 (n/8)2 (n/4)2

Θ(1)

225625 n

( ) ( )( ) 13

1652

165

1652

L++++n

Total =

= Θ(n2) geometric series

Page 18: The analysis of divide and conquer algorithms require us to solve …gprovan/CS4407/Lyz-MasterTheorem.pdf · The master method The master method applies to recurrences of the form

Appendix: geometric series

1

11 2

xxx

−=+++ L for |x| < 1

1

11

12

xx

xxxn

n

−−=++++

+L for x ≠ 1

CS 4407, Algorithms University College Cork, Gregory M. Provan

1 x−

Page 19: The analysis of divide and conquer algorithms require us to solve …gprovan/CS4407/Lyz-MasterTheorem.pdf · The master method The master method applies to recurrences of the form

The master method

The master method applies to recurrences of the form

T(n) = a T(n/b) + f (n) ,

where a ≥ 1, b > 1, and f is asymptotically positive.

CS 4407, Algorithms University College Cork, Gregory M. Provan

positive.

Page 20: The analysis of divide and conquer algorithms require us to solve …gprovan/CS4407/Lyz-MasterTheorem.pdf · The master method The master method applies to recurrences of the form

Idea of master theorem

f (n/b)f (n/b) f (n/b)

Recursion tree:

f (n) a

f (n/b2)f (n/b2) f (n/b2)…ah = logbn

f (n)

a f (n/b)

a2 f (n/b2)

CS 4407, Algorithms University College Cork, Gregory M. Provan

Τ (1)

f (n/b )f (n/b ) f (n/b ) a f (n/b )

#leaves = ah

= alogbn

= nlogba

nlogbaΤ (1)

Page 21: The analysis of divide and conquer algorithms require us to solve …gprovan/CS4407/Lyz-MasterTheorem.pdf · The master method The master method applies to recurrences of the form

Three common cases

Compare f (n) with nlogba:1. f (n) = O(nlogba – ε) for some constant ε > 0.

• f (n) grows polynomially slower than nlogba

(by an nε factor).

CS 4407, Algorithms University College Cork, Gregory M. Provan

(by an nε factor).

Solution: T(n) = Θ(nlogba) .

# leaves inrecursion tree

Page 22: The analysis of divide and conquer algorithms require us to solve …gprovan/CS4407/Lyz-MasterTheorem.pdf · The master method The master method applies to recurrences of the form

Idea of master theorem

f (n/b)f (n/b) f (n/b)

Recursion tree:

f (n) a

f (n/b2)f (n/b2) f (n/b2)…ah = logbn

f (n)

a f (n/b)

a2 f (n/b2)

CS 4407, Algorithms University College Cork, Gregory M. Provan

Τ (1)

f (n/b2)f (n/b2) f (n/b2)… a2 f (n/b2)

nlogbaΤ (1)CCASE 1: The weight increases geometrically from the root to the leaves. The leaves hold a constant fraction of the total weight. Θ(nlogba)

Page 23: The analysis of divide and conquer algorithms require us to solve …gprovan/CS4407/Lyz-MasterTheorem.pdf · The master method The master method applies to recurrences of the form

Three common cases

Compare f (n) with nlogba:

2. f (n) = Θ(nlogba lgkn) for some constant k ≥ 0.

• f (n) and nlogba grow at similar rates.

CS 4407, Algorithms University College Cork, Gregory M. Provan

• f (n) and nlogba grow at similar rates.

Solution: T(n) = Θ(nlogba lgk+1n) .

Page 24: The analysis of divide and conquer algorithms require us to solve …gprovan/CS4407/Lyz-MasterTheorem.pdf · The master method The master method applies to recurrences of the form

Idea of master theorem

f (n/b)f (n/b) f (n/b)

Recursion tree:

f (n) a

f (n/b2)f (n/b2) f (n/b2)…ah = logbn

f (n)

a f (n/b)

a2 f (n/b2)

CS 4407, Algorithms University College Cork, Gregory M. Provan

Τ (1)

f (n/b )f (n/b2) f (n/b )… a f (n/b )

nlogbaΤ (1)CCASE 2: (k = 0) The weight is approximately the same on each of the logbn levels.

Θ(nlogbalgn)

Page 25: The analysis of divide and conquer algorithms require us to solve …gprovan/CS4407/Lyz-MasterTheorem.pdf · The master method The master method applies to recurrences of the form

Three common cases (cont.)

Compare f (n) with nlogba:

3. f (n) = Ω(nlogba + ε) for some constant ε > 0.

• f (n) grows polynomially faster than nlogba (by an nε factor),

CS 4407, Algorithms University College Cork, Gregory M. Provan

and f (n) satisfies the regularity condition that a f (n/b) ≤ c f (n) for some constant c < 1.

Solution: T(n) = Θ( f (n)) .

Page 26: The analysis of divide and conquer algorithms require us to solve …gprovan/CS4407/Lyz-MasterTheorem.pdf · The master method The master method applies to recurrences of the form

Idea of master theorem

f (n/b)f (n/b) f (n/b)

Recursion tree:

f (n) a

f (n/b2)f (n/b2) f (n/b2)…ah = logbn

f (n)

a f (n/b)

a2 f (n/b2)

CS 4407, Algorithms University College Cork, Gregory M. Provan

Τ (1)

f (n/b )f (n/b2) f (n/b )… a f (n/b )

nlogbaΤ (1)CCASE 3: The weight decreases geometrically from the root to the leaves. The root holds a constant fraction of the total weight. Θ( f (n))

Page 27: The analysis of divide and conquer algorithms require us to solve …gprovan/CS4407/Lyz-MasterTheorem.pdf · The master method The master method applies to recurrences of the form

Examples

Ex. T(n) = 4T(n/2) + na = 4, b = 2 ⇒ nlogba = n2; f (n) = n.CASE 1: f (n) = O(n2 – ε) for ε = 1.∴ T(n) = Θ(n2).

CS 4407, Algorithms University College Cork, Gregory M. Provan

Ex. T(n) = 4T(n/2) + n2

a = 4, b = 2 ⇒ nlogba = n2; f (n) = n2.CASE 2: f (n) = Θ(n2lg0n), that is, k = 0.∴ T(n) = Θ(n2lg n).

Page 28: The analysis of divide and conquer algorithms require us to solve …gprovan/CS4407/Lyz-MasterTheorem.pdf · The master method The master method applies to recurrences of the form

Examples

Ex. T(n) = 4T(n/2) + n3

a = 4, b = 2 ⇒ nlogba = n2; f (n) = n3.CASE 3: f (n) = Ω(n2 + ε) for ε = 1and 4(cn/2)3 ≤ cn3 (reg. cond.) for c = 1/2.∴ T(n) = Θ(n3).

CS 4407, Algorithms University College Cork, Gregory M. Provan

Ex. T(n) = 4T(n/2) + n2/lgna = 4, b = 2 ⇒ nlogba = n2; f (n) = n2/lgn.Master method does not apply. In particular, for every constant ε > 0, we have nε = ω(lgn).