[go: up one dir, main page]

0% found this document useful (0 votes)
31 views28 pages

Module 2 Divide and Conquer Method

The document discusses the Divide and Conquer strategy used in algorithms like Merge Sort and Quick Sort, explaining the process of breaking down problems into smaller subproblems, solving them independently, and then combining the results. It includes details on the efficiency analysis of these algorithms, specifically focusing on their time complexity and recursive nature. Additionally, it outlines the steps involved in binary search and provides pseudocode for the discussed algorithms.

Uploaded by

sohamparab38
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)
31 views28 pages

Module 2 Divide and Conquer Method

The document discusses the Divide and Conquer strategy used in algorithms like Merge Sort and Quick Sort, explaining the process of breaking down problems into smaller subproblems, solving them independently, and then combining the results. It includes details on the efficiency analysis of these algorithms, specifically focusing on their time complexity and recursive nature. Additionally, it outlines the steps involved in binary search and provides pseudocode for the discussed algorithms.

Uploaded by

sohamparab38
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/ 28

PAGE NO.

Saraswati Education Society's


1301| 24
SARASWATICOLLEGE OF ENGINEERING DATE:
Modale- 02
Divide Conguen

General Method,
Method Menge sort Quick sort
Pinding minium f maoMum clgaithm
thei analysis Analysis of Binary Seach

Genenal Method Divide & (onguer)


The techniqu diuides he Jarger psblero
salutianafthe
tnto smaler sbr problem
arginal Jange_p1ablem obtanedby combinung
la soluti on maller
of smaller Sub prab lems.

Divide & conguex shateqy o2eratyn 3


stag<s'
Divide 2 Solve B) ombine

|Recursively divide Sabproh: salved Combi ne solof sabp1ob.


fproblern unto independentlyn-oxder to denve the
lsmaller subpnb solut jon of the oigna
|Aa1ge piablem
Subprob dye Similay lo the Cmqinal prcb. wit
emalle aiqum ents, hene Suh prob: can be
easily solued ing TerSion

loiide Conquen i mult -branched, to p-doasn


ont
gecriVe appTOah i Each banch indicat)
subprob(em it calls itsele oith the smaller ag
itself -oith
Saraswati Education Society's PAGE NO.
SARASWATICOLLEGE OF ENGINEERING DATE
Graphical Tepresentet of diuice CCongLLeN
shateqy
erob sith size n

n3
ng nang
Smles sJble
gove Smallt
Sub PToberm
Sol
n/3 n/3 /
of s e n/
Sol uidn of onginal prabt

Applicahicns
Sear ch
Binare
1Menge Qwck so
3Fndung
0doses pas prablem
sSrassen's mdtiplicahien alaa
* Conthol Abshachon
Dyide d Congue apppoacb tUcyks
Shaleg stages
1) Recursiuely diuide the problenm inta Smales
Suhpsblems.
2) Subprablems solvel ndependanty
s) Combiae sol° of Subproblem iader fo deive
the soln of the oiinal big Problem
PAGE NO.
Saraswati Education Society's
SARASWATICOLLEGE OF ENGINEERING DATE:
Conhrol abshachon
appitach w giNen beloc0
Algcithm Dc CP)
lLe is the blenm to Le 0lved

1f P
P y Small enaugh
meturnSoluion af P
elie
diuide darger pxob. P into
-3bproblenx
Salve each small subercb. Pi ying Dc
Shategy Combined. so)n Dc (e.) iDc (Pe)
DcPe))

tEfficiency Aalyais i-
h qenexal foY m the tuMe cOmplesct
problerm solvedingdiuid conguer approach
given by olloaoi ng Yecurnce

f n y joo
Smly
JTo,)+
T(n)t T(nz)+ T(n3)t tTa +fln): xh

Here
Tn): otal time Tequied to solve the problem
of SiZe
lgcn)= Cost of salving vexy smnal prdb.
|Tni) (ost ot soluing Subprob of Size ni
Fn) =frepresent Hme eg u red to divide problem <on bine.
solution of subproblems
SaraswatiEducation Society's PAGE NO.:
SARASWATICOLLEGE OF ENGINEERING DATE:

|rerenalized YeUY ene fo thu shaleqy


ontten T(0)= q-T(o)b) + fn)
of subprcblems
nlb the size of each subproblem
Com bine

