105
Lecture 5: Solving Recurrences via the Master Method 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler, and Dr. Steve Cole. https://www.youtube.com/watch?v=OWnC9tSA3iA

Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

  • Upload
    others

  • View
    19

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Lecture 5:

Solving Recurrences via the

Master Method

1

These slides include material

originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,and Dr. Steve Cole. https://www.youtube.com/watch?v=OWnC9tSA3iA

Page 2: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Overview: recurrence-solving strategies

● Problem: given a recurrence for T(n), find a closed-

form asymptotic complexity function that satisfies the

recurrence.

● Possible strategies

● Guess and check (a.k.a. substitution)

● Recursion tree accounting (for certain kinds of recurrence)

● Master Method (for certain kinds of recurrence)

2

Page 3: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Example: T(n) = 3T(n/4) + cn2 [T(1) = d]

● [Copied from end of last lecture]

3

Page 4: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Example: T(n) = 3T(n/4) + cn2 [T(1) = d]

● This time, a = 3, so

each node branches

3 ways!

4

...

......

Page 5: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Example: T(n) = 3T(n/4) + cn2 [T(1) = d]

● This time, a = 3, so

each node branches

3 ways!

● This time, b = 4, so

problem size goes

down by factor of 4

per level.

5

T(n)

T(n/4)

T(n/16)

...

...

T(n/4j)...

T(1)

Page 6: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

6

DepthProblem

Size

# Nodes Per

Level

Local Work

per Node

0 n

1 n/4

2 n/16

j n/4j

??? 1

T(n) = 3T(n/4) + cn2

T(1) = d

Page 7: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

7

DepthProblem

Size

# Nodes Per

Level

Local Work

per Node

0 n 1

1 n/4 3

2 n/16 9

j n/4j 3j

log4n 1 ???

T(n) = 3T(n/4) + cn2

T(1) = d

Page 8: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

8

DepthProblem

Size

# Nodes Per

Level

Local Work

per Node

0 n 1

1 n/4 3

2 n/16 9

j n/4j 3j

log4n 1

T(n) = 3T(n/4) + cn2

T(1) = d

3log4 𝑛

Page 9: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

9

DepthProblem

Size

# Nodes Per

Level

Local Work

per Node

0 n 1

1 n/4 3

2 n/16 9

j n/4j 3j

log4n 1

T(n) = 3T(n/4) + cn2

T(1) = d

𝑛log4 3

Page 10: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

10

DepthProblem

Size

# Nodes Per

Level

Local Work

per Node

0 n 1 cn2

1 n/4 3 c(n/4)2

2 n/16 9 c(n/16)2

j n/4j 3j c(n/4j)2

log4n 1 d

T(n) = 3T(n/4) + cn2

T(1) = d

𝑛log4 3

Page 11: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

11

DepthProblem

Size

# Nodes Per

Level

Local Work

per Node

Local Work

per Level

0 n 1 cn2

1 n/4 3 c(n/4)2

2 n/16 9 c(n/16)2

j n/4j 3j c(n/4j)2

log4n 1 d

T(n) = 3T(n/4) + cn2

𝑛log4 3

Page 12: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

12

DepthProblem

Size

# Nodes Per

Level

Local Work

per Node

Local Work

per Level

0 n 1 cn2 1 x cn2

1 n/4 3 c(n/4)2 3 x c(n/4)2

2 n/16 9 c(n/16)2 9 x c(n/16)2

j n/4j 3j c(n/4j)2 3j x c(n/4j)2

log4n 1 d

T(n) = 3T(n/4) + cn2

𝑛log4 3 𝑑𝑛log4 3

Page 13: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

13

DepthProblem

Size

# Nodes Per

Level

Local Work

per Node

Local Work

per Level

0 n 1 cn2 cn2

1 n/4 3 c(n/4)2 3c(n/4)2

2 n/16 9 c(n/16)2 9c(n/16)2

j n/4j 3j c(n/4j)2 3j c(n/4j)2

log4n 1 d

T(n) = 3T(n/4) + cn2

𝑛log4 3 𝑑𝑛log4 3

Page 14: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

14

DepthProblem

Size

# Nodes Per

Level

Local Work

per Node

Local Work

per Level

0 n 1 cn2 cn2

1 n/4 3 c(n/4)2 3c(n/4)2

2 n/16 9 c(n/16)2 9c(n/16)2

j n/4j 3j c(n/4j)2 3j c(n/4j)2

log4n 1 d

T(n) = 3T(n/4) + cn2

𝑛log4 3 𝑑𝑛log4 3

Page 15: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Let’s Break This Summation Down a Bit

