CSC236 Week 4 › ~236 › larry › 4 › lec04.pdf · Every recursively defined function has an...
Transcript of CSC236 Week 4 › ~236 › larry › 4 › lec04.pdf · Every recursively defined function has an...
![Page 1: CSC236 Week 4 › ~236 › larry › 4 › lec04.pdf · Every recursively defined function has an equivalent closed-form function. And we like closed-form functions. It is easier](https://reader033.fdocuments.net/reader033/viewer/2022053008/5f0c56387e708231d434e605/html5/thumbnails/1.jpg)
CSC236 Week 4Larry Zhang
1
![Page 2: CSC236 Week 4 › ~236 › larry › 4 › lec04.pdf · Every recursively defined function has an equivalent closed-form function. And we like closed-form functions. It is easier](https://reader033.fdocuments.net/reader033/viewer/2022053008/5f0c56387e708231d434e605/html5/thumbnails/2.jpg)
Logistics
2
![Page 3: CSC236 Week 4 › ~236 › larry › 4 › lec04.pdf · Every recursively defined function has an equivalent closed-form function. And we like closed-form functions. It is easier](https://reader033.fdocuments.net/reader033/viewer/2022053008/5f0c56387e708231d434e605/html5/thumbnails/3.jpg)
NEW TOPIC
RecursionTo really understand the math of recursion,
and to be able to analyze runtimes of recursive programs.
3
![Page 4: CSC236 Week 4 › ~236 › larry › 4 › lec04.pdf · Every recursively defined function has an equivalent closed-form function. And we like closed-form functions. It is easier](https://reader033.fdocuments.net/reader033/viewer/2022053008/5f0c56387e708231d434e605/html5/thumbnails/4.jpg)
ConcepTest
4
![Page 5: CSC236 Week 4 › ~236 › larry › 4 › lec04.pdf · Every recursively defined function has an equivalent closed-form function. And we like closed-form functions. It is easier](https://reader033.fdocuments.net/reader033/viewer/2022053008/5f0c56387e708231d434e605/html5/thumbnails/5.jpg)
Recursively Defined Functions
5
The functions that describe the runtime of recursive programs
![Page 6: CSC236 Week 4 › ~236 › larry › 4 › lec04.pdf · Every recursively defined function has an equivalent closed-form function. And we like closed-form functions. It is easier](https://reader033.fdocuments.net/reader033/viewer/2022053008/5f0c56387e708231d434e605/html5/thumbnails/6.jpg)
Definition of functions
● The usual way: define a function using a closed-form.
6
● Another way: using a recursive definition
A function defined in terms of itself.
![Page 7: CSC236 Week 4 › ~236 › larry › 4 › lec04.pdf · Every recursively defined function has an equivalent closed-form function. And we like closed-form functions. It is easier](https://reader033.fdocuments.net/reader033/viewer/2022053008/5f0c56387e708231d434e605/html5/thumbnails/7.jpg)
Let’s work out a few values for T₀(n) and T₁(n)
7
T₀(n) T₁(n)n=1
n=2
n=3
n=4
n=5
1
3
7
15
31
1
3
7
15
31
Maybe T₀(n) and T₁(n) are equivalent to each other.
![Page 8: CSC236 Week 4 › ~236 › larry › 4 › lec04.pdf · Every recursively defined function has an equivalent closed-form function. And we like closed-form functions. It is easier](https://reader033.fdocuments.net/reader033/viewer/2022053008/5f0c56387e708231d434e605/html5/thumbnails/8.jpg)
Every recursively defined function has an equivalent closed-form function.
And we like closed-form functions.
● It is easier to evaluate○ Just need n to know f(n)○ Instead of having to know f(n-1) to know f(n)
● It is easier for telling the growth rate of the function○ T₀(n) is clearly Θ(2ⁿ), while T₁(n) it not clear.
8
![Page 9: CSC236 Week 4 › ~236 › larry › 4 › lec04.pdf · Every recursively defined function has an equivalent closed-form function. And we like closed-form functions. It is easier](https://reader033.fdocuments.net/reader033/viewer/2022053008/5f0c56387e708231d434e605/html5/thumbnails/9.jpg)
Our Goal:Given a recursively defined function,
find its equivalent closed-form function
9
![Page 10: CSC236 Week 4 › ~236 › larry › 4 › lec04.pdf · Every recursively defined function has an equivalent closed-form function. And we like closed-form functions. It is easier](https://reader033.fdocuments.net/reader033/viewer/2022053008/5f0c56387e708231d434e605/html5/thumbnails/10.jpg)
Method #1Repeated Substitution
10
![Page 11: CSC236 Week 4 › ~236 › larry › 4 › lec04.pdf · Every recursively defined function has an equivalent closed-form function. And we like closed-form functions. It is easier](https://reader033.fdocuments.net/reader033/viewer/2022053008/5f0c56387e708231d434e605/html5/thumbnails/11.jpg)
Repeated substitution: StepsStep 1: Substitute a few times to find a pattern
Step 2: Guess the recurrence formula after k substitutions (in terms of k and n)
For each base case:
Step 3: solve for k
Step 4: Plug k back into the formula (from Step 2) to find a potential closed form. (“Potential” because it might be wrong)
Step 5: Prove the potential closed form is equivalent to the recursive definition using induction.
11
![Page 12: CSC236 Week 4 › ~236 › larry › 4 › lec04.pdf · Every recursively defined function has an equivalent closed-form function. And we like closed-form functions. It is easier](https://reader033.fdocuments.net/reader033/viewer/2022053008/5f0c56387e708231d434e605/html5/thumbnails/12.jpg)
Example 1
12
![Page 13: CSC236 Week 4 › ~236 › larry › 4 › lec04.pdf · Every recursively defined function has an equivalent closed-form function. And we like closed-form functions. It is easier](https://reader033.fdocuments.net/reader033/viewer/2022053008/5f0c56387e708231d434e605/html5/thumbnails/13.jpg)
13
Step 1
Substitute a few times to find a pattern.
Substitute T(n-1) with T(n-2):
Substitute T(n-2) with T(n-3):
Substitute T(n-3) with T(n-4):
![Page 14: CSC236 Week 4 › ~236 › larry › 4 › lec04.pdf · Every recursively defined function has an equivalent closed-form function. And we like closed-form functions. It is easier](https://reader033.fdocuments.net/reader033/viewer/2022053008/5f0c56387e708231d434e605/html5/thumbnails/14.jpg)
Step 2:
Guess the recurrence formula after k substitutions
14
The guess:
![Page 15: CSC236 Week 4 › ~236 › larry › 4 › lec04.pdf · Every recursively defined function has an equivalent closed-form function. And we like closed-form functions. It is easier](https://reader033.fdocuments.net/reader033/viewer/2022053008/5f0c56387e708231d434e605/html5/thumbnails/15.jpg)
Step 3: Consider the base case
What we have now:
15
We want this to be T(1), because we know clearly what T(1) is.
So, let n - k = 1, and solve for k
k = n - 1This means you need to make n-1 substitutions to reach the base case n=1
![Page 16: CSC236 Week 4 › ~236 › larry › 4 › lec04.pdf · Every recursively defined function has an equivalent closed-form function. And we like closed-form functions. It is easier](https://reader033.fdocuments.net/reader033/viewer/2022053008/5f0c56387e708231d434e605/html5/thumbnails/16.jpg)
Step 4: plug k back into the guessed formula
16
Guessed formula:
For Step 3, we got k = n - 1
Substitute k back in, we get ...
This is the “potential” closed-form of T(n).
![Page 17: CSC236 Week 4 › ~236 › larry › 4 › lec04.pdf · Every recursively defined function has an equivalent closed-form function. And we like closed-form functions. It is easier](https://reader033.fdocuments.net/reader033/viewer/2022053008/5f0c56387e708231d434e605/html5/thumbnails/17.jpg)
Step 5: Drop the word “potential” by proving it
17
Prove is equivalent to
Use induction!
Define predicate: P(n): T₁(n) = T₀(n)Base case: n=1
Induction step:
# by I.H.
# by def of T(n)
![Page 18: CSC236 Week 4 › ~236 › larry › 4 › lec04.pdf · Every recursively defined function has an equivalent closed-form function. And we like closed-form functions. It is easier](https://reader033.fdocuments.net/reader033/viewer/2022053008/5f0c56387e708231d434e605/html5/thumbnails/18.jpg)
We have officially found that the closed-form of the recursively defined function
18
is
![Page 19: CSC236 Week 4 › ~236 › larry › 4 › lec04.pdf · Every recursively defined function has an equivalent closed-form function. And we like closed-form functions. It is easier](https://reader033.fdocuments.net/reader033/viewer/2022053008/5f0c56387e708231d434e605/html5/thumbnails/19.jpg)
Repeated substitution: StepsStep 1: Substitute a few time to find a pattern
Step 2: Guess the recurrence formula after k substitutions (in terms of k and n)
For each base case:
Step 3: solve for k
Step 4: Plug k back into the formula (from Step 2) to find a potential closed form. (“Potential” because it might be wrong)
Step 5: Prove the potential closed form is equivalent to the recursive definition using induction.
19
![Page 20: CSC236 Week 4 › ~236 › larry › 4 › lec04.pdf · Every recursively defined function has an equivalent closed-form function. And we like closed-form functions. It is easier](https://reader033.fdocuments.net/reader033/viewer/2022053008/5f0c56387e708231d434e605/html5/thumbnails/20.jpg)
ConcepTest
20
![Page 21: CSC236 Week 4 › ~236 › larry › 4 › lec04.pdf · Every recursively defined function has an equivalent closed-form function. And we like closed-form functions. It is easier](https://reader033.fdocuments.net/reader033/viewer/2022053008/5f0c56387e708231d434e605/html5/thumbnails/21.jpg)
Summary
Find the closed form of a recursively defined function
● Method #1: Repeated substitution
○ It’s nothing tricky, just follow the steps!
○ Take care of all base cases!
○ It’s a bit tedious sometimes, we will learn faster
methods later.21
![Page 22: CSC236 Week 4 › ~236 › larry › 4 › lec04.pdf · Every recursively defined function has an equivalent closed-form function. And we like closed-form functions. It is easier](https://reader033.fdocuments.net/reader033/viewer/2022053008/5f0c56387e708231d434e605/html5/thumbnails/22.jpg)
Handout, Exercise 1
22
![Page 23: CSC236 Week 4 › ~236 › larry › 4 › lec04.pdf · Every recursively defined function has an equivalent closed-form function. And we like closed-form functions. It is easier](https://reader033.fdocuments.net/reader033/viewer/2022053008/5f0c56387e708231d434e605/html5/thumbnails/23.jpg)
Handout, Exercise 2
23
![Page 24: CSC236 Week 4 › ~236 › larry › 4 › lec04.pdf · Every recursively defined function has an equivalent closed-form function. And we like closed-form functions. It is easier](https://reader033.fdocuments.net/reader033/viewer/2022053008/5f0c56387e708231d434e605/html5/thumbnails/24.jpg)
Next ExampleSometimes you’re not given the recursive definition directly,
so you need to develop the recursive definition first
24
![Page 25: CSC236 Week 4 › ~236 › larry › 4 › lec04.pdf · Every recursively defined function has an equivalent closed-form function. And we like closed-form functions. It is easier](https://reader033.fdocuments.net/reader033/viewer/2022053008/5f0c56387e708231d434e605/html5/thumbnails/25.jpg)
Example
Give a recursive definition of
the number of 2-element subsets of n elements.
(Pretend that you never knew “n choose 2”, and let’s develop it from first principles).
25
For example, when n=4, we have 4 elements, e₁, e₂, e₃, e₄, the 2-element subsets are (e₁,e₂), (e₁,e₃), (e₁,e₄), (e₂,e₃), (e₂,e₄), (e₃,e₄)There are 6 of them.
![Page 26: CSC236 Week 4 › ~236 › larry › 4 › lec04.pdf · Every recursively defined function has an equivalent closed-form function. And we like closed-form functions. It is easier](https://reader033.fdocuments.net/reader033/viewer/2022053008/5f0c56387e708231d434e605/html5/thumbnails/26.jpg)
Strategy
1. Find the base case(s) that can be evaluated directly
2. Find a way to break the larger problem to smaller problems, so that you can define f(n) in terms of f(m) for some m < n.
26
![Page 27: CSC236 Week 4 › ~236 › larry › 4 › lec04.pdf · Every recursively defined function has an equivalent closed-form function. And we like closed-form functions. It is easier](https://reader033.fdocuments.net/reader033/viewer/2022053008/5f0c56387e708231d434e605/html5/thumbnails/27.jpg)
27
Give a recursive definition of
f(n): The number of 2-element subsets of n elements.
1. Base case:
n = 0
f(0) = 0
![Page 28: CSC236 Week 4 › ~236 › larry › 4 › lec04.pdf · Every recursively defined function has an equivalent closed-form function. And we like closed-form functions. It is easier](https://reader033.fdocuments.net/reader033/viewer/2022053008/5f0c56387e708231d434e605/html5/thumbnails/28.jpg)
28
Give a recursive definition of
f(n): The number of 2-element subsets of n elements.
2. Break the larger problem to smaller ones:For the set of n elements, how many 2-element subsets does it have?
Our set looks like:
Break all subsets of S into two parts● the subsets that contain en ● the subsets that do NOT contain en
![Page 29: CSC236 Week 4 › ~236 › larry › 4 › lec04.pdf · Every recursively defined function has an equivalent closed-form function. And we like closed-form functions. It is easier](https://reader033.fdocuments.net/reader033/viewer/2022053008/5f0c56387e708231d434e605/html5/thumbnails/29.jpg)
29
The 2-element subsets that contain en
How many?
Two elements …● One has to be en
● The other can be e1, e2, …, en-1
So there are n-1 of such subsets!
The number of 2-element subsets of n elements.
![Page 30: CSC236 Week 4 › ~236 › larry › 4 › lec04.pdf · Every recursively defined function has an equivalent closed-form function. And we like closed-form functions. It is easier](https://reader033.fdocuments.net/reader033/viewer/2022053008/5f0c56387e708231d434e605/html5/thumbnails/30.jpg)
30
The 2-element subsets that do NOT contain en
How many?
They are subsets of
The number of 2-element subsets of n-1 elements
It’s f(n-1) !
The number of 2-element subsets of n elements.
![Page 31: CSC236 Week 4 › ~236 › larry › 4 › lec04.pdf · Every recursively defined function has an equivalent closed-form function. And we like closed-form functions. It is easier](https://reader033.fdocuments.net/reader033/viewer/2022053008/5f0c56387e708231d434e605/html5/thumbnails/31.jpg)
Sum up after breaking the larger problem into smaller problems
The number of 2-element subsets of n elements
● number of subsets with en: n-1● number of subsets without en: f(n-1)
31
![Page 32: CSC236 Week 4 › ~236 › larry › 4 › lec04.pdf · Every recursively defined function has an equivalent closed-form function. And we like closed-form functions. It is easier](https://reader033.fdocuments.net/reader033/viewer/2022053008/5f0c56387e708231d434e605/html5/thumbnails/32.jpg)
Combining with the base case, the final developed recurrence:
f(n): the number of 2-element subsets of n elements.
32
Home exercise: find its closed form using the substitution method. You know what the result should be.
![Page 33: CSC236 Week 4 › ~236 › larry › 4 › lec04.pdf · Every recursively defined function has an equivalent closed-form function. And we like closed-form functions. It is easier](https://reader033.fdocuments.net/reader033/viewer/2022053008/5f0c56387e708231d434e605/html5/thumbnails/33.jpg)
ConcepTest
33
Try this: write an efficient program that computes the number of ways to pick up the marbles given the number of marbles in the row.