Professional Documents
Culture Documents
Ada Unit-2 Chapter-2 Divide N Conquer
Ada Unit-2 Chapter-2 Divide N Conquer
Ada Unit-2 Chapter-2 Divide N Conquer
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.
svb-probleni- sub-6olutioni
Sub-problem2- sub-solutiong
(Probem Solutiorn
A
Combine
.Conguu.
d o we divide aud Conguen me4hod
we U he davnde omd Congue method be cauue
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
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")
ndex 3 4 516 8 9
Elements-5-6 0 7 3 s4 8lo
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
Index
On he avU1age.
hox no peset.
BIN SRCH
fox all
fox o he Yemairin possiki liti us
ananunsucceful secmch i
(3+3+3tutu+3t 3t3+utu)/1o'= 3ho
34
worSt
Succunsu Seanches Best Avonage
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
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
on tach call of Mu
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,
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)
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)
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
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
f lahigh hen
-a(m), i-m, -P
3uhle (12a):
a = aG], ag):=V; he patstion elaunt
belongs at posstion P.
Yetuin 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
85 60 55 50 F0
15 80
65 45
85 60 55 50
65 45 5
5 80 85 60 55
65 55 85 60 75
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
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 %
en
en 00 1 e noie T(o)-, which insignikat
Se CuwUene
T T(n-1) +cn n>l
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
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
Replac n n
tY+a[T)+ T)t - .
+TM-
n-Tn)--1)T(n-1) = 2n+ 2T(n-1)
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)
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
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
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
.' 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
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