15

Page 16: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Let’s Break This Summation Down a Bit

16

(pulled first term out of sum)

Page 17: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

17

T(n) = 3T(n/4) + cn2

T(1) = d

Which parts of the tree

contribute to which parts

of the sum?

Page 18: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

18

T(n) = 3T(n/4) + cn2

T(1) = d

Page 19: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

19

T(n) = 3T(n/4) + cn2

T(1) = d

This term is from the

base case (i.e.

bottom of the tree).

Page 20: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

20

T(n) = 3T(n/4) + cn2

T(1) = d

Page 21: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

21

T(n) = 3T(n/4) + cn2

T(1) = d

This term is from the

top-level call (i.e. the

root of the tree).

Page 22: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

22

T(n) = 3T(n/4) + cn2

T(1) = d

Page 23: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

23

T(n) = 3T(n/4) + cn2

T(1) = d

This term is from the

non-base-case

recursive calls (i.e.

the rest of the tree).

Page 24: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Let’s Generalize

● We split up the sum for a particular recurrence

24

T(n) = 3T(n/4) + cn2; T(1) = d

Page 25: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Let’s Generalize

● We split up the sum for a particular recurrence

● Let’s do this for a general recurrence

25

T(n) = 3T(n/4) + cn2; T(1) = d

T(n) = aT(n/b) + f(n); T(1) = d

Page 26: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Let’s Generalize

● We split up the sum for a particular recurrence

● Let’s do this for a general recurrence

26

T(n) = 3T(n/4) + cn2; T(1) = d

T(n) = aT(n/b) + f(n); T(1) = d

As you saw in

Studio 4, we could

start from T(c0)

rather than T(1);

would not affect

asymptotic result.

Page 27: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

27

T(n) = aT(n/b) + f(n)

T(1) = d

a

a a a

Which term, if any,

dominates the sum?

Page 28: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

28

T(n) = aT(n/b) + f(n)

T(1) = d

a

a a a

If top-of-tree work

dominates,

T(n) = ???

Page 29: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

29

T(n) = aT(n/b) + f(n)

T(1) = d

a

a a a

If top-of-tree work

dominates,

T(n) = Θ(f(n))

Page 30: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

30

T(n) = aT(n/b) + f(n)

T(1) = d

a

a a a

If bottom-of-tree

work dominates,

𝑇 𝑛 =? ? ?

Page 31: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

31

T(n) = aT(n/b) + f(n)

T(1) = d

a

a a a

If bottom-of-tree

work dominates,

𝑇 𝑛 = 𝚯(𝒏𝐥𝐨𝐠𝒃 𝒂)

Page 32: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

What if the top and

bottom work balance?

32

Page 33: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

What does “balance” mean?

● Top and bottom work are asymptotically the same.

● In other words,

33

𝒇 𝒏 = 𝚯(𝒏𝐥𝐨𝐠𝒃 𝒂)

Page 34: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

What does “balance” mean?

● Top and bottom work are asymptotically the same.

● In other words,

34

𝒇 𝒏 = 𝚯(𝒏𝐥𝐨𝐠𝒃 𝒂)

For intuition, we’ll pretend that 𝑓 𝑛 = 𝑐𝑛log𝑏 𝑎

Page 35: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

35

T(n) = aT(n/b) + f(n)

T(1) = d

Page 36: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

36

T(n) = aT(n/b) + f(n)

T(1) = d

Page 37: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

37

T(n) = aT(n/b) + f(n)

T(1) = d

Page 38: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

38

T(n) = aT(n/b) + f(n)

T(1) = d

Page 39: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

39

T(n) = aT(n/b) + f(n)

T(1) = d

Page 40: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

40

T(n) = aT(n/b) + f(n)

T(1) = d

Page 41: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

41

T(n) = aT(n/b) + f(n)

T(1) = d

Page 42: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

42

T(n) = aT(n/b) + f(n)

T(1) = d

𝑇 𝑛 = 𝑑𝑛log𝑏 𝑎 + 𝑐𝑛log𝑏 𝑎 log𝑏 𝑛

Page 43: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

43

T(n) = aT(n/b) + f(n)

T(1) = d

))

When top and bottom of tree balance, all

levels contribute equally to sum – and there

are Θ(log n) levels.

𝑇 𝑛 = 𝑑𝑛log𝑏 𝑎 + 𝑐𝑛log𝑏 𝑎 log𝑏 𝑛

Page 44: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Summary of Intuition

● Given recurrence T(n) = aT(n/b) + f(n)…

● If f(n) dominates 𝑛log𝑏 𝑎, then solution should be Θ(f(n))

