3 Binary - Search
3 Binary - Search
Recurrences
Note: Slides were written by Dr. Monica Nicolescu at University of Nevada, Reno
Recurrent Algorithms
BINARY – SEARCH
• for an ordered array A, finds if x is in the array A[lo…hi]
1 2 3 4 5 7 9 11 mid = 4, lo = 5, hi = 8
1 2 3 4 5 7 9 11 mid = 6, A[mid] = x
Found!
3
Example
• A[8] = {1, 2, 3, 4, 5, 7, 9, 11}
– lo = 1 hi = 8 x=6
1 2 3 4 5 6 7 8
1 2 3 4 5 7 9 11 mid = 4, lo = 5, hi = 8
1 2 3 4 5 7 9 11 mid = 6, A[6] = 7, lo = 5, hi = 5
1 2 3 4 5 7 9 11 mid = 5, A[5] = 5, lo = 6, hi = 5
NOT FOUND!
4
Analysis of BINARY-SEARCH
Alg.: BINARY-SEARCH (A, lo, hi, x)
if (lo > hi) constant time: c 1
return FALSE
mid (lo+hi)/2 constant time: c2
if x = A[mid] constant time: c 3
return TRUE
if ( x < A[mid] )
BINARY-SEARCH (A, lo, mid-1, x) same problem of size n/2
if ( x > A[mid] )
BINARY-SEARCH (A, mid+1, hi, x) same problem of size n/2
• T(n) = c + T(n/2)
– T(n) – running time for an array of size n
5
Recurrences and Running Time
• Recurrence: an equation or inequality that
describes a function in terms of its value on
smaller inputs.
• Recurrences arise when an algorithm contains
recursive calls to itself
• Iteration method
• Substitution method
• Master method
8
The Iteration Method
T(n) = c + T(n/2)
T(n) = c + T(n/2) T(n/2) = c + T(n/4)
= c + c + T(n/4) T(n/4) = c + T(n/8)
= c + c + c + T(n/8)
Assume n = 2k
T(n) = c + c + … + c + T(1)
k times
= clgn + T(1)
= Θ(lgn)
9
Iteration Method – Example
T(n) = n + 2T(n/2) Assume: n = 2k
T(n) = n + 2T(n/2) T(n/2) = n/2 + 2T(n/4)
= n + 2(n/2 + 2T(n/4))
= n + n + 4T(n/4)
= n + n + 4(n/4 + 2T(n/8))
= n + n + n + 8T(n/8)
… = in + 2iT(n/2i)
= kn + 2kT(1)
= nlgn + nT(1) = Θ(nlgn)
10
Iteration Method – Example
T(n) = n + T(n-1)
T(n) = n + T(n-1)
= n + (n-1) + T(n-2)
= n + (n-1) + (n-2) + T(n-3)
… = n + (n-1) + (n-2) + … + 2 + T(1)
= n(n+1)/2 - 1 + T(1)
= n2+ T(1) = Θ(n2)
11
The substitution method
1. Guess a solution
12
Substitution method
• Guess a solution
– T(n) = O(g(n))
13
Example: Binary Search
T(n) = c + T(n/2)
• Guess: T(n) = O(lgn)
– Induction goal: T(n) ≤ d lgn, for some d and n ≥ n0
– Induction hypothesis: T(n/2) ≤ d lg(n/2)
14
Example 2
T(n) = T(n-1) + n
• Guess: T(n) = O(n2)
– Induction goal: T(n) ≤ c n2, for some c and n ≥ n0
– Induction hypothesis: T(n-1) ≤ c(n-1)2
15
Example 3
T(n) = 2T(n/2) + n
• Guess: T(n) = O(nlgn)
– Induction goal: T(n) ≤ cn lgn, for some c and n ≥ n0
– Induction hypothesis: T(n/2) ≤ cn/2 lg(n/2)
16
Changing variables
T(n) = 2T( n ) + lgn
– Rename: m = lgn n = 2m
T (2m) = 2T(2m/2) + m
– Rename: S(m) = T(2m)
S(m) = 2S(m/2) + m S(m) = O(mlgm)
(demonstrated before)
T(n) = T(2m) = S(m) = O(mlgm)=O(lgnlglgn)
Idea: transform the recurrence to one that you
have seen before
17
The recursion-tree method
18
Example 1
W(n) = 2W(n/2) + n2
2 2 2
1
W (n) = lg n
+ 2 W (1) = n
2 2
+nn2
+ O(n) =n + O(n) = 2n 2
i=0
i
i=0 i=0 1− 1
2
W(n) = O(n2)
19
Readings
• Chapter 4
20