Ada Unit-2 Chapter-2 Divide N Conquer

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

Divide amd Conquo

GLenenal Method:
Divide d
Con9u an algorithmic patlean,
T 4his, dtvide he problem nstance tnto lwo/m ore
Smal incslances ohe sane roblem, Solve he smalky
insiances vecunsive, aud aMemble. he Soludiom o form
solution
a he oginal inslance. the e cunson stops
hops
Lohn an instonce i Yeached ohich u Hoo mall to divid
when dividipa he instance, One con Eh e
phokeve divrei on cbm es mot easi to hand wvet
me in makin he division Coeful solhat the

ommb Spsied.
Th technigue. Can be divided into 3 Parts
)Divide : Bividinq the problem nto Smaller Sub-Proelems,
cohuei Solve sub-prcbles j cillng wcunsive
Untsl Solved.

Combine Con otne the sub -prodevm o et the fnal


solukion o the whole. paoble.
Alacaithm-iobe
DANDCCP)
14 SMALLP) hen edunn S()
else
COYG dtvide p ioto smallo instance,
,B,Pe R21,
apply DANDC Ho Each of 4hee.sub probles
veunn (coMBINE DANDCCD , DANDC(R),--DANDc()
3
Hee SMALL() a Cookan Valued funchon
He
Lhich deteines whethtn the input Siae Small enoua
So +hat he answon an be Computed widhout l tHinhn
Suncion S invoked. O4hwiis
The problenp io Small sub problems.
Th sub problem , fa , k one Soved ba e tunsive
applhcoton o DANDC.
The Si3 o he Huoo sub Problems One

approwinael cquad hen he Compuintime oDANDC :


n Smau.
TO)
(9TCna) +Fm) Othenn se

whene T) the tme for DANDC on


n input
3 ) t h e kme to Complete he mso dieclla
for Smal npu aud
S h e -me for Divide amd Combine
Advontog o9ivide aud Congu ,-
B ysing ivide dud Congueu, we n solve
0 dhe
One
bigaest Problem, uch a Towen oHanoi, a
mathemodi col puzzle. t
cha|lenging to Solve Comphcated
roble f1 wich dou have no bastc idea, but with
he help the dvide and Congu approach, e
cinding 4he main psoblem into 2 helvo Od 4hen
Solve Hum secunse T much fastn 4han
O-lhu als
t eciol wws Cache memoidhout occupgina
much Space becaw tsoles simple subpioblems within
he cahe memolnsrad o acceniog -lhe slowor main
Mem
S moie proicicnt han Brute Forre. Techniue
isadantagcs
An Expicit stack mo4 overte Ihe Spa ce.
t ma EVen ash 4he jstem i the ecwsion
. Pofomed vgOxow eadtn than Jhe stack pre sent
in 4he 6 Cpo

svb-probleni- sub-6olutioni
Sub-problem2- sub-solutiong
(Probem Solutiorn
A

Combine

ovi de Svb-probem n - Suh-solution n

.Conguu.
d o we divide aud Conguen me4hod
we U he davnde omd Congue method be cauue

tan be irul to Solve


à Problen LAh °n ip.ts.

S0, we divide. t to Subprobleim


until tt Ea_y to jet
a Soluton, omd hn we Combine all solulions of -he

otvideo ubpoble to One.

APplicottorv t
Eina seaxch
Finding Max A Min
3Henge so sAromwenk Matx Hultiplication
Qquick Sot
Bino Seanch
we have necoxds which have been
oxdered
Re sothat X <*2 <N3<, Kn. hen we aiven
an clement 'x', binary Seanch
% ed to find he
CoeSpondrng element fsom the ist. Tn caex'
present, we have to detomine a value auch hat
asj X CSuCeful seanch). Tf X not Piesent
n he st he SsSet to
uo(Unsuccnfal Seanch)
AlgoviHbhm (Tteakve)
BINSRCH a,n, x)
Ceno i ) o elements in increastng ovde, n20,
deemine tohedhei x Pre sent and if so ser
Such 4hat X =a
(i).
else etunn j.
Jouw=1 high: =n;
phle (lo =igh) do

