Académique Documents
Professionnel Documents
Culture Documents
&tru+ture o) 2ar53all-5 algorit3m:
Algorit3m:
Algorit"# 5ar"all-A[1...n, 1...n]. // A i t"e adjacency #atri$
3
-%.
A
for k 1 to n do
for i 1 to n do
for j to n do
3
-k.
[i, j] 3
-k/1.
[i, j] or -3
-k/1.
[i, k] and 3
-k/1.
[k, j].
ret!rn 3
-n.
The worst case cost is P(n
?
, so it is not better than the br$te force al"orith!% In fact, for a
sparse "raph the br$te force al"orith! is faster%
Ti!e Co!ple,ityF
3. E>plain 3oA )lo*6-5 algorit3m i5 u5e6 in )in6ing t3e 53orte5t pat3. ;i8e e>ample.
,!V -(0 . B# -(01
)loyd al"orith! is a "raph analysis al"orith! for findin" shortest paths in a wei"hted
"raph is the !ini!$! s$! of wei"hted ed"es for all paths between the pair% All pairs
shortest path proble! is to findin" the !ini!$! wei"ht path between any two vertices in
the "raph%
Prepared By
Mrs.A.Subathra Devi AP / MCA
The distant !atri, "ives the wei"ht of ed"es for ad7acent vertices% Note that if the
two vertices are not ad7acent then the correspondin" distant !atri, entry is S%
"lo*6M5 Algorit3m an6 +orre+tne55
)loydQs Al"orith! is very si!ilar to 2arshallQs al"orith! calc$lates the !ini!al
distance $sin" a se#$ence of n !atrices, where n is the n$!ber of vertices
D
(0)
, ..., D
(kN()
, D
(k)
, ... , D
(n)
The i, j entry in 6
(k
, dij
(k
, is the !ini!al distance of path between vertices, vi to vj
s$ch that all the inter!ediate verte,, ,4 is a!on" the first k vertices, ie% . M 4 M k%
Note that 6
(/
is the distance !atri, and 6
(n
is the sol$tion that we are see&in"%
Consider the all the paths fro! vi to vj with inter!ediate vertices less than k, they can be
divided into setsF
.% -aths with no verte, n$!bered k
>% -aths with a verte, n$!bered k
In set ., all paths with no verte, n$!bered k, the !ini!al distance is dij
(k'.
In set >, all paths with a verte, n$!bered k, the !ini!al path will visit the v& only once%
Then the paths be split into parts at vkF
vi, ,4 (where . M 4 0 k, vk% ,4 (where . M 4 0 k, vj
The shortest path for set > is !in(dik
(k'.
A dkj
(k'.
%
*et 5 represent the wei"ht of the path% Then
57(8vi,%%%,vj9 1 Hi04Mj6;4'., 4<
The !ini!al path fro! vi to vj is then the !ini!al of the two setsF
or dij
(k
1 !in(dij
(k/.
, dik
(k/.
A dkj
(k/.
Algorit3m:
Algorit"# Floyd-5[1...n, 1...n]. // 5 i t"e ,eig"t ditance
6
-%.
5
for k 1 to n do // iteration t"ro!g" ditance #atrice
for i 1 to n do
for j to n do
6
-k.
[i, j] #in-6
-k/1.
[i, j], -6
-k/1.
[i, k] ' 6
-k/1.
[k, j]..
ret!rn 6
-n.
Time Comple>it* i5 O(n
3
)
4. 2rite a 5uita4le e>ample e>plain !ptimal Linar* &ear+3 Tree5 pro4lem u5ing
6*nami+ programming algorit3m. ,B# -(( , !V -(0, -(( . /(01
A binary search tree is a tree where the &ey val$es are stored in the internal nodes,
the e,ternal nodes (leaves are n$ll nodes, and the &eys are ordered le,ico"raphically% I%e%
for each internal node all the &eys in the left s$btree are less than the &eys in the node, and
all the &eys in the ri"ht s$btree are "reater%
Prepared By
Mrs.A.Subathra Devi AP / MCA
1!
2hen we &now the probabilities of searchin" each one of the &eys, it is #$ite easy to
co!p$te the e,pected cost of accessin" the tree% An !L&T is a B(T which has !ini!al
e,pected cost%
E>ample:
6ey 'I . T U .? >.
-robabilities .NT .N?> .N.J .N?> .N@ .N>
The e,pectation'val$e of a search isF
ItQs clear that this tree is not opti!al% ' It is easy to see that if the >. is closer to the root,
"iven its hi"h probability, the tree will have a lower e,pected cost%
Criterion )or an optimal tree:
Each opti!al binary search tree is co!posed of a root and (at !ost two opti!al
s$btrees, the left and the ri"ht%
Met3o6:
The criterion for opti!ality "ives a dyna!ic pro"ra!!in" al"orith!% )or the root we
select one val$e to be stored in the node%
5nce this choice is !ade, the set of &eys which "o into the left s$btree and ri"ht s$btree is
co!pletely defined, beca$se the tree is le,ico"raphically ordered% The left and ri"ht s$btrees
are now constr$cted rec$rsively (opti!ally% This "ives the rec$rsive definition of the opti!
costF *et denote the probability of accessin" &ey , let denote the s$! of the
probabilities fro! to
Prepared By
Mrs.A.Subathra Devi AP / MCA
11
The e,planation of the for!$la is easy once we see that the first ter! corresponds to
the left s$btree, which is one level lower than the root, the second ter! corresponds to the
root and the ? to the ri"ht s$btree% Every cost is !$ltiplied by its probability%
)or si!plicity we set and so si!plifies to % This
proced$re is e,ponential if applied directly% Eowever, the opti!al trees are only constr$cted
over conti"$o$s sets of &eys, and there are at !ost different sets of conti"$o$s &eys%
In this case we store the opti!al cost of a s$btree in a !atri, The +atri,'entry
will contain the cost of an opti!al s$btree constr$cted with the &eys to
2e now fill the !atri, dia"onal by dia"onal% It is c$st$!ary to fill the !atri, with
that we save a lot of !$ltiplications and divisions% *et then
An opti!al tree with one node is 7$st the node itself (no other choice, so the
dia"onal of is easy to fillF %
Algorit3m:
072&8A9/B:2-;, 4, n.
for i 1 to n ' 1
do e[i, i / 1] 4i/1
,[i, i / 1] 4i/1
for l 1 to n
do for i 1 to n / l ' 1
do j i ' l / 1
e[i, j] <
,[i, j] ,[i, j / 1] ' ;j ' 4j
for r i to j
do t e[i, r / 1] ' e[r ' 1, j] ' ,[i, j]
if t ( e[i, j]
t"en e[i, j] t
root[i, j] r
ret!rn e and root
Time E))i+ien+*:
An opti!al binary search tree for n &eys and n A . intervals with &nown re#$est
fre#$encies can be constr$cted in 0(n1 ti!e%
9. De5+ri4e 4rie)l* a4out Memor* )un+tion5. ,!V -((, B# -(0 . -((1
Dyna!ic pro"ra!!in" solves proble!s that have a rec$rrence relation% Usin" the
rec$rrences directly in a rec$rsive al"orith! is a top'down techni#$e% It has the
disadvanta"e that it solves co!!on s$b proble! !$ltiple ti!es% This leads to poor
efficiency, e,ponential%
Prepared By
Mrs.A.Subathra Devi AP / MCA
12
The dyna!ic pro"ra!!in" techni#$e is botto!'$p, and solvin" all the s$b'proble!s
only once% This has the disadvanta"e that so!e of the s$b'proble!s !ay not have been
necessary to solve%
The techni#$e $ses a top'down approach, rec$rsive al"orith!, with table of s$b'
proble! sol$tion% Before deter!inin" the sol$tion rec$rsively the al"orith! chec&s if the s$b
proble! has already been solved by chec&in" the table%
If the table has a valid val$e then the al"orith! $ses the table val$e else it proceeds
with the rec$rsive sol$tion%
+e!ory f$nction al"orith! for the &napsac& proble! initiali:es V;i, j< to '. e,cept
row / and col$!n /, which is initiali:e to /%
Algorit3m:
Al"orith! 8F=na;ack(i, j NN i, j represent the s$b proble!
i) V;i, j< 0 / NN !eanin" not already calc$lated
i) j 0 5eig"t;i< t3en
val!e 8F=na;ack(i'., j
el5e
val!e !a,(8F=na;ack(i'., j, Val!e;i< A 8F=na;ack(i'., j'
5eig"t;i<
V;i, j< val!e NN p$t valid val$e in the table for both cases
return V;i, j<
6. E>plain t3e Jnap5a+J pro4lem. ;i8e rele8ant e>ample. ,!V -(0, -(( . B# -(01
The &napsac& proble! or r$c&sac& proble! is a proble! in co!binatorial
opti!i:ationF Diven a set of ite!s, each with a wei"ht and a val$e, deter!ine the n$!ber of
each ite! to incl$de in a collection so that the total wei"ht is less than a "iven li!it and the
total val$e is as lar"e as possible%
It derives its na!e fro! the proble! faced by so!eone who is constrained by a
fi,ed'si:e &napsac& and !$st fill it with the !ost $sef$l ite!s%
The proble! often arises in reso$rce allocation with financial constraints% A si!ilar
proble! also appears in co!binatorics, co!ple,ity theory, crypto"raphy and applied
!athe!atics%
Rule5 to )ill t3e ta4le:
To desi"n a dyna!ic pro"ra!!in" al"orith! to solve &napsac& proble! we need to
derive a rec$rrence relation that e,presses the sol$tion to the instances of &napsac& in
ter!s of sol$tion to its s!aller s$b instances%
Prepared By
Mrs.A.Subathra Devi AP / MCA
13
Divide the s$bset of first ViQth ite! that fit into the &napsac& of capacity V7Q%
This can be divided into two cate"ories%
.% A!on" the s$bsets that do not incl$de the ith ite! then the val$e of opti!al s$bset
is K;i'.,7<%
>% A!on" the s$bsets that incl$de ith ite! then the val$e of opti!al s$bset is
KiAK;i'.,7'2i<%
)ro! this we can derive two e#$ations or for!$lae to calc$late they are "iven belowF
K ;i,7< 1 8 +a, 8K;i'.,7<, KiAK;i'., 7'2i<, if 7'2iL/9 '''''''''''''' (.
K ;i'., 7<, if 7'2i0/9 '''''''''''''' (>
Then,
K ;/, 7< 1/ for 7L/ and K ;i, /< 1 / for i L/
Algorit3m:
Algorit"# =na;ack-i, j. // i, j re;reent t"e !+ ;ro+le#
if V[i, j] ( % // #eaning not already calc!lated
if j ( 5eig"t[i] t"en
val!e =na;ack-i/1, j.
ele
val!e #a$-=na;ack-i/1, j., Val!e[i] ' =na;ack-i/1, j/5eig"t[i].
V[i, j] val!e // ;!t valid val!e in t"e ta+le for +ot" cae
ret!rn V[i, j]
This sol$tion will therefore r$n in ti!e and space% Additionally, if
we $se only a .'di!ensional array to store the c$rrent opti!al val$es and pass over
this array ti!es, rewritin" fro! to every ti!e, we "et the sa!e res$lt for
only space%
Prepared By
Mrs.A.Subathra Devi AP / MCA
14
AA #%VER&%T: P#E&T%!&
Part N A
.% Define Dyna!ic pro"ra!!in"% ;=ef% No%F .<
>% 2hat is transitive clos$reW (tate the al"orith! $sed to find transitive clos$reW
;=ef%No%FT<
?% 2rite the proced$re to find the transitive clos$re of a directed "raph $sin" warshall3s
al"orith!% ;=ef% No%F .><
@% 2hat is principal of opti!alityW ;=ef% No%F .X<
I% 2hat is the i!portance of !e!ory f$nctions in dyna!ic pro"ra!!in"W ;=ef% No%F >><
J% Eow !any binary search trees can be for!ed with Vn3 &eysW ;=ef% No%F >J<
Part ' L
.% Eow to co!p$te Bino!ial coefficient% Disc$ss its space and ti!e co!ple,ity with Dyna!ic
pro"ra!!in"% Dive e,a!ple% ;=ef% No%F .<
>% Disc$ss abo$t 2arshall3s al"orith! in detail% ;=ef% No%F ><
?% E,plain how floyd3s al"orith! is $sed in findin" the shortest path% Dive e,a!ple%
;=ef% No%F ?<
@% 2rite a s$itable e,a!ple e,plain 5pti!al Binary (earch Trees proble! $sin" dyna!ic
pro"ra!!in" al"orith!% ;=ef% No%F @<
I% Describe briefly abo$t +e!ory f$nctions% ;=ef% No%F J<
J % E,plain the &napsac& proble!% Dive relevant e,a!ple% ;=ef% No%F I<
Prepared By
Mrs.A.Subathra Devi AP / MCA