CSC236 Week 4 Larry Zhang 1
Announcements PS2 is out Larry s office hours in the reading week: as usual Tuesday 12-2, Wednesday 2-4 2
NEW TOPIC Recursion To really understand the math of recursion, and to be able to analyze runtimes of recursive programs. 3
ConcepTest 4
Recursively Defined Functions The functions that describe the runtime of recursive programs 5
Definition of functions The usual way: define a function using a closed-form. Another way: using a recursive definition A function defined in terms of itself. 6
Let s work out a few values for T₀(n) and T₁(n) T₀(n) T₁(n) n=1 1 1 n=2 3 3 Maybe T₀(n) and T₁(n) are equivalent to each other. n=3 n=4 n=5 7 15 31 7 15 31 7
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 #1 Repeated Substitution 10
Repeated substitution: Steps Step 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. 11
Example 1 12
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): 13
Step 2: Guess the recurrence formula after k substitutions The guess: 14
Step 3: Consider the base case What we have now: So, let n - k = 1, and solve for k We want this to be T(1), because we know clearly what T(1) is. k = n - 1 This means you need to make n-1 substitutions to reach the base case n=1 15
Step 4: plug k back into the guessed formula 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). 16
Step 5: Drop the word potential by proving it Prove Use induction! Define predicate: P(n): T₁(n) = T₀(n) Base case: n=1 Induction step: is equivalent to # by def of T(n) # by I.H. 17
We have officially found that the closed-form of the recursively defined function is 18
Repeated substitution: Steps Step 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 Example Sometimes 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). 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. 25
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
Give a recursive definition of f(n): The number of 2-element subsets of n elements. 1. Base case: n = 0 f(0) = 0 27
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 28
The number of 2-element subsets of n elements. 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! 29
The number of 2-element subsets of n elements. 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)! 30
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. Home exercise: find its closed form using the substitution method. You know what the result should be. 32
ConcepTest 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. 33