md:-|lo +hgh)/a
1f
(x2aCmd]
than hizh = mid 1;
ela if (x
>a nid) Ahen. low: mid +1-

ee etun whd;
3
3
Proam For Bina Scanch (erative

int binay_Sednch (irt AC3, int k, int len)2-T)


fnt o0, htgh=len-1
ahile. (low<= high) i
ost-ngh-lo/a)
CADid3- l)
vetun mid;

hi gh = mid-1;

e i
low miol +1
3 Yedurre
Jetuwin -l
hadr
i t moinc)
int aCo z ,3,5, 7,9, 1, 13 15,14, 21
int 3
int postton binapSeanch (a, k, 1)
if Cpostion = -1)
Prwdf Not Found")

Priskf(Found.it ad1d", fostion)


Yedunn O
Th8)A1
x:4Bny scanch
Let elements

ndex 3 4 516 8 9
Elements-5-6 0 7 3 s4 8lo

Lel Seanch elemeat f r x =l0)


ou high mid

6
8

found v

No: of Companisbrm 4

Seanch elumant x =
82
loc high md
9 5
6

found
No: Of CompaiSoN= 3.

3 Seanch element X 42
lou htgh mid
5

6 6 6
6 Ne -found
No: of Com p aníSos 4
elment X -14
Se anch

Not found

paiso . 4
No.o Com

Continuing in his mannu he no:of+ element


needed to fino tach o 9 elemuks le.
Conpaisom

Index

elements -5- 7|9 2354 82


Comp aisom 3 2 3
No element eguüre moe than y Coipanísons
needed to
to be tound. Summi0ghe Compantsom
find all 9 tHem Omd duridina b 9,icloding 25/
u Succful Seanch
o approX.14 Compadso

On he avU1age.

Thee 0e 10 Possible a s tho an

un-6uctfu Seonch moA4ominM ate. dependina


Upon +he Vale of X

x 0nl, aJ<x< a[2, ak<x< ac3


ars14 aLSS, ar <
a[3 6) a C<X <a teJ.
he o cgui b 3 element compa 80m) Ho datomíin e

hox no peset.
BIN SRCH
fox all
fox o he Yemairin possiki liti us

gepires 4 element Compasom.

Thus he Ovonaje no: of element Compatiso

ananunsucceful secmch i
(3+3+3tutu+3t 3t3+utu)/1o'= 3ho
34

Time complexit fov a succfül Seanch Oll8n).


fo an unsucful Sench 6Clogn).

worSt
Succunsu Seanches Best Avonage

un-succemful Seaadhe) beSt, oVonage. , wonst Bloan)


Mege sort
Merge so alg s a closic Example. of dtvide
Omo Con9ue. To Sovt an aoona, xecunsi velg, Sot
ts lest omdsght habyes seponate omdhen mage them,
The.Tme complox of mepe sord
in he best caae, wORst A
avoae u
b Onkegn
The. no:
o Compai son Med ne anoptimal
Th slaodeas So
Simple, amd
but he
So
Eicient
hoblem he
hat the. seems to be
no E a WO
mge tuo0 adljacem Sovted
ed Oa
aoumays
Hogehe in Place (The Vesult must be build up in
OSeponde Oomo).
he fundamental
opeaBjon [n th algovitm
mugina Auo Sorted ists. Becaue he ists ane
Sorned, -th Can be done n one
pos hvouzh the
mput, f the oudput put n 3 ist.
AgorHhn:
Aigoidw MCRGESORT (low, htah)
olowhig) a alobol avono to be
F (Jow< hih Sorted
2
Th)
o+hiak )lefnds where to spt he set.
T
MERGESORT (oo, wd Sot one Sobset
MERGESoRT (mid+l, gh) 600t he. othe subset
MERGE ot, iid, hig3 CoMbine. -}he
öesutts
Alovhn MCRGE Clow, mid hiah)
Soted
oCloo: : h:ah) a global osme Contairir wo
Subsets.
in a (low; mid) and in a mid-+1: bigh )

