Lecture 2
Lecture 2
Recurrences
(Appendix A, Chapter 4)
Recurrences and Running Time
• An equation or inequality that describes a function in
terms of its value on smaller inputs.
T(n) = T(n-1) + n
no 1
f
so_
• Recurrences arise when an algorithm contains recursive
calls to itself
2
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)
o
– Recursive algorithm that halves the input in one step
• T(n) = T(n/2) + n Θ(n)
0
– Recursive algorithm that halves the input but must
examine every item in the input
• T(n) = 2T(n/2) + 1
0
Θ(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]
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 = 5
low high
1 2 3 4 5 7 9 11 mid = 5, A[5] = 5, lo = 6, 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, x) same 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 and try
to bound it using known series
– Iterate the recurrence until the initial condition is
reached.
– Use back-substitution to express the recurrence in
terms of n and the initial (boundary) condition.
9
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)
10
iteration method
solve this Rec using
TCU 2 C
TIN
a 2
Ctt
T n C
TIS T
CT Ct
T I it
C T
C K T
TIM
11
1091 98 TIN.IN
109M
K
TIN Mt 27111
NEW
2T
nth a 8T
n T 11
T a Ntnt
TIN n K 2 TI
12
In 1
Tat n Ign 1
n 2 219
TIM In
K 19
0111916
111
109N
109,219
109,2
tf
41091
1094 1019011091
9
0
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)
0
= nlgn + nT(1) = Θ(nlgn)
12
The substitution method
1. Guess a solution
13
Substitution method
• Guess a solution
– T(n) = O(g(n))
14
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)
11st dec
T n 0 Ign
Guess
10 d 190
inductorgoal
induct bypo TM 2 TITLE
d 19
digate d
iff c do
d Ign
c d o
c d
sdethsRec.nsy
s h d
T n T II
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 for all k < n
17
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)
21
Example 1
W(n) = 2W(n/2) + n2
5 In
If 1
ñ
5
in in in in 23
m
size at level
is
we hit when I
ng
11H
I
n 2
e_ ui
test
Eeo
ñ
i
i
ñ I
2ñ 0 n
Example 2
E.g.: T(n) = 3T(n/4) + cn2
e
( ) ( ) ( )
3 2 3 1
T ( n) = cn + n
16
log 4 3
cn 2 + n log 4 3 =
i = 0 16
3
cn 2 + n log 4 3 = O(n 2 )
i =0
1−
16
T(n) = O(n ) 2 24
n
4m w
w
9
a n
17
It
whiffing
n
13 10
109
i
19 n 109
1
i
Total cost
n
19,1
O n
gn
Example 2 - Substitution
T(n) = 3T(n/4) + cn2
• Guess: T(n) = O(n2)
– Induction goal: T(n) ≤ dn2, for some d and n ≥ n0
– Induction hypothesis: T(n/4) ≤ d (n/4)2
• Proof of induction goal:
T(n) = 3T(n/4) + cn2
≤ 3d (n/4)2 + cn2
= (3/16) d n2 + cn2
≤ d n2 if: d ≥ (16/13)c
25
Example 3 (simpler proof)
W(n) = W(n/3) + W(2n/3) + n
• Total cost:
lg n
W (n) n + n + ... = n(log 3/ 2 n) = n = O(n lg n)
3
lg
2
W(n) = O(nlgn) 26
Example 3
W(n) = W(n/3) + W(2n/3) + n
• Total cost:
(log3 / 2 n ) −1
W (n) n + n + ... = i =0
n + 2(log3 / 2 n ) W (1)
log3 / 2 n
lg n 1
n
i =0
1 + nlog3 / 2 2 = n log 3/ 2 n + O(n) = n
lg 3 / 2
+ O ( n) =
lg 3 / 2
n lg n + O(n)
W(n) = O(nlgn) 27
28
Example 3 - Substitution
W(n) = W(n/3) + W(2n/3) + O(n)
• Guess: W(n) = O(nlgn)
– Induction goal: W(n) ≤ dnlgn, for some d and n ≥ n0
– Induction hypothesis: W(k) ≤ d klgk for any K < n
(n/3, 2n/3)
• Proof of induction goal:
Try it out as an exercise!!
• T(n) = O(nlgn)
29
Master’s method
• “Cookbook” for solving recurrences of the form:
n
T (n) = aT + f (n)
b
where, a ≥ 1, b > 1, and f(n) > 0
n
T (n) = aT + f (n)
b
where, a ≥ 1, b > 1, and f(n) > 0
Case 1: if f(n) = O(nlogba -) for some > 0, then: T(n) = (nlogba)
af(n/b) ≤ cf(n) for some c < 1 and all sufficiently large n, then:
T(n) = (f(n))
regularity condition
31
masthm
TIM AT fca
ftp.murtb
poly
cases n fin Ek 32
n n
n fin TM
cases
fin
n f n Tcn
cases
2T Ñ
9 2 b 2 finish
iii
fin
n n
casel
n
a
n
27
Ten 2T 4 n
2 6 2 flat n
9
n n In
1095 f a
n a
case E
IITIN
n Ign
alga
TCM
9 2 b 2 far N
fin
n ñ
case
That fin
Tm É
9 1 b 2 femenign
i
Ign
does not Apply
2 T 1 ñ
THE
a not a constant
TIME
does not Apply
TIM T logn
TCM 3T 1 3
Examples
T(n) = 2T(n/2) + n
a = 2, b = 2, log22 = 1
T(n) = (nlgn)
34
TAI 2T F
154m
1m as
T i 2T
m 2 m
51m 25 2
5 1 T
b 2 f m m
sa T 2
air
is
iii m
s
35
m f m
m m
case
SIMI G m g m
n T 2n Scm 0 m g m
T
Ignigign
Examples
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)
36
37
Examples (cont.)
T(n) = 2T(n/2) + n
a = 2, b = 2, log22 = 1
T(n) = (n)
38
Examples
a = 3, b = 4, log43 = 0.793
T(n) = (nlgn)
39
40
Examples
a = 2, b = 2, log22 = 1
41
42
Readings
43