0% found this document useful (0 votes)
20 views15 pages

Divide and Conquer Algorithms

The document outlines various algorithms and methods for solving optimization problems, including Divide and Conquer techniques such as Merge Sort, Quick Sort, and Strassen's Matrix Multiplication. It also discusses the Greedy Method and its applications, such as the Knapsack Problem and Activity Selection Problem. Additionally, it provides pseudocode and complexity analysis for the algorithms presented.
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)
20 views15 pages

Divide and Conquer Algorithms

The document outlines various algorithms and methods for solving optimization problems, including Divide and Conquer techniques such as Merge Sort, Quick Sort, and Strassen's Matrix Multiplication. It also discusses the Greedy Method and its applications, such as the Knapsack Problem and Activity Selection Problem. Additionally, it provides pseudocode and complexity analysis for the algorithms presented.
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/ 15

Diviele and Conque

Pie
Se
S S

Search
Bina
Maximum and
Algoithm 2 finding Miurn
DAC (P) Megesort
i (Smaii() (4) Qwck sot
Momis Muhipeati
s(P) S) Srassen's
3
else
divid p into
DAC(P), DAC (P).
-)
Combin (DAc(e.), DAC(P),

Search
8inag
4 47 s3 SS 6
2s (29)3) 36
|4
3
S
2
mid

Key 42
mid =
h

int Bin Seaeh (A, n, ke)

2-1,h n
whid (loJ <= high)
mid (24h) 5
=A[mid3)
iF (key vetun mid
min timu - o )
h mid -1 ma hm o(lgn)

3
3
Maimum

2 heignt of tu cormpars iD
Logl6 =(
Recursive Algoithm
TO) Algenthm RBin seavch ( e.b, key)
if (e:h)
if
(i)
(ACJ= = kuy) Singu <lement

ese Smoll P,obemn


3

mid= (2h)
iF (ky Amid))
tetusn mid

if (ky < AÇniaj) tavga


TO) veturn R6in Search (l, mid-1, ky) Arobum
else we ane dind

A
Bin Searchl midt!, h, key) into smatl
3 Subprobkny
3

T(n2) +1 o(eogn)
Merse Sort

Me«ge Merg mose than +wo Usts

A C
2 -K
L’2
4 3 2

I2

4- wy Merging
Algorithm Merg (A,B, m, n) Not a 900 d M
thod

i=!, j= i, k1
So, use 2 wayMegiá
if (AC)<8C3))
c[K++] Alit+]
c[K++] = B[j++]
(Ans
3
element
A B D
wkia (ism)
L1:
3

wa(in)
c[k+*]= Bj+ L3
3
ThY atv Process
2 lway Merge Sost

Considex +his as 8 G'sts


array
A 3|7 S 4 8 lisb qre alyeady Sostd
2 4

jst Pass 3 9
4

of Pas5es = oq n
4
2 Pass S 2

3d Pa5s 2 3 4
Merge Sost

mid h

ir (ieh) Aoben s (arse Di'da into ubprbus.

mid +))
T(b)mergesot (a, mid) n

T(Q) rnerge so4 (mid 41, hgh) 21()+n:


merg( e, mid,h)

2,8

Merging is do ni
12 Postord e trav(is al.
3,4

mesg

9|3|3
Pros of aey Sost
Lary Siu st
(2) linked i'st RAM
ExKYna So« hng
4) S+abu
S

duplcate
4
Lsost Merged Arraregmert
not d'sturbe
3 4 S

Stable
uo)/ Me* Sos
Cons of Merg Sot On) fet a space ) Tnseon sot
in placa Sost) 2) Bubbu so
Space (not
Smatl Problem
2No Slow
Fox small si U'sts, it is
J beHey*
wase hme in seussion

Recursive ’ Stoack (Coqn) stack <paeo it neds .


spae taken
o(n+ tog")
Quk Sost
Studert -7eache

ate Snallc than


posihon
than
is in ngnt should be (argeY
Ideq An ee ment eleme t on
ment and
that ele
that ele mn

|2 I3
6
|6
3 4
Snalles
han

follos DACG stratgy of Ust


Y s e r tend

4 S

3
A

Pivot = 10 of Pivot
find soyt poHon

Procedue than pivot


find an ele ment q7cate
until sou |(ssn than Pivot
elemet
jou ind a n

Swap Parthoning procedurt


(arsu than )o
Smalley th an|o

| '
Porhion (l,h)
Pvot A[4]
i=R, j=h
whia (i<i)
Qicksost (e.h)
do
perfosm quickint on
wki'a(aliÜ< pivot)
and
3
do
Quicklor(,j)
(AL')>pivot)
Quicklort (j t1, h) 3 whia
if (i<i)
Swap(Ali),ALi))
3
j’ Posinon
whre paihony Suap (ALW,A[))
is lo
3
1,1S
Pabhon at

9, Is
1,?

s. (9,1

o(n togn)
Pasihon at every mdd e eu ment
Best time’ 0 (ncoan)
wosst Case ’ 0 ( n ) List is atrea dy Sod

2,) ]

3,2]

S,

Impronng Ruek Sot


midda elemennt as Pivot
Selcct
Rand om
eleme t as pivot
Select
2
size ’ o(begn ) to o (n)
Stack
Strassen's Max Mulhplcation

b b
A=
be

for (izo, ikn, it+)


for(=o; j<n',j*1)

fos (ko; K<n,+)


C[i,3I += Ali, k] * B[*,i)

Small Problcm

If Si2e of matix is greatY than 2.


2x 2
}t not

A= Au A1 bi bi3
bay b

434
bg bet
A
ba3
b4 b43 b4
Algonthm MM (A, &, n)

if (ns 2)
C- + formulas

3
else Mabie adeiha

mid- n /

MM (An, B1 )Mm(Ain, 6a1, nla)


B22, nz)
B2t, nh)
MM (A,6.,, nl-) + MM(A21,
n/)
MM(A,B,s, nh ) + MM(An2, 6a2,

ns2
T(n) =
8T(nlz) +n?

6
(ase I )

Shassen'has done ?
what mulhp¯ cations
C) = A, Bn t An B2)