The. obiective to mge heu. Sorteod sets in to


Siggle Soted.
set eSid, 10 a Clow: hrgh). An auxillag omg| B Used,

h =low =mid+1
i=low ; =
ohle(Chsmid) ond (is hsh)) do
4aCh aJ) Ahen
L aLh h=h+1
3
e
bC3:
2
a C3 :-itl
i itl
3
h d Jhen
for K to hrgh do

3
Ase
for k: =h to mid do

bC3: a [I 1- i41;
3
fot k low to high do
afk3b3;
3 5 8
14 19 2 23
lo high

17 8
Q 10 14 23

10 23

-Lous gh 1a23
1D 23

1 0 14 192324
T n-

21()-4 n n
a2 2 fn) n

Y,2=1
Cau-2
n-1 os,k
8Cnloan)
2 4
Mu sort '
Moge soH (1, &)

ms(5,8
rns(1,4)
,Jo wid i g h
ms (1, 4) wsh
mid

m(s,6Mses)ms(5,48)

ms(ams(3,v) me(1,a,4)

ms( ms(MS34)
ms(5,5m6A) ms6s4)
m6)1,2)
ms1,1)
4

s(4, is(g,8) ma,3


Yms (8)

ms (,4

ws1,1)
ControX w)l g0 to adtacent duunt

mso,)
ms C,1,9

wic, s)
Mergu low, Mid, hau)
o

iow midt K lo
olile(is md) 6z43))

ehe o

MERGESORT(loi,kish)
# Ci d)E
kile Cis hih) high)
f (lot e

wd- (Jo+high)/,
MugoH ( lous, midl)
MwgoH (midtl, high )
Lakiis kid) u (oay d, hith )

3 144 K++;
3

aK. b3;
iawbacks oMone Sovt:

hupesort 1 s 2n localiors.
ocaioms Wne. necded. becaun
The addtional n

we couldn't menne. QSoded sesin place.


O
e sut paced rto bl:h Om
Usi 2 sPace
omd qqan back tio aflow: iah

on tach call of Mu

To Solve Hhis problem, coa to ano ciate


O ne ficld o h kach .
daments in aC3 o alhd
alhd K

elaked nfo e called econds.


Ti reld wwd to ank thu g nd r t
Onociohed info egehot m a sovded st
Then the meg t h e sorted kst prceedy
no Tecovo need o
chorgn3 he ank values, Qmo

be moved o ol.
A ild hat ConHaim o a nk wllgnoo
be Srnolle han Om tyse e coio, So les sface. ille
tntkalized to 3n0.
USin Link aonay T
55
50,10, 25, 30, 15, 70, 35,

AlJorthm Mugesonl (low, high)


16
hish-low<15hn
hizh);
0okun nstlionSoll(a,knk ,lovs,
id: = (lo+ Ligh)/
Moge soti (low, md)
Mozsot i (md+l,high)
Telun Muot! (7,);
J

30 15 Fo 35 55
25
O O

9P O O 0 00.(19,5o)
2 O 1 O
O O (10,50es30)
4 O
3 3 3 O
232 2O 4 1 O0O 0 lo,25,30, 50)
6 0 o lo,, 30,so) , 7=
565 5
6 o 8 0 (925,39.9d6s
575 5 O O8 (lo,25, 39Jor3sss,2o

252 2 8 5 7 33 O 6 (19,15,2,3o35, 5o
s, 1o)

Hone eshouxd -thot hoto nk ona Chag akn


Eoch Coll of MengsoHl1 Comlde
O tach Yow ha. valu of Pointo P Poima To -lhe lis4-
tnJank
nink hok we) oakd A auk compltin af ^uzi.
Vecunsion,
2) Muae. sot ib We the stack space bthe wae Of