* AnalysS o Binars earch.


Algoitbm BiarySeanch (A key)
Perfer m BinaTy Search
||Fnpu- Amay eles A' of size nf key ele
ll ole :- foundat found

high <I
wile Jow < igh do

mid < ( Jowt high) 2


if CA Cmid] = key) then
netun mid
else
if (A Cmid ] < key ) then
Jow mid+ )
ele
hih= mid-l
end loatex kelse
lerd while
end
Saraswati Education Society's PAGE NO.
SARASWNATICOLLEGE OF ENGINEERING DATE

Bìnary Semch ecuce Seauch Space by half


evCTy ifcachon.

Recunence yualion
if Single ele in aILay
TCH)

nSiZe dlvided nto doo pat


Rfer n eveny itexahicn ony sing le
COmpansiOn takey place

mplextity Analygis
Best Caue!
Runniny tHme caongleaiby o£ algo Cwaald be

becaye hh binar4 seanch


binary scaTch the key
comparec coith the aiddle
ele o the
may J£ the keyy in the 02iddle
posh o he day the algo
Companson iepechue
the aray

Tf ker at the last


pTerent af alL.
Reinrenee

T0):I
Saraswati Education Society's PAGE NO.

SARASWATICOLLEGE OF ENGINEERING DATE


TO) =A)tL

By wing Sobstitu ion mcthod

T(h4)t2

Ie Tn) T(^/2)+ 2

Subshtutc 2

T(0)=T(n|23)t 3
Aftes itegeation

k
|Suppos e

To)
Saraswati Education Society's PAGE NO.

SARASWATICOLLEGE OF ENGINEERING DATE

Sating
which ave difercnd facys conuidencd fr
sex hnq elements 2
Saing y jatenyi vely huclted &eatplerecd aiea
of comput cr scicnce n maly cpp licehans
ntiag i5 inheacat ned
Large dasse af algo ane Qvadable r sating
AL haue certain limdaliany d fist applicaion
Ldomawn
alge SeYVe) all the purpaseHaweuer.
niAq im y the most Commaaly cyed
to selet dhe best al¡a
no of ele ene smal
smaU enaugh to so
n main Quled nteraa/
3cxhng lanae Such thatSOme af
them Tesl de on Cat. strage dinq scrinq
LorOcelure,it y calle d eset ernal snbag
Srhng alga shodd oSseJ 1lauig
prcpei4
Jo placei ny zequire 'coRStaddittaal
Space to Soyt the al go

Stasle
alter the gelative pos" of
SCume elenets after sahinq
>ll Onine :-Soyt the data a it amye
PAGE NO. :
Saraswati Education Society's
SARASWATICOLLEGE OF ENGINEERING DATE

Adaptiue
pexfermance af algo pateo
Vamy a ilh JIP parn

IDeental
Baild sted sequen ce

Menae Snt!
Merge Sort
soTt Sts the it siny divide d
LconqueyShategy work S on folloung
two bayic pnnciples:
Soxing Small it oter than ssip the
lange
i JLCombinng two sted Sublist fayte
than that af tuwo SatedJists

Merge nt oor ks

ilDide i- Reursively divide the Single Lut


into two sublisks nil each Sublist
Contau 1 elem ent

Srt both scblists


Conquey i- Sat 2rerts ist
List of ele ú sated

Recursi vely com bine rteel sub Jsst


cnto he ist of size h praduced
Saraswati Education Society's PAGE NO.
SARASWATICOLLEGE OF ENGINEERING DATE
T64 2 | | a 3 5
divide
64 2 83

642| 3

Mergecom bine
4|6 S

24461 B89
12345|e/9

Alacithom
MERGE -SORT (AP,r)
// nput!- A y an ammay of si2e n p- in de
of 131 ele, = indea of Jast ele
uGLP:- Sosted anay
if P<r

MERGE S0R T (AiÇq


ME RGE- SORT (A s 9 )
MERGE (A pqL)
ere
PAGE NO.
Saraswati Education Society's
SARASWATICOLLEGE OF ENGINEERING DATE:

MERGE (A e,g
nNe q- Pt 1

reate Amay LC n R [ ! nz+t]


to
do LCi] A Ce+í-)7
to n
do Ri] -A[q+i]

