1 of 66

CSE 373 Section 3

Recurrence Party 🎉

​

(Grab a handout as you enter)

2 of 66

Agenda for Today

  • Announcements
  • Recurrences and their approaches
  • Recap: What is a recurrence?
  • Practice with recurrences
  • Master Theorem
  • Tree Method

3 of 66

Announcements

  • P1 deadline extended to tonight!
    • Hard deadline also extended to Sunday July 10 at 23:59
  • P2 is out!
    • 2 weeks to complete the assignment
    • Due Wednesday July 20 at 23:59
    • START EARLY!! 🕒
  • Exercise 2 releases tomorrow
    • Due Friday July 15 at 23:59
    • Hard deadline July 18 at 23:59

​

4 of 66

Quick Reminder: Big-O Complexity

For simplification of mathematical expressions you can use Wolfram Alpha

If you’re unsure how the graph of a function looks like, desmos is your best friend!

5 of 66

Recurrences and their Approaches

6 of 66

Big Idea

Three* steps in solving for the runtime of a recursive function.

​

Model Code with Recurrence

Use Tree Method to Turn Recurrence into Summation.

Solve Summation for Closed Form

If Possible, Use Master Theorem for Tight Bound

7 of 66

Modeling Code with Recurrences?

​

8 of 66

What is a Recurrence?

An expression that lets us express the runtime of a function recursively.��

​

9 of 66

What is a Recurrence?

An expression that lets us express the runtime a function recursively.��

​

Base Case

​

Recursive Case

​

10 of 66

What is a Recurrence?

An expression that lets us express the runtime a function recursively.��

​

Base Case

​

Recursive Case

​

Recursive Call

11 of 66

Do Problems 1a + 3a Together!

​

​

Hint 2: You should use the answer from 1a to get 3a more quickly!

​

Hint 1: This is helpful for 1a (see “cheat-sheet” at end of PDF):

​

12 of 66

1a: Finding Bounds

13 of 66

1a: Finding Bounds

Outer loop runs N times

N

14 of 66

1a: Finding Bounds

The inner loop depends on the outer loop

N

15 of 66

1a: Finding Bounds

Inside the inner loop, we have a constant time operation

​

N

16 of 66

1a: Finding Bounds

Outer loop runs 0 ~ n - 1 times which defines i

Inner loop runs 0 ~ i - 1 times

As inner loop depends on the outer loop, we can express as the summation below.

N

N

17 of 66

Remember: Gauss’s identity

N

18 of 66

1a: Finding Bounds

​

POSSIBLE RUNTIME:

T(N) = N(N-1)/2 = ½ N2 - ½ N

​

WORST CASE RUNTIME:

Θ(N2)

​

​

N2/2 + N/2 => N2

Best/worst cases agree!

19 of 66

3a: Code To Recurrence

20 of 66

3a: Code To Recurrence

21 of 66

3a: Code To Recurrence

Base case when the input N <= 1

22 of 66

3a: Code To Recurrence

We saw the runtime for this loop earlier!

23 of 66

3a: Code To Recurrence

It’s N(N-1)/2!

(see Finding Bounds a)

24 of 66

3a: Code To Recurrence

Call f(N/2)

Constant addition

Call f(N/2)

Call f(N/2)

Call f(N/2)

25 of 66

3a: Code To Recurrence

Calling 4 functions and the input is divided by half each level

Work happening in both outer and inner loops

(see 1a)

26 of 66

3a: Code To Recurrence

Calling 4 functions and the input is divided by half each level

Work happening in both outer and inner loops

(see 1a)

Can also simplify non-recursive term (and only this term) using Big-O rules.

​

So you could also have put N2 here.

27 of 66

3a: Code To Recurrence

Calling 4 functions and the input is divided by half each level

Work happening in both outer and inner loops

(see 1a)

Can also simplify non-recursive term (and only this term) using Big-O rules.

​

So you could also have put N2 here.

In fact, you should simplify before doing Tree Method!

28 of 66

Shortcut: Master Theorem

29 of 66

Where are we?

Three* steps in solving for the runtime of a recursive function.

​

Model Code with Recurrence

Use Tree Method to Turn Recurrence into Summation.

Solve Summation for Closed Form

If Possible, Use Master Theorem for Tight Bound

30 of 66

Master Theorem: A “Cheat Code” (Kinda)

NOTE: If you get lucky and your recurrence matches the Master Theorem form, then you may use this formula to jump right to the final closed form.

