Unit 3
Unit 3
In short, while making a choice there should be a Greed for the optimum soluion
In this chapter we will first understarnd the concept of Greedy method. Then we v
discuss various examples for which Greedy method is applied.
3From
the set of feasible solutions, particular solution that
objective of Such a satisfies or nearly satisties
the solution called optimal
is
method works in
ÁS Greedy Based on stages. At each stage only one solution.
cachtime. this input it is decided input is considered at
solution or not. whether particular input gives the
optimal
Applications of Greedy Method
te
I
wee (Wil Uawelu)
30
deceasi4 ORde
loo 5
Is l6o 4
I4 30 3
85 x60
factienal paut 40 Is<35
To tal vae
Tota = Bo +loo t 140 I,-s
y60
30 |2o
Page No
Date
QUrangi ng Anto tte deeeautng
vale
Temo Valve Pi n o
I
I3: 30
I I2:20
I3 30 lao 4 I lo
+loot80
yalue Ok :6o
Total Q4O
longest Conwmon Sabscuence (LCs) :
gicn two eguences X=xi,2',
l we wid, o ind
Fos. eg
Macimum length Common subsequente fxy.
Subcquemcee ab, bod a Schaateu slald be in
2 (n- ne-of chiatenu) incAvingovder al theit position
X=<ABCB DA B> when we
Y= < 6DC ABA
,85
B5AG
‘0.oRII
22
3C ‘2k2 2‘2
4BoI 2 ‘2 R3 3
5o02 1212 ^3 ^3
GAo 42 72 K3 3 4
78 12 ^23 4 44
o tomnon Subscquence. = 4
BCBA
Iutiay fes (-1 to cli,o] =0
o = o to6 cl o, i7 =0
Now
i 9 te. AA#B
cli-,j] = C[o,1] =0
Ctl, j-LJ= Cl!,03 = 0
cltl, J= ciyj- so.c li, 1=0 & b[,] =
X = a b a ab a
y= b a b bab
b a b a
ooo
I 2 ^ 2 <2^2
a
A212^2 3 3
a 11 522^2 s3 ^3
b
O N2|73|7 3 44 4
So
, ugte of comuon
abab
subsqunte, = 4
clt, 2]= 0
So, Clt, 3] =0 L b[, 37 = ""
Now i=l
clI, y3 =|
Noo A 5
13.
19.
het cli,jlc-, j1
15,
16,
cloe cli,jIcli,j-]
hetun ch b.
o/ knapsock Probln
2 Knapiack =6
2 2
354
Se = (0, o)
For 7st TLtem
Lttw
S, = 0,2)
s, - ((90), (,2))
for pd zm
S; -(2,3)
$=(too),(i,2),(2,9), (35)
fer p em
S, (5,4)
S- (t9o), ),(3),(32,5)6 6), ("%,P),(6,2)
XX
Discard (77)& (8, 9) as
S4 = (o,0),(,2),42, (5,4), (64) )
value = (6,6)
we get t's value
So,
Matsix Chain Mutpication!
we
For getting tese 15 elemerts
A X B C
Fae to% 5x3x4 bo maltipliakios
5X4 5x3 Cast laltpl'akos, Px
te fofal elonent in 5X3=15elenent
resettat Marix=
Shold bejme
penpoimingmatpiation
These are 4 aticee e have to
A A2.
Yx6 6x2 2xT
analyse or find hono we mubtipy ese
A3 Ay 1> Matiices
5x4
we select so hat tofel tost minimise
-> ich pair should
or (A,. A2) * (As 'Ay)
"A Cezuttant
ReAultat matrix mati'x afe
Az Ay
A A2- A3 Ay
multplaton
A A2
Parerthesgotlon
paenthisisation of Matrieas 2
fways fo
X A X A,X A, Hese n= 4 ie 4 mrices
So
(4) =
So thee ae 5
was topentchise matricet.
C= AxB = Cobyt a b,;tagb
2X2
b¡taybg
Tota no. muttiolieetiorg = 2 X3X2 = 12
A, x A, x Az
2x3 3x 4 4X2
do d,
Twonaysto
multioythaee
(A,XA). Ag maees A,x (A,x A,)
2x3 3Xu 4x 2
2K3 31, Yx26 A
isK<j
vale
K 6) vae i l 2 QE0
Tb K = 2 fut hT min È. E
cl)I cost multyiations,
Cte,j3 = min
3 2 2 4
cig4c(k+ij3+4-dxy
4 Ag2 2
do
dy
2 3
ci,21 = min o24|28]58
1<K<2 2
o636
3
3otot 3x2x4 KI 2 3
c,2] 24 t2 23
3
c (2,3]=nin)c/323+c [3,
QKK <3 3+ d xd,xd
SoK= 2
O+O-t 2X4x 2
c(2,3] = 16
C[3,4] = min
3[ k<4 c[3,3] +c[4,43+ d,x dstdy
= min k= )
ClI,13t c[2, 4] + DoX D,X Dy
K-l,2 3 O + 36+ 3x2X5 = 6É
k= 2.
C[i,23tc[3,43
24 t
DoxDXDy
3 YDt X VX5 124
K:3
fof(S 3) for(S,3,4) )
PePs,,)
Pop(S,I, ) VoP(S, I, 3) lrid Ay
c]
for (S, 2.,2) PoP(S, 2+1,3)
Pof(s, ?3,
((A, (A2 A)) Ay
Novo fascrtebisahon g tese 4 matices
(A, Az Ay 2 2,3
3
As )) Ay
A A2 A3 Ay
2
Tie takento rmd panctan 2
O(n') Kvalueg
goes faom
Ito
n:nouutics
then pAirdt "A
3. elie phint "C"
STi,j]
SI,4]=3
5
pint- opia fasens (S,stj3+1,j)
0/Konpuack fotlem
may. P w (8,8) e s 3
rts (8,8) E
butcheck whotte s Not
So, Solut'on is
a:50, b: 10, e =30 d=5,e3, f:2
(Chasactes oitl% theiA oRCUAence /tout
find Hoffoan Tree 4alo0 Caltulete Hulfnanate lo each dhat.
Total chaates= 50 +10+ 30+5+3+2
in mesage 100 thasote
a Tree
"Take tuo chasactess with minimuLm count
(chasactes with min. vae at tupt)
a50
50 b= 10X
C=30X
30
d= 5x
e3 X
b F: 2x
e
2
No (ode chas Bits woe These ase tota! too characte
50 X| = 50
b= l00 0X3 = 30 2 tota t85 bitp
3ox 2 = 60
d= [o0 5x4 20 So Aveage bits apised to
e |0[|| 3x 5= 15
epresent each charactes
f 1o to 2 x5 : l0 = 85
Total = 185 bits 85i/chas
bits
wocd
Optial Bivasy Seasch
TAee'
Suppose you are gien ith 3 Kes tree- 0, 20, 30
then total no. BnaysenAth TACS We Can have woing
these Keys is i 2n Cn
2C3 3I
5 BST;
2
(20)
(30)2 2
(20)3 3(20
3
(V)
N9. No 'opauislos in 1tree = t2t3- 2
3
thee = l+2+3 = 2
3
- l+2t2 = 66
3
"gu - 2
*tee = 2
So we can find tthe tre nsith min. heiglt has fvalles
aeiage no. compasisions. ie. gdtee
But if we aie given no s octuktntls oith each Key then
Hlow to find Cptiwa
totimal BinaniySanck Tree.
20 30
Count 3 2 5
5x 3 (30 -(8
3+4t1S 2\)
5x1
5x )
30 (30)
3x 2 2x 2
(20
3x3
2X 3 (20
5+6+6
5+4+
(8