RCnt ]

foy k 6 e tor

else

erd.

ompleeuty Analysis-'
On ea ch recusiye cal the lis j iided
ino two gub|ists p10 blem gi2e ve cdyced
by half
After sahng tw0 sublisis of sizG (n|2), Combine
procedyre takey fo for) the sertel
is of size h
Tötal Tunning time of merge
mere s07 ý computed by
SuMmn complesity of fo llo wing 3 Steps .
SaraswatiEducation Society's PAGE NO.:
SARASWATICOLLEGE OF ENGINEERING DATE

Divide hy step COmputes the oiddle


af the ny cuhidh Can be conc
iindex of
) consBant
cCnstat hme (huw DCn)- 0(1)

Conguer We TeC4rs0Mely alve o subpioblen


each si2e (h)2)uhich ohabutey 2ra|2
to the wning ime.

3) mbíne - mergey tuo Sublsts each f sizc


nl2 dáer o omaisons thus co): @ln)

Hence
TU): T(Onguer)t T(divide) + T (Gnbine )

|Tch) TO/2) + T(n)z) + o(i) + o(n)


ieft sublist ig sublist
TO) 2Ih\ol1)+ Gn)

we cn solve hu nunene Swbstitte


Rut nn|2
nctodp
T(n): 2(2T (hl4) + n) +n ]
2

(hl4 )
Sabstitte un (3)
PAGE NO.
Saraswati Education Society's
SARASWATICOLLEGE OF ENGINEERING DATE
Aftey K jterationy
T(n)= 2 . T ( )+ kn

SppoSe

T ) n-Jog
TOn + n-Jog

TO(n Jog, )
Time compleai Mege seyt
Best caye Avercge Caye

ndogh ndo9
Properhes of mexqe sort !
Not adaptive J-TuNnin Hme doey not chainge wjth Hp,S)2¬
stable uutabe
3) Not unemerta does not sot one by on e n each pas
4) Nof onune Need all data n memry a the tint of
Not n plae:-Need a(n) esehra spa e to Sot sorttry
Sublist of s'ze
Saraswati Education Society's PAGE NO. 13
SARASWATICOLLEGE OF ENGINEERING DATE:
33 2244 68
(1)

MERGESORT(A,93)
S3 22
MERGE- 30R 1(A,4,5)

mi

(A,2,3) (4,4s)
33 diude
4 17
mid +
(14
A,l0 (9,2,2)| (A:3,3) |[+4 ASJ
88

G9,4) (A,4,4 S)
233 00 44 88 9

(A,0,3 ) A45,<)
Go e33 44 88 g9

A,0,3, 6)
PAGE NO.
SaraswatiEducation Society's
SARASWNATICOLLEGE OF ENGINEERING DATE:
uck Sot

Guick snt u abo tno wr


lescchenae sort
iteqationquiCk
qwck sort selet ne
lele
euery
y Pvat Pi vof to the
Omet Jocathon Fiocinq the eivot
pi cn its
ceNTect docah'on diuide the ist no two
sublist af equa
Fach sblist solved recurs Mely

Vnltke merge sart hene partihoning of the list


uamed ant dyamially
Merge sortpatihons amaybay ed
posihien af aay elenerts whik quick sert
dae it baied an actua vatue of elemerts.
herrge sort divides ist fom the middle
whereas quick Gnt doey not ensure Such balaned
division
Division purey depeds an the value of
not fhe position
any Tandom elemert Can be selectec as
le vot

Each sublist problem


new problem of size Jey
thcn n Newaivot selected fhepro cey
|cotin ue unil it hits the beye
PAGE NO.:
Saraswati Education Society's
SARASWNATI COLLEGE OF ENGINEERING DATE:

Alamthm: Quicksat (9. Jaw high )


|| A y an a SL2en
ewnsated anaqoith sze n, Joy 9, hydhzh

if Jow high then


Quicksert (Ar do w, q
Quick sort (Aiq+l high)
end

PARTTION (A.Jow ih)


ACgh / y pivet ele. det ele of qay

fori dow to hgh-i do


f A CI3K then

Suap (ACi3, AC).


end
end
ACi+J,AChigh]
ret wn (it) conet )nder of pivot after sohng.

