0% found this document useful (0 votes)
14 views34 pages

Unit 3

Uploaded by

jc1177
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)
14 views34 pages

Unit 3

Uploaded by

jc1177
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/ 34

Unit 3

Introduction to Greedy Approach and Dynamic Programming

Greedy approach and Dynamic programming are two different algorithmic


approaches that can be used to solve optimization problems. Here are the main
differences between these two approaches:
Greedy Approach:
 The greedy approach makes the best choice at each step with the hope of
finding a global optimum solution.
 It selects the locally optimal solution at each stage without considering the
overall effect on the solution.
 Greedy algorithms are usually simple, easy to implement, and efficient, but
they may not always lead to the best solution.
Dynamic Programming:
 Dynamic programming breaks down a problem into smaller subproblems and
solves each subproblem only once, storing its solution.
 It uses the results of solved subproblems to build up a solution to the larger
problem.
 Dynamic programming is typically used when the same subproblems are being
solved multiple times, leading to inefficient recursive algorithms. By storing
the results of subproblems, dynamic programming avoids redundant
computations and can be more efficient.

Difference between Greedy Approach and Dynamic Programming


Feature Greedy Approach Dynamic Programming

Guarantees an optimal solution


May not always provide an
if the problem exhibits the
optimal solution.
Optimality principle of optimality.

Subproblem Does not reuse solutions to Reuses solutions to overlapping


Reuse subproblems. subproblems.

May involve backtracking,


Does not involve
especially in top-down
backtracking.
Backtracking implementations.
Feature Greedy Approach Dynamic Programming

Typically simpler and faster May be more complex and


Complexity to implement. slower to implement.

Suitable for problems where Suitable for problems with


local optimization leads to overlapping subproblems and
Application global optimization. optimal substructure.

Minimum Spanning Tree, Fibonacci sequence, Longest


Examples Shortest Path algorithms. Common Subsequence.
yMeth
4.1 Introduction
In an algorithmic strategy like Greedy, the decision of solution is taken based
information available. The Greedy method is a straightforward method. This
popular for obtaining the optimized solutions. In Greedy technique, the netho
constructed through a sequence of steps, each expanding a partially
obtained so far, until a complete solution to the problem is
constructed si
reached. At
soluton
choice made should be,
each step t
Choice
of solution Feasible - It should satisfy the problem's constraints.
made
at each Locally optimal - Among all feasible solutions the best
step of choice is to be made.
problem solving
in Greedy Irrevocable - Once the particular choice is made then
approach it should not get changed on subsequent steps,

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.

4.2 The Greedy Method


In this section we will understand "What is Greedy method ?".
Algorithm
Greedy (D, n)
//In Greedy approach D is a domain
I/from which solution is to be obtained of size n
1/Initially assume
Solution0
for i 1to n do

Check if the S select (D) // section of solution from D


selected solution
if (Feasible (solution, s)) then
is feasible or solution Union (solution, s);
not.
Make a feasible
choices and select
return solution optimum solution.

In Greedy method following activities are performed.


1. First we select some solution from input
domain.
2. Then we check whether the solution is
feasible or not.
Desl

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

1'arious applications of Greedy method are .


deadline
1.Job sequencing with
Knapsack problem
3.Minimum spanning tree
4.Single source shortest path
5.Optimal merge pattern
6. Container loading

L22 General Characteristics of Greedy Method


1.1Greedy choice property : By this property, a globally optimal
arrived at by making a locally optimal choice. That means, for findingsolution can be
the solution to
the problem. We solve the subproblems, and whichever choice. We
find best for that
subproblem considered. Then solve the subproblem is considered. Then solve the
is
subproblem arising after the choice is made. This choice may depend
previously made choices but it does not depend on any future choice. Thus upon the
in greedy
method, greedy choices are made one after the another, reducing each
given problem
nstance to smaller one. The greedy choice property brings
problem with the help of subproblems. efficiency in solving the
L. Optimal substructure: A problem shows optimal
Othe problem contains optimal solution to the substructure if an optimal solution
has optimal sub-problems. In other words a problem
substructure the best next choice always leads to the optimal solution.
if
denoted
0sx;
som capacity
has is that
which is, words,
i thegoal such
object
obiect haveour other i
Each W object
we approach
each profit. In S
;X;
2,..n. problem W. solutions. an
bes W
follows. with maximum of
Greedy x:
1, associatedKnapsack should 1 to fraction
= feasible subject
as W.
statedifrom weight using give objects
of the
is mentioned
problem
that p;X;
4.4 becan objects
Problem
Knapsack valuemost set
selected
objects the
carry
problem profitthe this
n obtain
n can
maximized
are above of Knapsack
someat solvethoseweight
there carrysolving only can
Knapsack
and Knapsackto
Suppose try totalwe
Choose then the
w,
The weight Whilewe The Where
1<isn.
This When 1. 2.
And
cemsidlx 5 tems along
Quss cenuide u th heiz eabeetiue
weigit and vales following tne
He actiol Knask Buoblem.

25 ; lo; 203 3o 4 6>


