0% found this document useful (0 votes)
10 views20 pages

DAA Unit-2 YNotes

The document discusses control abstraction for the Divide and Conquer technique, detailing algorithms and their time complexities. It covers various examples, including Fibonacci sequence calculations and the Towers of Hanoi problem, while also introducing the Master's theorem for analyzing recursive relations. Additionally, it explores sorting algorithms like Merge Sort and searching algorithms like Binary Search, providing insights into their efficiencies and complexities.

Uploaded by

Yagneswar Naidu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views20 pages

DAA Unit-2 YNotes

The document discusses control abstraction for the Divide and Conquer technique, detailing algorithms and their time complexities. It covers various examples, including Fibonacci sequence calculations and the Towers of Hanoi problem, while also introducing the Master's theorem for analyzing recursive relations. Additionally, it explores sorting algorithms like Merge Sort and searching algorithms like Binary Search, providing insights into their efficiencies and complexities.

Uploaded by

Yagneswar Naidu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

34

Dsol2o
ONIT-2

UVIDE AND CONAUER (DAC)


Control abstraction for Divide k longjur Techbnigue

Algorithm Dand LP)


if small(P) then _etum solution CP);
else

Divide "p in th P,2,B,..insdandas, kal,


Aply Dk on
evy instantej
Combine (DandCLP),Dandcd.).,dclA);_
3

Diyide &Longue.D&2C) Dkinition


Given a function to compute 'n' inpus the
divide k Conqure strategy uggests sphting o
inputs in to 'ksub problems such Hat 1<kin

These ub problems must be sdved k Cornaini He


ub olutons into ohole Aolution
Example Fibbhuci Segote
Fib lo)= O Fib 4
Fib)=
Fibn) Fibln-)tFLUn Fibla) Fible)
Fijb l) Fibl) Fibl)) Fiblo)
Fibli Fiblo
2S

Then the Compufing ime of Dand is descried


TecurenLe relation is: by
Tb) (n)
Uni)+T
+T(n2)t.. +Tln,)+ fn
.

okore,
dn)lime to conclude the divect ansuwr for srelipedb
Fn)Time fof dividing k combining tu solufion fors a
7-

Thoxelore. the reu4tne relation is


Tn) TLI); if n=i
aTlnlb) +Fln)if Ch>D
okore 'ak'b are Constans & aume'N is poioer o'b
2e n=
?logn> k log b k -logn k= log,
lo b

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

Etrde:a-2, b-2, Tl)=2 & fln)=n. Find Tme lamplexity?


Ae kale, Tln)= aT(n/b) +flh)
Th) =2T(n/2)+ n_
l2T(n/u) +n]tn llsuhstituting
4TCn/4) +2n
t 2Tn/8)+n] +2n Isubsituhing
3T(h/8)+3n
36
21l1o/201
So bheneralized tqtation is
TUn)= aTlnz) +kn -
loq,n b-2)
Lohere, k=log n
=

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

Find Tine lomplexity for louebof Hano i


Tn) = 2 T (n-1)+1

Algorithm TowersafHanoi Ch,A,8,C)


ib0.o uskA,B,COn foweuA.
ifCn>1)

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

Tn)= a. T(/b) tFCn)

f(n)

f/4) fln/b) Flr/b) a.flrh)

Flr/) fln/) F(n/) afl"/*)

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

EKonpe- T(n) =}Ttn/e) +18n2

Sol a7,b= 2, FLn)=1gn


.. hln) =fln)1 8 n =18n =18n i
n's h2-8 nlos
Since, = -0.g <0

.uh) OU)
Tlh)= n, LTUD +tUCn]
no c +ou)]
-

n Lc+ oud
OCn8)
= OCn))

XampleTn)= 9. T(M) +4n


4iven, a=9 b 3 Pln)- 4n6

, nln)= n ) = un 4n =1n
n9
n
Since, 4>O
. v n ) eChin)) = elun)

Th)= n%T(I) +O(tn")|


n[c+ e(un
O (un$) +C
OCn$)
s l o l o y

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

Un)= 6 Llogn)" =9llu n)


' TÚn)= ct&lba n
4LCtologn)"J
n)

logn
h

CXample a=2 b -2 fln)=C-n


n = Cn°
hn)=Cn
hlog
UCn)= oClog n) or | : Ollogn)

'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

iven, a:s b=4 FLi)= Cn

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

(j, ajrn, itz) aj)


z/plze

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

Comespanding uln)= e(log0°lo*1= 6la n)


'Tn)= n%pTUD +uln)]
=nLcte loyh)D
n°Le Llog h]
OClog n)
Best Case eC)
Averege Coae: GClgn)
Worst Case : OCloqn)
Exerciae
Prive recursive binary Ssearch fiee
oith ku=31 _for the Jollouwing dab
S731,s,17,19,22,26, 31
allol2o2 Su

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

f aLiJK2 alj3) thon


fempLk:alitt]j

else
templk]:a[jt+]j

3 ohile i <amid)

termpLkt]:: alirt];
while Lj<=high)
2
tempLk::atjitlj

forlh low to high) do

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°

Correa pond uln)= 9Clogn/orl=e llog n)


Therelere Tn) n"TCD tuln)]_
-no LCtel h
nle(la n)
Tn)- 6nlogn)
2lol2o2
Findinq Maximum Minirun

Objective This problem & to find the raximum kminimum iten


in a list of n elemnts .

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

Aan uick Sodt


This sorting algorihm e s the dea of divide k Congier.

Gluick Sort Algorithrm


Algorithm uck&orlarr,lou,high)
if(low= high) Hen Yeturni
Pivlacpaxtitionlarr, loua,high)
uick Sort Lar, loo, plot-1
GuickSort Cart, pivott), high))
3

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

Staplarr Ci, arrc)3)


itt j-
else i
arrLlow=4rr L}7; anLi3: =pivot
retum j
49

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

I n delective choss oard, it s aluxys _considered


that box is dolutive in the ches boord
Size 2x 2 4 k= 0,1,2,.n
TLn)= 4T(h-) tG
Tln-)= 4T(n-2) +¢

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

basic matrix mullipliation's


W e knoo hat
Oln3) =
Time Complexity duce d to
Tt we are intro
T o redule the
Strassen's matyix rultipiation

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);
=

C22 Dmatral (A21, B12, nle)t Dmatmul[A22,82,nla)

Strassen's Kecwene yelation


TCn )= 7T(n/2) + Ch2
whore a7, b-2 amd fln)= Cn
hln)=Cn/n?31 =c.no
uln)= OC)
Thoreprt Tn) *LTUD +uln
= OCn2)

You might also like