Mugeso- spleh sach set ilo


Max deplh of Aack
Epual si3ed Cappao) Subrcts
lozn.
-

Hoehe
Hone pxoblem , Jor Small set Si3e, most of-he
ime ill be. spent+ pocewing he e cusion insteod o

so
T Ovcome lhis Situadion, Jor smal-si3ed sehs
Sovt wo fat on
instead o YecunSion, Thseton
6 lewents
CompuBi time O)

Algothm TnsoonSort (a,n)

to n do
for 2
i-i-13
itm = aCi3
ohile( Ci 2) omd (ttem 2all)

a C+ ati]
1- 1-
3
Quick Sot:
uick Sot aljorithn poikiore he ongiral
ovo rnangatindo 3ups
hou clements ls han
The 1t gioup ConBaim
SOMe abitxar choun value laken from the Set,Omd
The 2"d aioup contains -hou
nd elsment, araten than
orEualto he chosen value(Pivot eement)
value is known % he Pvot element.
The chosen

hoy been eawanged n hs wo4


Oncc he anOA

t h Yespect to
1th -the pivot, -he
veyj same pontitiomn
ach two Subsets.
Yecusive aftlied to o he
when all he subruts have been patitioned aud

+he ovginal asna Sorted.


Ycaanged,
Qvick sot pieks an element 0s Pivot aud
onttiom he iven avo
nd. the piecked prvot.
ditonent Vnsiom ouicksort kat
Thee 01e
mon
Prck avot n ditment s .
Alios rck S element as Pivot
pivot
Albo Pick laut element
as

Prck oa Yandom element a pivot.


hck median ivot.
The funchion pankton() makes e o. tuo fpitevs
Omd ahich cne moved owond ach otho in the
folota
Repeatedb ncyea-he. poiotuunt atj2 pivot
Repeotedl deCYeanc he poinlo untkl aC] <Pivot
3>1, intenchauge acj tateth dti).
Repeat he steps 4, 2 ad 3 till he i Pointer
CYOMeS he powten. Ifiporen CTOM

fointten, positon for pivot foundl


he ond Place

Pivot elewent mpaei Posihon.


AlgovHhm
QUTCRSORT (loco, high)

f lahigh hen

3: PARTITION (a, low, high+1)


h e Pesstion o ke panttion agelewmt
QUTCkSORT ( lew, i-1) ;
QUICkSORT(S+1, hr);
3
Algothm PARTITION (a, m, P)

-a(m), i-m, -P

loo i tl until aC) 2v moves lest to Hght


loop= i-1 Until aC)V P movey ngt tb left
F (i<5) then TNTERCHANGE (a,i,3)

3uhle (12a):
a = aG], ag):=V; he patstion elaunt
belongs at posstion P.
Yetuin i
3

Alaovdhm INTERCHANGE (a, i, 3)

P: aCi3
aCt a J ;
a P;
80, 85, 60, 55, 50, 45
65, 7075,
Pivot element.
Let 4st element a

Pivot=65.
6
85 60 55 50 45
65 45 80
Pivot

60 55 50 45
15 80 85
65

i> fivot iPrvod so SLMP(aC3,at)

85 60 55 50 F0
15 80
65 45
85 60 55 50
65 45 5

Sanp (aCi1, aI)


65 45 80 85 60 55 15

5 80 85 60 55

Soaf (aC13, GCI)


45 55 35 GO 80 15

65 55 85 60 75

65 45 55 Suap (Ci3 al])


60 85 80 75

H(i: ) ak Suy (a Cr, aG)


60 455 55 85 80
65
65
55 65 85 80 75
60 45 50

Left sblb block 60 45 50 55.

Pivot 60

45 50 55
9ivot

45 50 55 fCiz3) break
Suoap(ar, a )
545 50
55 45 50
Pivot

45 50
55 .2 Suoar (a P3, a )
(50 45) 55
45 3ap (20], af)
Pivot

45 50
Left sub block 45 50 55 6o