V 30 o, l0o , 90 160
caþaity of Knaþsak We 6o Takirg vaa
Ifems Lueigut(aj vaue (Vi) Paatt (Pi')
HH 80
I2 lo o 2
5
30 3
IS 4-6

te

I
wee (Wil Uawelu)
30
deceasi4 ORde

loo 5
Is l6o 4
I4 30 3

NOLO tt the knabsack aco vding to the daxei,


value
Fuiat choose item I whose
weight 5
Hen item Ia whose cuet ght
choose
NOw Hu to tal eig ht tn knap salk
6o¢ 25 - 35 est weigt
knapsack
Now, the next item Is whose eeght d fo
but ue want 35 " Só' Ule hhoose actio

85 x60
factienal paut 40 Is<35

To tal vae
Tota = Bo +loo t 140 I,-s
y60

Cind te to aton knas saek


wSO Rnd the
þroblem of knaksak
HH tw; Cue'gut ) V; (velue)
I lo 60
I2
3o |20

vae ot eigut Aat'on


Taging
Tlemo
H
6o

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

Nouo the knapsack


knaf sack aceoxdi'ng to he
de cueasig vaul of Pi.
Fist choose tem Ii whose wuaigut
weight lo
cloose emI2 whou ceeght á 20
NOw the totoal weigt of Knsack di
fo+ 20-30
est
eigot
the chooeÀtem whoe weight 3o
Ro:so choose ationa
bt wewant
þoit
Ceact'enat þct: 3oo x12 0
L3

+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

Determinx the Lcs e <I,0, 0,1, o, 1, ol7k


<0, l,9 , ,o1,, 0>
cli,j2=
j
(mex (ccij-13, cT-1,j) 'j>ok xityi
No t=1 2j= 2
e. Ap
c[o, 2] = 0
C[1, 13 = 0
Clt-t, j3 = clj-13 So, CCt,2] =o bli, 2] + ^
No i=| &3

clt, 2]= 0
So, Clt, 3] =0 L b[, 37 = ""
Now i=l

cli,4] = c (I-I, 4-13t |


C [o, 33+ 1

clI, y3 =|
Noo A 5

c lo, 5 3=0, cl,] =|


Thus cti, j-J> c[-I,jl e. cli,5] =cCi, j-1J=|kbisl
No te. A =A

So, clt, 61 = c[I-L, 6-13+!


- clo, 5]+1 =0+|=|
Cli,63 =| & bl, 67
Novo i=2 2 j'=1 we get X2 ie BzB
cl2, 13 = C[2-l, I-13 +1
C[2,13 = | 2 bl2,13 =
&
e
AN all ale
LCS-leugth(x,Y)
1, mngth (x)
3. o ie to m
4. do cli, o] 0
5.
6. do clo, jeo
for i -l to m
do Jo iciton
9. do
han clijl<-clit, j-1] +1
12.

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

3x4Y2 =24 muHipliatiod


2X3v4= 24mut.
2x3 3 X2
2 X4 4x 2
?x3 X2, = 12 malti
2x42, 16 mutieione do
do de d3 Ho'24+0+12 36 multliati
24 +0+1G = 40 mutipiatiens
say that he paserteiisation A, X{A,xA,)
ae altipliations. L45 better thay
AjKA).A3
Dbsave foaula
(A,xA)xAs,
C12]-24 C(3,33:0 c u 0cl2,3]- 24
Toje mu iolicahons =
ci,j+ C[3,33tddxda
24 +0 t 2xX 2
0 t 24 +2x3X2
36

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

C[,31= ymin (cr, 13 + c[ 2, 2]+


|s K<3 K2 o + I6 + 3X 2x doxdxda
2 =28
- 28
K: I22 C[! 2]-t c[3,3] t dox d, xda 48
2 424
K=2
C(a432<K<4
= nyc(2, 9]+cC3,4]+ Dyx Dy XDy-8o
ot4o +2 xX5
K-23 C[2,3J +c[u,4] -+ D x D3X Dy
k-3 16 t o +2 X2X5
=16 20 = 36

= 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

c[i, 3]t c[y, 4]+ DoxxDy


558
28+o + 3x2x

Se, miwnn cost 4 mcies


given
mult ipl'iatiens
ie 58 Tmutplitiors
Por (S,1,9

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

PAint-Optnal faens (Si,j) i-e.

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

m=8 (mar. weiglt thet a Knapsockcan hold)


(no, ol items)

P:{!, 2,5, 6 (rofit)


W{2,3, 4, 5 (wit)
Sel.
s'f(o,0)3
S
Prelitel aglt - 2
Pralitl
Consides (t chject having
(Ad l 2) to s°)
Thn, thion s'Gs;
s'f(o,o) t,2)}
(add(,w) b; to i'
s{(2,3),(35)
11
te 's' elemente
2

o,0, (,2),(2,3), (3,5)}


Add (5,4) to s
si 60,(4,)(a),(6)5 Now hion Sí4 Ss
s(oo),(,2), (2,)(0,) (50,66), (9,
iykt is de oLaning
)f
s : f(45),(9,), (&, ),(9, u2,10(P)E

may. P w (8,8) e s 3
rts (8,8) E
butcheck whotte s Not

So (6,8) e objet 4inaed


(8 -68-5) =(2,3)
(2,3) s es objet not indlude
check c2,3) e s Yes So,
(2,3) s '
obj =(2,3)
SUbrtact th's
(2-2,3-3) - (o,0)
(ao) e s so
(o,o) e s'2 aao (90)

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

Find to4al compaAisiors by nuttipyig cout each


Then
Key oith ts ludl(herglt). o) X3 (20) |X 2
(10 2x3 (36)2X5
(30 2X5 (o
2@ x 2 20)3x2 2+6+1O

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

that although huight 3tree is


So, we (an ind 4te hass les'no f topasistons
minimuun (ie.2) buts) on the
than othes ie. 1t. So t depnds
each Seascla
Thee.

You might also like