Professional Documents
Culture Documents
and Design
By
(1) if n 1
T ( n) n n
T 2 T 2 (n) otherwise
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Assumption in Solving Recurrence Relation
• We do not give any explicit value for small n
Because changing n, T (1) changes solution to
recurrence. Solution typically doesn’t change by more
than a constant factor, so order of growth is unchanged
• Solve recurrences, often omit floors, ceilings, etc.
• First analyze without these details and later determine
whether such assumptions matter or not.
• Usually it dose not matter but it is important to know
when it does and when it does not
(1) if n 1
T ( n) n
2.T ( 2 ) (n) otherwise
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Methods Solving Recurrence Relation
Proof
let us suppose that
log s b
a log t
c
b
a b and b c
s t
Now b t c, (a s ) t c
a c,
st
log ca s t
s t log ca , log ba log bc log ca proved
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Some Important Rules used in this Section
log n4 log 34
Prove that 3 n
Proof
log n4 log 34
3 n
log n4 log 34
log 3 (3 ) log 3 ( n )
log log n
4
n
4 log log log
b
a
c
b
c
a
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
The Substitution Method
Solve the recurrence relation given below.
1 if n 1
T ( n) n
3 T ( 4 ) n otherwise
Solution:
n
(1) T (n) 3 T ( ) n
4
replace the value of n by n 4 in (1)
n n n
(2) T ( ) 3 T ( 2 )
4 4 4
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
The Substitution Method
n n n
(3) T ( 2 ) 3 T ( 3 ) 2 and so on
4 4 4
n n n
(4) T ( k 1
) 3T ( )
4 4k 4 k 1
n
Now substitute the value of T ( ) from (2) to (1)
4
n n
(5) T (n) 33T ( 2 ) n
4 4
n 3
32 T ( 2 ) ( ) n n
4 4
n
substitute the value of T ( 2
) from (3) to (5) equation, we have
4
n n 3
T ( n) 3 3 T ( 3 ) 2 ( ) n n
2
4 4 4
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
The Substitution Method
4 4 4 4 4
Let us suppose that n can be expressed as n 4 k
n 3 3 2 3 k 1
T (n) 3 T ( ) n 1 ( ) ( ) .... ( )
k
n 4 4 4
3 k
1 (4) k 1 1 xk
T ( n) 3 T (1) n 1 (
k
) 1 x x ... x
2
1 ( )
3 1 x
1
4
3k
T (n) 3 1 4n(1 k )
k
T (1) 1
4
4 n
n3 k
3 4n(
k
)
n
3 4( n 3 ) 1 3 4 n 4 3
k k k k
log n
4 n 3 3 4n 3 3
k 4
4 k n, k log n4
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
The Substitution Method
log n4
T ( n) 4n 3 3
log 34
T ( n) 4n 3 n
T ( n) 4 n 3 n 0 1
T ( n) 4n 3 n
b b b b b
n a 2 a
a T ( 3 ) ( ) n ( )n n
3
b b b
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
A Hard Example
Continuing this process we get
n a k 1 a k 2 a 1
T (n) a T ( k ) ( ) n ( ) n .... ( ) n n
k
b b b b
n a a 2 a k 1
a T ( k ) n 1 ( ) ( ) .... ( )
k
b b b b
a a k 1
T (n) a T (1) n 1 ( ) .... ( )
k
b b
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
A Hard Example
a k
1 ( ) k
b b a
a T (1) n( )(1 k )
k
a T (1) n
k
1 a ba b
b
b b a k k
a 1 n(
k
)( )
ba b k
b b a k k
a n(
k
)( ) n b k
ba n
b
a ( k
)(b a )
k k
ba
b b
a k
b
k
a k
ba ba
b bab k
b (
k
)a
ba ba
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
A Hard Example
b a logbn
T ( n) ( )b
k
a
ba ba
b a log ba let log ba
b
k
n
ba ba
a b log 1 0 1
a
b
b a
T ( n) n n T ( n) ( n)
ba ba
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
More Hard Example using Substitution Method
1 if n 1
T ( n) n
3 T ( 4 ) cn otherwise
2
Solution:
n
(1) T (n) 3 T ( ) cn 2
4
n n n 2
(2) T ( ) 3 T ( 2 ) c( )
4 4 4
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Contd..
n n n 2
(3) T ( 2 ) 3 T ( 3 ) c( 2 )
4 4 4
n n n 2
(4) T ( 3 ) 3 T ( 4 ) c( 3 )
4 4 4
and so on
n n n 2
(5) T ( k 1 ) 3 T ( k ) c( k 1 )
4 4 4
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Contd..
n
Now substitute the value of T ( ) from (2) to (1)
4
n n 2
(6) T (n) 33T ( 2 ) c( ) cn 2
4 4
n n 2
3 T ( 2 ) 3c( ) cn
2 2
4 4
n
substitute the value of T ( 2 ) from (3) to (6), we have
4
n n 2 n 2
T ( n) 3 3T ( 3 ) c ( 2 ) 3c ( ) cn 2
2
4 4 4
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Contd..
n n 2 n 2
3 T ( 3 ) 3 c( 2 ) 3c ( ) cn
3 2 2
4 4 4
n 3 2 2 3 1 2
3 T ( 3 ) ( 2 ) cn ( 2 ) cn cn
3 2
4 4 4
n 3 k 1 3 k 2 3 0 2
T ( n ) 3 T ( k ) ( 2 ) ( 2 )
k
( 2 ) cn
4 4 4 4
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Contd..
Let us suppose that n can be expressed as n 4 k
3 3 2 3 k 1
T (n) 3 T (1) cn 1 ( ) ( )
k 2
16 16 16
3 k
1 ( )
T (n) 3 k 1 cn 2 ( 16 )
1 3
16
k
16 3
3 k cn 2 (1 k )
13 16
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Contd..
since 4 n (4 ) n
k k 2 2
(4 ) n 2 k 2
16 n k 2
k
16 3
T (n) 3 cn (1 k )
k 2
13 16
16 n 2
3 k
16
3 k cn 2 ( 2
) 3 c (n 3 )
k 2 k
13 n 13
16 16
c n (1 c)3
2 k
13 13
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Contd..
16 2 16 log n
T (n) cn (1 c)3 4
13 13
16 2 16 log 3
cn (1 c)n 4
13 13
let log 34 where 0 1
16 2 16
T (n) cn (1 c)n
13 13
Hence T (n) (n ) 2
k
log b 1
en fn
k
b
k1 . log b
en f .n
k
b en f .n
k k1
( n max( k , k1 )
)
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Recursion Tree Method
c.n2
c.n2 c.n2
Suppose n=4k
2
c(n/16)2 c(n/16)2 c(n/16)2 c(n/16)2 c(n/16)2 c(n/16)2 c(n/16) c(n/16) c(n/16)
2 2
(3/16)2.c.n2
2 2 2
c(n/16)2 c(n/16)2 c(n/16) c(n/16)2 c(n/16)2 c(n/16) c(n/16) c(n/16) c(n/16)
2 2
(3/16)2.c.n2
(3/42)k-1 cn2
T(n/4k) T(n/4k) T(n/4k) T(n/4k) T(n/4k) T(n/4k) T(n/4k) T(n/4k)
T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) (nlog43)
log n4
T ( n) 3 cost at Levels above child level
log 3 4 3 0 3 1 3 k 1 2
T ( n ) ( n ) ( 2 ) ( 2 ) ( 2 ) cn
4 4 4
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Recursion Tree Method
Now total computational cost can be calculated as
log 3 4 3 0 3 1 3 k 1 2
T ( n ) ( n ) ( 2 ) ( 2 ) ( 2 ) cn
4 4 4
Where 4 n k log 4 n
k
log 3 4 3 0 3 1 2
T ( n ) ( n ) ( 2 ) ( 2 ) cn
4 4
log 3 4 1 log 3 4 16 2
T ( n) ( n )( )cn (n
2
) cn
3 13
1 ( )
16
Hence T ( n ) ( n 2
)
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Master Theorem
log b a log b a
1. If f (n) (n ), for some constants 0, then g (n) (n )
a a
2. If f ( n ) ( n log
),b
then g ( n ) ( nblog
. lg n )
3. If a.f(n/b) ≤ c.f(n), for some constant c < 1 and for all
n b, then g(n) = (f(n))
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Master Theorem
Proof:
log b a
Case 1: f (n) (n )
n n logb a
Which implies that f ( j ) (( j ) ) (1)
b b
log b n 1
n
We are given that: g (n)
j 0
j
a .f ( j )
b
(2)
j 0 b j 0 b
a
• Assume that: logba
x
b
a
• Taking log on both sides: log b (
logba
) log b x
b
logba
a b a a b
log b log b log b x log b log b . log b log b x
a a
log b log b log b x 0 log b x x 1
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Master Theorem
n
log b 1 n logb n 1
n logb log b 1
a.b
j
a .( j )
a
n log b a
. ( logb a
) n
j logb a
. )
(b j
j 0 b j 0 b j 0
log b a
Case 2: f ( n) ( n )
n n log b a
Which implies that: f ( j ) (( j ) ) (3)
b b
log b n 1
n
We are given that: g (n)
j 0
j
a .f ( j )
b
(4)
a
We have already proved that logba
1
b
log b n 1
logb a
g (n) (n logb .
a
1) (n . (1 1 ... 1 )
j 0
logb n number of terms
log b a n
( n . log b ) case is proved
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Master Theorem
Case 3: n n c
Given that: a. f ( ) c. f (n) f ( ) . f (n)
b b a
n c n c 2 n c 2
f ( 2 ) . f ( ) ( ) . f ( n) f ( 2 ) ( ) . f ( n)
b a b a b a
n c 2 n c 3 n c 3
f ( 3 ) ( ) . f ( ) ( ) . f ( n) f ( 3 ) ( ) . f ( n)
b a b a b a
n c j
In general: f ( j ) ( ) . f (n)
b a
n
Equivalently: a . f ( j ) c . f (n)
j j
b
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Master Theorem
log b n 1
n
)
We are given that: g (n(5)
j 0
j
a .f ( j )
b
n
We have proved that: a(6). f ( j ) c . f (n)
j j
b
From equation (5) and (6)
log b n 1 log b n 1
n
g ( n)
j 0
a .f ( j )
j
b c . f ( nj
) c . f ( n)
j
j 0 j 0
1
) f (n)(
g (n(7) ) ( f (n))
1 c
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Master Theorem
Since f(n) appears in definition of g(n) and all
terms of g(n) are non-negative, we can conclude
easily that
(9)
g (n) ( f (n))
3. If f (n) (n
logb
) for some > 0, and a.f(n/b) ≤ c.f(n) for some
constant c < 1and sufficiently large n, then T(n) = (f(n))
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Master Theorem
Proof: Case 1
Given that
(1) if n 1
T ( n) n
a.T ( ) f (n) if n b i
b
logb n 1
n
By Lemma 4.2: T (n) (n
logb a
) j 0
j
a .f ( j )
b
log b a log b a
By Lemma 4.3: T (n) (n ) ( n )