Rrart SUb Hock' 80 15 40


H0

vot

45 o ap (atP], aC)

0 80 75 85
O 15 F015 8 85
Merge the Sublists
86 85
55 60 65
45 50

So
Andlysts oj Qutck
a xandom Pvot.
Assume

1e T = TU) =1|
Eual b
The unntag time Of uick son %

two secunsive Calls plus


cal,
hc Yunnkng time ohe
spent n he Pantition.
the inean time

seleckon ako, on Constant time.


Nole: Ptvot

Quick sot ecuoinence


relation is
TOM)= TC) + T(n-i-)+ cn
Hene i s) no: of elument n S.

OrSt Coa Analysis

The Pvot h e smallestelenent, al 4ime

en
en 00 1 e noie T(o)-, which insignikat
Se CuwUene
T T(n-1) +cn n>l

Tn-)T(n-2) +COn-) nn+1)


TM-)= TCn-3) + c(n-
TO= T)+co)
Sum u TO TO+ O
uick Sort
WoYSt Cate T)- TG) +TM-i-I)4# o1
Awo ahead sordd.
n.5 115 ime

n-tim duk
3 3,9,13,153
iwol

3 a,13, 15
-3
213, 153
He all let sub asmay-

153 ne

H+ 3 ut 5t
Som OfF n nombu

2434t-- - -n =nC
O

T) =
T(%) +n
(o1

O(nlag n
Best Ca AnasiS
middle.
he but Ca Pvot i n he
h al he Siae ofk ornd
.'. two
wo sub-ist ane Exactl
n
TM- 2T(%) +
Makon 4worem -
= 1:|

- Avuna
P-l4hen
O-But Caw.
0 aCnl3n)
Best caN = OCnogn
Anajsis o quick SoH
Piide Conguv
Rettwive
Nol slable sovkng 8
Time Conplexit
L ologn) F Best
>
oAvo@ae Co

O( wors cau (This can be avotded


usip andomi3ed
vsion o Quick Sovt)
Alodhn
QUTCKSORT (loo, high)
ohegh Jhn
PARTITION (a, low , haght1)
abeposikion ohe. Pantitontga elemnt
QUTCKSORT (oo, i-i):
QurcksoeT (S+1, kiah)
Avon@ge C Thme Complextt O.f QuickSovt
c ime
uehicksortt loo2 hagl >
T) - C1 Left hal T(t-1)
To)- Tt-)+T0-pcon vighthatf > T(n-i)

Ava oalU Pos3iblk portitios,

The no: of elzmnt Compoousom euned fatihon on

coll nt
nt
T)= +
n4}
ST()+TÚ-R)
n41+. TÚ-)+Th-) + T)+ Tn-)*Th-D+TO

n41+ n
T)+T(n-)+T() +Tn-i)4..+T(n-1)+T)

T+T) +TO+
-
+Tn-D
= ntt
n

Mubkpla bolh sides ky n

n-1= nn+) + 2 STt) +T()+ - +"T(n-)J20

Replac n n

(r-)Tn- -ar-i) ++ 2TÖ+ +TCn-)-

n T)-(n-)T(n-i)=ag+ asrto)+ Tn-D -

tY+a[T)+ T)t - .

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

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


n 1 ) 2 +2+n-D}Tn-)
n-T) 2n++DTn-) non
. Dvide b
(n+i):T(n- both Sides
T =
n
n

T = 2+ 6+) T(n-1)
.'nde h41)
TO)
n+

Veplace
TOn-) T-2) 4 2
n n-
Tn)
T-)
n-
Svbstitude i n

T T)
n ++
n-2
In olace
T(n-0) 4T-)
n- n-2

Substitue in
T) T(n-3)
N+ n-2 n- n

T0s R
3SKSn+r
Since
34KSt1

T)

T 2(+) Shg,Ot)=- lee]

o (nlagn)
n gen)logn+)-angg-lga
Chlagn
n4)+ xan TOR-)470n-)
n krgn