+ Ba)
Pe (A +Az) (61 C2| A2, B t Azz B2

Q (A| + A22) B
R= Au (62 -B22) ng2
S= A22 (Bz1 - B)
T (A) + An) B22
Bi)
U= (A2 - An) (6+
V= (Aj2 - Aaz) (B21 t Ba) o(ntog?)- o(n)
C) P+s-T+ V
Max- Min Using Dind and Conquer

4
|4 Ma|+
6
6

mid: 4
Max 9 |4
+

4
|lo 4
9 6
Mar 1|
Max Min lo Min 8

Ma
T(n) = mar
MihM,n

:. 0 3n2)= o()

Algorithm MaxMin Ai, . ma*, Min)

man mìn ALi)

else if (==j-i)
if (ACi) <ACS1)
max mas
max AS
max maX 1
else
max A[i
mine A(i) min =min
3 el6e mih min

mid = (+3))
pmar Min (A, , mid, max, min)
Man Min(A, midt, , max, min)

T(n) =
n2

l2r (nl3) + n>


hreedy Metho
used for Solng opimzation Probems

requse eitheY min oy may

P: A B Cons aunt 12 hs min cost


(CondiKon)

waue Bike
mad
Solution Satis ying Condihon The decision is
without wosying about
min feasib Solution
the effect of the
Curent cdesion jn
oph'mal Soluton not be opimat)
future
(nly 1)
Soling ophmization Probltms
St agies for
O Greedy Method
2 Dynamic Progran ming
and Bound
Braneh

Method
Grcedy
Poblen Shold be Sold in &tges
ns

Algorithmn Geedy (a,n)


9 4
L1 to n do
for
Slect (a)
if feasiba(2) then
Solution =

3
3
3

20
0 20 D

30
Knap cack
Problem (fractionay Knapsa)

Objcts (0): 2 3 4 S n7
1S 18
Profit (P): 4
3
wights(w): 2

(faction)
Mavimize tha

ophnation problem

Consraint
3 4

4'S 3
Plw: objectie
1 ma
2|3
M3

I 5 - 2 - 4-S-|=2- 2 -0

Profits = II6 t 213 S + Jxs +

with DeadUnes
Tob Sequening
n=S

Jobs
Profits 20 IS S

deadints 2

Machin

Highest job pr ofit shoul be do fivst


factional Knapsack cweights, values, w)

n= len (wights)
ims e enptg ist

iems
APPend ratio, w) V[iy, ') 4o
Sost lescending ordet of Yatio
items in descending
V_may e-o, x[]+ array of Os of siun, vemaining Capacity<w
fos each item in items!

let (atioo oight, value, inder) - tem


If (remainingcapaity weignt)
nlinde]<1
Vmay Vmax 4 valwe
Yemainingcapa city<renaining cepa'y- wight
else
Raction emaing-capau ty wg ht
[inde]e fraction
Vmay Vna t faction x valw
Breat the (oo pP

Retun
Vmas,x)
Q’ Mazimum mechngs
Activity Selection Proble n

A'go'
0) Sort the actirihes as per finishng time in ascending ordey
2 Seteet fiyst actint is ale s than
time
(3) Seleet ne w achrty if its star hng
time.
poeousy seleched actity's Fir'sh
aYe checked.
actiihes
(4) Repeat slep 3 till all

Acoity 4 2 3 4

Start 1
2 8
4
Fini'sh to fish hn
sort aceosding

ag 4 S
Achiital 4

Stat
4
KN'sh
icishjXst4rt1 Start ; > fnish

Actiity
3 S
Stat
S
Finish

Pseudocod
Ago
etor (s,f)
Greeedy Aetiritysll
ne length [s]

fos i 2 to n
do if si fi
+hen A EAU{is

Tetun A
Dptinal Mesge patten

|List A C

2
Sies ’

2 2

Total cos t t)3 t6= 40

(04)

TC I|+5+I6 =3 2

A D

Lists’ 30
30
20

(Go

30
20

You might also like