0% found this document useful (0 votes)
15 views22 pages

Adobe Scan 23 Nov 2024

Uploaded by

patildhan061
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)
15 views22 pages

Adobe Scan 23 Nov 2024

Uploaded by

patildhan061
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/ 22

YOUVA

Date

A5SIGNENI NO. 1: DIVIDE AND


CONQUER
1 whot is time ond Space Compleæity Explain
dith eeample
Thete ate tDO Suchmethods used time Complexty
and Sspace Compleeity Hhich ate discussed belowi
Time Cornplex'tyi
The time Compleeity of on olqomthm defines the
amounb of time taken by an algomhm
Func ion of the length of the input
the valid algoithm tokes a Anite omount of tine
For eecutioo

The time meguimed by the olgomthm to Solve qiven


poblem is called tine Complexiy
TÉme cornpleity is vety useful measute in algomthm
analysis
It is he +ime needed for the Comple tion
algoithm.
Ta estimote the time Compleæiy we need to
Considet the Cast of eoch fundormentol ins+TUCHion
and the numbet of times the insiucHion is
ecuted
Date

VOUVA

Scalat.
Exbrmpe. AddiNon
cf too vatiobles::
ADD SCALAR CA, G) addiioo
Algonthm atithmeic
Petforn
WDescnption : Dumbebs
YotbioblesA ondA
IlInput:To Scolat
IlOukput: vatiob)e C Which holds the addiion of A

C<-A +B
etutn C

The oddition of tuoo ScolakDumbets reguires


one addition opeko+on The hme Compleity afthÙ
algoithm is Constont so Ten) 0 ) :
The pSeudo- Code is Os Follocosi

int aCnli
for (int i:o; i<n it+)
Cin>> ol]

for Cint i=0;i<n;)


for (int j=0j j<nj j})
qetutn tue
metutn false

Spoce Coropleityi
roblem-Soluing usingdotqComputet
to bold emparaty equiTesNhile e
oo Aino cesut
papgom_ 15S in eceution.
M T

Page No.:
yoUVA
Date:

The amount of memomy requiTed by the algonthm


to Solue qiven poblem is called Space Conpexity
of the algonthn
The space Compleseity o£an algomthm quonifes
the omount of Space taken by on algooritbn toto nun
as a Aunction o the length Of the input

Tt S the omOunt oA memoy needed for the


Cornplehon of On alganhm

To eStirmote the memony equimement we need


to focus on to0 patbs
gA Azed patt:
Tt iS independernt of the input siz
This includes memoy foor inStuctioos ÇCade), Constant
|vabjoble ebc

A Natioble pabti
It iSdependent on the inpus Size.
It incudes memoy forr recuksion Sack, mefEetenced
VabiableS ebc

Ecormp)e : Additioo of£ t o Scalat. vabiables:

AlgonthmADD SCALARCA.B)
Descoipion:Petforrn atithne+ic add ition Of two
Dumbets
Input Tuso Scolat uatiables A gnde
/Qutput: vatiable C ANhich holds the addition of A
lond
C<-A + 8
etutn C:
numbeS
addition of two
SCalat reguires one
The to bold the cSult
location
algomthm 1s Thus
Constant the
Spoce Cormplexity Of this
bence Sp)
is as followS:
The pseudo - code

int freg Cn]i


int oCnli
for (ins is0; i<n; it+)
Cin >> alil;
foreg Lq CiJJ++;
nple.Bigoh Bi9- Onega. and Theta
eca
9 fxplain with qraph tor Etw funclond:
also plo* a.
aig + oh &ig- Ormega (2)Big Theta (0)
T is ixe (5redc It js ike
C>=) I4 is ike (:) meaning
goowth of an ate of gmwth is the ajc of osth is
ocqual.to a specificd
algoidbm. AS. le38 than, qoeatctdhan
egual o a Specific equal to a sprcified Yalue
alue Yalue

the uppet bound of The algothm's The bounding of


alaoridbm js nepre[anhlocoet bouhd is functi o0 omabove
A big onojalen nepve[er, ted by and belbw is epesentd
only 4hc above
fhe
foncti on is bounded
mega botalon
byby theta notoc

ASymptoti cuppet ind The asyra ptoti C The ezact $sym ptoic
k qiven by biq (owet bound is bebavior is done by
notahon qiven by orneya HhiS tbeBa notatior
Dototon

o-uppet BoundBi Omega (S) Big Theta (o) -


-Louoet Bou nd Tighà Bound
hathemahcally Marhenah cally: maihematicalty - Big
eig oh is 0 6iq Omega is o Theta i 0<eC24in)
for allnD0
Date
Page

definedas
definedstis
Hghdestbound
T+ is
and tocoet bound and hhest bound is he
ppet ourd apJouek bound onon bes t of all the Wost
ppet hound oco alqoithm l's the Case imes that the
algonthm s he
tnount ofeast omeund oF aloithm Canaxe.
-most ime megured
-Hme eguimrd
Grrophia Example fo
fxample Biq- Theta
Gmophic foeomple Grmaphic

TmeY

fen)

no

C49cn)
aassMate
Date,
Page.