lerocedure
rom_left to )ìaht unhil an
San anay fron an ele greater
|lthan plvotu bund
Scan amay rom nght to left wl an ele equel
rsmaller thanpi vot -founed.
ele
|After fincung such
then echng e
/F dow, high then çehung e ACPi vot]intod a2 Chrghù
SublisT
will split qrmay
PAGE NO.
Saraswati Education Society's
SARASWATI COLLEGE OF ENGINEERING DATE :
hom the niddle
* Best caye oCCrs when Jist j diujded
Recurence
T(n) =2T(/2) + dh)

2T (h) 2 ) time Scqleired 4o solve two


problem_of si2e nl2
a(h) DHme egured to so ioc fhe pos
af the ivot
firiny eivot eqires
enhire amray

egTYace quck sort fr the data set


A=3
, 4 9 , 2 ,6 r s

3|14 ss26 s
pAoTo
1<3
4>3 :. Jous poin to 4move high
S3
6>3
223 high poi to 2

3145 926|S
Piuot
Jouw < high ex change 4c

Jow
high
PAGE NO.: 17
Saraswati Education Society's
SARASWATICOLLEGE OF ENGINEERING DATE
2<3 moue low
Jow poj to movr, hËgh
4)3
9>3
S3
2<3 gh point to 2

312|34|<|s
low s high
.

exchang high

igh
togh
low= high eechanse plvofe d high
S

ptyot Jou kgh

exchange
Aow<ig

pivote
Moye lo
low 6

moue kigh
6 ys
4<5 high 4
excharige
PAGE NO.
Saraswati Education Society's
DATE
SARASWATICOLLEGE OF ENGINEERING
sest example yng

Sot hum bers uing qujcksat


637O,75, SO , §S60, SS S0,
1 J 7

6s 107
bugh
hmove_ow to mgh Aow zpivgt
70s65
hQVe
naove high to Jeft if piuot, igh
to<65 L. hË h
low < h
exchange Jow high
2 7

6s 45 7 s 6 8S60 SSSo70
high

756S Joc 2 Move, igh


70>6S
. high 7
lowk high ekchange
S 7

65455o 0 9S Go 75 70
poot

75> 65
go 6S SS<6S

high
loco < hugh exchevge go, s
( 2
7
65 |45|50s gs|o Bo 75 70
pivot "igh
PAGE NO.: 13
Saraswati Education Society's
SARASWATICOLLEGE OF ENGINEERING DATE:

.loo- 4
ex(iang
S 7
|6s45so) ssGo SS |so7570
(ow igh
60 <65 SS>6S

SS > 6s ). ki h 4
doco >kigh eohange co

1
Ligh.

20 <

SK6o 75<8s .igh =3


SstK6o 70 <8s

c o 6 !. Jow
dow>high .'. ex Change Sxhange hgh deiuot

SO6o 65 %|8o75
(

70 9o)75
piodt Jocd igh
80> 70 7S>7S
807 70

gh o
:. exchange igh
PIuot
hut both
Srted Qmag
60 61 70
o45 30|7)
PAGE NO.:
SaraswatiEducation Society's
SARASWATICOLLEGE OF ENGINEERING DATE
Complexity i
Worst ase
Best cae Average
O(nlo, 0ln)

ohen the
for quck snt
Aist 4 alraudy sotel
Soted. Jn that the pit
will be at eithey end
The Caye beha viaur fey gck sar
Creat es subpcblems with in-1) element f o elemn
12 13 14 IS

Pivot ow high
12>I| move Jow

l6)

(|12134S16

12 13 14 l6

13|14 516

Tota ig of ree wsuld be h Size of ist)


PAGE NO.:
SaraswatiEducation Society's
SARASWATICOLLEGE OF ENGINEERING DATE

Constant beCayC
becaus
|Combine cost foY g uck sorl
each all fies he cemcet
Aike thc os piot
exha wOrk y equ bed

ADiuide he Jis algo scans the aay


the comect posih on of he Pvot, o the dLuiston
inegr 0(n)
cost of clqo
sublists hay si 2e o
h oonst ces e,one of the
Si2e (n -1)
Hhe
& otheY ha
alao: has to solve aorablen of siLe (n-)

Thuy
(Boye Caye
T(O) 21
Tn)T(Conauert T (divide)+ T combine)

