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

Unit 4 Notes

The document discusses various algorithms related to dynamic programming, including the Binomial Coefficient, Floyd's Algorithm for shortest paths, and the Knapsack Problem. It contrasts dynamic programming with divide and conquer strategies, highlighting differences in problem-solving approaches and memory usage. Additionally, it covers Prim's Algorithm for constructing minimum spanning trees and Dijkstra's Algorithm for finding the shortest path in graphs.
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)
10 views15 pages

Unit 4 Notes

The document discusses various algorithms related to dynamic programming, including the Binomial Coefficient, Floyd's Algorithm for shortest paths, and the Knapsack Problem. It contrasts dynamic programming with divide and conquer strategies, highlighting differences in problem-solving approaches and memory usage. Additionally, it covers Prim's Algorithm for constructing minimum spanning trees and Dijkstra's Algorithm for finding the shortest path in graphs.
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

UNIT II

Dynamic Programming

similar to divide conquer prob divided to subproks


Solms of subproblem are stored inatable
to solve a prob 1 optimal substructure
2 recurrence
3 approach maom

Binomial coefficient
The objective of B Coop is to find the no
of combinations of K'elements
a set of n'elements
It is denoted as C n k or K

1 and cluck 1 r n
m 59
k tCn 1 k 1
if Ken or k to

n K 3

Cy 423 4
24
Table is construe with Int rows k 1 columns

I 2 3
end n 6 k 4
6 0 1 2 3 4
4 151
23 4 9 i

i
6
it EEatomin
ALGORITHM Binomialoff Gsk
11computes n k
by dynamic programming Tnf complexity
11input two non negative integers on E K
11output value c n n Tn 1
of z

t.EE S E.ie and


i E I j c i i j B
return Cn I

reared
ifeng.ITitmiiif
EdisAlg
goal all pain'sshortest path Alg
input a weighted graph

findshortestpath
f fromeachmode to the other

Constant a matrix 4 4 since n 4


a b c d

ipf There are 4 nodes i efloyd's


alg will have 4 iterations Dl Dz Ds Dy

If Foreach iterationtakeeachsow E eachcolumnrespectively Outputof last iteration willbepafh.at


D
Itis est columnremainssame as the'D'raph

EEE test if peer has a ten


6 6 0 9 0 put all 0s in next
iteration also
022 2 0 5 A cotofDD
lindow 2nd
compare with peer table getmin

I I
7
85 Hint if so
being compared
is

directlytakepererreals
54 of

1
ALGORITHM Floyd's W I n1 n
11implements Hoyds using Tm
fff
dynamic prog
11input weighted graph i e a matrix
i e all pain's
HFEwhatrix 0
for i
for j
to
1 to

k
I to
n do
n do

me
shotgrath
Dei Dei n DER is
return D
Ésd
T
f compute transitive closeness of a directed graph not weightedgraph
a directedgraph with n'vertices canbe defined as an'nxn boolean matrix
of

ED n s

wat a b c d e self 0
noconnoe 0

direct
output
method
100 0 0

output
a b e d e
I there exists a ninitiittincedpate
1
if no
I if exists
path in the
directed path
as we cannotreachanyothernodefrom d

How to trace
0
m 4

i t a b
f cygenations steps o as it is perennals
0 0 I sep8 ANDoperation withinmatrix
Std OR operation with pre And

µ
0
f
1 0 0
Hit O
itthe is write
reference re
peer values in that
a column
8 8
particular now
to Basically

feet Iff and


0 0 0 1,181 R

99 99 99 9 01 11
ii
we can also do it
directly and check
that it is correct

ALGORITHM Marshall AC n 1 D
11 implements wearshall alg to compute transitive closureness
11 Ilo adjacency matrix A of directedgraph with n'vertices
11O P Transitive closureness of diagraph

RFI town do

tort I rule or ti n and R Ej k


retrom Rn

Time Complexity

3 for loops.ly

Diff b w Dynamic Poog and Divide and Conquer

Divide Conquer Dynamic Programing


