Lec3 - Design and Analysis of Algorithms
Lec3 - Design and Analysis of Algorithms
Recurrences
Recurrences and Running
Time
An equation or inequality that describes a function
2 involves n
Example Recurrences
T(n) = T(n-1) + n Θ(n2)
Recursive algorithm that loops through the
input to eliminate one item
T(n) = T(n/2) + c Θ(lgn)
Recursive algorithm that halves the input in
one step
T(n) = T(n/2) + n Θ(n)
Recursive algorithm that halves the input but
must examine every item in the input
T(n) = 2T(n/2) + 1 Θ(n)
Recursive algorithm that splits the input into 2
halves and does a constant amount of other
work
3
Recurrent Algorithms
BINARY-SEARCH
for an ordered array A, finds if x is in the array
A[lo…hi]
2 3 5 7 9 10 11 12
if (lo > hi)
return FALSE mid
lo hi
mid (lo+hi)/2
if x = A[mid]
return TRUE
if ( x < A[mid] )
BINARY-SEARCH (A, lo, mid-1, x)
if ( x > A[mid] )
BINARY-SEARCH (A, mid+1, hi, x)
4
Example
A[8] = {1, 2, 3, 4, 5, 7, 9, 11}
lo = 1 hi = 8 x=7
1 2 3 4 5 6 7 8
1 2 3 4 5 7 9 11 mid = 4, lo = 5, hi = 8
5 6 7 8
1 2 3 4 5 7 9 11 mid = 6, A[mid] = x
Found!
5
Another 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
low high
1 2 3 4 5 7 9 11 mid = 6, A[6] = 7, lo = 5, hi = 8
low high
1 2 3 4 5 7 9 11 mid = 5, A[5] = 5, lo = 5, hi = 5
NOT FOUND!
1 2 3 4 5 7 9 11
high low
6
Analysis of BINARY-SEARCH
Alg.: BINARY-SEARCH (A, lo, hi, x)
if (lo > hi) constant time: c1
return FALSE
mid (lo+hi)/2 constant time: c2
if x = A[mid] constant time: c3
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,same
x) problem of size n/2
T(n) = c +T(n/2)
T(n) – running time for an array of size n
7
Methods for Solving Recurrences
Iteration method
Substitution method
Master method
8
The Iteration Method
Convert the recurrence into a summation
condition is reached.
Require more algebraic knowledge.
In the iteration method we iteratively
10
Iteration Method – Example
T(n) = n + 2T(n/2) Assume: n =
T(n) = n + 2T(n/2) 2k
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)
11
The substitution method
1- Guess a solution
12
The substitution method
n
T (n) aT f (n)
b
13
Substitution method
Guess a solution
T(n) = O(g(n))
Induction goal: apply the definition of the
asymptotic notation
(strong induction)
T(n) ≤ c g(n), for some c > 0 and n ≥ n0
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)
n0
Induction hypothesis: T(n-1) ≤ c(n-1)2 for all k <
n
Proof of induction goal:
≥ n0
Induction hypothesis: T(n/2) ≤ cn/2 lg(n/2)
T (2m) = 2T(2m/2) + m
Rename: S(m) = T(2m)
19
Example
W(n) = 2W(n/2) + n2
20
Example
W(n) = 2W(n/2) + n2
21
Example
22
Master’s method
“used for solving recurrences of the form:
n
T (n) aT f (n)
b
where, a ≥ 1, b > 1, and f(n) > 0
f(n) is asymptotically equal with
23 nlogba
Master’s method
n
T (n) aT f (n) where, a ≥ 1, b > 1,
and f(n) > 0 b
T(n) = 2T(n/2) + n
a = 2, b = 2, log22 = 1
T(n) = (n)
25
Examples (cont.)
T(n) = 2T(n/2) + n
a = 2, b = 2, log22 = 1
T(n) = (nlgn)
26
Examples (cont.)
T(n) = 2T(n/2) + n2
a = 2, b = 2, log22 = 1
Compare n with f(n) = n2
f(n) = (n1+) Case 3 verify regularity
cond.
a f(n/b) ≤ c f(n)
2 n2/4 ≤ c n2 c = ½ is a solution (c<1)
T(n) = (n2)
27