UNTT 1:- Dide and Conguct


bebaween prion and poskebon analys.
A prion Analysis
A postenmoi Analysis
Gn abSoude
analy Sis analysis

It is depen dent on language T6 is iddeperdent of larqucgt.


of compilek and type of O£ Compilet and types
hatdounte. hatdusate

t Aill qive exact Anbw et T* N) qive appnfpaate


anßwet

t do esn use osyrnptatic. T$ used the asy mptoic


Dototions to gepreóen* he. hodrtionå to reparesent ho)
ie Cormpieaity: much time the algostbm
toke in omdet toComplcte
i8s eectution:

The time Cormplee1ty of an The ime


algarithr using a. postebi or dlqos thm
Compieoeity an

anglysis difAo foon sysienm laoaly[1'sis using


a pio
Game for eveby
to Sy$em

TE the thrne daken by the


peDgerarn isless then the fasset tdit qocd to the
orsedit iN 9o to Com pilet prngrommeki
and batdwatc
classMAte
Date
Page

done et eeccution Tt Js donc before eRccution


Qn aiguvithnn of analqomthm
isCosier than It is cheapet than posdebiia
paionanalysis analysis

maintaingnce pbabe is nob


Main tainapce p base ís
Tequyired to tutbe the equírd to tune tbe
alqothm atyonthom.

) Ezplain oilieb -Rabin primality dessing algoorithm


oillet Ra bin is a fast appoDach to est pimality
of the latge Oumbebs

This alg6 nthrn is called a Rabin -millet pimaliy


Jsk" and this alq oonbrndecides Nhetbet numbet is
pime Nhich is Some to othet est'

This est isbased on equatity of gouo of tgualiies


that hold& the fue for pnme
oe values"
milet Rabin validate that he numbet IS Compasite
60 dhi S S Qalled Composike ness t t othet han
pamoli ty tesb
Date
Page

CompaSibe numbel ntheta


heach3)4 ate hitnesSes o
least o£nurobet s
Composi be ness of n

simple esetension oF Fetaks.


ass o cia tively
It is OSS fon
Theoorem enoble uS to st
that
LHle

-Rabin Tesb
The Pse udoco de fo milet

Lailet- Rabin -Ttest Cna)


lo iS the bum bet o is the base
And m and k Such that -)e mx 2*

T a" mod n

f o r ( 1 to k-1) ) k-1 is the mazimum purmet


2

Composide "

retuto 'q Cormposi br"

Fæample Apply jllek -Rabin algosthrn sing bse 2 to


desthe+hek the numbet 94) is ormposi e
hob'
classMAte.
Date
Page

solution:

step 1
- 2X 85. Thus
p 341 kE2 and q85

step2 X 2 Cqivên)
seo3= 985 mod g4: (9") x 2° mod g4
mod
ELx81g2 mod 34|= 8)9 2 mod 341

Sep 4
29 ood

a.22°d 94 x2 mod ig490 mod 34


=|x256 x 256

Noo E 256 1

and rebul is io conclusive


s0 341 is nob a composile numbet.
foreg CoCiJJ++;

Conguet Maeinum
22Eeplain
prove
Divide
that is
and
cormpleeity is 3nl2-2 algonithm
LDivide ond Conquet apprnach hos thee Stens at
each leve) o£ recuSiooi
Divide theproblenn into numbet o Smallet uniks
called subpmblenS
Conguet (Solue) the 5ub-poblems meCut8ively
Comnbine tbe Solu+ioos of all the sub-prblemj int
a Soluion far the omginal poblem
mocimum and Minimumi
Let us conSidet [inmple poblem that can be solued
by the
diuide and Conguet. technique:
YOtVA

he blemiS to
valucLO a sct of
tDdthcDirnunandLiDiDum
n elccobs

AGy compaçng nunbcts of elcacabsthe tioe


ComplCuty cf this algomtha Cun bc analyzed:

Hencehe tinme is dedctrmincd mainy bythe


totol Cost of the elemcnt CornpatiÓ0D

Alqonithm traight mmakMin (a.nmax min)


USes MOk t the naimum and min to the
mimmum of aCn]

Max= Min: QC1]


for i= 2 ton do

IÆ ( Qti] <min)then min = QLil;

Explonaticn:
9]Stroight MapMin requires 2(n-l) element Compabison
in the best ovetage and ewoonst cases
J By real izing +he Compotison of aci] mak is folse
imjpovement in o algonthm Cao be done:

Hence He Con meolQce the canenbs of the for loop


by if (oCiJ> Mox) then mak QciJ; EISe 1E (alWk2

on the avetage atiis> ma% half +he time and [0


the aug no; of Compation is 9n/22
Dale

yOUVA

Conguet Algoithm tom this poable


LA Diuide ond followsi
Nould pDceed 0s

a) Letps (n.aCl
af the problem
QLj)denote
atbitaty inShance
JiSt (ot).
Hete 'n'is the no: of elemenbsS the
ond Ne ate in+etestedin Anding the mairmum
minimum of the Lisk
Qnd
IF he iSt bos more han 2 elemen&SPhas to be
dvided into Smallet iAStonces'

d) Por esomple He might divide 'p into the 2inskans


aLn1) AAet bauin divided pinto 2 Smallek
sub poblenscoe Can Solue them by ecutSiuely
invoking the dame dividc and Conguet algonthm
Exomplei
A 4 9

WalueS 99 -5 -2 15 31 44

Tree DiagTom
M W

Page No.
YOUVA
Date:

19.&. 9
5

1.548, -5 L6.9.5o,-9
4 6

1322, -5 4.560_1O 6.73.-9 L8.9.50,15


2

L2.33, -5 3.9.,2.28

fig:- Tree diagrom


ASShown in gboveGigute each hode hos 4 iHems
Of infomation iijna2c ond min
)In Hiqute4 ootinodei Contoias - and 9os the Volue
OF 1 and j Comespondng to theinital call to MoseMi

Gii This erecUtion pmduces2 new cals to Mainhet


iand j have dhe values 15 ond S.9 respectively ond
thus split the set into 2 Subset of appooimajely
the [ame óize

MORimum depth OÂ recuksion is 4

Compleeity i
T Ten) geprcóenbs S no. then the mesuling
recubt ence relation iS
T¢n): T(Co12]+TCn|2]+2 n2)
1
nEIK fo boa
IWhen n is
q_powet
integek'k dhen
OF 2,
pasiive
Tn)= 2T(n/2)+2
32 (97(ol4)+2)+2
=4T (nj4)+4+2
+E 131<k-)K2i
=2k-1 I(2)
=2k-1+2K-2

Tin) : (3n)2 )-2


(3n)2)-2 is the best-avetoge ond czsk
CoCÁsk
Node that
COse Do: 0f Compabiaon Ahen nJ5 apQweb af 9.

tbot
9)Ezeploain metge Sort algorithrn ond Sho)
Libs Cormplezeity O(n logn)
AS Anothet eeomole of Divide and Conguet
invesigoteq Soochng algomthm thot has thenice
popetty thob in thÇ Nonst Cose iBS Compleeity
isOo lagn)
Ne 0sSune thooughöut thot the elements ake to b
Sorted in nondeoreasing 09det:
Alqomthm foor mebge dorti

Algomithm metge. Sor*(loua high)


maloco: high]/s a global attay to be sormted
LSmoll (P) i5 toue £ thete is only one elemens
-to SortIA this Case the ist is aortody Srted
IPags Nr
YOUVA

if clow < high )+hen I|IA thete ate more thon ane
lelement

WDivide Pinto Subproblems


WEind Nhete to Gplit the seb
midi= Llo @
+ high) /2] »
)Soluethe dubpsblemS
Metoe Sort(loo. mid)i
Metge sot(mid 1higb)
LICormbine the Zolution
Mekge (losmid.high Ji

Exomple fom Metqe sort Hith the Compleity of

179 954,285, 310 351 423, 45o, 520 652.86L)

1.10

1.5

45 6 9lo

19 44 55 6,7 8,8 9,9

1.1 66
Fiq: Tree of calISs o£ Metye Saart (1.t0)
M

The obove tee is meprGsen bs he seguenre.


Ccutsive coll3 hat ate prpduced by metge Sont
Hhen it is opplie toen elem ents:

The pojr of valuesin each n0de abe the vol.


patametcts Joo and bigh. Nohce hoto he Spli
Coninues unil Sess containing a single
ate pamduced. elemens
TE the time foa the metging petaion i propoc
to n, then hê compuhng Hme tom metae sort ik
deson bed by the eubtencc Reloon

Tin)=a
2T(n)9)+ cn nz1,Ca constaot

When n is o poLoet of. go:9*, we can [olvethi


eguothooby suCLassive substitutions '
TCn) = (2T(n)4)+Cnl2)+co
= 4T(nl4 )+ 2cn.
=9KT)+ kCO
= ant Co loq
It is easy to Gee that iA 9K<nsgk*hen Tn)
T(9*+).
Paye No. YOUVA
Date

hite algoithm to óe)ect nth Srnallest eleroeot