● If 𝑛log𝑏 𝑎 dominates f(n), then solution should be Θ(𝑛log𝑏 𝑎)

● If f(n) = Θ(𝑛log𝑏 𝑎) [balance], then solution should be Θ(f(n) log n)

44

Page 45: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Summary of Intuition

● Given recurrence T(n) = aT(n/b) + f(n)…

● If f(n) dominates 𝑛log𝑏 𝑎, then solution should be Θ(f(n))

● If 𝑛log𝑏 𝑎 dominates f(n), then solution should be Θ(𝑛log𝑏 𝑎)

● If f(n) = Θ(𝑛log𝑏 𝑎) [balance], then solution should be Θ(f(n) log n)

45

This is not yet a

theorem – in part

because we haven’t

carefully defined

“dominates,” and in

part because we didn’t

do a careful proof.

Page 46: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

So is there a

theorem that

captures our

intuition?

46

Page 47: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Master Theorem (p. 94 of text)

47

Page 48: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Master Theorem (p. 94 of text)

48

We won’t actually prove it

(see the book), but we will

break down the statement.

Page 49: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Master Theorem (p. 94 of text)

49

Page 50: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Master Theorem (p. 94 of text)

50

This is the scenario we’ve

been studying!

Page 51: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Master Theorem (p. 94 of text)

51

Page 52: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Master Theorem (p. 94 of text)

52

Theorem generalizes to non-

power-of-b input sizes!

Page 53: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Master Theorem (p. 94 of text)

53

Page 54: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Master Theorem (p. 94 of text)

54

“𝑛log𝑏 𝑎 dominates f(n)”

Page 55: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Master Theorem (p. 94 of text)

55

Page 56: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Master Theorem (p. 94 of text)

56

“f(n) dominates 𝑛log𝑏 𝑎”

Page 57: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Master Theorem (p. 94 of text)

57

Page 58: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Master Theorem (p. 94 of text)

58

“if f(n) is not a weird function”

Page 59: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Master Theorem (p. 94 of text)

59

“if f(n) is not a weird function”

[polynomials, logs, exponentials,

and sums and products of them

are not weird! (exercise) ]

Page 60: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Master Theorem (p. 94 of text)

60

“if f(n) is not a weird function”

[Otherwise, check. See

Wikipedia on Master Theorem for

examples of weird functions.]

Page 61: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Master Theorem (p. 94 of text)

61

“if f(n) is not a weird function”

“Weird” ~ middle-of-tree work

“blows up” compared to root

Page 62: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Key Elaboration of Theorem vs Intuition

● Precisely defines “dominates”

● “f(n) dominates g(n)” iff f(n) grows polynomially faster

than g(n)

● This is a stronger condition than f(n) = ω(g(n))

62

Page 63: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

63

f(n) = O(𝑛log𝑏 𝑎−𝜖)

f(n) = Ω(𝑛log𝑏 𝑎+𝜖)

f(n) = Θ(𝑛log𝑏 𝑎)

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

f(n

) g

row

s f

aste

r

Page 64: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

64

f(n) = O(𝑛log𝑏 𝑎−𝜖)

f(n) = Ω(𝑛log𝑏 𝑎+𝜖)

f(n) = Θ(𝑛log𝑏 𝑎)

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

T(n) = Θ(f(n))

T(n) = Θ(𝑛log𝑏 𝑎)

T(n) = Θ(𝑓 𝑛 log 𝑛)

Case 1.

Case 2.

Case 3.

Page 65: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

65

f(n) = O(𝑛log𝑏 𝑎−𝜖)

f(n) = Ω(𝑛log𝑏 𝑎+𝜖)

f(n) = Θ(𝑛log𝑏 𝑎)

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

Case 1.

Case 2.

Case 3.

f(n) = ω(𝑛log𝑏 𝑎), but f(n) = o(𝑛log𝑏 𝑎+𝜖)

f(n) = o(𝑛log𝑏 𝑎), but f(n) = ω(𝑛log𝑏 𝑎−𝜖)

Page 66: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

66

f(n) = O(𝑛log𝑏 𝑎−𝜖)

f(n) = Ω(𝑛log𝑏 𝑎+𝜖)

f(n) = Θ(𝑛log𝑏 𝑎)

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

Case 1.

Case 2.

Case 3.

f(n) = ω(𝑛log𝑏 𝑎), but f(n) = o(𝑛log𝑏 𝑎+𝜖)

f(n) = o(𝑛log𝑏 𝑎), but f(n) = ω(𝑛log𝑏 𝑎−𝜖)

?

?

Page 67: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

67

