Vous êtes sur la page 1sur 14

1

Mailam Engineering College


(Approved by AICTE, New Delhi, Affiliated to Anna University, Chennai
& Accredited by National Board of Accreditation (NBA, New Delhi
Mailam (Po), Villupuram (Dt). Pin: 604 304
DEPARTMET !" C!MP#TER APP$%CAT%!&
DESIGN AND ANALYSIS OF ALGORITHMS MC9223
Part ' A
(. De)ine D*nami+ programming. ,!V -(( . !V /(01
Dyna!ic pro"ra!!in" is a techni#$e for solvin" proble!s with overlappin" s$b
proble!s% Dyna!ic pro"ra!!in" is an al"orith! desi"n !ethod, where the sol$tion to the
!ain proble! can be viewed as the res$lt of a se#$ence of decisions% It avoids the
calc$lation of sa!e st$ff twice, by &eepin" the table of &nown res$lts of s$b'proble!s%
0. 23at are t3e 4a5i+ element5 o) 6*nami+ programming7
($b str$ct$re
Table str$ct$re
Botto! $p co!p$tations%
3. 23at are t3e appli+ation5 o) 6*nami+ programming7
)ibonacci n$!bers
*on"est increasin" se#$ence
+ini!$! wei"ht trian"$lation
The partition proble!
Appropriate strin" !atchin"
4. 23at are t3e 5tep5 in8ol8e6 in 6*nami+ programming7
Develop a !athe!atical relation that can e,press any sol$tion and s$b sol$tions for
proble!%
-rove the principle of opti!ality%
Develop a rec$rrence relation that is a sol$tion to its s$b sol$tion $sin" !athe!atical
notation in step .%
9. 23at i5 4inomial +oe))i+ient7
It is denoted by C(n,& is the n$!ber of co!binations ( s$bsets of & ele!ents fro!
an n'ele!ent set( / 01 & 01n
6. 23at i5 2ar53all-5 algorit3m7
2arshall3s al"orith! is $sed to find the transitive clos$re of a "iven directed "raph%
The transitive clos$re of a directed "raph with n vertices can be defined as the n by n
Prepared By
Mrs.A.Subathra Devi AP / MCA
#%T %%% D:AM%C PR!;RAMM%;

Co!p$tin" a bino!ial coefficient 4 2arshall3s and )loyd3 al"orith! 4 5pti!al
binary search tree 4 6napsac& proble! 4 +e!ory f$nctions%
2
!atri,% In which the ele!ent in the ith row and 7th col$!n is . if there e,ists a nontrivial
directed path fro! the ith verte, to the 7th verte,% 5therwise /%
<. 23at i5 an a6=a+en+* matri>7
The ad7acency !atri, A1 8 ai7 9 of a directed "raph is the Boolean !atri, that has .
I its ith row and 7th col$!n if and only if there is a directed ed"e fro! the ith verte, to the
7th verte,%
?. 23at i5 tran5iti8e +lo5ure7 &tate t3e algorit3m u5e6 to )in6 tran5iti8e +lo5ure7
,!V /(01
The transitive clos$re of a directed "raph with n vertices can be defined as the n by n
Boolean !atri, T18 ti79, in which the ele!ent in the ith row (.01i01n and the 7th col$!n
(.01701n is . if there e,ists a non'trivial directed path fro! the ith verte, to the 7th
verte,, otherwise ti7 is /%
@. 23at i5 "lo*6-5 algorit3m7
)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% The distant !atri, "ives the wei"ht of ed"es for ad7acent vertices%
(0. 23at Are t3e Con6ition5 %n8ol8e6 %n T3e "lo*6-5 Algorit3m7
Constr$ct the ad7acency !atri,%
(et the dia"onal ele!ents to :ero
A&;i,7<1!in A&'.;i,7<
A&'.;i,&<and A&'.;&,7<
((. 2rite t3e Pro+e6ure "or T3e "lo*6-5 Algorit3m.
Floyd [var A: array [1. n, 1n] of real.
Var C: array [1. n, 1. n] of real]
Var i,j,k:integer
Begin
For i=1 to n do
For j=1 to n do
A [i,j]=C[i,j] //contr!cting t"e adjacency #atri$//
For i=1 to n do
A [i,i]=%
For k=1 to n do
For i=1 to n do
For j=1 to n do
&f A [i, k]'A [k, j](A [i, j] t"en
A [i, j]=A [i, k]'A [k, j])
*nd
(0. 2rite t3e pro+e6ure to )in6 t3e tran5iti8e +lo5ure o) a 6ire+te6 grap3 u5ing
Aar53all-5 algorit3m. ,B# /((1
2arshall(A;.%%n,.%%n<
=
(/
1A
)or &1. to n do
)or I1. to n do
)or 71. to n do
=
(&
;I,7< 1 =
(&'.
;I,7< or =
(&'.
;I,&< and =
(&'.
;&,7<
Prepared By
Mrs.A.Subathra Devi AP / MCA
3
=et$rn =
(n
(3. 23at i5 meant 4* optimal 5olution7
A sol$tion to an opti!i:ation proble! which !ini!i:es the ob7ective f$nction is
ter!ed as an opti!al sol$tion% An opti!i:ation proble! is a co!p$tational proble! in which
the ob7ect is to find the best of all possible sol$tions% +ore for!ally, find a sol$tion in the
feasible re"ion which has the !ini!$! val$e of the ob7ective f$nction%
(4. "in6 t3e num4er o) +ompari5on5 ma6e 4* t3e 5eCuential 5ear+3 in t3e Aor5t
+a5e an6 t3e 4e5t +a5e.
The n$!ber of co!parisons !ade by the se#$ential search in the worst and best
case are one and n &ey co!parisons respectively%
(9. 23at i5 t3e 6i))eren+e 4etAeen 6*nami+ programming an6 6i8i6e an6
+onCuer7
D*nami+ Programming Di8i6e An6 ConCuer
.% Divide the "iven proble! into !any s$b'
proble!s% )ind the individ$al sol$tions and
Co!bine the! to "et the sol$tion for the
!ain proble!%
>% )ollows top down techni#$e%
?% (plit the inp$t only at specific points%
@% Each proble! is independent%
.% +any decisions and se#$ences are
"$aranteed and all the overlappin" s$b'
instances are considered%
>% )ollows botto! $p techni#$e%
?% (plit the inp$t at every possible point
rather than at a partic$lar point%
@% ($b'proble!s are dependent on the !ain
proble!%
(6. De)ine 5u4 5tru+ture, ta4le 5tru+ture an6 4ottom up +omputation5.
A proble! is said to have opti!al s$bstr$ct$re if the "lobally opti!al sol$tion can be
constr$cted fro! locally opti!al sol$tions to s$b proble!s% 2e can either b$ild $p sol$tions
of s$b'proble!s fro! s!all to lar"e (botto! $p or we can save res$lts of sol$tions of s$b'
proble!s in a table (top down A !e!ori:ation%This for!s the table str$ct$re%
(<. De)ine multi 5tage grap3.
A !$ltista"e "raph is a "raph
;D(V,E) with V partitioned into E FD 0 dis7oint s$bsets s$ch that if (a,4) is in E,
then a is in Vi , and 4 is in ViG( for so!e s$bsets in the partitionB
and H V( H D H VE H D (%
(?. De)ine Verte>.
The verte, 5 in V( is called the so$rceB the verte, t in VE is called the sin&% ; is
$s$ally ass$!ed to be a Aeig3te6 grap3% The cost of a path fro! node v to node w is s$!
of the costs of ed"es in the path% The C!$ltista"e "raph proble!C is to find the !ini!$!
cost path fro! s to t% Each set Vi is called a sta"e in the "raph%
(@. 23at i5 prin+ipal o) optimalit*7 ,!V -(( . B# /(01
Prepared By
Mrs.A.Subathra Devi AP / MCA
4
It states that in an opti!al se#$ence of decisions, each s$b se#$ence !$st be
opti!al% To $se dyna!ic pro"ra!!in" the proble! !$st observe the principle of opti!ality
that whatever the initial state is, re!ainin" decisions !$st be opti!al with re"ard the state
followin" fro! the first decision%
00. Deri8e t3e )ormula )or t3e "lo*6-5 algorit3m an6 running time o) t3e algorit3m.
for &0''. to n do
for i0''. to n do
for 70''. to n do
D;i,7<1!in8;i,7<,D;i,&<AD;&,7<9
=UNNIND TI+E 5) TEE A*D5=ITE+F 5(nG?
0(. 23at are t3e pre+on6ition5 )or t3e "lo*6-5 algorit3m7
The precondition for the )loyd3s al"orith! is that the "raph sho$ld not have a cycle
of ne"ative len"th or cost%
00. 23at i5 t3e importan+e o) memor* )un+tion5 in 6*nami+ programming7 ,B#
-(0, !V -(0 . /(01
+e!ory f$nctions co!bine the stren"th of top'down and botto!'$p approaches% The
"oal is to "et a !ethod that solves only s$b proble!s that are necessary and does it only
once% ($ch a !ethod is based on $sin" !e!ory f$nctions%
03. ;i8e t3e re+urren+e )ormula an6 initial +on6ition )or )in6ing 4inomial
+oe))i+ient.
& i'. n &
A(n, & 1 H H . A H H .
i 1. 7 1. i 1 &A. 7 1 .
04. 23at i5 t3e optimal 5olution o) 0I( Jnap5a+J pro4lem7
The &napsac& proble! or r$c&sac& proble! is a proble! in co!binatorial
opti!i:ation% Diven n ite!s of &nown wei"hts w.,w>''''wn and val$es v.''''vn and a
&napsac& of capacity w, )ind the !ost val$able s$bset of the ite!s that fit in to the
&napsac& ie, find a s$bset of the lar"est val$e a!on" the!%
09. 23at i5 root ta4le7
The root table is a table which has / to n col$!ns and . to nA . row% The root val$e
is "iven by = ;., n<% The & val$es the entries in the !ain table is entered into the root table%
06. KoA man* 4inar* 5ear+3 tree5 +an 4e )orme6 Ait3 /n- Je*57 ,B# /((1
*et $s ta&e a tree with I nodes (n1I%
Prepared By
Mrs.A.Subathra Devi AP / MCA
5
It will have only J (ie,IA. n$ll branches% In "eneral, a +inary tree ,it" n node "a
e$actly n+1 n!ll node.
0<. 2rite t3e Criterion )or an optimal tree7
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%
0?. 2rite t3e pro+e6ure )or Linomial Coe))i+ient7
Algorit"# Bino#ial-n, k.
for i % to n do // fill o!t t"e ta+le ro, ,ie
for i = % to #in-i, k. do
if j==% or j==i t"en C[i, j] 1 // &C
ele C[i, j] C[i/1, j/1] ' C[i/1, j] // rec!rive relation
ret!rn C[n, k]
0@. 2rite t3e time E))i+ien+* o) !L&T7
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%
30. KoA to +al+ulate t3e Jnap5a+J u5ing 6*nami+ programming7
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/
3(. %n t3e gi8en 4inar* tree, u5ing arra*, at A3i+3 lo+ation +an *ou 5tore t3e no6e
47
Answer: At location J
Explanation:
2here *Cn !eans *eft Child of node n and =Cn !eans =i"ht Child of node n
Prepared By
Mrs.A.Subathra Devi AP / MCA
6
Part ' L
(. KoA to +ompute Linomial +oe))i+ient. Di5+u55 it5 5pa+e an6 time +omple>it*
Ait3 D*nami+ programming. ;i8e e>ample. ,!V -(0 . B# /((1
Co!p$tin" bino!ial coefficients is non opti!i:ation proble! b$t can be solved $sin"
dyna!ic pro"ra!!in"% Bino!ial coefficients are represented by C(n, k or (
n
k and can be
$sed to represent the coefficients of a bino!ialF
(a A +
n
1 C(n, /a
n
A %%% A C(n, ka
n'k
+
k
A %%% A C(n, n+
n


The rec$rsive relation is defined by the prior power
C(n, k 1 C(n'., k'. A C(n'., k for n L k L /
IC C(n, / 1 C(n, n 1 .

Dyna!ic al"orith! constr$cts a n,k table, with the first col$!n and dia"onal filled o$t $sin"
the IC% Constr$ct the tableF
The table is then filled o$t iteratively, row by row $sin" the rec$rsive relation%

Algorit3m:
Algorit"# Bino#ial-n, k.
for i % to n do // fill o!t t"e ta+le ro, ,ie
for i = % to #in-i, k. do
if j==% or j==i t"en C[i, j] 1 // &C
ele C[i, j] C[i/1, j/1] ' C[i/1, j] // rec!rive relation
ret!rn C[n, k]

The cost of the al"orith! is filin" o$t the table% Addition is the basic operation%
Beca$se k M n, the s$! needs to be split into two parts beca$se only the half the table
needs to be filled o$t for i 0 k and re!ainin" part of the table is filled o$t across the entire
row%

A(n, k 1 s$! for $pper trian"le A s$! for the lower rectan"le
1 Hi1.
k
Hj1.
i'.
. A Hi1.
n
Hj1.
k
.
1 Hi1.
k
(i'. A Hi1.
n
k
Prepared By
Mrs.A.Subathra Devi AP / MCA
7
1 (k'.kN> A k(n'k O P(nk
0. Di5+u55 a4out 2ar53all-5 algorit3m in 6etail. ,!V -(( . !V /(01
2arshallQs al"orith! deter!ines whether there is a path between any two nodes in
the "raph% It does not "ive the n$!ber of the paths between two nodes% Co!p$te all paths
containin" node ., then all paths containin" nodes . or > or . and >, and so on, $ntil we
co!p$te all paths with inter!ediate nodes selected fro! the set 8., >, R n9%
2arshallQs al"orith! $ses the ad7acency !atri, to find the transitive clos$re of a
directed "raph%
2arshall3s al"orith! is $sed to find the transitive clos$re of a "iven directed "raph%
The transitive clos$re of a directed "raph with n vertices can be defined as the n by n
!atri,% In which the ele!ent in the ith row and 7th col$!n is . if there e,ists a nontrivial
directed path fro! the ith verte, to the 7th verte,% 5therwise /%
Tran5iti8e +lo5ure:
The transitive clos$re of a directed "raph with n vertices can be defined as the n'by'
n boolean !atri, 2, in which the ele!ent in the ith row and jth col$!n is . if there e,ist a
directed path fro! the ith verte, to the jth verte,, otherwise it is :ero%
Transitive Clos$re can be solved by "raph transversal for each verte, in the "raph% If
a verte, is reached then the correspondin" !atri, ele!ent is filled with .%
Ass$!in" that the "raph was represented by an ad7acency !atri, then the cost is
P(n
?
where n is the n$!ber of vertices in the "raph% )or a sparse "raph, ad7acency list is
!ore appropriate, then the cost is P((nA#n%
2ar53allM5 Algorit3m !utline an6 Corre+tne55:
2arshallQs al"orith! calc$lates the transitive clos$re by "eneratin" a se#$ence of n
!atrices, where n is the n$!ber of vertices%

3
(/
, %%%, 3
(k'.
, 3
(k
, %%% , 3
(n

=ecall that a path in a si!ple "raph can be defined by a se#$ence of vertices%

The definition of the ele!ent at the ith row and jth col$!n in the kth !atri, (3
(k
,
rij
(k
is one if and only if there e,ist a path fro! vi to vj s$ch that all the inter!ediate verte,,
,4 is a!on" the first k vertices, ie% . M 4 M k%

The 3
(/
!atri, represent paths witho$t any inter!ediate vertices, so it is the
ad7acency !atri,% The 3
(n
!atri, has ones if there is a path between the vertices with
inter!ediate vertices fro! any of the n vertices of the "raph, so it is the transitive clos$re%

Consider the case rij
(k
is one and rij
(k'.
1 /% This can occ$r only if that there is an
inter!ediate path thro$"h vk fro! fro! vi to vj% +ore specifically the list of vertices has the
for!

vi, ,4 (where . M 4 0 k, vk% ,4 (where . M 4 0 k, vj
Prepared By
Mrs.A.Subathra Devi AP / MCA
8
This can happen only if rik
(k'.
1 rkj
(k'.
1 .% Note the k s$bscript

If rij
(k'.
1 . then rij
(k
sho$ld be one%

rij
(k
1 rij
(k'.
or (rik
(k'.
an6 rkj
(k'.


&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

Vous aimerez peut-être aussi