in list Compuje jb's Compleeity
Sraallestelementi
Alqomhm for Seleching nth
function quicKSeleck (Jist, left mightk)
etutnist Ceft]

Select pivot Trdee between le brmght

Pivot Indexeis pabtiiooa Clisk left mgbtpivotTrdex)

elSe ifK< pivotIndex.


mght pivotInder-1
elSe
Jefk i= pivos Iodee t 1

The Complezity of the aboue algoithm ate as


folloos
Ons in the Honst case but on avebaqe Nork in
oln logn) time and petforms beHet thorn pionity
queue based algonthm
Auzeilaty Space
Ocn) toT rCCutsion calL Stack_in Hors& case. on
auekage ifa(lag n)
the [eguence a
epTcSenBs etoe So
tree is pnduCeg by
The obove colls that ate
ecubsive ten elem ents
is opolied to
Hhen it node ake the olues
The poi of
oluesin coch
high. NoHcebow the fthe
Spliu:S
patometots loo and containing a Single elern eDks
Continues unil Se$s
abc pabduced
metgin9 opetahon is popori
the
JÉ the time forconmpuing toormetge sot s
ime
to n, hen the
the mcubtencc Relahon
desoi bed by

Tin)= Sa
L2I(n!2) + cn nz 1.Ca conStant

can [olve this


When n iso potoet of. g0:2*, We
eguotion by succesS ivc substitutionsi
Tn) =2(2T (n)4)+ Cal2)+ca
:47(nl4)+ 2cn
= 4(2T[n /8) +cn | 4)2cn:
:2kTu) + kcn
=an+ Cn loq h.

It is easy to Gee thot i£ gk<ns 9k*hen Tun)


T(2**).
Ttn): O(n log n)
Explain 9eneto) method of Divide grod corguet uSly
Eseormp)e:
M

Page tio
YoUVA

Givena Functioo to Compue on n iopubS the


divjde ond conguet Gtrategy suggesbs splihiog the
iapubs into K distinct subsebs, 1<k n yielding K
subproblems

These subprablemS must be solved aod then ameth


od must be found to cormbine bubsoluons into a
solution of the Hhole.

If +he gubpnblens ate still elatively lat ehen


the divideand coaguet Stnateqy Can possibly be
heapplied'

Often he subpmblem esulting foma divide and


Canquet desiqn ate he dane type as tbe ongiral
pooblems
Ezormple and Algooathm foor divide and Conguet

Algonithm DAndcep)
£ Smal(P) then retutn Stp)i
elSe

divide P into Smallet instonces A. a .Pis.k ;


Apply DAnd C to each of these subpablenSi
retutn Combine çpADd cfp).DAnd c(pa)DAndc
cpk) );
Daic:

Contol ahsracion for divide Qnd Conqueb

opplicotions Of DAndc. Combine


Recuksive toP uSing the
mine5 the óolution
that dejet
to the k subpsblerna
hond the SizesoA
the 3ize of P is the k
mespechuely. then h
Compuing pmblem time of DAndcis descasbed
eccuttence nelahon
byg,
Ttn)= gn)
LTea)+ T(na)+
alhete Tn) i5he tme for DAndC on any input a£
Size n Qnd qcn s the me to Compute the ansuet.
diectly for Small inpubs
The Aunction a£ fn)i the time for dividing Pond.
Combining the Solu+ion& toSubpablemå
or divide - and-Conguet based algorithmd that pmdue
subpoblems Of the [ame type as the oniginal nblam
The Complexity o£ many divide-and-Conquet algont
is qiven hy
by re cuttences of the form
Ten)=Ty
LaTenlb)+ftn)nzL
Ahetca andb ate knoon as Constont He QSSumc

thot TO)IS Knouwn and n is a pouet o£ blie n:b?


Date
Page

andonte
Di£fetence behween Las- VegaS
J8)
catlo algorn'thm
monte Catlo alg0mthn
Las Yegas Algonithn
Aspecs |may prduee incog
Aleway S Corecb
re[uibs
Corsectness
Non-zeto bus bounded
zeto cettor pmbability Can
on
porohalitl: oflenbé meduced)

Runtime onAed om predicBo ble


Nabiable_(deperdon
Landon choices) untime

both Cosirecknels
Randonizatia AfecbS nuDtme bukA£fecbs
not corecbness Qnd vntime
Role

focus Ens ute S comc conessLnsuteS prdictable rtn


unime oay vaty Conmekness is ppba isic
Examples LRon domized QuicKsorb Pobalisi c pimality test
Ran dornized
Rondbcoized min -cus: mobte catlo iteratlog:
USe Cases: Cohcal omecbnes S Lfast Ppmjmadions
vatiable pebformane Hith to)étable cttoos
ble.
acepkl

You might also like