f(n) = O(𝑛log𝑏 𝑎−𝜖)

f(n) = Ω(𝑛log𝑏 𝑎+𝜖)

f(n) = Θ(𝑛log𝑏 𝑎)

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

Case 1.

Case 2.

Case 3.

f(n) = ω(𝑛log𝑏 𝑎), but f(n) = o(𝑛log𝑏 𝑎+𝜖)

f(n) = o(𝑛log𝑏 𝑎), but f(n) = ω(𝑛log𝑏 𝑎−𝜖)

?

?

Page 68: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Limits of the Master Theorem

● If the form of the recurrence does not match the statement

of the theorem…

● …or the recurrence falls into “gap” between two cases…

● …then the Master Theorem does not apply. (*as given

here—some cases still fit the extended version we'll see in Studio 5)

● (You must find another way to solve the recurrence.) 68

Page 69: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Limits of the Master Theorem

69

● See the Wikipedia page on the Master Theorem

● Examples of situations where Master Thm doesn’t apply○ Note: a and b in Master Theorem don’t have to be integers

■ (though for recursive programs, a is an integer – why?)

■ a must be ≥ 1

■ b must be > 1 – why?

● Example of function that fails non-weirdness condition

Page 70: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

n log n vs. n1+ε

70

● Example recurrence: T(n) = 2T(n/2) + nlogn

● Question: does Case 3 apply?

○ I.e. does n log n = Ω(n1+ε) for some ε > 0 ?

Page 71: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

n log n vs. n1+ε

71

● Example recurrence: T(n) = 2T(n/2) + nlogn

● Question: does Case 3 apply?

○ I.e. does n log n = Ω(n1+ε) for some ε > 0 ?

● Analysis by limit test○ lim (n log n) / (n1+ε) = lim (log n + 1) / (1+ε)nε

Page 72: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

n log n vs. n1+ε

72

● Example recurrence: T(n) = 2T(n/2) + nlogn

● Question: does Case 3 apply?

○ I.e. does n log n = Ω(n1+ε) for some ε > 0 ?

● Analysis by limit test○ lim (n log n) / (n1+ε) = lim (log n + 1) / (1+ε)nε

○ = lim (1/n) / ε(1+ε)nε-1

Page 73: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

n log n vs. n1+ε

73

● Example recurrence: T(n) = 2T(n/2) + nlogn

● Question: does Case 3 apply?

○ I.e. does n log n = Ω(n1+ε) for some ε > 0 ?

● Analysis by limit test○ lim (n log n) / (n1+ε) = lim (log n + 1) / (1+ε)nε

○ = lim (1/n) / ε(1+ε)nε-1

○ = lim (1 / ε(1+ε)nnε-1)

Page 74: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

n log n vs. n1+ε

74

● Example recurrence: T(n) = 2T(n/2) + nlogn

● Question: does Case 3 apply?

○ I.e. does n log n = Ω(n1+ε) for some ε > 0 ?

● Analysis by limit test○ lim (n log n) / (n1+ε) = lim (log n + 1) / (1+ε)nε

○ = lim (1/n) / ε(1+ε)nε-1

○ = lim (1 / ε(1+ε)nnε-1)

○ = lim (1 / ε(1+ε)nε) = 0, because ε > 0

● Hence, n log n is o(n1+ε) for every ε > 0

● So NO, Case 3 of Master Theorem does not apply.

Page 75: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

n log n vs. n1+ε

75

● Example recurrence: T(n) = 2T(n/2) + nlogn

● Question: does Case 3 apply?

○ I.e. does n log n = Ω(n1+ε) for some ε > 0 ?

● Analysis by limit test○ lim (n log n) / (n1+ε) = lim (log n + 1) / (1+ε)nε

○ = lim (1/n) / ε(1+ε)nε-1

○ = lim (1 / ε(1+ε)nnε-1)

○ = lim (1 / ε(1+ε)nε) = 0, because ε > 0

● Hence, n log n is o(n1+ε) for every ε > 0

● So NO, Case 3 of Master Theorem does not apply.

But for Studio 5, see Wiki for a

more general “balanced case”

that specifically allows for f(n)

to have extra log terms.

Page 76: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

A little practice with “polynomially larger”

76

polynomially larger than?

n2 n

n2 log n n2

n3 log n n2

n2.001 n2

n log n 1𝑛log4 3

Page 77: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

A little practice with “polynomially larger”

77

polynomially larger than?

n2 YES n

n2 log n NO n2

n3 log n YES n2

n2.001 YES n2

n log n ??? 1𝑛log4 3

Page 78: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

A little practice with “polynomially larger”

78

polynomially larger than?

n2 YES n

n2 log n NO n2

n3 log n YES n2

n2.001 YES n2

n log n YES! 1𝑛log4 3

Page 79: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Applying the Master Theorem

● T(n) = 4T(n/2) + cn

79

Page 80: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Applying the Master Theorem

● T(n) = 4T(n/2) + cn

● a = ???, b = ???, f(n) = ???

80

Page 81: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Applying the Master Theorem

● T(n) = 4T(n/2) + cn

● a = 4, b = 2, f(n) = cn

81

Page 82: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Applying the Master Theorem

● T(n) = 4T(n/2) + cn

● a = 4, b = 2, f(n) = cn

● Compare 𝑛log𝑏 𝑎 vs f(n)

82

Page 83: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Applying the Master Theorem

● T(n) = 4T(n/2) + cn

● a = 4, b = 2, f(n) = cn

● Compare 𝑛log2 4 vs cn

83

Page 84: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Applying the Master Theorem

● T(n) = 4T(n/2) + cn

● a = 4, b = 2, f(n) = cn

● Compare 𝑛2 vs cn

84

Page 85: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Applying the Master Theorem

● T(n) = 4T(n/2) + cn

● a = 4, b = 2, f(n) = cn

● Compare 𝑛2 vs cn → f(n) = O(𝑛log𝑏 𝑎 −1)

● Therefore T(n) = Θ(𝑛log𝑏 𝑎) = Θ(n2)

85

Page 86: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Applying the Master Theorem

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

86

Page 87: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Applying the Master Theorem

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

● a = ???, b = ???, f(n) = ???

87

Page 88: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Applying the Master Theorem

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

● a = 2, b = 2, f(n) = cn

88

Page 89: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Applying the Master Theorem

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

● a = 2, b = 2, f(n) = cn

● Compare 𝑛log𝑏 𝑎 vs f(n)

89

Page 90: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Applying the Master Theorem

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

● a = 2, b = 2, f(n) = cn

● Compare 𝑛log2 2 vs cn

90

Page 91: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Applying the Master Theorem

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

● a = 2, b = 2, f(n) = cn

● Compare 𝑛1 vs cn

91

Page 92: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Applying the Master Theorem

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

● a = 2, b = 2, f(n) = cn

● Compare 𝑛1 vs cn → f(n) = Θ(𝑛log𝑏 𝑎)

● Therefore T(n) = Θ(f(n) log n) = Θ(n log n)

92

Page 93: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Applying the Master Theorem

● T(n) = T(2n/3) + c

93

Page 94: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Applying the Master Theorem

● T(n) = T(2n/3) + c

● a = ???, b = ???, f(n) = ???

94

Page 95: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Applying the Master Theorem

● T(n) = T(2n/3) + c

● a = 1, b = 3/2, f(n) = c

95

Page 96: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Applying the Master Theorem

● T(n) = T(2n/3) + c

● a = 1, b = 3/2, f(n) = c

● Compare 𝑛log𝑏 𝑎 vs f(n)

96

Page 97: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Applying the Master Theorem

● T(n) = T(2n/3) + c

● a = 1, b = 3/2, f(n) = c

● Compare 𝑛log3/2 1 vs cn0

97

Page 98: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Applying the Master Theorem

● T(n) = T(2n/3) + c

● a = 1, b = 3/2, f(n) = c

● Compare 𝑛0 vs cn0 → f(n) = Θ(𝑛log𝑏 𝑎)

● Therefore T(n) = Θ(f(n) log n) = Θ(log n)

98

Page 99: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Applying the Master Theorem

● T(n) = 3T(n/4) + cn log n

99

Page 100: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Applying the Master Theorem

● T(n) = 3T(n/4) + cn log n

● a = ???, b = ???, f(n) = ???

100

Page 101: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Applying the Master Theorem

● T(n) = 3T(n/4) + cn log n

● a = 3, b = 4, f(n) = cn log n

101

Page 102: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Applying the Master Theorem

● T(n) = 3T(n/4) + cn log n

● a = 3, b = 4, f(n) = cn log n

● Compare 𝑛log𝑏 𝑎 vs f(n)

102

Page 103: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Applying the Master Theorem

● T(n) = 3T(n/4) + cn log n

● a = 3, b = 4, f(n) = cn log n

● Compare 𝑛log4 3 vs cn log n

103

Page 104: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

Applying the Master Theorem

104

Page 105: Lecture 5: Solving Recurrences via the Master Method · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic complexity function

You’ll get more Master

Method practice, plus

bonus experience with

Binary Search, in

Studio 5.

105