DAA Unit-2 YNotes
DAA Unit-2 YNotes
Dsol2o
ONIT-2
okore,
dn)lime to conclude the divect ansuwr for srelipedb
Fn)Time fof dividing k combining tu solufion fors a
7-
Gukstitatiorn Methed
Here, repeatedly made euhstitutions for each OccurenLe
of the Function T in the right hans tide (R Hs
unti all the occen(es disappears
From e,
&o62aT(nllda") +nlog,h)
rl62T(n/n8°) tn (log,n)
n Tn/n )t n(log2h J
nTLID +h ll6seh)
antnl og, n) [U)-2]
Tn) =O(nlos,n)
Example 2: al, b:2,TLI)=2 k fln)zn Find Timelonpleity
SoluhenTln)=aTUh/h)+ F(n)
Tn/2) + -O
= T (n/u) + nl2+n = TlnM)r3N/2 -2)
= T(n/s) +n/+3h/2
T(n/g)+ 7n/4
So, he qeneralized egjuation s
Tin)=T)+2 kelog,n=la,n
No
TUn)=T(2
+no2-1Y
282
T ) + (2hCn-))
T)+2n-2 = 2+2n-2 LTD-2
2n
oglol202) 38
Toucers of Haoi(n-1, A, C, B)
Towes ofHonoi (n-1, L, BA
Tn) 2 T (n-1) t
2(2 T (n-2)+1) t
2 (2.2I h3)t)+) +1
a.T(n-n) t (n)
a+n)
OC)
bhewexalized egaton j
Tn)2*TUn-k) +z*- K-n
2T(ntn-))+21
2 T I ) + 2*'-1
2 x2"-)
2-
OC2")
C/ok02)
Master's heorem
f(n)
OC) n
Aouo tke regarence relation for master's theorem ib
delice d ax-
Ta)-n LT)+UUn)]
hn)=_Fln)
n
The asymtotie valuas of Un) for hln) a e
s folld
hn) UCn)
oU)K<0
elln)i20 (logn)"h*)
nen) Y>0
osliol 202) 40
.uh) OU)
Tlh)= n, LTUD +tUCn]
no c +ou)]
-
n Lc+ oud
OCn8)
= OCn))
, nln)= n ) = un 4n =1n
n9
n
Since, 4>O
. v n ) eChin)) = elun)
yopTn)= T()+C
a , b =n
hLn)= C
Sinle Vn) =
e llog n )otot
o e Clogn
.'Tn)=n*cte (laqn
nLC+ log n)
1Lc+6Cloq)
=elag
sople-TCn)= TCa) +c
a= b - FCn)
hln) C
hloga
legn
. Tln) =n Lc+0(log n) 1
n telog n))
=[cte (logn)
ello4
42
18l1sl22
carple Tln) =2 T l ) tlog n
Jia a 2, b , AM)»logh
hn) =-FCn) =losh=logn log n
laa 22
n
logn
h
'Tln)=nsTVn)J
n L C+ Olgn)J
n Lcte log n)
a l a l 2 o
a b2 f(n)= Cn
as b4 FCn)=Cn2
2/
a-28 b=3 FLn)=Cn
aL b=2 FLn)-Cn
i ive
-Cn'
hn)=_Cn
n
Cn no
Y>O
. Dn) =
_e (hn)) =
©ltn)
Tn)= nl LTLI)+UCn)
n LC+OCCn))
[C+0(tn)J
h(n)=Fln) n : Ch Cns yo
n h h2
u(n) = 0lth)
Tin)=nb6LTCI) +Uln)
nTtt9ln)J
n 6ln) on2)
Given a228 b3 fln)= Cn
hn) =f ln) =Cn3 =
C n= 0.88
YKO
yla h28 n
12
g ol202
Dn) O(1)
Tn)= n3[TLI) +Un)]
nd2cto)
=OCn)
Binoru Search
Agorithm Binag(SearchLn, key,saet,host)
Mid i
F CALiJ=Xu) Hon
turn
tlse ifCi=i)
etun O
lseifA A)<keg) ndd-
return EinarySorh(n,key,b,)
lse idt
etun Binatysearchn, kes i i
Let, Pa (n, ai,i, duotes In'is he Xunbr
elumentsokare ai,,g is he list of elnenta k
Ixi the seorched elemant
Let "small(p)" is tue it n=l, otterwise it fates neo.
t p ha More ton on elunant it can be divided md
ML Sub problees as follouas
of Lbu,hahJ.
First ind h mid value in the Yange
TF X= Amid then the problemp is immediately soled
IF X<ky heh seafth only letf sublistl4,4it. mjd-i)
T X>kay Hon seach only right suslist l4maa)a4j)
of Binary Search:
Analysis
Kecursive Equation: TCn) =Tln/2) +C
Tn) =aT(n/b)tfln) a1,ba2 fln)=C
and
hn)-fln) - ela nd
ns4
Merge Sort
This sorting algorithm ues tha ides of divide kiom
biven a selante of elomants such as
aLII,a[2],
Coquor
the deneral idea is to split info
tuo sub problena suh a
aL, aL21, aL3)...aLh
ali, Ln/2] a[hl2 +3,.. aLn]
Now, each au-pro blem bo1ted indiv
he reultiug Sorted elbunts idually
a Single 2orted
ae Merged to rodute
seule 7elments:
Morge Sort Algorithm
Algorithn Mexge Sort lous, high)
ifClow s high) Hen
mid: (lowthigh)/2;
Merge Sortllouw, mid)
MrgeSortLmid +l, high)
3
Merge Llouw, mid, kigh)
Merge Aqorithm
Algerithm Mege Llouw, mid, high)
i:lousJ:=mid tl, k:= loo
whilelCi<=mid) and Cj<=hiah)) do
else
templk]:a[jt+]j
3 ohile i <amid)
termpLkt]:: alirt];
while Lj<=high)
2
tempLk::atjitlj
alh tempLhl
3
Mexge Sort Analysis
hecrsive Equation: TLo)=2Tn2) + C.n
Tl)-aT (nlb) tfCn) a=2,b=2 k Fln)= cn
hln) f(n) Cn =(n =C n°
Max-Min Aprithm
Algorithm Max Min li,J, max,min)
F Ci=i) then
max min = aLi
5 elae if li=j-) hen
flaLi<aLi3) then£
max aLj3;
min ali
3
3 else
mid:=(itiD/2;
Maxlin li, mid, max, min);
MaxMin Lmidt1, j, maxl, min!
iE LmaxCmaxl) then
maxmaxl
FCmin >min ) then
Min mid li
3
3
to2
Analysis
O ,n=E
T(n)= n z2
TA)T(2)+2 , n>2
Tln): T(+T(E)+2
= 2T(2)+2
2 kT(4)+2]42 =4T(A)+4+ 2
2T)+2*+2
=2T()12t2*+2
= T ) +2"+2 +2+2'+ 2
K- numberof Lomparison6,
Nou Sulshihi
TUn)-2T +22 +2°+22 ('ns2)
T2)+2tt2
E2k+-Dl2-) I '2.+2 in P)
E
2(2-1) ( S:alrM-D/r-))
2 2
Noud, substituting k- log,n Jh the akove _eqjiation
Tlo): s 2k+2 2
Los2$2
+2 2
= (za)/2 +2es."-2
TUn)= 2"2
-2
nl2)+n-2 = On)
(3nl2)-2
Therelpre
Time Com plexi ty=On)
2c1ol2oel
PartitionAlgorithm
Alorithn partitèn Lar,loo,kigh)
i i owtl, Ji= high Fivet=anLlou]
hile lis=j)
ohile llarCilkpivot andikhigh))i
it+
3
whileárrLj3 >pivot)£
jtt
3
Analysis
Bestlasel Averege Case
In this case, Ha quick
sort would be when we aetE
two Sub-lists are of eual lists
Tn)=nt 2(n/2) + 4lnle)+. tnln/n)
ntNtNt th
n(logah) = oln logn)
2)Woyst Case'
smaller or largest eloant of the
If the pivot is the
ist hen it dividesthe lis+ into tuo Sub-lists frem
hch one is "empty and other contains un- elamuut
So the total nunber oF comparjaions (2A lbei-
Tlh) =n- th-2 + h-3t. t)
n l n ) = OCnZ)
2 3
20lol202 O 5 21
beective hesboard
ax2
k=0 IXI
K=1>2x2
k-2IX
K=3 8Xg
cR7
221617 17
1419 13 1 1
T(n) 4l4TlnDIC]tC
43T(n-3) + t HC+C
T(n-n)+yct4tt4Ct4tc
=AT(o)+4c+4tt4tCHC
Ld t4C+4Ctt4CtC
L4")
Algorithm lileBosrd LtopR, tope, defet R,defect C, size)
Iflsize=D Yeturn
tit olse tilet+
=
QuadSize = sizel2
PC defeet R<LtbpR+quadSize) and defetc topCtd
Tle BoardltopR, topC,defectR, delectC, Jeasze))
elsei
boardLtbpKtyaaze-LLitopl+jadsize -=tleTole.
TleBard(topk, top, topRt4uadsize-1,topltpakizet, q\uada
3
3
211ol2o
Shassen's matrix
muliplication
Block and
multiplication
Dand&algorithm for Matyix
Algorithm Dnatmal LA, B,n)
if Cn 2) hen i
=
Matmul CAB,4,n);
elsei
CL= Dmatrmd (All,BL,nlz)+Dndtral (A1282)n)
DmdrulA12,822ne
C12 Dmatml(A),B12, h/2)+
c2] Dmdmul (A21,B1),n/2) tDmatrnulA23,82), ne);
=