dir conquer combine approach divide solve combine approach
breaksproblems into smaller Breaks the problem into overlapping
subproblem subproblem
no shared infob w sub
problems stores solnsin a tableforfuture reference
subproblem are solved sequentially subproblems are solved in any order
Typically uses recursion Typically uses iteration
high mem overhead low mom overhead
generally has hightime complexity has low time complexity
generally
E Binarysearch ee WI knapsack et
0 1 knapsack Problem
In the knapsack prob there will be n objectsofknownweights denotedby Wi wz.ws ws
profits denotedby Y v2 v3 Vn
knapsackcapacity is denoted
byN
Aim add moreprofitable objectsintothe knapsack with a constraintthattheweight ofthe addedobj's
should notexceedthe knapsack capacity
let x denote whethertheobjis included or not c 0 1 knapsack
knapsack probcan be denotedas
Maximize
EI riv 11
themethod 3 at pe fandw
fdntEy
Ff Ffs nobis

2 10 FE.IE
may
3 8 a
es
112 is 3
p É man profitobtained 18

I I ahf
na nous en no go.is o in and Wttcolumns w hiscapacity
let the table be denotedbyV the entries in V are cal as
may
iii i wist.is a s wie

profits
LE n 4 w 5 nutshts 2,13,23 86,161B

1st row 1st Col Of


0 I 2 columns I 1st now canhavenalsequal to 8onlysince
theprofitof placingthe 1stobjis87
2nd row canhavenalsequalto8,6 or 14as
the part ofpeacing and obj config
p find
ai
lillyfor 3rd now the man entry canbe
8 6 16 30 only
5rows
V
Ej j
Iff
2 0 comesunder 1stcategory insertnalonpeernowi e
Oy
V
E j w 2 2 secondcategory findmanf 0,2VC0,07 83 man 0 0 83 81
J wi 11 00 0 2nd man iD 1,0 63 0 0 63 611 Illy to others
V.EE j w 2 1 20 man v21 23 VCI 63
8 0 63 8 1
YE.ws

aeemt
ff Iin.ee iai iiiiiir i.tasaenotomae
So the 4ᵗʰobjshdbe included
do the same to v 3,5 1 41 v 3,3

v 2,3
good
3 V 2
b s reisig
stopwhen it comes to 0 1
i Objs included are 3rd 4thobj with weights 3 2 5
Wy

Memory Fine KnapsackProblem


whiff In this it
approach was required to find all vals like VC4,03 V4 D V54.23
ae comations
Fuse adf.onaidcotsis.io tatseTk'inmsn.im Em mtt
mem June are recursive in nature It uses Bottom
up and top
downapproach both

WE initialize all nals to


TI wut man profit is at the lastposition we needto
cal only for last
0 1 2 3 45 sole all to the Alg in next page
O O O O O O O start from r 4,53 and find allnals

3
SEE
IE ii i
if not V i max vCit ii j at if re
V14 5 MFKP

IIIv54.5
E no
man I rÉÉ ehow v23.5 and V53.3
v 35 MIT V3 3 man Vez3 v52 a 16
v 3,5 o ys
5 3 no V535 mesd 2,5 V 2,2 16 1119 cal for 2 5 Cz D 2,0
V 2,5 man vers VC1,47 6
v 2 a manCreya ve 6
EEi EE
EE E
v4.43 mantra a read to Ingham

it 3 cc
I D r o D 0
0 1 2 3 45
00 0
0 1 8 88 8

4 0

Alg used forfilling the table MemJuneAlg


Algorithm MemFineknapsack i j
11 implements mem June method to solve the knapsackporb
A inputtwonon negativeintegers i and j whatsitorapacity
11 output the nailsof an optimalfeasiblesubset of first items
1strow 0 1stcoe 0 remaining 1
Iv i i so
it j net is
value M KP i 1

value man MFKP i i j MFKP i i j wteD i


v ig value
return i j

The
pros andsatisfytheprobconstrains
Steasible
Greedy Technique locallyoptimal ithastobethebestchoiceanyall feasiblesons

iii iii i.is


ii vm
Isolves a probusinggreedymethod Jim's
which is a list of n elements p alg
ifeng.gs theinput dijkstra'salg
toi factorial k sprob
I.io ca
if feasible a
solution Union Solution
a
returnsolution
Priming to construct a Min Spanning Tree

goal is to construct a MST min spanningtree


A sparming treeof a connectedgraph is a acyclic subgraph whichhas all the
vertices
of the graph Many no of Spanningtrees can bemadefor a graph
M.SIthe one with min weight
is
This M.SI can be found by Prim's Alg
Steps select any neater edge and smallest weightfromG
choose

