DAA Recursion
DAA Recursion
RECURRENCEs BY 8UB8TITUTIO
METHOD
3.2. 8OLVING
method conwintn of two ntepu.
The substitution
uolutíon.
Btep 1. Cuens the form of the
CHAPTER-3 Step 2. Use mathematical inductin t find the cmstants (nundary enditumai, and
show that the gueKG ÍB CorTt.
induetín
Guess the ansver, and then prove it eorreet by
he used to prve both upper bounda lvmer iornda and
Recurrence The Bubstitution method can
M). When applying
this method a good gues in vítal. If the ínítíal guess
is wrong, the
to be adjusted later.
guess needs
removed and don't affeet the solution to the
recunrence.
Floors and ceilíngs ean easily be the ruaning
conditíons such as Tin) 1 ) hecause it i l l ehange
=
We can also omít boundary a n 24Jmite,
But when we desire an ezad, rather than
tíme by a constant factor only,
condítiíons.
solutíon, we need to deal with boundary
3.1. INTRoDUCTION
EXAMPLE 3.1. Solve the reeurrence Tin) = 2T(n/2) +n
usingssubstitution method
algorithm contains a recursive call to itself, its running time can be described by 2T (In/2]) + n
a TVn)
When an Solution:
=
recurrence or recurrence equation. A recurrence is an equation o r inequality that descríbes 1. Guess: Tin) = O(n lg n) ie., Tin) s en log n for zome enstant e.
a function in terms of its value on smaller inputs. For divide-and-conquer algorithms, we base eaze
holds for sme
2. Induetion : Base c a s e : We need to show that
o u r guess
get recurrence like
(not necessarily n 1, some small n is ok).
=
iffnse
o1) Our guess does not hold for n 1, because the bound Ttn) cn
=
=
lg n gields TI1) = el ig 1
aTn/b)+ D(n)+C(n) otherwise
= 0, which is contradiction with T(1)
= 1 now for n =2 and n =3.
number N2) 2 T13) 3 TY3) 3 lg 3 = 4
of subproblems = = . .
=
a =
are derived from the recurrence relatíon
n l b size ofthe subproblems (in terms of n)
Dn) time of divide problem into subproblems ofsize n for n 2, TY2) = 2T(1) +2 = 2
Con)-time
size n.
ofcombine the subproblem solutionsto get thesolution for the problem of for n 3, T(3) = 2711) +3=3
N2)s 2 Ig 2) and T(3)S A3 lg 3
So,
eg., the running time Ttn) of the merge-sort algorithm can be expressed as a recurrence. 2
for any
Tn)= Je) ifn=1 Inductive step: Assume holds for n/2: Tint2) s en2 log ni2
27(a/2)+e(n) ifn>1 Now prove that it is holds for n: Tin) S en log n
There are several ways to find an asymptotic bound for a recursive running time. In the
Substitution method we guess a bound, and prove that the guess is correct by using
Ttn)s 2T a/2b+n T1)=1
Ttn)s 2e.Un/2J.logi2b+n
mathematical induction. The recursion-tree method converts the recurrence into a tree whose
node represent the costs incurred at various levels of the recursion; Iteration method, convert s cnlel/2j +n
the recurrence into a summation and then use the techniques of bounding summations t s c.nlg n -en.lg2+n
the as the Master Theorem the master method
solve recurrence. Apply standard results such
is the most powerful which provides the bounds for the recurrences of the form.
s cn.lg n -c.n.+ n
Ttn) = aT(n/b) + An) s c.n.lg n
which holds for c21
where, a 21, b> 1 and An)> 0.
54
Desi9n diid
56
Recurence
How To Guess some
values. that T(n) s Cn for
try to show
Compute table of function
that the solution is O(n) and we
We guess
choose the middle one.
bounds and constant C.
Findlower and upper method and then guess. in the recurrence we have
Substituting our guess
Do a few steps ofthe iteration Ttn) = Tln/2.) + TÚn/2b+7
tree to generate good guesses.
Draw the recursion
s dn/2]+ CTn/21+ 7
Master Method.
By Applying = Cn+7
.Ifa recurrence is similar to one you are Tamilnar, then
guessingaa simila.
similar solutionj
reasonable. So, instead, we adjust our guess to
then we have
T(n) s Cn -b and b >0 is constant
Some Consideration When Applying Substitution Method
Ttn) = Tln/2.) + Tln/2) +7
1. Making a good guess. s (CLn/2]-b) + (CTn/21-b) + 7)
2. Subtleties.
= Cn-2b +7
3. Avoiding pitfalls,
Cn
4. Changing variables.
We thus have
1. Making a Good Guess Cn
Cn 2b +7S
Consider the recurrence b2 7/2 3.5
Therefore
Tn)= 2TLn/3]+ 105) +n Now for any n >
no 2, there is a constant C = 1 such that
In the given recurrence the additional term T(n) s Cn - 3.5 holds
"105" added in the argument to T on the
right hand side, so it looks like as a difficult Recurrence.
i.e., Tn) =
On)
This additional term cannot
difference between
affect the solution to the recurrence. When n is large, the Avoiding Pitfalls: Consider the recurrence
Tln/3|) and T(ln/3]+ 105) is not large, so we can make the guess as: Tn) = 27T (ln/2)) +n
Tn) = O(nlgn)
We can falsely "prove" T(n) =
O(n) by guessing T(n) s Cn
Another way to make
choose middle.
a good guess is to find the lower and Tn) = 2T(ln/2) + n
upper bounds and then
For example: Consider the 2CLn/2+n
recurrence
s Cn + n
Tn) 2Tn/2)) + n
The lower bound of the s (C+1)n
above recurrence is
= O(n) = Wrong !!
and
Tn) Qn) The error is that we haven't proved the exact form of the inductive hypothesis, that is
we can also prove
initial upper bound of
an
T(n) s Cn.
Then, we can T(n) O(n2) for the given
= irrence.
gradually lower the upper recurtnverge Changing Variables: Sometimes a change of variables can help, a little algebraic
on the correct. Thus the bound and raise the lower bound
asymptotically tight solution is unu manipulation can make an unknown recurrence similar to one that solve easily.
Tn) = eCnlgn)
Subtleties :Sometimes you may EXAMPLE 3.2. Consider the recurrence
the induction then. guess right but the math does not seem w
out in
Tn) =2T daD +lgn
Inductive
Revise the assumption is
not
strong enough Solution: Tn)= 2TlVn )+lgn
guess by
subtracting alower-order term consider the Looks difficult, we can simplify this recurrence by replacing n with 2m then, we have
Ttn) TMln/2) + T(n/2)
=
rec urrence.
+7 n 2 log, n = mlog, = m
gn = m
Recurrence
59
27(2m/4) + m
and T(2) =
T(2m)= T(m) then we have
Suppose n+2n2+8T+ n2
Tim) = 2T(m/2) + m
n2 +2n2 +22n2 + 23 n2 + 24 n2+...
But we know already that
has constant size, i.e.,
Tm)= e(m log m) It expends until the sub-problem
Now substitute the value of m, we get
o 1 i =logn
Tn) = Tlgn) = 0(lgn. log lgn)
So the last term will be
3.3. SOLVING RECURRENCES BY ITERATION METHOD 8 T(1) = 8logn
Tn) = n2+2n2 + 22 n2 +23 n2 +24 n2+25 n2+.. + 2log.n -1 n2 +8log n
The iteration method does not require guessing Now,
than the
the
substitution method. The basic idea of
answer, but it may requir
knowledge iterative methe a logn-i
iterate) the recurrence and express it as a summation of 2 n2 +8logn
the initial conditions. In the iteration method we
terms, dependin o =0
we "see the pattern". unfold the
iteratively "unfold" the rorecurrence log n-i
n2 2' (23)log n
EXAMPLE 3.3. Solve the recurrence
T(n) = 8T(n/2) + n2 logn i
n 2 , 2 + (2log n)3
T) = 1
by using iteration method.
logn-
Solution: Tn) 87n/2) n , 2+n3
=
+ n2
=n +8T(n/2)
Now, we have
log (2) -1
2-1
:
Ttn/2)= 8T(n/4) + (n/2)2 = 2og 1
n-1
n2
=
+
8T: e(n)
Then Tn) = n2. etn) + n*
= n2+2n2 +88T = e(n3)
EXAMPLE 3.4. Solve the Recurrence
=
n2 +2n2
+8 T(n) = 3Tln/4) + n by using iteration method.
Solution: Ttn) = 37ln/4)) + n
= n + 3T (n/4)
na +2n2 +8
=
n+2n + 83
Algorithms
Design and Analysis öf Recurrence / 61
60
= n + 2n + 16T
n+3.
= n + 2n + 16|
'= n + 2n + 4n + 64T
until the subproblem has conste
The ith term of this series is 2 n this recurrence expends until the subproblems has
Theith term isthis
recurrence expends
size constant size is
e.
n =2
1 n=4 i.e
log n = i log 2
n= 4
i= lgn
have
by taking log on both side, we 2n +23 n 28.n T(1)
Now, T(n) S n +2n + +... +
logn = i log4
i= log, n We know that, aBs n = noE,
Hence it expands for log, n steps. T(1)
Tn)Sn + 2n +2 n 23 n + +.. + n2 . n
Tn) Sn + 2n +
2 n +23 n + . . +n. n T(1)
Sn+3. 3 .34" T(1) +n2. T(1)
Tn) S n + 2n +2 n +
23 n + . .
logn-l 205, +n2 T(1)
2n + 2n + 23 n + . . + n
opm)sa. ) Tn) n +
=
Sn.
.o) Now by geometric progression,
Now
374 By using,
for * 1
+n2T(1)
22 23 20g2-. n
Hence Tn)s 4no We have, Ttn) = n + 2n + n + n +.. +
= (1+2 +22 +23+.. 2logan-1 )n +0(n2)
We can write
glog,n as nE which is less than n because
Tn) s 4n +0(n) log,<1 (2os n+ 0(7*)
Tin)= 0Cn)
EXAMPLE3.5. Solve the recurrence 2loE = nos2
T(n) 4T (n/2) + n
by using iteration
Solution: method. (n - 1).n +0(n)
Tn)=n+4T e(n)+6(n*)
= e(n)
Or A I g O I s
Anailysis
Design and
ECURSION TREE METH Recurrence 63
RECURRENCES
BY RECU
3.4.
SOLVING
is a good way
to i l l u s t r a t e
the i t e r a t i o n .
by the
method. Arecursion.
The
recursion tree method
a good
guess,
which
particalar call
is then
ofthe
verified
th recursive
e
bstitution methot
is best used
to generate
corresponds
to a
child for
eaohction nction duri
this tree,
each node
algorithm. Therefore a
node
has one
the function not incld
recursive call T(n/2 T(n/2)
execution of the
execution. It is
labelled with the cost of cluding thecost of
during its
Then the total
cost 7{n) of
the algorithm is calculated lculated by adding the cos T(n/4)
recursive cals. T(n) T(n/2) T(n/4) T(n/4) T(n/4)
tree.
T(n/2)
all levels of the (i1)
the following
recurrence by using the recursion t e
EXAMPLE 3.6. Solve method
(n/2) (nr2
Solution. Reursion tree for Tn) = | + n
(n4y (n4) (n/4) (n/4)
Tn) T(n/8) T(n/8) T(n/8) T(n/8) T(n/8) T(n/8) T(n/8)
T(n/8)
(iv)
Fig. 3.2.
gn+1 levels n/2-
(h lgn)
ndgn+n n2
(n/2)
(im)
(iv)
The Fig. 3.1. (n4)
solution of the recurrence is Tn) (n4) (n/4 (n/4)
EXAMPLE
=O(nlgn).
3.7. Solve the l092
following recurrence by using T(n/8 T(n/8) T(n/8)
recursion tree T(n/8) T(n/8) T(n/8) T(n/8) T(n/8)
(UPTU MCA 200300 AA AA
Solution.
We can draw
the
To)- 2
recursion tree for the
times
given Recurrence as
follows
Algorithms
Design and Analysis of
64 Recurrenc 65
e1) if n S no
Tn) =
n> no
where n is some constant, a 21, and b >1. Often, fn) = n* for some k> 0.
Tn)s 2n2
Tn) = O(n*) The master method provides the solution for the following type of recurrence
by using the recursion troe
EXAMPLE 3.8. Solve thefollowing
recurrenee
method T(n) =
where a 21,6>1are constants
and fln) is an
asymptotically positive function (i.e., fn) > 0,
for n 2 some constant).
Solution: The Master Theorem
Let ftn) be a function, and let T(n) be defined on non-negative integers as
T(n)
Tn) = aT
T(ni2)-******* T(ni2) (7E) ******************.T(n/2)
(n/2)
where, a2 1,
b>1 are constants.
T(n/4) T(/4) Then, T(n) can be bounded asymptotically as follows :
(Gi)
1. Tn) =
e(nos) ifftn) =
O(nlog0-) for some
constant e> 0
n
2. Ttn) = e(nlos°lgn) if fn) = e(nlos,")
3. T(n) 0ftn) if ftn) = 2(no8,a+¬) for some constant e > 0
(n/2).*********************(n/2) - 8(n/2)= 2n
and if a(ftnlb) s elfn)) for some positive constant
C<1 for all sufficiently large n.
(n4)./4)
Above all three cases do not cover all the possibilities for fln).
There is a gap between cases 1 and 2when fn) is smaller than , but not polynomially
smaller.
T(1)T(1) -- There is a gap between cases 2 and 3 whenfn) is larger thann's, but not polynomially
T(1) larger.
8 T(1)
(v) Ifshown
the function falls into of these two gaps, if the regularity condition can't be
one or
to hold, then the master method can't be used to solve the recurrence.
Fig.33.3.
Tn) =n2 +2n2 + 22n2 EXAMPLE 3.9. Solve the following recurrence by using the master method
Tn) =O(n? 23n2 + 24n2+.+ 2log n
-1n2+80
3.5. SOLVING RECURRENCES
BY T(n) =9T (UPTU B. Tech. 2005-06)
In the MASTER METHOD
each ofanalysis
of
size nlb recursive
and algorithms,
the Solution: We know that the master method is used the following type of recurrence
solution of the need time fln) problem is
original problem, then theto combine the divided into a
solutions of number o ems toa
nber of subproblems
running time T(n) is thesesuDe Tn) = a l . (1)
given by the followng* currence
Algorithms Recurrence 67
Analysis
of
Design
and We h a v e
(1).
equation
the
with n2
n'og,9=
=
compare
66 recurrence
sOLVED EXAMPLES
=
n, nl
the given
fn)
=
Now
a
9, b=3,
smaller than nlog,a
.
get
polynomlaly
with n~. We
= 7T +n describe the running time of an
n is 3.12.
compare
fn) = solution bf
the EXAMPLE The recurrence T(n)
And On24) so fn) and the
n= theorem recurrence j
' master
the
case 1 of
apply = aT"
can
we
Hence algorithmA. A competiting algorithm A has a running time of Tin)
Ttn) = B(n. methn
master
the hod
usng
recurrence
by value for a such that A'is asymptotically faster than A?
following
What is the largest integer (UPTU, MCA 2005-06)
the
EXAMPLE
3.10. Solve
recurrences of the form
Solution: We know that the Master method is used for solving the
T(n) recurre
tollowing type of . (1)
is used the Ttn)- a7 +fn»
method
master
know that the
Solution: We
we have
Now comparing the given recurrences with equation (1),
Ttn) = o 7 + n a = 7, b = 2 and fn) = n2.
with the equation
(1). We have
recurrence
compare : log,7 = O510 = 2.807
Now the given = n° = 1 and nOE nog, 7 n2.807
=
a
1, nos" =no8 log10
=1, b=2, fn)
fln) = 1 with 1. We get We have that
And compare
tonlos.
1 etn ) i.e., ffn) is polynomially equal n2 = O(n"s for some constant e > 0
master theorem and the solution of the recurrence is
Hence we can apply case 2 of the so applying the Case I of the Master method:
Tn) = elngn) » 0llgn).
We get,
master method Tn) = 0(,los, a ) =» 0los7) 0(n2507)
EXAMPLE 3.11. Solve the following recurrence by using the
Tn) = 0(n2.807)
Ton) we have
Now, comparing the given second recurrences with equation (1),
a = a, b = 4, fn) =n?.
Solution: We know that the master method is used the following type of recurrence
Using Master theorem
Tu)-a7 ) ... (1)
Now the given recurrence
compare with the equation (1). We have
Now, we can say that,
a= 1, b= = = 1
2, fn) =n?, nlos, nlos,l =n° = a
=
And compare fn) n* with 1. We log log,a : 48.
get
=
Hence we can
n 9Mn), i.e., fn) is
polynomially larger. recurrence - =1
T(n)
apply case 3 of the master theorem and the e ls EXAMPLE 3.13. Show that the solution to the binary-search
Tn) = n). solution of tae
+01) is T(n) =
lgn) by using the Master theorem.
Analysis of Algorithms Recurrence 69
Design and
recurrences of the
68 the form
for solving
Master method is used 10
We know that the
a = 1, b= and fln) = n.
Solution:
.. (1)
and n oBs ng10/ n°»1.
Tn)-a+Am) have
we We have that,
recurrences
with equation (1),
Now comparing the given
= 1. n = n o 1 0 / 1+E ) for some constant E> 0.
= 2 and fn)
a 1, b
So, applying the Case II of the Master method.
and We get
We have that Tn) = 0(fn)) =0n).
1 0noal) =1
the Master method. EXAMPLE 3.16. Solve the =
So applying the Case II of recurrence: Tn) 167l+n* by using Mastermethod.
We get,
Solution: We know that the Master method is used for solving the recurrence of the form
» > 0dgn)
= O(1.lgn)
Tn) e(nlog, a.lgn )
recurrences.
So elgn) is the solution of the given Tn)= ar+fn) . (1)
Now comparing the given recurrence with equation (1) we have,
recurrences: T(n) = 2T +n* by using Master method.
EXAMPLE 3.14. Solve the a = 16, b =4 and fin) = n2.
recurrence of the form
method is used for solving the
Solution: We know that the Master and >n2.
nog, og" log log =n2
.. (1) We have that,
Tn)= aT +fm)
we have
recurrences with equation (1), n2 log)
Now comparing the given
= na.
= =2 and So applying the Case II of the Master method.
a 2, b fln)
and nOE n0822 n We get,
We have that, Ton) = e(no8.Ign) = e(n.lgn).
n3= l o s , 2+¬ ) for some constant e > 0.
So applying the Case III of the Master method. EXAMPLE 3.17. By using the Master method solve the r e c u r r e n c e : T(n) =
7T|
We get,
Solution: We know that the Master method is used for solving the recurrence of the form
Tn) = 0(fn))» O(n*).
Ta)- a +An) .(1)
EXAMPLE 3.15. =
Solved the recurrence: Tin) T 1 nby using Master method. Now comparing the given recurrence with equation (1), we have
Solution: We know that the Master method is used for
recurrence of the form a = 7, b=3 and fn) = n?.
solving the
Tn)= aT fn) and noE nlo8> n.771
.. (1)
We have that,
Now comparing the given recurrence with equation (1), we have
n2 2,log" ) for some constant e > 0
Design and Analysis of Algorithms Recurrence
70
Ttn) = T(n) + 1
71
IlI of the Master method, Solution:
So applying the Case
Let n Tn)
We get, 2 =Vn =2/2
T(n) =
04fn)) Taking lo82 on both sides, we get
=0(7*).
log, n = m log,2
= +n?
recurrence: T(n) gn = m
the 7T
EXAMPLE 3.18. Using the Master method solve
Thus T(2) = T(2m/2) + 1
recurrence of the form
method is used for solving the
Solution: We know that the Master Now suppose Sm) = T(2m)
.. (1) We have S(m) =S(m/2) + 1
Ta)- er +fm)
Now by Master method
recurrence with equation (1) we have
Now comparing the given a = 1, b=2, An) =1
a 7, b =2 and n) =n2.
and nog nlog,l n° =1
and nog n2.80
We have that
We have that
1 e(no8;1)
n2=O(noES) On for someconstant e>0 So applying the Case II of Master method, we get
method.
So applying the Case I of the Master
Tn) = e(n gm)
We get, = e(1. Igm)
Tn) = 0nloss)»0(n*"). = 0(lgm)
Now, Put the value of m 6(lg .lgn).
EXAMPLE 3.19. Solve the recurrence: T(n) =2T +n by using the Master method
EXAMPLE 3.21. The solution to the recurrence
recurrence of the form =
Solution: We know that the Master method is used for solving the Tn) + is T(n) =
100T(n/99) log(n!) nlog(n).
(UPTU B.Tech 2002-03)
.(1)
Tn)-a7Am) Solution: TCn)= 100 T99 +Ig(n!)
Now comparing the given recurrence with equation (1), we have
a = 100, b = 99, and fln) = lgln!)
b=4 and
a =2, fn) = vn.
now Es 100 =n1002187
and
no nlos n0 Vn
We have that Ig(n!)< n8 100, for n23
So, n 100
fn) = lg (n!) = Q (n 100))
So applying the Case II of the Master method.
Now, we have to check that is 100 lea|sclg (n), for c< 1?
We get, 99
This condition can hold so problem lies in case II and the solution is-
Ttn) =
e(noss. ign) = Vn .lgn) Ttn) = e n ) ) = e(gln!))
=
We know that Igtn!) e(n lgn), therefore, T(n) =
EXAMPLE 3.20. Soluve the recurrence: (nlg n)
So, given solution is correct.
Tn) T(vn) + 1.
Analysis of Algorithms Recurence
Design and /73
recurrence.
72 Define Master Theoro
orem The recursion tree is full for l o g 1 - / levels, each contributing cn. So we guesS
method for solving UPTU B.Tech. 2003 C.o
What is Master cn, So we guess
EXAMPLE 3.22. atn =
suitable example.
losu -an)Olnlgn n). lgn), it has log,va n levels, each contributing s
using a On logva n)
Section See. 3.5. (ii) Given
Solution. Refer
=
methodd
Tn) TY9n/10)+ n
= 167 +n° by m a s t e r
EXAMPLE 3.23. Solve
T(n) We know that the master method is used for solving recurrence of the form.
Tn) = aT (nlb) + An) ...(1)
theorem.
Solution: By master
=
=
sO
a = 1, b = 10/9, and fn) =n
a 16, b 4, and fln) n
ands," n0s n° =1
and We have that
We have that
for some constant e > 0.
constant e> 0. n= 2
some (os0rs )
n2= 0S,l *5) for
Now we have to check that is af (n/b) s ef (rn) for any c <1.
falls in third case and
S cn for any e < 1. This is possible so problem
Now check for 16 i.e., is 9 n/10
solution is
Tn) = (ftn)) = B(n).
=
16sen,for e= to solve r e c u r r e n c e T(n) 4T(nl2)
so
EXAMPLE 3.25. Can the Master method be applied [UPTU B.Tech. 2004-05]
it is true regularity condition holds.
+n* log n
? Why o r why not ?
Hence. by case I , of master method = 4T (n/2) + n2 log n
Tn) = eytn) = 0(n9)). Solution: Given that T(n)
Now comparing with
recurrences:
EXAMPLE 3.24. Solve the following Tn)= aT a/b) +fn)
= +n 0<a <1
T(n)
T (a. n) +T(1 - a) Now even though it has the proper form
Tn) = T(9n/10) + n. UPTU B.Tech. 2003-04J =
=
a 4, b =2, and fn) n?log n
Solution: (i) T\n) = T\aln) + T ((1-a}n) +n case II should apply
since
so < 1/2 and 1/2 sa <1 and " o,2 n2log;2 =n2, it might seem that
a 21-a, that 0
without loss ofgenerality, let 1-a s
Now recursion tree =n2.
= is asymptotically larger than
fn) nlog n
The problem is that it is not polynomially
larger. The ratio
---- CN
fn) Og log n is
recurrence
constant e. consequently, the
for any positive
asymptotically less than n-*
09 101) c(an) c(1-a)n Cn and case 3.
falls into the gap between case 2
log 1la recurrence.
master theorm can not apply for this
So, the
UPTU B.Tech., 2005-061
1-a}n reeurrence
cal1-an Cn
' (1-a)n EXAMPLE 3.26. Solve the followving
Tin) = 1
= 4T(n/3) + n*
T(n)
Fig.3.4. Total: O(nlgn)
Recurrence
and Analysis of Algorithms 15
Design
74
+ n2
= 4T (n/3)
Given that T(n) =n2
Solution:
standard
forma =4,b =3 and fln) sco
the recurrence yields
Now comparing with Substituting into
,2x(63 n1.26
, 2log,2 n**
and "
%
TonsClog|*1
We have that
constnat e> 0.
n = 2(n3"*) for some sClo
lies in third case and = C log n - Clog 2 +1
This condition holds, so problem
Now check for "<en fore «1. = C log n -C+1
hence the solution is sClog n for C21
» etn)P. Tn) = 0 (log n).
Ton)= rn)) Hence,
recurrence exactly:
EXAMPLE 3.27. Solve the following
i f n=0,1,2,or 3 EXAMPLE 3.29. Using the Master method, give tight asymptotic bounds for the following
n
recurrences
otherwise UPTU MCA 2001-02]
+*n
UPTU B.Tech. 2006-071 (i) T(n) =4T(n/2)
the Onotation. + n? [UPTU B.Tech. 2006-07)
Bxpress your a n s w e r using (i) T(n) = 4T(n/2)
we write recurrence as + n",
Solution: First (iii) T(n) =4T(n/2)
0
-a-1 -3*'n-4 = 4
as Solution: (¢) Given that T(n)
its characteristic polynomial can be written
= =
0 = = n
-r3- x *+1 master method we have a 4, b 2, and fn)
By the
= 0
xx - 1)-1 (x- 1) and o8,a 4n2
= 0
(x -1) (r3-1)
= some constant e > 0. So applying the case I of the
(x-1)(r-1) (2 +1+ x) =0 We have that n2 On84-e) for
Cx-1) (x2+x + 1) = 0 master method
= e(n2).
roots We get T(n) 0(n°E,")
1, 1, 2
=
General solution can be written as, (ti) Given that T(n) 4T+*
= =
= n2
Now master method we have a 4, b 2 and fln)
by
Tn) G1"
arefa.« and oB n2
Tn) = e(n).
We have that
EXAMPLE 3.28. Show that the solution =
we can apply case 2of
of T(n) Tln/2) +1 is log n). Hence,
(UPTU B.Tech. 2006-06)
n 0 (n")= 0(n2) i.e., fln) is polynomially equal.
master method.
Solution: We guess that the solution is
=
Tln) = 0log n). Our method is to prove 0tn2.
Tn) se lgn for an appropriate choice that Tn) e(ftn).lg n) lg n).
of the constant c > 0. We start by assuming that n (iii) Given that
bound holds for that is
|9 T(n) =
Algorlthms Recurrence
Deslgn and
Analysls of 7
= n*
= 2 and /An)
76 =
havo a 4, b
method, we 3n+
master
Now by tho
andD, , n
constant e >0.
for some =3n+
=
n" Q (n"*")
Wo have that
Now check for a / s efn) 3n+
Let the iteration stops at i
3n+
seifore
holds. Honco, by cuse III.
it ia truo. So rogularity condition
Tn) 0 n ) ) = 0tn"). 2-1
2-1 = n i=lg n +1
recurrenoe relation by iteration
EXAMPLE 3.30. Solve the
Ton) Tn - 1) +n' UPTU MCA 2002-03 n3n
Ttn) =
3n
Solutlon: Givon that
Ton) t n - 1)+n4
Ttn-1)= n - 2) + (n - 1) +n *-D+lln
Ttn-2) TYn -3) + (n - 2) + (n - 1 + n*
TY1) Tto)+ 1 + 2 + 3 . +(n-34 + (n -2 + (n - 1 + n
+11n
tn) 1+2
3[2(n-1)]+1ln=6n*SnT(n)=oln*)
Henco, 7tn)0(n').
EXAMPLE 3.32. Show that TYn) Tlna) +n recurrence have been bound
EXAMPLE .31. Solve the following recurrence relation
nlgn)
UPTU MCA 2003-04)
Tn) 7Yn/2)+Jn? IUPTU MCA SO3.C.O Solution: We guess that the solution is 7ta) Otnlgn). Our method is to prove that
Ttn) s Ca lg for an appropriate choico of the constant c > 0. We start by assuming that this
Solutlon: Given that
boundholds for thatis that
T)
71) 11
Substituting into recurrance yields.
i--uLZ=
I+-PD=
and Analysls of Algorithms Recurrencs
Design
Solution: Using tthe recurwion tree rhown
80 belovw, we get a =
guenn of Tn) 9n)
since,
have that
Henco, case 3
applies, we
Tn)-0(Vn /2
/4
= 2T
recurrence T(n) og |ogn
recurolon tree for the
EXAMPLE 3.36. Ilustrate
tree.
means of a recurwion n/16
can gueas by n4 n/8 n/16 n32 n16
8olutlon: We get a n/32 n/64---tP (718f n
nlgn gn
total cost = Hn)
Fig. 3.7.
on/2)
g(nv2)
Hence, total cost
gn
4 n+... og,
n4 lgfn4)
kgni4) lo(n/4) g(ni4)
... log,n
Total cost. ng lon) = 0 (n).
Fig. 3.6.
Hence. total cost at depth i
Ign-i EXERCISES
1. Show that the solution of T +1 is Ollg n).
n.8 (lg lg n)
2. Using the master method you can show that the solution to the recurrence Tni = T
0(n Ig lg n).
is Tin) n ) . Show that a substitution
EXAMPLE 3.37. Illustrate recurslon
Tn)= T
proof with the assumption Tn) s enfais
Then show how to subtract off a lower-order term to make a substitution proof work
by recurslon tree
method.
. Solve the recurrence Tn) = +
3T(Wn log n by making a change ofvariables
82 Design and Analysis of Algorithms
4. Use a recursion tree to solve the following recurrence
your a
Tn)= and use the substitution method to verify
472n CHAPTER-4
Use a recursion tree method to solve the recurrence T(n) = T(n - 1 ) + T n
5 and use the
substitution method to verify your answer.
6. Using the master method you can show that the solution to the reeurrence
nn)= 47|+n is Ttn) = tn*). Show that a substitution proofwith the assumption Ttn)s
Sorting Algorithms
en fails. Then show how to subtract offa lower order term to make a substitution
proofwork
7. Use a recursion tree to solve the following recurrence.
Ttn) 27n - 1)+ 1. Use the substitution method to verify your answer.
= 4.1. INTRODUCTION
recurrence Ttn) 27|G|+0(n).
Using the master method solved the
a elements
In computer science and mathematics, sorting algorithm is an algorithm that puts
9. Solve the recurrence Ttn) = 8T of a list in a certain order. The most-used orders are numerical order and alphabetical
order.
for the following recurrence.
10. Use the master method to give tight asymptotic bounds
Many algorithms a r e present in the computer world to manage the data in a n order and
(b)
these algorithms are known as sorting algorithms. Each one of the algorithms has its merits
(a) Tn))= and demerits, some algorithms such as Bubble sort, selection sort and insertion sort are the
example of simple sorting but on the other hand quick sort, merge, heap sort are the example
(c) Tin))=; (d)Tn)= * of complex sorting.
The sorting problem has attracted a great deal of research, new sorting algorithms are
(er Tin)= Tn-2) +2 lgn Ta)= Vnr(n)*n still being invented (for example, library sort was first published in 2004).
Sorting algorithms used in computer science are often classified by:
(h) Tn) =
(Tn)=T(Vn)+1 3T Computational complexity (worst, average and best behaviour) ofelement comparisons
in terms of the size of the list. Big O notation provide ideal behaviour fora sort but this
is not possible in the average case.
000 Computational complexity of swaps (for "in place" algorithms).
Memory usage (and use of other conmputer resources).
Recursion: Some algorithms are either recursive or non-recursive, while others may be
both (eg. merge sort).
Stability: stable sorting algorithms maintain the relative order of records with equal
keys (i.e., values).
Adaptability: Whether or not the input affects the runningtime.Algorithms that take
this into account are
known to be adaptive.
4.2. INSERTION SORT
ertion sort is a simple sorting algorithm, an unsorted object can be inserted in the sorted
set i
proper place, and the sorted array is built one entry at a time. It is much less
(or list)
83