tvot
Lt eumets
left
-

.'. T) =
T(o) + t (n-) O
n n-2

TOn) = TCD +T(n-a)


3o
n n-3
.T T) +TGn-s)

T) Tn-)+ T)

2TO)+ Tn-)+2T(h-2)+
.

TO

a-T+0+T + - -

T(n-)

neluwms olardson

',T0)n+1 + +T(n-
T-D+
12n

To4as
Strassen's Matix Muliplication :
two nxn matvix.
Le A amd B be he
The product malyix C =AB » calculotedl k usap

h fomula,

c(1,) =
A(R) B(K,i) for all awo
k<n between1 oud n.

A Aa B12 C
hen
AR Aa2 2 C21 C2
2 92

C An tAj2Ga
CIi2 Ay Bi2 + Al O22
ApBn+ Az1
C22 A612tAz2 b22

or i I to n do

foo = 1 to n do
cCi =0
for K1;to n do
c,J cCLJ+ aliKI *BIKAJ

T h e Aime Complexit for Ihe ennal Matir

Mukphcodion O
The Divide ond Conui melhod sygest Onother
p0 to Compute he rodutt of ntn madix
we Omume hat N Powo of . In -he (au_N
Oud Colums
a powol o , h e n Enouh Yows
not So thot
hat hu
A and .
added o both
Cam be
eo
Of he
the Pown Of 2.
ane
dimension
e Sulkina
Used
used to
above formula
Jf n-8 hen he for he elemen
elemeit
mulkiphcation opnation -for
matx
Compute

f A &6. into
elements ane pattroned
Partthoned

S f n>2, then he 2 he
n Powen 6f
Sn e
n*n ustnthe
bsmthe Saue
SoMe
Sub moHri7.
Computed
can be e cunsive
roduct
fSnula.

b b13 blu
O12 Al b3 by
A 6 021
b32 bas kay
blue

will CoHinue. ayplgina itul to


Th Thv 1oblem og
ix ontd N beco mo small (n-1 sothat
Smallen sub matr

he Paoduck computed dtectl


n
V:2
n2
8TOh)+c
b,C Constant

.' TO)= OC
ivide * Cogu

Algolhm MM(A,6,n)

A Gn + A* B2
+ An * B f (n 1)
2=AnX6j2
Aa* B Aza *G2
Ca C4 fosmulay wn
cedculek
Ani B12 tAaz*6B22
Cy -

Mulkplicoom mid
Addihom
hyn,)+ a,h4)

HM(AG+MAa,6
Cl =P+S-T+V
Ciz=R+T

C @+S

C PtR-+U

The esubhnsecuwtence
atlodion for TM)

n2 Cb > onstants
b
Tn) 4T(%)+a n>2

a b-2

2
K 2 8
J3,
OCo3)= O( 28)
:
Sassen's MaBnx MuHiphcadion
P (AntAa) (B\1tH2a)
- (Aat Aaa) Bn
R An Gp-6aa)
S Aana B)
T (AtAn)Brz
U CA-A1)Ce+

V (Ap-Axa)(®2tBaa)

C Pts-T+V

Cp R+T

Ca +S
Cog = P + R - Q + U .
Stvammnn's

4I1 o 0 4
o 3 0 3 5 O
5 0 2

e
PAutA)nt |

4+0 8+0
2t

(Pp1 (A21 +22) 61

SO

8
R A (B -62)

o - O
4 5

S A2 (G2 1)

33-GD
6
0

T(An +An)G
(2 (1 1
LI
3
24 5)

s
23

LS
C P+S-T+V

20
5 4
4 5
Ca R+T

4 lo
-
8+S
811
41+6

Cy P+R-Q+0
3

20 4

(31
3

8
Noke- o u wOst o doMati us
Muipkcokion us Ssam

MabX sheuld be )
4xu, 8xe, 1x16

93 2 3 o
S Conn Phis
2 to form
8
nearto 3 4
3

S6 0
2 O

o o 0D o0

You might also like