Upload
others
View
3
Download
0
Embed Size (px)
Citation preview
CSC236 Week 4Larry Zhang
1
Logistics
2
NEW TOPIC
RecursionTo really understand the math of recursion,
and to be able to analyze runtimes of recursive programs.
3
ConcepTest
4
Recursively Defined Functions
5
The functions that describe the runtime of recursive programs
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.
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.
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
Our Goal:Given a recursively defined function,
find its equivalent closed-form function
9
Method #1Repeated Substitution
10
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
Example 1
12
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):
Step 2:
Guess the recurrence formula after k substitutions
14
The guess:
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
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).
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)
We have officially found that the closed-form of the recursively defined function
18
is
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
ConcepTest
20
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
Handout, Exercise 1
22
Handout, Exercise 2
23
Next ExampleSometimes you’re not given the recursive definition directly,
so you need to develop the recursive definition first
24
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.
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
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
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
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.
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.
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
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.
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.