Stuff at each step choose edge of smallest weight joining a water already
included to vertex not yet included
tepf continue until all vertices are included
F error
if
0
8
stoked ae s 4 a b
t
W.li
might 3 2 1 2 to d
8 1
1 2 27 3 4 12 weighty
write it like this in exam

a
s 4 no of vertices 6
no of iterations 6 1 5
6 3

sd
IS Iskacontain cases used in the const a mst 32 taken to
Vertices a
or

edges
stitute
i.EE ii
vtset Etm.IE
eggmgygtamtsetothev
a f
Ie
cab addtheminedgetoedgeset
mins dint
if ae
u
of.es ftim
jEiEEi
LasalaD casesup ca.ae
ET a b b c3
Etf a b b c b e 31
4min21 e saints
i
a figests
hgt d Gd E
Et can ideae ce.gg fs
a s
cypy.az
weight 3 It 4 2 3 1311
a.fr 2
3

Algorithm perims G
I output E i e the set of edges used in constantof MST
V03 1 Vo is the startsource neater
VI I
Find mintweight edge et v u among all the edges v x such
that is in r and u is in V VT

IEEE
Timelompletity

if the input graph G is rep as a weightedmatrix efficiency 0

if the inputgraphbyis rep as an adjacency list a queueis implemented as a min heapthe


mm me
payounin 1 deletions andmakes IE verifications and changethe heap

i
CEI ff.dz InIs0cdI
Discs Tofind the shortestpath

Based on greedy method


goal tofind the shortest pathfrom a source in a Ulrighted graph
aka single source shortestpathalgorithm
all edges in digalg shd havethe values

Fffff.ae source a

herton a É pyn
8 mi soon

d the

staff w ga b c d
_gamed
am
natena de pe may
gun Nia soon is
3 a ratio
d 20 a

mango w b cd
gggamaywsfbit.mil
I
heating
É sog
75 C
a c distwillnotchange

Y 38dimdnw g.is a smallest


my
neatens de PC soon L ab
005 I Jasitis
3 a
d 6 C

s
minds in d

heartend d PC Soon a cbd

05 Y
36
d ddoes not have outgoing edges

Algorithm Dijkstra G S
Montput Theshortestpathfroms to all vertices in V
foreachvertex r in V do
4
dEs 0
seen
IN v
while
I min W
extract
Soen solnU us
IN w us
foreach vertexV in Vadjacent to a

c in
return d

Timecompanity

If by is rep as adjacencymatrix
IVI
If G is rep as a adjacency list andminheapis createdfor all the edges O

extract min tos O log r

i C É Ocular Eggs
Huffman Coding
Mcdombsett

used to dataencoding used


Intestity

encoding can bedone in 2 diffways fixedlengthencoding


variablelengthencoding

we build a BinaryTree whichgives the codeword


for the characters
OfInitializechild
n noof Chara'stobeencoded o 1 tee binarytree leftchildmarkedwith0
marked with 1 and label with theappropriate charal as well as record
right
thefreq of that chonal inthe tree root to indicate the tree weight

repeatthe foll until a single tree is obtained


find 2 faces with smallest weight and create a 0 1 tree with
the smallest as left child and second smallestas night child Record
the sum ofweights in the root of the new tree

I draw H.tw find compression ratio encode BADDAA

FE
fam.bg gfg
f
MI Eathanaffitiencone
Astin admin

arrange in increasing
SHI of order in each step
vein win

If
and
0.25 0.35
of
0 0

If mistaking am
min
0.6 2nd
1stmin 1
0
If 0.25 0.35

HuffmanTree

ÉÉ
length 1
ÉÉÉ
1

9m

angno asbits rechara life


2.2
1
Compression ratio
tf
26 1
no

11
encode BAD DAA

Fractional Knapsack Problem using Branch Bound

The objective of his probis to maximize the profit hence it is an upper boundfun
Here we shd always order the itemsin thedescendingorder oftheir profitstoweightrat
ie
III
The upperbound
oftheknapsackprob is cal Ub MW
f
n 4 M 10 W 4753 v 40422512

i onto us e
moment
if
10 10 10g Cti w o 448

him
1 is
with without

Kiss I p i I c can
win

a with im
unesent

etatj.fi
FIE ost.ci
up a

Manprofit 65
objs tobeplaced 1 and 3
1

You might also like