Intro to Recurrance Relations
Notes
- I have this marked as chapter, 4
- Divide and Conquer
- Or sometimes Divide Conquer and Combine
- But we need a tool for algorithm analysis for most of this.
- A recurrence relation is an equation where the nth term of a sequence is equal to some combination of the previous terms.
- Consider binary search
-
BINARY-SEARCH(x, A, b, e)
- if b > e
- return fail
- mid = ⌊ (b + e)/2 ⌋
- if A[mid] == x
- return mid
- If A[mid] < x
- return BINARY-SEARCH(x, A, mid+1, e)
- else
- return BINARY-SEARCH(x, A, b, mid-1)
- Trace it.
- How does this work? (you should be able to do this)
- Proof:
-
Prove the base case 1: end - start ≤ 0 A binary search on an array of size 0 will return the correct value By this case, it is not in the array, so by line 2, false will be returned. Base Case 2: end-start = 1 A binary search on an array of size 1 will return the correct value If it is in the array, it will be located at the one position in the array This will be mid (line 3) , and that is checked in line 4-5 and true will be returned If it is not in the array, either line 7 or line 9 will be called In either case, false will be returned by above. Assume true for k > 1 A binary search of an array of size k will return the correct value. Prove true for k+1 If the element is at the middle of the array, the correct value will be returned by line 4-5 If it is not at the middle of the array, an array of size (k+1)/2 will be searched in line 7 or 9. These are of size ≤ k, therefore by induction they must return the correct value Thus binary search on an array of size k+1 must return the correct value.
-
- $$ T(n) = \begin{cases} c & \text{if } n=0,1 \\ T(n/2) + c & \text{if } n>1 \end{cases} $$
- This is called a recurrence relation
- It is self referential.
- It is ideal for determining the performance of recursive functions.
- Solving recurrence relations is a major component of our curriculum.
- There are several methods for doing this.
- I will use use the substitution method
- In this method, you use iteration to look for a pattern
- Then you reduce this pattern algebraically
- Solving the relation
T(n) = T(n/2) + c Note: T(n/2) = T([n/2]/2) + c = T(n/22) + c T(n) = T(n/22) + c + c = T(n/22) + 2c Note: T(n/22) = T(n/22/2) + c = T(n/23) + c So T(n) = T(n/23)+ c + 2c = T(n/23)+ 3c In General: T(n) = T(n/2k)+ kc Assume that n = 2k We can always pad to make this true. T(2k) = T(2k/2k) + kc = T(1) + kc T(1) = c Since n = 2k log2 n = log2k so k = log2 n T(2k) = c + clog2n Thus is is O(log2n)