cs204 pp19 40p
cs204 pp19 40p
cs204 pp19 40p
Re urren es
While sum play a role in analysis of iterations, re-
ursive programs requires analysis of re urren es.
A re urren e is expressed by itself using smaller
parameteri values.
Merge sort for example,
( ) = (1)
T n
, if n = 1
2T (n=2) + (n), if n 1
We know T (n) = (n log n).
But how? { Substitution method involves
{ Make a good guess (?) then apply indu tion
to nd the onstants and show that solution
works.
" Department of C S E !
R. K. Ghosh Le ture Notes, January, 2003 19 of 395
# Algorithms: mathemati al ba kground
Re urren es
Consider the re urren e T (n) = 2T (bn=2 )+ n.
Assume solution to be O(n log n). So
( ) 2( bn=2 log(bn=2 ) + n
T n
n log(n=2) + n
n log n n log 2 + n
n log n n + n
n log n, for 1
Use mathemati al indu tion to show that the so-
lution holds.
" Department of C S E !
R. K. Ghosh Le ture Notes, January, 2003 20 of 395
# Algorithms: mathemati al ba kground
Re urren es
The onstant must satisfy the boundary on-
ditions.
Suppose we hoose to be large, but how large?
If T (1) = 1, then as 1 log 1 = 0, even if we let
to be very large the solution does not satisfy
boundary.
Also annot be arbitrarily large.
Sin e asymptoti time omplexity is needed, it
suÆ es to let onditions hold when n n , 0
where n0 is a xed positive number.
So, T (k) for some k 2 an be onsidered as
boundary onditions for indu tion.
From re urren e T (2) = 4, T (3) = 5.
Letting 2 implies
T (2) 2 log 2, and T (3) 3 log 3;
" Department of C S E !
R. K. Ghosh Le ture Notes, January, 2003 22 of 395
# Algorithms: mathemati al ba kground
Consider
( ) = T (bn=2 ) + T (dn=2e) + 1
T n
" Department of C S E !
R. K. Ghosh Le ture Notes, January, 2003 23 of 395
# Algorithms: mathemati al ba kground
( ) bn=2
T n b + dn=2e b+1 = n 2b+1
Implies
( )
T n n b, as long as b 1
Now an be hosen to handle boundary ondi-
tions.
Similar problems also arise when we try to prove
T (n) = 2T (bn=2 ) + n as O (n) as follows.
T (n) 2( bn=2 ) + n
T (n) n + n = O (n)
" Department of C S E !
R. K. Ghosh Le ture Notes, January, 2003 24 of 395
# Algorithms: mathemati al ba kground
Changing Variables
p
Consider T (n) = 2T (b n ) + log n.
Change n = 2 , then T (2 ) = 2T (b2 )+ m.
m m m=2
lation be omes
S m( ) = 2S (m=2) + m
So solution be omes S (m) = O(m log m) =
O (log n log log n).
" Department of C S E !
R. K. Ghosh Le ture Notes, January, 2003 25 of 395
# Algorithms: mathemati al ba kground
Method of Iteration
Simply go on iterating the re urren e down to
base ase.
Consider, the iteration
T (n) = n + 3T (bn=4 )
= n + 3(n=4 + 3T (bn=16 ))
= n + 3n=4 + 9(n=16 + 3T (bn=64 ))
= n + 3n=4 + 9n=16 + 27T (bn=64 )
Obviously the iteration an ontinue till
bn=4 = 1 or when i ex eeds log n times.
i
4
" Department of C S E !
R. K. Ghosh Le ture Notes, January, 2003 26 of 395
# Algorithms: mathemati al ba kground
Re ursion Tree
It helps to visualize progress of iteration.
Consider the gure below for
T (n) = 2 T (n=2) + n
2
n2 n2
n2 n2
2
(n/4) 2 (n/4) (n/4) 2 (n/4) 2 (n/4) 2
" !
Total Θ( n2 )
Department of C S E
Master Theorem
In general a re urren e relation may be of the
form
( ) = aT (n=b) + f (n)
T n
for onstants a 1, b > 1 and f (n) is an aymp-
toti ally positive funtion.
Master theorem states that
1. If f (n) = O(nlogb a ) for some onstant >
0 then T (n) = (nlogb a).
2. If f (n) = O(nlogb a) then T (n) =
(nlogb a log n).
3. If f (n) = (nlogb a+) for some onstant >
0, and if af (n=b) f (n) for some onstant
< 1 and suÆ iently large n then T (n) =
(f (n)).
" Department of C S E !
R. K. Ghosh Le ture Notes, January, 2003 28 of 395
# Algorithms: mathemati al ba kground
Master Theorem
Intuitively f (n) is ompared with fun tion nlogb a
and solution to re urren e is determined by larger
of the two fun tions.
In ase 1 nlogb a is larger and the solution is of
(
O nlogb a ).
In ase 2 both are same. In this ase fa tor log n
is multiplied.
In ase 1 nlogb a is smaller. The omplexity is
determined by f (n).
" Department of C S E !
R. K. Ghosh Le ture Notes, January, 2003 29 of 395
# Algorithms: mathemati al ba kground
" Department of C S E !
R. K. Ghosh Le ture Notes, January, 2003 30 of 395
# Algorithms: mathemati al ba kground
" Department of C S E !
R. K. Ghosh Le ture Notes, January, 2003 31 of 395
# Algorithms: mathemati al ba kground
2 T = f2 ; 2 ; 2 ; 2 ; : : :g
1 2 3 4
Similarly
E T = f2 ; 2 ; 2 ; 2 ; : : :g
1 2 3 4
Thus
(E 2) T = f0; 0; 0; 0; : : :g
Then (E 2) is said to annihiliate A.
In our rst proof we use the annihiliator opera-
tion to solve Master theorem by applying te h-
nique of domain transformation.
" Department of C S E !
R. K. Ghosh Le ture Notes, January, 2003 32 of 395
# Algorithms: mathemati al ba kground
= a T (b =b) + (b
m mk
)
= a T (b =b) + (b
m mk
)
= a T (b ) + (b
m 1 mk
)
Setting S (m) = T (b ), m
S (m) = a S (m 1) + (b )
mk
S (m) a S (m 1) = (b )
mk
Substituting m by m + 1,
S (m + 1) a S (m) = (b )
(m+1)k
Department of C S E
to obtain
(E a) S (m) = (b ) mk
" Department of C S E !
R. K. Ghosh Le ture Notes, January, 2003 34 of 395
# Algorithms: mathemati al ba kground
= ( + log n)n
1 2 b
logb a
" Department of C S E !
R. K. Ghosh Le ture Notes, January, 2003 35 of 395
# Algorithms: mathemati al ba kground
T (2 ) =
m
a + b
1
logb n
2
k logb n
T (n) = n + b
1
logb a
2
logb nk
= n + n 1
logb a
2
k
= (n + n ) logb a k
" Department of C S E !
R. K. Ghosh Le ture Notes, January, 2003 36 of 395
# Algorithms: mathemati al ba kground
Dire t Proof
Let b > 1 and n be an exa t power of b, i.e.,
n =b k
for k > 0. So
( ) = (1)
T n
aT (n=b) + f (n)
, if n = 1
, if n = bi
where i is a positive integer.
Apply iteration method
( ) = f (n) + af (n=b) + : : : + a
T n
logb n
T (1)
Sin e a = n a T (1) = n
logb n logb a logb n logb a
.
Thus
logb n
X1
T n ( ) = (n logb a
)+ j
a f n=b( j
)
j =0
" Department of C S E !
R. K. Ghosh Le ture Notes, January, 2003 37 of 395
# Algorithms: mathemati al ba kground
Proof: Case 1
For proving ase 1, f (n) = nlogb a
logb n
X1 logb n
X1 n logb a
f (n=b ) =
j j j
a a
bj
0 0
X 1 a j
logb n
= nlogb a
b
(b )
j
sin e b > a
logb n logb n
X1 X1
a
j
f (n=b ) n
j logb a
(b ) j
0 0
" Department of C S E !
R. K. Ghosh Le ture Notes, January, 2003 38 of 395
# Algorithms: mathemati al ba kground
(b ) = (b )b 1
logb n
X1 logb n
j
1
1
0
n
= b 1
Thus
logb n
X1
a
j
f (n=b ) = O(n
j logb a
)
0
" Department of C S E !
R. K. Ghosh Le ture Notes, January, 2003 39 of 395
# Algorithms: mathemati al ba kground
Proof: Case 2
In ase 2,
f n( ) = (n logb a
) ) f (n=b ) = ((n=b)
j logb a
)
Thus
logb n logb n
X1 X1
a
j
f (n=b ) =
j
a
j
(n=b ) j logb a
0 0
logb n
X1
= nlogb a (a=(blogb a
)) j
0
n logb a
log b n
" Department of C S E !
R. K. Ghosh Le ture Notes, January, 2003 40 of 395