106
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.

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

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

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

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.

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

Announcements

● Exam 1 tomorrow night (see Piazza post for all details)

○ Crib sheet, ID, where to go

● Lab 1 grades posted, Lab 3 grades in progress

○ 1 week regrade request deadline from posting time

● Studio 5 on Thursday as normal

2

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

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)

3

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

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

● [The same one we did at the end of last time]

4

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

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

● This time, a = 3, so

each node branches

3 ways!

5

...

......

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

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.

6

T(n)

T(n/4)

T(n/16)

...

...

T(n/4j)...

T(1)

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

7

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 8: Lecture 5: Solving Recurrences via the Master Method · 2019. 9. 3. · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic

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

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

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

3log4 𝑛

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

10

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 11: Lecture 5: Solving Recurrences via the Master Method · 2019. 9. 3. · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic

11

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 12: Lecture 5: Solving Recurrences via the Master Method · 2019. 9. 3. · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic

12

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 13: Lecture 5: Solving Recurrences via the Master Method · 2019. 9. 3. · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic

13

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 14: Lecture 5: Solving Recurrences via the Master Method · 2019. 9. 3. · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic

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 · 2019. 9. 3. · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic

15

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 16: Lecture 5: Solving Recurrences via the Master Method · 2019. 9. 3. · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic

Let’s Break This Summation Down a Bit

16

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

Let’s Break This Summation Down a Bit

17

(pulled first term out of sum)

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

18

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

T(1) = d

Which parts of the tree

contribute to which parts

of the sum?

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

19

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

T(1) = d

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

20

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

T(1) = d

This term is from the

base case (i.e.

bottom of the tree).

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

21

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

T(1) = d

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

22

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 23: Lecture 5: Solving Recurrences via the Master Method · 2019. 9. 3. · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic

23

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

T(1) = d

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

24

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 25: Lecture 5: Solving Recurrences via the Master Method · 2019. 9. 3. · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic

Let’s Generalize

● We split up the sum for a particular recurrence

25

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

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

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

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

Let’s Generalize

● We split up the sum for a particular recurrence

● Let’s do this for a general recurrence

27

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 28: Lecture 5: Solving Recurrences via the Master Method · 2019. 9. 3. · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic

28

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

T(1) = d

a

a a a

Which term, if any,

dominates the sum?

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

29

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

T(1) = d

a

a a a

If top-of-tree work

dominates,

T(n) = ???

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

30

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 31: Lecture 5: Solving Recurrences via the Master Method · 2019. 9. 3. · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic

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 · 2019. 9. 3. · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic

32

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

T(1) = d

a

a a a

If bottom-of-tree

work dominates,

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

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

What if the top and

bottom work balance?

33

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

What does “balance” mean?

● Top and bottom work are asymptotically the same.

● In other words,

34

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

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

What does “balance” mean?

● Top and bottom work are asymptotically the same.

● In other words,

35

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

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

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

36

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

T(1) = d

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

37

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

T(1) = d

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

38

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

T(1) = d

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

39

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

T(1) = d

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

40

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

T(1) = d

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

41

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

T(1) = d

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

42

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

T(1) = d

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

43

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

T(1) = d

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

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

44

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.

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

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

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

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)

46

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 47: Lecture 5: Solving Recurrences via the Master Method · 2019. 9. 3. · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic

So is there a

theorem that

captures our

intuition?

47

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

Master Theorem (p. 94 of text)

48

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

Master Theorem (p. 94 of text)

49

We won’t actually prove it

(see the book), but we will

break down the statement.

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

Master Theorem (p. 94 of text)

50

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

Master Theorem (p. 94 of text)

51

This is the scenario we’ve

been studying!

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

Master Theorem (p. 94 of text)

52

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

Master Theorem (p. 94 of text)

53

Theorem generalizes to non-

power-of-b input sizes!

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

Master Theorem (p. 94 of text)

54

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

Master Theorem (p. 94 of text)

55

“𝑛log𝑏 𝑎 dominates f(n)”

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

Master Theorem (p. 94 of text)

56

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

Master Theorem (p. 94 of text)

57

“f(n) dominates 𝑛log𝑏 𝑎”

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

Master Theorem (p. 94 of text)

58

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

Master Theorem (p. 94 of text)

59

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

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

Master Theorem (p. 94 of text)

60

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

[polynomials, logs, exponentials,

and sums and products of them

are not weird! (exercise) ]

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

Master Theorem (p. 94 of text)

61

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

[Otherwise, check. See

Wikipedia on Master Theorem for

examples of weird functions.]

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

Master Theorem (p. 94 of text)

62

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

“Weird” ~ middle-of-tree work

“blows up” compared to root

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

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))

63

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

64

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 65: Lecture 5: Solving Recurrences via the Master Method · 2019. 9. 3. · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic

65

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 66: Lecture 5: Solving Recurrences via the Master Method · 2019. 9. 3. · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic

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 · 2019. 9. 3. · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic

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 · 2019. 9. 3. · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic

68

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 69: Lecture 5: Solving Recurrences via the Master Method · 2019. 9. 3. · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic

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.

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

69

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

Limits of the Master Theorem

70

● 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 71: Lecture 5: Solving Recurrences via the Master Method · 2019. 9. 3. · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic

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 ?

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

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ε

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

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

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

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)

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

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.

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

n log n vs. n1+ε

76

● 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 77: Lecture 5: Solving Recurrences via the Master Method · 2019. 9. 3. · Overview: recurrence-solving strategies Problem: given a recurrence for T(n), find a closed- form asymptotic

A little practice with “polynomially larger”

77

polynomially larger than?

n2 n

n2 log n n2

n3 log n n2

n2.001 n2

n log n 𝑛log4 3

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

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 ??? 𝑛log4 3

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

A little practice with “polynomially larger”

79

polynomially larger than?

n2 YES n

n2 log n NO n2

n3 log n YES n2

n2.001 YES n2

n log n YES! 𝑛log4 3

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

Applying the Master Theorem

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

80

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

Applying the Master Theorem

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

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

81

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

Applying the Master Theorem

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

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

82

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

Applying the Master Theorem

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

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

● Compare 𝑛log𝑏 𝑎 vs f(n)

83

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

Applying the Master Theorem

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

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

● Compare 𝑛log2 2 vs cn

84

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

Applying the Master Theorem

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

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

● Compare 𝑛1 vs cn

85

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

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)

86

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

Applying the Master Theorem

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

87

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

Applying the Master Theorem

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

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

88

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

Applying the Master Theorem

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

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

89

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

Applying the Master Theorem

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

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

● Compare 𝑛log𝑏 𝑎 vs f(n)

90

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

Applying the Master Theorem

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

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

● Compare 𝑛log3/2 1 vs cn0

91

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

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)

92

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

Applying the Master Theorem

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

93

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

Applying the Master Theorem

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

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

94

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

Applying the Master Theorem

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

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

95

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

Applying the Master Theorem

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

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

● Compare 𝑛log𝑏 𝑎 vs f(n)

96

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

Applying the Master Theorem

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

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

● Compare 𝑛log2 4 vs cn

97

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

Applying the Master Theorem

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

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

● Compare 𝑛2 vs cn

98

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

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)

99

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

Applying the Master Theorem

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

100

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

Applying the Master Theorem

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

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

101

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

Applying the Master Theorem

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

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

102

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

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)

103

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

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

104

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

Applying the Master Theorem

105

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

You’ll get more Master

Method practice, plus

bonus experience with

Binary Search, in

Studio 5.

106