Mailam Engineering College: Unit-III (2 & 16 Marks)
Mailam Engineering College: Unit-III (2 & 16 Marks)
Mailam Engineering College: Unit-III (2 & 16 Marks)
(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 DEPARTME T !" C!MP#TER APP$%CAT%! &
&u'(e)t
&u'(e)t Co-e: MC0122 3ear: 1021 4 1023 &eme,ter: %% Prepare- 5.: Mr,. A. &u'at*ra De6i, MCA, M.P*il.,
&u'(e)t %n)*arge
+!D
Prin)ipal
Mailam (Po), Villupuram (Dt). Pin: 604 304 DEPARTME T !" C!MP#TER APP$%CAT%! &
DESIGN AND ANALYSIS OF ALGORITHMS MC9223
# %T %%%
D3 AM%C PR!;RAMM% ;
Co!p$tin" a bino!ial coefficient 2 3arshall4s and )loyd4 al"orith! 2 5pti!al binary search tree 2 6napsac& proble! 2 +e!ory f$nctions%
Part 4 A 2. 7*at i, -.nami) programming8 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% 1. 7*at are t*e 'a,i) element, o/ -.nami) programming8 ($b str$ct$re Table str$ct$re Botto! $p co!p$tations% 3. 7*at are t*e appli)ation, o/ -.nami) programming8 )ibonacci n$!bers *on"est increasin" se#$ence +ini!$! wei"ht trian"$lation The partition proble! Appropriate strin" !atchin" 4. 7*at are t*e ,tep, in6ol6e- in -.nami) programming8 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. 7*at i, 'inomial )oe//i)ient8 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. 7*at i, 7ar,*all:, algorit*m8
3arshall4s 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 /% <. 7*at i, an a-(a)en). matri=8 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,% >. 7*at i, Tran,iti6e )lo,ure8 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 /% 0. 7*at i, "lo.-:, algorit*m8 It deals with all pair shortest path proble! to find the distance fro! each verte, to all other vertices% 20. 7*at Are t*e Con-ition, %n6ol6e- %n T*e "lo.-:, Algorit*m8 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< 22. 7rite t*e Pro)e-ure "or T*e "lo.-:, Algorit*m. Pro)e-ure "lo.- ?6ar A: arra. ?2@. n, 2@nA o/ real. =ar C> array ;.?% n, .?% n< of real< =ar i,7,&>inte"er Be"in )or i1. to n do )or 71. to n do A ;i,7<1C;i,7< @@constr$ctin" the ad7acency !atri,@@ )or i1. to n do A ;i,i<1/ )or &1. to n do )or i1. to n do )or 71. to n do If A [i, k]+A [k, j]<A [i, j] then End A ;i, 7<1A ;i, &<AA ;&, 7<B
21. 7rite t*e pro)e-ure to /in- t*e tran,iti6e )lo,ure o/ a -ire)te- grap* u,ing Bar,*all:, algorit*m. 3arshall(A;.%%n,.%%n< C(/ 1A )or &1. to n do )or I1. to n do
)or 71. to n do C(& ;I,7< 1 C(&'. ;I,7< or C(&'. ;I,&< and C(&'. ;&,7< Cet$rn C(n 23. 7rite t*e pro)e-ure to /in- t*e ,*orte,t pat* u,ing "lo.-:, algorit*m. D13 )or &1. to n do )or I1. to n do )or 71. to n do D;I,7<1!in( D;I,7<,D;I,&< A D ;&,7< Cet$rn D 24. 7*at i, meant '. optimal ,olution8 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% 29. "in- t*e num'er o/ )ompari,on, ma-e '. t*e ,eCuential ,ear)* in t*e Bor,t )a,e an- t*e 'e,t )a,e. 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%
26. 7*at i, t*e -i//eren)e 'etBeen -.nami) programming an- -i6i-e an- )onCuer8 D.nami) Programming Di6i-e An- 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!% D% )ollows top down techni#$e% E% (plit the inp$t only at specific points% F% Each proble! is independent%
.% +any decisions and se#$ences are "$aranteed and all the overlappin" s$b' instances are considered%
D% )ollows botto! $p techni#$e% E% (plit the inp$t at every possible point rather than at a partic$lar point% F% ($b'proble!s are dependent on the !ain proble!%
;D(V,E) with V partitioned into E FD 1 dis7oint s$bsets s$ch that if (a,') is in E, then a is in Vi , and ' is in ViG2 for so!e s$bsets in the partitionB and H V2 H D H VE H D 2%
20. De/ine Verte=. The verte, , in V2 is called the so$rceB the verte, t in VE is called the sin&% ; is $s$ally ass$!ed to be a Beig*te- grap*% The cost of a path fro! node v to node w is s$! of the costs of ed"es in the path% The G!$ltista"e "raph proble!G is to find the !ini!$! cost path fro! s to t% Each set Vi is called a sta"e in the "raph% 10. De/ine t*e prin)iple o/ optimalit.. 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% 12. Deri6e t*e /ormula /or t*e "lo.-:, algorit*m an- running time o/ t*e algorit*m. 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 CUNNINH TI+E 5) TIE A*H5CITI+> 5(nJE 11. 7*at are t*e pre)on-ition, /or t*e "lo.-:, algorit*m8 The precondition for the )loyd4s al"orith! is that the "raph sho$ld not have a cycle of ne"ative len"th or cost%
13. De/inition tran,iti6e )lo,ure The transitive clos$re of a directed "raph with n vertices can be defined as the nxn !atri, T = {tij}, in which the ele!ent in the ith row (1 i n) and the jth col$!n (1 j n) is . if there e,ists a nontrivial directed path (i%e%, a directed path of a positive len"th fro! the ith verte, to the 7th verte,B otherwise, tij is /% 14. 7*at -o .ou mean '. Promi,ing an- onI Promi,ing no-e8 A node in a state'space tree is said to be pro!isin" if it corresponds to a partially constr$cted sol$tion that !ay still lead to a co!plete sol$tionB otherwise it is called non' pro!isin"% 19. 7*at i, t*e importan)e o/ memor. /un)tion, in -.nami) programming8 +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% 16. ;i6e t*e re)urren)e /ormula an- initial )on-ition /or /in-ing 'inomial )oe//i)ient. & i'. n & A(n, & 1 K K .AK K . i 1. 7 1. i 1 &A. 7 1 . 1<. 7*at i, Enap,a)J pro'lem8 Hiven n ite!s of &nown wei"hts w.,wD''''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!%
The &napsac& proble! or r$c&sac& proble! is a proble! in co!binatorial opti!i:ation>
1>. 7*at i, root ta'le8 The root table is a table which has / to n col$!ns and . to nA . row% The root val$e is "iven by C ;., n<% The & val$es the entries in the !ain table is entered into the root table%
-art 2 B Computin a !inomia" Coeffi#ient 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 (nk and can be $sed to represent the coefficients of a bino!ial> (a A b n 1 C(n, / an A %%% A C(n, k an'kbk A %%% A C(n, n bn 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 table>
The table is then filled o$t iteratively, row by row $sin" the rec$rsive relation% Algorit*m Binomial(n, k /or i / to n -o @@ fill o$t the table row wise /or i 1 / to !in(i, k -o
i/ j11/ or j11i t*en C;i, j< . @@ IC el,e C;i, j< C;i'., j'.< A C;i'., j< @@ rec$rsive relation return 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 Ki1.k Kj1.i'. . A Ki1.n Kj1.k . 1 Ki1.k (i'. A Ki1.n k 1 (k'. k@D A k(n'k N O(nk 7ar,*allK, algorit*m 3arshallPs 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 D or . and D, and so on, $ntil we co!p$te all paths with inter!ediate nodes selected fro! the set 8., D, ? n9% Iere we co!p$te a se#$ence of !atrices -(/ , -(. ,?, -(n s$ch that -(/ 1 A, - 1 -, the path !atri,, and -(r shows all paths with inter!ediate nodes selected fro! the set of nodes 8., D, E, ?, r9%
(n
The al"orith! can be defined rec$rsively> Let P(0) = A. Let P(r) i a matrix !"h that# pi&(r 1 . if and only if there is a path connectin" nodes i and J thro$"h one or !ore of nodes 8., D, ?, r9 pi&(r 1 / otherwise Let P(r$1) i the matrix %here
pi&(rA. 1 . if and only if there is a path connectin" nodes i and J thro$"h one or !ore of nodes 8., D, ?, r, rA.9 pi&(rA. 1 / otherwise P(r$1) "an be obtaine& 'rom P(r) in the 'ollo%in( %a)# If pi&(r 1 . then pi&(rA. >1 . Else pi&(rA. >1 p
i rA. (r
Qp
(r rA. &
As a res$lt we have> pi&(rA. 1 . if and only if there is a path connectin" nodes i and J and containin" inter!ediate nodes selected fro! the set 8., D, E, %% rA.9 pi&(rA. 1 / otherwise%
E,a!ple>
Ti!e Co!ple,ity>