UNIT- 2
DiviDE CONGUER AL GDR rHM
A dividc Conquer algortfm ecursively
0 more
breaks douon a problem in to two
Sarme or volatadte
aub-proboms ofha. to khe
Until tiese be
oma. sinple enough
Solved d ivectty. to te subproblons
yfhe' soutions sourton to
to give a
4hen
ther Com birad
are
the
oriqinal problin.
£aN: Diridz Lcorqer
Tn)= aTlylb)+ 4(nyerg
noo qub alaments
in subproblon
Problem
n-size ofde probo b kased on the asnphi
assumph on
a all sulpoblems are o sone sio
MERGE So RT categooy f
Lttalls un de the
Conuar, technique : two halves
Diide 'probon in to,
’nd
segment a
We solvatem ndindualy Atter foding
halfue nerge tcm bark
each
Hie soln man probom
nt -fhe soln
to
dikit loox (n-+)
n=5
3
Las n=5phich vanies so karp t s
Commor,
ro smarge oponthen ts
whichaan be cwiten as h)
e slitirg tie
b how yau ane. subpotan
(valus)emnts in te
Master's Theoreng f0
o Didhag
Lone of tla inetod that a appliad to
alaulat tine tornplities t olgorittt
Masters thoore plidon eaeAa
oelations.
applied on deessing
yT can be
dividing ecuericg-tn.
Case
Llog=
3) i-f actd
Tn )=6(n).
PRoot: Maste-heor0M
leulst conploxity analyais
Ca
b2 d
ar 2
!. eln'ogn)
Tn) e Cnlog n).
Merggsot
aEatution Wettod (hachuoaN
MProot Jaig Subsitation
Tn) = 2rT (n/a)tn-l
Rrse Codtioe Tti) i t ana ha
mly oraeltment
-tan ee have
one oposehion.
=2T(2)+2-1
subs k-l > k-2 kwtth k-j
-T22+2-1
+2
k-z
2
k-h
-2Tlta2-3
A
+kra
-aT(aed) + a'|i]-(*-J
ar(aha)4 a.2 - [2-1
Gersral Representatiog
subs =k |
-aT(2)4k-(-)
K
wheve T ) |
+|
where na
K= log0
hence- pyoved .
uick s o r t o
Reanarge dee olamenb e spH Qraus
Divide Thdt each ebmeaf
in -to tuo subaras.
in eft sub ansy less an or equal to
in te
the pivot lerrent e eochalarient pit!
nght sub-dray is la iger -tan
middle lem
Currsioy Sort two sub araug
Corqur Re aray.
combine: omoine ttre aaduy soted
bo checked,
Ouicksort Condhions piot elament
Salect fvst elbmant as
Sa condeloment pointed by i
last alsment fontel by j
ds.piotinere mont i jpivot deeestj
Pvotj
88 744he
de j sbs Sunp iej
433 il 35 22 90 4o 6o 99 T1 8g
iepivotprs! 11 4Tha j-
0 e-fab bo > h4Thas0
4)33 J 85 22 4o qo 60 q9 TT 88
Jo< 44 The ’ t 90744Thia ’j-
40c44--false 40 >44-false
ALj crosses )Swap J wtk teand
35 22
33
B3 <4oTue t
swap j wirth Pirot
uajeros
35S22Te j-
P I1>22 false
33,<225-false.
stp Swap iaj
AEj
33 35
33>22’Te)-
Thie’t+ I>22’fabe
I1<22 talse
33<22
bted list
ft
baltt 3335
Zonted.
22.
Seard
hat 6o 49 S8
4fase
6o90
a9<90 >false
i jstop swap
40 6o 88
99<90-f
270-T-
Suwap P R
40) 44
40y T1-ST
T-9f
b0>7->f
Secord half -rted list
22 33 s5)44 6 1sto|49
Comploity rclyes for uick ort
Btase: Pivot elamet is plaod at te
Certer, then the valua et ta lott oy
and
Ret is os tan dee piot elamet
Vals. of de iakt piot is groatex
n1 ’ Base cordition
Tn)
gearches dera
hatfs
list is Splitod tn to tuo
Recane Reatien Dao)
Germl-fo)
2T(%) tsn
BaseCerndifsg
(n2
-nT )+Kn Boseco ndition
JTO)=1|
[k= log.n
bound
-OHnlog nate-ea opper
-Olnlogo).
worstCase Pivot element placed at first postion es
last posien
’ elrurt tothe ngd pvei is geattban
Pivot
Prcluding
dnPvot
n=l Base Conion
Recuena Rolation eqn
Tín-) =Tn-2)tm)
To) Tn-)tn.
T(n-2) n-| n
F 3n-(ot142) 4T(n-3)
Bn-(ot-Rk)+Tn-3)
2
4Tn-3)
Gehetal-tom
kn- KikDT(n-)
k(k-)
2
T) 1 assum 6-k
=kn-klK-1) 4T)
2
-(n-)n-n-i)(n-4|
o2
nTake
Taka teutes ue bound
=0lo)
Eneny Search
Condition is ist emnb must
be in Sor ted ordes.
’ (og) ist with n eloments
n=1 9 search 's dinct
e) :st ’ hos ore dament s
Seprck key -b
Hit has
ore damtbirety 5 is compard with kay 6
No need to divide tae list
,5 12 |S
Search keuy 6
Apply diide &lopquey
12
2
2
ntmicd"
is Cormpomdwith
Nouw key
Parts
divid tnto tuo
is
Now tte list
Jowto mi
mid4tto igh
mid
8
5
check Key <mid. wo are
en left half
soarcking
L
Cempared wit alo]
KeLy is
b= 5 fabe
dinded on to 2 parts.
lists
2
low tomid
Coetatns ony one ament so
ist aments
cirety Compare
66 element -found.
Tacl b Searehelomunt
N8
Soarch
Best 2) wcst a) vg
Best Gase ’ search elorment is egual-oeo
Vatuy t iddle oloro
(e) Senrch &. m = Lowthig h
2
m 041
2
aCs]=6
Seavch ele at3] ound
. Bestczse. ol)
worst case. ysoach elomert is pESe nt at
eitfer bog iningtheolonaovauy,o end
the anuy tac
le) H tae. olevunl "is not preatin
thay .(o)
arny
ea) seac h elament =2.
Lstconaiso0 m Lt4
-3
middls ele f search elameat
2rd cormparison
142^H
L Search than tmictHh ee so
aCiJ =3 Search elenat.
3d Cohn parise n
(
[5
middle ele
3
|Ly Soarch in lef s i d o . u
H
M0to
2
nCo]= 2 Seatch ele :Ctomentfoud
Confaiten divit elorst
ho oelornants diride
|’ 8 N untilsize
airays|
8. ’ = 2.
D22, no e Comparisons
Birryseach
Ttn)=T(n-Hc
-TRJtec.
n =l
k oga)
T)=1 n=ak k:
n=2 ide) T()+lo,n-c
Take toc en both -14 loq20
leave constcnt vale
N-9loq
ek= log N worst ase
Norst Cose >0(log.)onpaian
Poblem
*Maimum Sabaay
Sum
HS3
Onc-dirhensien al aray ’ which may contaig
both posi tive & nogative integers
fnd to Sum af coniguouslongest
S nunbers cohich kas the }ulavnySum
(e)
-3 -6
-R-56-2
Apply Divide&conquer
Larget suboray un=1
|-2.-5. -2-3
RSS 6
No2,6,) (s-6
c$s=
-3 S-6
-2-66:2
L Ls Rs3=6. Hals,-34)
LSS-1 RSs-5
ox(5;)
Han-2,-7,-5)
-2-s 6-2 LSS -3
cSs-1
s 6 2Ass2
LO=-2
-2
LSSLeft subaaySun
Max (Lss, Rss ,cs) Rss>Rigktsongun
Algn
Css
mid 4 CssHCronogatkemn
i= o [rnid to 6]
geanltay-t mar)
Tt is a nocursive. mettod e ike Gontlortty
Cun be exprssed. as
follouoing
Tin)= 21%)n
Tine Conglerity : olnlogn)
Mastere theorem
Tn)=2T(n]2 )+nd|
Q=2 b-2
en log n)
Minimurn eMoximom an may
t. ind the mid ef te avuy
Q.tind the mcximn A mnimun te Caft
Sabaay e Cuasi ly.
8. find the mecimum 2 minimun ef tee nigkt
Subaonay eCusivdy
4 Compave
5- Retn the mininum 2 maximum-,
32 |832 mid=o3
2
13 mid= otf2
|
2283|2 mid =4tG
2
min = min= 13 minr 32 mine2
mar lo max mat 83 max 2
min min 2, max=83
-min 2
maX =83
tuo subprb0ma si,
Reauvsively solng Compansos to get
22
rwaking
2min.
the ovevll mk
Recunene laton
Tn)-zra2T)=6
toreelerent no
T2)-1 Compisen
alomenb bN CoMparin
26
a2,b2,d o
azson)- 6(n)
-eln).
Sabstituhian otbod
ngk
Tn)2T ()42
=a[at nn)42y+2
Assume
k-l
a'n a(i)
as2,T:2
nk-1
2
a(2-)
0. c)42-2
n4n-2
2rtn-2= 20)-2
2
Cn)
STRA ssEN MATRIX MULTIPLICATION
S TA is the divide orque aqPprzch
An solve tre matrix muttipliartion prokbe
to
rati'x muttiplication metocl
Usual each Dw
twts aach cou)
multiplie produet matsx.
to achieve tee
tine Co mplexity t*kan by this pprd
oh) sine it takes two loog
is
to multi py b ba
Lasiaa
lba ba)
/a, bnt a,2 ba
a21 bda 22 b2l
are ncquind.
ovalion are
-) Gena al 0cuna olaion aan is as
llow's
TG)er()tan
Lstonploitysetr)
to. whth nesutt in a fastey
fron8
maligliaton algm camed ol
Shassens Natix mutipicatian
USing 7 mutiplihoo
delaz1 tazs)bi
(be-ba) d=(apas2Xhytb
d an
resulting Product rai
olanants
Cuditdy-ds tdy
d tely dsldy ds-tely
dytds djtdyt
hmiuo Con be combircd with
- strassan tec
divde &Comqer srataa
slps
obtaincd
H|| tics o 2x2 os deu is
Q. Vse shosents foormula te Cany ou
212
ultiplicoton
Corn bine e sults to got iral product maty
8.
is
RecauutA cqn foo strassen mati
subs i=k
n= k losn
2-807.
tohich is Smatles tran n
multiply
strassen metad 2
5 2/
a 12 =5 bzl0 b2) =1
az2-2
+a22) by (512)1 =Y
d2 Caz bi
alb2-b2) 2lo-1) =
dg - lo-1) =
(baj -by)2
d, oz
-a,taz) ba2 = (3+5)|
ds
-as2) (ba,-tbi) -(s-2)(ot)e3
d-laz
Cy - dit dy - ds tdy
-9-2-14 3-2
C12 d t 5 2+ s5
C2l dty 7-2 5
C 14322
:c-l2).
es) 2
463
a aj Be 1 I12
3 2 I2
311 3
Bol
pl
di- (A tA 2n) (B 4Bsz)
6414 428
31
10 16
d2 (A2jt A22)*Bn
ds- A (Bip- Boa)
d = Az2
(1)
ds - (Ay-4 An) Bz2
s 21\
(BBin)
2 -3
)
dy (A1p- Aaz) (B.,+B,,)
dg tds
AB (dtdy-dsteh
dztdy drlda-d2tde
3
(15 31
detdy =
81
d,tds-dates = 20 18
4 4
3)
AB=
6 12