Download Recurrence Relations, Recursion Tree Method - Slides | CS 361L and more Study notes Computer Science in PDF only on Docsity!
CS 361, Lecture 9
Jared Saia University of New Mexico
Outline
- Recurrence Relations
- Recursion Tree Method
- In Class Exercise
- Intro to Annihilators
Recurrence Relations
- T (n) = 2 ∗ T (n/2) + n is an example of a recurrence relation
- A Recurrence Relation is any equation for a function T , where T appears on both the left and right sides of the equation.
- We always want to “solve” these recurrence relation by get- ting an equation for T , where T appears on just the left side of the equation
A Note
- In most cases, T (n) = O(1), so we will leave out the “base cases” for recurrences when we want just an asymptotic so- lution.
Review
- Up to this point, I’ve been supplying you with good “guesses” for recurrence solutions
- Q: How do we get these guesses?
Getting Good Guesses (I)
Following are some good guesses for solutions to recurrences. log√ n n n n log n n^2 n^3 2 n
Better Techniques (II)
We will review two new techniques:
- Recursion tree method
- Characteristic polynomials
(note: we will not cover the Master Theorem given in the book since the method of annihilators is more powerful)
Recursion-tree method
- Each node represents the cost of a single subproblem in a recursive call
- First, we sum the costs of the nodes in each level of the tree
- Then, we sum the costs of all of the levels
Recursion-tree method
- Used to get a good guess which is then refined and verified using substitution method
- Best method (usually) for recurrences where a term like T (n/c) appears on the right hand side of the equality
Example 1
- Consider the recurrence for the running time of Mergesort: T (n) = 2T (n/2) + n, T (1) = O(1)
n
n/2 n/
n/4 n/4^ n/4 n/
n/8 n/8 n/8 n/8 n/8^ n/8^ n/8^ n/
n
n
n
n
Example 1
- We can see that each level of the tree sums to n
- Further the depth of the tree is log n (n/ 2 d^ = 1 implies that d = log n])
- So we can guess that T (n) = O(n log n)
Now Verify!
- We’ve got a “guess” that T (n) = O(n log n)
- We need to verify that this guess is in fact correct
- We verify using induction
- In particular, want to verify that T (n) ≤ cn log n for all n > 1
In Class Exercise (I)
Use the recursion tree method to guess a solution to the recur- sion T (n) = 2T (n/2) + n^2. Give the guess in terms of big-O notation:
- Q1: What is the total cost of the 0-th, 1-st and 2-nd level of the tree?
- Q2: What is the total cost of the i-th level of the tree for general i?
- Q3: How many levels of the tree are there?
- Q4: What is the summation giving the total cost of the tree?
- Q5: Give a good upperbound on this summation.
In Class Exercise (II)
Now prove that this guess works using induction!
- Q1: What is the base case? Prove that it holds.
- Q2: What is the inductive hypothesis?
- Q3: What is the inductive step?
Take Away
- Recursion tree method is good for getting “guesses” for re- currences where a term like T (n/c) appears on the right side of the equality
- Once we get the guess, then need to verify using the substi- tution method
- Recursion trees are useful but limited (they can’t help us get guesses for recurrences like f (n) = f (n − 1) + f (n − 2))
Another Tool
- We’ll learn another more powerful method for solving recur- rences called annihilators
- This will take three to four classes to go over
- Annihilators are similar to “generating functions”
Intro to Annihilators
- Suppose we are given a sequence of numbers A = 〈a 0 , a 1 , a 2 , · · · 〉
- This might be a sequence like the Fibonacci numbers
- I.e. A = 〈a 0 , a 1 , a 2 ,... ) = (T (1), T (2), T (3), · · · 〉
Annihilator Operators
We define three basic operations we can perform on this se- quence:
- Multiply the sequence by a constant: cA = 〈ca 0 , ca 1 , ca 2 , · · · 〉
- Shift the sequence to the left: LA = 〈a 1 , a 2 , a 3 , · · · 〉
- Add two sequences: if A = 〈a 0 , a 1 , a 2 , · · · 〉 and B = 〈b 0 , b 1 , b 2 , · · · 〉, then A + B = 〈a 0 + b 0 , a 1 + b 1 , a 2 + b 2 , · · · 〉
Annihilator Description
- We first express our recurrence as a sequence T
- We use these three operators to “annihilate” T , i.e. make it all 0 ’s
- Key rule: can’t multiply by the constant 0
- We can then determine the solution to the recurrence from the sequence of operations performed to annihilate T
Example
- Consider the recurrence T (n) = 2T (n − 1), T (0) = 1
- If we solve for the first few terms of this sequence, we can see they are 〈 20 , 21 , 22 , 23 , · · · 〉
- Thus this recurrence becomes the sequence:
T = 〈 20 , 21 , 22 , 23 , · · · 〉
Example (II)
Let’s annihilate T = 〈 20 , 21 , 22 , 23 , · · · 〉
- Multiplying by a constant c = 2 gets:
2 T = 〈 2 ∗ 20 , 2 ∗ 21 , 2 ∗ 22 , 2 ∗ 23 , · · · 〉 = 〈 21 , 22 , 23 , 24 , · · · 〉
- Shifting one place to the left gets LT = 〈 21 , 22 , 23 , 24 , · · · 〉
- Adding the sequence LT and − 2 T gives:
LT − 2 T = 〈 21 − 21 , 22 − 22 , 23 − 23 , · · · 〉 = 〈 0 , 0 , 0 , · · · 〉
- The annihilator of T is thus L − 2
Todo