T(n-)t s{n)
wing substifun method
n n-|
T(n-)= T(n-2)t (n-) -
Substitute

Tin)=T(n-2) t (n-))4n) (3)

6ubtitue
Tn-2)= T(n- 3) + (h- a
substitute )
To)= Th-3) + (h- 2) t (h)+ h -
Saraswati Education Society's PAGE NO.

SARASWATICOLLEGE OF ENGINEERING DATE


Aftcr
T(n)= T(n-K) + (h-(K-))+(n-(k-2))+ + (n- 1)+n

Le
.. TCn)= T(o) +lal+1)+2 +3+

O t t 2+3t +(n-) t n

= h (h)
2

= nh
2

TO) = wrst (aye

est taye !

Put

Sbshitte
T()2
put h| 24) +2n

2T(n2) + h|2
Subhtute (8)

After
K
Tn )
klo, h
T) 22 k
mn
-S
Lny ot2 mid
3) mIn
47 3| 17
so man
mid
47 31 |7 22
3
2
mid
n m'
|7 -5 15 22
6
CompasOns the reduce
0sing
CongueN
aNSar 2(n-1) totel esults Tt
ComparisoN of nd
finding (ompansony
fa
ured
AQue min Man
pdating
eachelehaditionalapproach ompaipq by
found be kan
the 4 Jn T
CLay 4iven bom ele
find pxdblcm
Jo s -mar Min
roblem Min-Mae
ENGINEERING
DATE: COLLEGE
OFSARASWATI
Society's Education Saraswati
23 NO. PAGE
PAGE NO.:
Saraswati Education Society's
ENGINEERING DATE
SARASWATICOLLEGE OF

Alqcailhn Jow hgh )


Min-Max (A.tul min

if loco = wgh) then or s ngle ele.

elac
if (Jow = wgh- ) then
if (Ado w)< AChigh] ) then
mar ALhigh
else
min ACugh]
max A Clocw]
-ere
ele
mid (dow t w'gh)/ 2
in Max (A, ow mid);
high)
end
erd

Q.Design ahalyze adivide cOqueo alge tor


inding
thet C39/2)-2 fe
at nSi 2e
any
Saraswati Education Society's PAGE NO.: 2S
SARASNATICOLLEGE OF ENGINEERING DATE:
6 |4
high 7 min
max= 14
-3s
14
min z4 nid ot3
hid=ot3

marl4

Compleity anayis
Min Max doe tuoo Campansny two delemne
min man. ele t Create too pToblems cf
S17e n2 S he Tecunrence be formdateel

2T()+2 |t n 2

Subsituth method.
ing
Substitute
T(n|2) = 2 T ( / ) + 2

TO) 2-2T(n|2)+z) +2
2 2
2 T(O')+ 4%2) 2 T(n/)+ +2
th
Aterk K
itegation,
T(neK) t2k
ASSume
Tn n: C)te kog,
Aot 2og,9
Saraswati Education Society's PAGE NO.:
SARASWATICOLLEGE OF ENGINEERING DATE
Subsihe n nl4

T4)= 2T(nlz9)+ 2
|Substitule un
Th)=22T(n)tz]+2 +2
+
+ 2

After k-l iteyat iong


TCn) K-T
2 T(nk-)+ 2
22
T + 2

2
(2-2) Simplifytng seme)

Let 2

2 -2 2

+ ¬n-2)

T()t (n-2)

(: T(2)

n t 2h -
2

TO) 3n-2
2 3n2
Saraswati Education Society's PAGE NO.: 27
SARASWNATI COLLEGE OF ENGINEERING DATE:
Geomehic Poocr|00: a, ar, ar
n-|

of
Sum n terms

ohen
h.a
ohen
Scum terr msL

when (r|<
diuergey wwheh
PAGE NO. :
Saraswati Education Society's
SARASWATICOLLEGE OF ENGINEERING DATE:

Uniseritu Ouestiony -

ite seeTchadgo. Sm
explaun
binay
alao for qujck sort& Sot the
folloaing ele Cyo, 72 17,2 49

srt ollousinq nos wing Merge sor t


lodesive e ti me co mplcoy
MerqeSot 2I9, 43, ,6 (to)

Eplain Qck Sat wthalgo


excample
ste C n alga to find
yinq divide t cong er srata
co mpleuby

You might also like