​

(Of course, a lot of the time

it won’t match. That’s why tree method is still important.)

31 of 66

Use Master Theorem to find closed form

Original recurrence:

Step 1: Does T(n) match Master Theorem form?

a = 4

b = 2

e = 1

c = 2

Recall: could also have put N2 here.

32 of 66

Use Master Theorem to find closed form

Original recurrence:

Step 2: Calculate logb(a) and compare it to c

a = 4

b = 2

e = 1

c = 2

logb(a) = log2(4)

log2(4) = 2

c = 2 so logb(a) = c

logb(a) = c so T(n) ∈ Θ(n2log n)

Ta-da!

33 of 66

4. Master Theorem

Use the cheat sheet attached to your section handout!

34 of 66

The Core Tree Method

35 of 66

Where are we?

Three* steps in solving for the runtime of a recursive function.

​

Model Code with Recurrence

Use Tree Method to Turn Recurrence into Summation.

Solve Summation for Closed Form

If Possible, Use Master Theorem for Tight Bound

36 of 66

Tree Method: Big Idea

This is a drawing approach that turns recurrences into summations.

​

37 of 66

Tree Method: Big Idea

This is a drawing approach that turns recurrences into summations.

​

i =0

i =1

i =2

i = 3

i = ???

38 of 66

Tree Method: Big Idea

This is a drawing approach that turns recurrences into summations.

​

i =0

i =1

i =2

i = 3

i = ???

Total Work:

39 of 66

Tree Method: Big Idea

This is a drawing approach that turns recurrences into summations.

​

i =0

i =1

i =2

i = 3

i = ???

But to actually compute this, there are multiple sub-questions we have to ask! ��(See Problem 5)

Total Work:

40 of 66

Problem 5a-g: Recurrence to Summation

41 of 66

The Tree

42 of 66

The Tree

43 of 66

The Tree

44 of 66

The Tree

45 of 66

5a: Input to a node at level i

We divide by 6 at each level, so the input at level i is n/6i.

46 of 66

5b: Work per node at (recursive) level i?

Same as the input to a node (in this case), so also n/6i.

47 of 66

5c: Number of nodes at level i

Each (non-base-case) node produces 3 more nodes, so at level i we have 3i nodes.

48 of 66

5d: Total work at the ith recursive level

(number of nodes in level) x (work per node at level)

49 of 66

5d: Total work at the ith recursive level

(number of nodes in level) x (work per node at level)

3i x n/6i

50 of 66

5e: Last Level of the Tree

We hit our base case when n/6i = 1.

51 of 66

5e: Last Level of the Tree

n/6i = 1

52 of 66

5e: Last Level of the Tree

n = 6i

53 of 66

5e: Last Level of the Tree

log6(n) = i

54 of 66

5f: Total Work Done in the Base Case

(number of nodes in base case level) x (work per node in base case level)

55 of 66

5f: Total Work Done in the Base Case

(number of nodes in base case level) x (work per node in base case level)

56 of 66

5f: Total Work Done in the Base Case

(number of nodes in base case level) x (work per node in base case level)

57 of 66

5g: Total Work Summation

58 of 66

5g: Total Work Summation

59 of 66

Problem 5h: Getting the Closed Form

60 of 66

Where are we?

Three* steps in solving for the runtime of a recursive function.

​

Model Code with Recurrence

Use Tree Method to Turn Recurrence into Summation.

Solve Summation for Closed Form

If Possible, Use Master Theorem for Tight Bound

61 of 66

5h: Simplify to a closed form

Simplify the summation if possible (look for terms that can be pushed out of the summation)

Does this match any of our identities?

62 of 66

5h: Simplify to a closed form

63 of 66

5h: Simplify to a closed form

NOTE: You don’t have to simplify further, but if you were, you would get the following: (See section solutions for steps)

64 of 66

Problem 5i: Master Theorem

65 of 66

5i: Use Master Theorem to find closed form

Original recurrence:

Step 1: Does A(n) match Master Theorem form?

a = 3

b = 6

e = 1

c = 1

66 of 66

5i: Use Master Theorem to find closed form

Original recurrence:

Step 2: Calculate logb(a) and compare it to c

a = 3

b = 6

e = 1

c = 1

logb(a) = log6(3)

log6(3) = x

6x = 3 so x < 1

c = 1 so x < c

logb(a) < c so A(n) ∈ Θ(n)

Ta-da!