0% found this document useful (0 votes)
20 views36 pages

DAA-unit 4

jutuh r18 DAA

Uploaded by

thatgirlsathvika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
20 views36 pages

DAA-unit 4

jutuh r18 DAA

Uploaded by

thatgirlsathvika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 36
y [unrr tv] Dynamic Programming 5.1 General Method Important Points to Remember | * Dynamic programming is technique for solving | problems with overlapping sub-problems. * In this method each sub-problem is solved only | once. The result of each sub-problem is recorded in a table from which we can obtain a solution to the original problem. * Dynamic programming is typically applied to optimization problems. * Examples of the problems that can be solved | using dynamic programming are - finding | Binomial coefficient, optimal binary search tree | (OBST), 0/1 Knapsack problem, matrix chain | multiplication. | | | J Q.1 Write control abstraction (General Strategy Algorithm) of dynamic programming. =n A, Marks 3) Ans. : Dynamic programming design involves four ‘major steps : 1. Characterize the structure of optimal solution. ‘That means develop a mathematical notation that can express any solution and sub-solution for the given problem. 2 Recursively define the value of an optimal solution. 3. By using bottom up technique compute value of optimal solution. For that you have to develop a recurrence relation that relates a solution to its sub-solutions, using the mathematical notation of step 1. 4. Compute an optimal solution from computed information. Q2 What is the difference between divide and ‘conquer and dynamic programming ? ES [INTU : Part A, Marks 3] Ans. : Sr. Divide and conquer No. into small a These subproblems are solved independently. Finally all the solutions of subproblems are collected together to get the solution to the given problem. 2 In this method duplications in subsolutions are neglected. ie, duplicate subsolutions may be obtained. Divide and conquer is less efficient because of Tework on solutions. The divide and conquer ‘uses top down approach of problem solving (recursive methods). Divide and conquer | splits its input at specific deterministic | points usually in the middle. Q.3 What are the common steps in the Programming to solve any problem ? 1. The problem is divided solving (iterative method). Dynamic progr splits its input at e possible split points Tather than at a Particular point. trying all split point determines which Point is ESE [INTU : Part A, Mar ___ Dynamic Programming the value of ‘YD Optimal substructure - The dynamic 8n optimal Programming technique makes use of principle of optimality to find the optimal solution from. subproblems. 2) Overlapping subproblems - The dynamic Programming is a technique in which the Problem is divided into subproblems. The Solutions of subproblems are shared to get the final solution to the problem. It avoids’ repetition of work and we can get the solution more efficiently. Q7 What are the applications of |Dynamic Programming ? GP [INTU « Part A, Marks 2] ‘Ans. : Various applications that are implemented ol Problem we may using dynamic programming are - #Of solutions. From all those solutions | 1 Multi-stage graph mum solution and such an optimum | 2 Optimal binary search trees. the solution to the given problem. | 3. 0/1 Knapsack problem. Beepblem 3s solved only once. The | 4. All pair shortest path problem, Sie ct Tecorded in a table from | 5 Traveling salespensdn pace @biain a solution to the original | Retiabilty design “in dynamic programming many | ees are generated and all the instances are considered. Q8 Find a minimum cost path from ‘S' to in multistage graph using dynamic programming. Sar[INTU : Part B, Marks 5] of optimality. NGF [INTU : Part A, Nov.-16, Marks 3) programming algorithm obtains the Principle of optimality. ‘of optimality states that “in an ‘of decisions or choices, each also be optimal.” possible to apply the principle of ‘almost impossible to obtain the dynamic programming approach. ling of shortest path in a given inciy Himalit rinciple of optimality. di ‘Ans. : At stage 1 We will consider vertex 1 ie. 's' 3 o : ‘ ean wea ares 3) [At stage 2 We will consider vertices 2 and 3 At stage 3 We will consider vertices 4, 5 and 6 J. An up thrust for knowledge Devignand Anaiysisof Algoritims At stage 4 We will consider vertices 7 and 8 At stage 5 We will consider vertex 9 ie. 't! Now, using backward approach, let us compute d(s, *) as follows - d(s, t) = 4, t) = 4G, = min{5 + dQ, 8, 2 + d@, min(3 + d(4, t), 3+ 46, ) min(6 + d(4, t), 5 +d, 8), 8+d 61) min(l +d, t), 4+ d@, )} min(6 + d(7, t), 2 + d(@, )} min(6+ d(7, t), 2 + d(8, t)} -7 d(4, ) = dG, = d(6, t) = a7, 0) 46,0 (6,8) 46,0 dG, 5) 46,0 dq, ) a4) = 7 4,9 43,0) (2, t) = min(3 + 7, 3 + 5) 42, = 8 do, t) = » di, ) = 12 5-3-5-8-t ‘Thus we get the minimum cost path from s to t as =3 = min (6+7,2+3) =5 6-8-t Now, = min (6 +7,2+3) =5 5-8-t Now, = min (1+7,4+3) 4-8-t Now, = min(6 +7, 5+5,8+5) =10 3-5-8-t Now, 2-6-8-t Now, min (5 + 8,2 +10) Cost = 12 Fig. 8.2 Q.9 Solve the given multistage graph. ‘S&F [INTU : Part B, Marks 5] TT TECHNICAL PUBLICATIONS" An up thrust for knowiedge At stage 1 we will consider vertex 1. At stage 2 we will consider vertex 2, 3, 4. At stage 3 we will consider vertex 5, 6, 7. At stage 4 we will consider vertex 8, 9 At stage 5 we will consider vertex 10. Now using backward approach, let us complete d(110) ie. source to destination. (110) = d(2,10) = min{3+d(5, 10),6+4(6, 10),5+4(7, (3,10) = min+d(5, 10), 1+d(6, 10), 2+4(7, (4,10) = min{3+4(6, 10), 2+4(7, 10)} (5,10) = min +4(8, 10), 6+4(9, 10)} (6,10) = min {4+ 4(8, 10), 6+4(9, 10)} (7,10) = min{2+-4(8, 10),7+4(9, 10)} 4(8,10) = 6 4(9,10) = 5 + 4710) = min{2+4(8, 10), 7+4(9, 10)} = minQ2+6, 7+5} (7,10) = 8 +d (6,10) = min{4+a(s, 10), 6+ d(9, 10)} = minf4+6, 6+5} 4(6,10) = 10 + d(5,10) = min{6+d(2, 10),5+d(3,10), 4+4(4, min + d(8, 10), 6+4(9, 10)} = minf+6, 6+5} 4(5,10) = + Let fay, 9y---aq) be a set of identifiers 21032) Let pi) be the eae with which We can search for a and q(i) be the a Of searching element x such that a, <<< a,., and called backward reasonin, ° We optimal binary search . i The je can obtain method is as follows. i acne. shows that optimal binary search tree follows the Principle optimality. = . Q.11 Consider n = 4 and (@;, qo, a3. a4) = (do, c M, ht, wile. The satMM Sead abies on ‘Cost = 14 as p(l : 4) = (3, 3, 1, 1) and q(@ : 4) = 2, 3, 1, Fig. Q.9.2 (a) 1, 1). Construct the optimal binary search tree. RC A EGP LINTU : Part B, Marks 15, Dec.-09, Marks 8) cle approach. Ans. : Let, GP [INTU : Part B, Marks 5] Wit amin OG, Gr (G, stages, n, p) ot ~ + Got) * Pry + This algorithm is for backward nea ciel ge graph F a=.) Git * W* dae * Prony number of stages Wy > Wipes of vertices of G as Gym E Minimum value of C. We will select k = 2, as we obtain C value as minimum when k = 2. Hence Ry) = 2 then Con = Wor + Cog = 15 +7 = 22. Let us compute Cy, the range for k is Ry» = 2 to Rog = 3. When k= 2,i=1,j=3, Gs = Cu + Gy = 047 G3 = 7 > Minimum value When k = 3,i=1,j=3 Cia = Cig + Cay = 10+0 Ga = 10 [Design end Anctypis of lgeritome _ ‘As we obtain minimum value of C,3 with k = 2. Hence value of k becomes 2. Ry = 2 Cig = Wy + min ( Cys} = 13+7 Cy = 20 Now we will compute C4. i=2jr4 Value of k lies between Ry3 = 3 to Ryy = 4. When k=3,i=2andj=4 Cau = Cnt Coy =0+3 4 = 3 > Minimum value When k= 4,i=2,j=4 Cg = Cg Cy =7+0 Gy =7 Hence we set k = 3, Ry = 3 Cag = Wry + min (Czy) = 943 Cy = 2 To compute Cy i= 0, j= 3. k lies between Ry) = 2 to Ry = 2. ‘That means k = 2. Rog = 2. Cos = {Con +Co3} + Woy, = 7+74+18 Gs. = 32 To compute Cyy i= 1,j=4, K lies between Rys = 2 to Ryy = 3. When k= 2,i=1,j=4 Cu = Cu + Co = 0412 WF Tecunicas pusuicarions”- An up thrust for kromedge Dynamic Progra Cyq = 12 > Minimum value When k=3,i=1,j=4 Cag = Cin + Cou = 10+3 Cys 3 We will set k = 2 Ry = 2 Cig = Wag + min (Cia) 15 +12 Cy = 27 To compute Cog i=Oj-4 k lies between Rgg = 2 to Rig = 2 ‘That means k = 2 Ry =2 Sou = {Cor +Cau} + Woe Cop = (7 +12} +20 Coy = 39 The values are summarized as below. Let us build tree using Ry, @) ir As Rog = 2, ap will be root. 0. That means there is = 1 right child is right child to node = 0. That ‘Print’, For dis Roy = 4. That means a, = stop uld of node ‘print. The final OBST is Fig. 0.12.1 OBST elements a; < ap < a3 < ay = 8/16, ap = a3 = a = V6. Ps = Py = 1/16. ‘optimal binary search tree as a table of values Wy, Cy. Vy algorithm to compute the roots EGP [NTU : Part B, Marks 5] we will multiply the P; by 16. Hence p; and qj values =4 211) = 43111) es of W first. For Wi, Was = 1 Wa = 4 Wa =1 Compute Wi; using formula Wit = G+ +1 * Pist Wo = Wot = Hence, qotatp, =4+3+4 i Wai = glities tenis Se Wp = 6 Was = Wo ='3 @tatPs =1+1+1 Woe = 3+ 44+ Pg = 14141 Wy = 3 For computing Wy we will use following formula fy ~ Wy-1* B+ Gy = Wo+pota =1+2+1 = = Watpsta, 64141 -8 = Was + Pat =3+1+1 =5 Now, = Wor * Ps +45 = tied Wos = 16 Woe = Wos + Pa * 4 = 16+1+1 q Wor = 18 To summarize W values. To compute C and r values we will use Cy = 0 and nO. Hence Co = 0 a Cu = 0 0 mm = 0 o Cy = 0 T= 0 To compute C;,, and r;,, we will use following formulae me sat Gist G+ G61 * Piet Hence ty = 1 ; Con = G0 + 4 + Pr = 44+3+4 Cy =n ty =2 Ca-ht+etp = 341+2 Cn = 6 ty = 3 a iy 92495 Py = 1+14+1 Cy 3 ty 4 Cy = Bt aytps = 14141 Cy = 3 To compute Cy and ry we will compute the k first. The value of k lies between values of x yy The min {C,_1 +Cy) decides value of k. Consider i= 0 and j=2 To decide value of k, the range for k is roy = 1 ty," 2 when k = 1 and i=0,j=2. We will Cy using formula Gy ~ eit Sy Co = Con + Con Cog = 6 — Minimum value of Cyp. When k= 2, and i=0,j=2. Ce = Co + Ce Cy = 1+0 when k = 1. Hence the value of k becomes 1, As yak tg =1 Gy = Wy + min IC; + y) Con = Won + Coe Cg = 14+6=20 Now for 13 and C,3 k is between 1) = 2 to r3 = 3 then when k =2 ix1,j=3. Gis = Cr + Cy =0+3 For Wek =2 wd 04, j03, a" Carly “Nes Gan Hence vie oi 1 get 8d y= WgtCy = bon G@-2 Now ot sd Cy Vile oki etme 1g #280 ty = 3 When k= 2 and i= 1,j= 4 then Ca Cy 4 Gy =0+8 Cig = 8 + Minions vale of Cy When k=3 and i=1, j= 4 then Cu CC = 643 Syed Hence value of k=2 Ty 72 md C= Wy t Cy = 10+8 Cu 8 Now for computing ty and Cog ‘Value ofk is between 195 =1 to r5g=2 then, When k= 1 andi=0,j=4 Cu = Caps Cy =0+8 Cy = 18 - Miniomam vale of Cy, When k= 2 and i=0,j=4 To build the OBST 4 = 1 Hence a, becomes root node @ tu To compute children of a; we will apply following formula For node ry = k then | left child is ry. and right child is ry Hence for to = 1, for node a, i= 0,j=4and k=1, | Left child = t99 = 0. That is empty node. (oa) (4) Right child = 14 = 2. That means ay is right child of 4, FF reci0acas Puouscarions”- Anup tas tr howteign For node ry = 2 for node ay ist j=4andk=2 Left child = ry, =0. That is empty node. Right child = rp, = 3. That means ay is right pay © to! (3) (41 (@3) (41 For node rz = 3 ie. for node a3 i=2j-4andk=3. Left child = r= 0. That means no left Right child = rz, = 4. That means a, is right node a3. Fig. 0.13.1 OBST is the required OBST. 2.14 You are given a list of words, W1, W2, ~- Wn and their corresponding probabil occurrence pl, p2, p3, ... pn. The problem arrange these words in-a binary search tree way that minimizes the expected total time. Suggest a good algorithm to impl Also prove the complexity of the algorithm by you. GS [INTU : Part B, April-11, Ans. : Algorithm We(p,q,W,n) Jp is an input array [1..n] //qis an input array [0..n] a.W,C,x) first two rows //eal for algortinm //Min Value value of C, is obtained for Ig value of k ] + Clie-1) + CIsl (Crd ;//print values of Cy 24% | tid) irene Prog = (mCriij-1} to rli+1J)) do Comitcimine minimum then minimum < Cli.m-1]+Clm4J); kem; } : retum ky//This k gives minimum value of C + Following algorithm is used for creating the tree Ty Algorithm build tree(t,a,ij) { T new(node); //allocate memory for a new node ke riijh; T-> data < alk}; T->left=build_tree(r,a,ik-1); ‘T->right=build_tree(r,a,)j); , In order to obtain the optimal binary search tree we will follow : 1. Compute the weight using Wt function. 2. Compute Cy and ry using OBST. 3. Build the tree using build_tree function. The optimal binary search tree for a set of n keys for all the keys that are equally likely to be searched for can be constructed as follows - 1. Compute the weight using Wt function. 2. Compute Cy and ry using OBST algorithm. 3, Build the tree in such a way that average depth of its nodes can be minimized. ‘The average number of comparisons in the tree can be denoted by the cost of the tree, to find a key for given density function. Following formula can be used to find average number of comparisons - Chi) = i< MP W, is true hence we will % W)) ie. (3, 5) from S*. and si) Will find tuple denoting weight 6. It is we will set x3=1. Now (6-5, 6-4) = (1,2). The pair (1, 2) but it is originated in S'. he solution x, =1 and x3 =1. ® following 0/1 Knapsack problem caring P= (11, 21, 31, 39), } 15), C = 40,n = 4. EGP [ONTU : Part B, May-13, Marks 8] | (11, 21, 31, 33) STIONS”- An up thrust for Knowtedge ae reer repens C= 40, na=4 ‘We will compute the subsequences for the solutions S° = {0,0} St = {01,29} S! = {0,0),(11,2)} ; S{ = {Select next (P, W)pair and add it with st} St = {(21,11),(21+0, 11+0),(21+11,11+2)} = {21,11),(21,11),(32,13)} St = {(21,11),(32,13)} S* ~ {Merge candidates from $1 andS}} = {(0,0),(21,2),(21,11),(32,13)} Sj = {Select next (P, W) pair and add it with s?} Sf = {(31,22),(42, 24), (52, 33), (63, 35)} S* = {Merge candidates from S?and si} = [@.0),(11, 2),(21,11), (32,13), {oe (42, 24), (52, 33), (63, sal S} = {Select next (P, W) pair and addit with s3} __ [(23.18), (44,17), 64, 26), (65, 28), (64, 37), (75, 39), (85, 48), (96,50) } St = {Merge candidates from SS and s3}} (00),(41,2), (21,11), (32,13), (31,22), (42, 24), = }(©2,33),(65, 38), (33,15), (44,17) (54, 26), (65,28), (64, 37),(75, 39), (85, 48), (96,50) But by purging rule we must eliminate because (8%) = (6.95 (Pe Wi) = (83,15) and w, > Ww, Similarly, (62,33), (42,24) and (31, 22) can be eliminated. (63, 35) (0.0), (11,2), (21,12), 92,13), ((4417),(38,15) (54,26), (65,28), ((64.37).(75,39),(85,48),(96,50) With C = 40, we search for the tuy weight 40. aie iple containing There is no such tuple but we find (75, Design and Analyeis of Algorithms 5-17 39). The is just close the capacity. The tuple (75, 39) € S* (Note that it was not present in S°) Hence set, xe + (75-33),(39-15)) = (42, 24) The tuple (42, 24) € S° (Note that is was not present in S?), Hence set x» » ((42-31), (24-22) = (11, 2) The tuple (1, 2) is present in S* but it originally belongs to S!. Hence set x, = 1 ‘Thus the solution is {1, 0,1, 1}, Q.19 Find optimal solution for 0/1 Knapsack problem (w;,W2,wW3,wW4) = (10, 15, 6, 9), (1 Pz, Ps, Pa) = (2,5,8,1) and m = 30. ‘S&P [INTU : Part B, Marks 5) ‘Ans. : Given that, (Wy,W2,W3,W4) = (10, 15, 6, 9) (P1/P2/P3/P4) = (2 5, 8, 1) m = 30 n-4 We will compute the subsequences for the solutions 8° = (0,0) sf = (@ 10) S! = {(0, 0), (2, 10); St = (5, 15), (7, 25)) S? = ((0, 0), (2, 10), (5, 15), (7, 25)}; S} = ((8, 6), (10, 16), (13, 21), (15, 31)) S? = {(0, 0), 2 10), (5, 15), (7, 25), (8, 6), (10, 16), (13, 21), (15, 31)) But by purging rule we must eliminate (7, 25) because (pj, w;) = (7, 25) and (py, wy) = (8, 6). As Pj SPx but w; >wy, Similarly (2, 10) and (5, 15) can also be eliminated by purging rule. S° = ((0, 0), (8, 6), (10, 16), (13, 21), (15, 31) 9 TECHHICAL PUBLICATIONS”. An wp trust for hrowodge Dynamic P= (C1, 9, (, 15), (11, 25), (04, (16, 40)) = 100, 0) (8, 6), (1, 9), (9, 15), (AN, (14, 30), (16, 40)) Note that we have to eliminate pairs (10, 16), and (15, 31). With m = 30, we will search for tuple value 30. A pair (14, 30) is present in S* xed + (14 = 1), 0 ~ 9) = (13, 21). We will search pair (13, 21) and it is present in $°. 1 Xgl. + (13 = 8), (21 ~ 6)) = (5, 15). We will search pair (5, 15) and ite S*, Hence sét x, = 1 4 (B= 5), (15 = 15) = (0, 0) Thus we obtain the optimal (&1/X2/%3/%4) # (0, 1, 1, 1) solu 2.20 Consider - n= 3, (wy, We, w; (1, 2, 4) and m Find optimal solution for the given data. EGP [INTU : Part B, May-09, ‘Ans. : Given that - = @ 3, 3), wroe Initially 8° = (0,0) ° = (a, 2)) (0, 0), (1, 2)) St = ((2, 3), (240,340), (2+1,3 SI! = ((2, 3), (3, 5) S? = (Merge candidates from 5! sl = S? = (0, 0), (1, 2), (2, 3), (3, 5)) St = ((4, 3), (5, 5), (6, 6), (7, 8) gandidates from $? ang st) Dy (1, 2. 9. 8), (4 9), (6 6), (% 8)) F eliminate (8, 5) similarly ¢, because m = 6 a” G2) @ 9, 9. 6,5), tuple (6, 6) € S°. Hence t solve knapsack programming, GAP LINTU : Part B, Marks 5) Ziaumber objects for // knapsack 1/ profits of objects // weights of objects // Knapsack capacity lution indicated by Os and 18 // for storing the tuples (pw) 71 koops track of location? in 0) ‘/inoxt free spot in array H/ start index of Si-1) ~ fend index of S(-1) IJ index of object to be added Fg neat object co rupree nt!) | ersing 50-1) at fits ob) Pairl1.p=paicia buterioimt, (Bianing at end of 519) start=1; end: Dufferit|=nexe= ans (ft rope through S{i-1) 1 Return index of lazgest tuple in Si) that I) S00 J] not overfow the knapsack up=Big 1 (pair,w1,start.end i Total_wt); / Add object to next tuple in S(i-1) to form 1/ (now _proft.new vt) // and merge S{i-1) tuples into S(i) for (j=start; }<=up; j++) { new_profit=pairlj].p+ P1lil; new_wt=pair{jj.w+ wilil; /(Copy the tuples in S(i-1) with less weight than /foow /feuple. while ((k<=end) AND (pair{kl.w new_profit) new_profit=pair[k!.p; kets , //s new_profit > profit of previous tuple, insert //oew_profit and new_wt if(new_profit > pair[next-1}.p) then { pair[next],p=new_profit; pair{next]. w=new_wt; LUSH nd Meats of Algorvttons ment tt) | , 7 AML vemaining types in St) hawe weight > We were vet seSkip over (pruning rule) any tuples with less ee prem while (0 <= end) . WHat <= paixfrert-2}p) &A{pairikh.w >= pairtuert-t].w)) ke » Nrend of tamer for loop fegeing Uarough all tuples i SC-tehat fe object in | Rnapssck Then Merge in remaining tuples from We seay while (k< send) t s-19 petrfnest] p=pairiti.p: | pasrfnent! w= park we; | nett ts k++ » (MPaitialize S(i+2) for the next object stateend+t; —// set index for start of Seen) eod=next-t; /) set index for end of S0+1) Dufferti+ 1} =nest: } (end of outer for loop (dTrace back the sohution. ‘Trace_sol (p2.w! pairx.Total_wt.n buffer): » Q.22 Explain Q/1 Knapsack problem and explain the approach te solve it using dynamic programming. Explain with an example. SPENTU : Part B, Apeit-1t, Marks 15] Ans. : Refer Q20 and Q21. _SS All Pairs Shortest Path Problem Q.23 What the all pair shortest path problem ? SPENT = Part AL Marks 2] ‘Ans.: When a weighted graph, represented by its weight matrix W then objective is to find the distance between every pair af nodes. : Dynamic Q.24 Write an algorithm of all pairs problem, GS [INTU : Part 8, May-09, April-1s, Ans. : ‘Step 1: We will decompose the given subproblems. Let, Aj) be the length of shortest node i to node j such that the i) Path going from i to j viak ii) Path which is not going via ‘only shortest path from two cases. eee nam es Ag min Loree Aga where Algorithm All Pair(W_A) < fori=itondo for j=1tondo fork=1tondo { feri=itondo { for j=1 to ndo t Alijj=min ( Al] ALE] + All: 22 0mNn CO OeN CROMmN ——____Dynamic Programming 152 ee a NO 4 03ers AY = 2137 Stee eeae! 8 x 415 9 13 0 Thus shortest distances between all Pairs is obtained. S28 Pte stent pete Metseee an arty nodes in the following graph c= pany A 5 8 4] ES 3 g D Fig. 0.26.1 Ans.: We will write adjacency matrix for given graph, Assume A=1,B=2,C=3,D=4, 8, May-09, Marks 8] Now, let us compute A1,A?,43, a4 by using a formula Ali j= min (A fi i A fy K+ A Ok, j) ish jeyk=1 Fig. 0.26.2 AL 1] = min (A (1, 1), A[L 1)+A (1, 1) = min (0, 0+ 0) Al,i = 0 in tin3 eee A( 2] = min (A [1 2) 4, 1]+A (1, 2) = min 6, 0+5) Ata = 5 Design and Analysis of Algorithms i=1,j=3,k=1 A [1,3] = min (A [1, 3, A[1, 1] +A (1, 3)) = min (4,0 +4) AlL3] = 4 istje4k=1 ; A (1, 4] = min (A [1, 4), Aft, 1] +A [1, 4] = min (1, 0 +1) Al, 4] =1 is2j=1k=1 A [2 1] = min (A [2, 1], A [2, 1] + A 2, 1) = min (6,5 +0) AM=5 i=2,j=2,k=1 A 2, 2) = min (A [2,2], A 2, 1] +A (1, 2) = min (0,5 +5) AR, =0 i=2,j=3,k=1 A[2, 3] = min (A 2,3), AP, +A [1, 3) = min (2, 5+4) AB, 3) =2 i=2,j=4k=1 A [2,4] = min (A 22, 4], A [2 1] +A [1, 4) = min 3,5+1) AL, 41 = 3 i=3,j=1k=1 A, 1] = min (A [3, 1] A [3, 1] +A [1, 1) = min (4, 4 +0) AB,UW=4 i=3,j=2k=1 AB = min(AB 2,4B, +A [1 2) = min (2, 4+5) A [3,2] = 2 (9 recinucas PUBLICATIONS". in up tht fr kita i=3,j=3,ke1 A [3,3] = min (A [3, 3), A 13, 1] + AU, = min (0,4 +4) AB 3 =0 i=3,j=4ke=1 A (3,4) = min (A (3,4) A 13, 1) +A ft, = min (6,441) ABA = 5 iv4j=1ke1 “AA 1] = min (A [4 1) A 14, 1) A (1, = min (1, 1+0) Aw&=1 i=4j=2k=1 Als 2] = min (Af BA I+ A lL = min @,1+5) A a=3 i-4j-3,k=1 AG, 3] = min (A [4 3, A 1+ A 1 = min (6,1+4) Aa] =5 i=4j=4k=1 ALL 4) = min (a4 LA +4 iL = mn (0,1+1) A d=0 ‘The matrix is - ( BEAT Asa 1 1 (0, 5 +5) A [1 2), A (1, 2} 5+0) *Al2 2 1 3) A (1, 2)+4 p, 3) (4, 5 + 2) n (A [1, 4], A [1, 2) +A (2, 4)) n (1, 5 + 3) AB IWLAZ +A (2,1) 6, 0+5) ARAB 7+A22) (0,0 +0) (Az 3), AZ +A 2 3) Dynamie Programming $3 j=4iee AB 1] = min (ap, 1, A (3, 2] +4 (2, 1) = min (4,2 +5) AB = 4 “*3j=2ka2 41B.2] = min (A 83,2), A (3, 21+ 4 2 2) = min (2, 2 +0) AB 2 =2 i=3,j=3ke2 AB, 3] = min (A 13, 3], A 3, 2] +A [2 3) = min (0, 2 + 2) AB,3] =0 i=3,j=4k=2 A[B, 4] = min (A 3, 4], A [3, 2) + A [2 4)) = min (5, 2 + 3) A[3,4] = 5 =4j21k=2 AT, 1] = min (A [4 1], A [4, 2] + A [2, 1) = min (1,3 +5) A&I] = 1 i=4,j=2,k=2 A[4, 2] = min (A [4, 2}, A [4, 2] + A [2, 2)) = min G,3 +0) Al, 21 = 3 i=4,j=3,k=2 A [4,3] = min (A [4, 3], A [4, 2} + A [2, 3)) = min (5,3 + 2) Al, 3] = 5 ie4,jrdk=2 A[4, 4] = min (A [4, 4], A [4, 2] + A [2, 4)) = min (0, 3 + 3) Als 4] = 0 Design and Analysis of Algorithins The matrix A? will be = i=1jeUke=3 A [1,1] = min (A [1, 1], A [1, 3) + A (3, 1) = min (0, 4+ 4) A111 =0 i=1Lj=2k=3 A [1,2] = min (A [1 2), A [1, 3] + A (3, 2)) = min (65,4 +2) Al, =5 i=1j=3,k=3 A [1 3] = min (A [1, 3}, A [1,3] + A (3, 3)) = min (4, 4 +0) AU, 3] =4 i=Lj=4k=3 Af, 4] = min (A [1, 4) A [1,3] + A (3, 4) = min (1, 4 +5) Al,4j=1 i=2,j=1k=3 AZ 1] = min (A (2, 1) A (2, 3) + A (3, 1) = min 6, 4+ 4) ARN =5 i=2,j=2k=3 A [2,2] = min (A [2,2] A (2, 3+ A (3, 2) = min (0,2 +2) ARA=0 Dyna Leja bad (2,9) = min (A 2, BL AD A = min (2, 2+ 0) A, 3 2 1aajauked A (2,4) = min (A (2, 41 A 12 A = min (3,248) Al, a= 3 in3, jot kod A (3,1) = min (A 13, 1) A 13, 3) + A = min (4,0 +4) AGN 4 1=3,J02, kod A 13,2) = min (A (3, 2), A (3, 3) + = min (2,02) AB M2 13,jm3,kmd A (3,3) = min (A (3, 3) A (3, 3) A = min (0,0 +0) ABB = 0 1=3,jut kos AB, 4) = min (A (3, A) A (3, 3] + = min (5,0 +5) AGM = 5 in4 joka? ALA, 1) = min (A (4, IL A 4, + = min 0,5 +4) AWMN=1 inh ju2knd AMS, 2) = min (A (6, BA 4, Be = min (4,542) At, B= 3 min (A [1, 1], A (1, 4]+A 14 1) = min (0,1+1) o = min (A [1,2], A 1, 4] +A [4 2) = min (5, 1+ 3) min (A [1 3) Al 41+ 43) min (4, 1 + 5) 4 Sin (4 (1, 4) A 11, 41°41 4D min (1, 1 + 0) -1 AL 1] = min (42 1) AB, 4)+ 4 (4, 1)) > min 6,3 +1) ARM=4 i=2,j=2k=4 AB, 2] = min (A 2 2), AB, 4] +A 14,3) = min (3, 3 + 0) AR’=3 i=2,j=3,k=4 AB, 3] = min (A [2,3], A [2 4] +A [4 3) = min (2,3 +5) AR 3} =2 is2jeak=4 AL 4] = min (A [2, 4, A 2 4] +A [4 4)) = min @,3+0) Al, 4] =3 i=3,j=1k=4 A [3, 1] = min (A 3, 1], A [3, 4] + A [4, 1]) = min (4 5+1) AB, 1=4 i=3,j=2k=4 A 3, 2] = min (A [3, 2], A [3, 4] + A [4, 2]) = min (2,543) AL, =2 i=3,j-3,k=4 AB, 3] = min (A [3, 3], A [3, 4] + A [4, 3) = min (0, 5 +5) AT, 3] = 0 i=3,j-4k=4 A [3,4] = min (A 3, 4], A 13, 4+ A [4 4)) = min (5,5 +0) 4) =5 AB, Design and Analysis of Alyorithis Inajeaked AU A} = min (A (4 1], A [A A} + A (4 1) = min (1, 0+ 1) Alte t iedjerkead A (4 2] = min (A [4 2}, A (4 4] +A [4 2D) = min @, 0+ 9) Ala) = 3 ied jasked A [4 3] = min (A [4 3), A [4 4] +A [4 3) = min (5, 0 +5) Ala = 5 iedjegkea A [4 4] = min (A [4 4], A [4 4) +A (4, 4) = min (0, 0 +0) Ali 4] = 0 Finally the matrix representing all pairs shortest path will be - Fig. 0.26.5 This is the final solution. 2.27 Give an example of a graph or digraph with negative weights for which Floyd's algorithm does not yield the correct result. SGPLINTU : Part B, April-14, Marks 8] Ans. : Consider following digraph with negative weights. OF recencas PLELCATON. An ut owe 5-25 | | | Dynamic Now by Floyd's algorithm we will get - er Ew." BE] 1.2 eee [ale] The last matrix does not give the correct the shortest path. In fact we get ~ = because the cycle enough times makes the length arbitrarily small. This shows that for a digraph with negati the Floyd's algorithm does not yield result. Q.28 Explain the problem of all pairs path problem and write its algorith dynamic programming. Prove that it is with a numerical example. ‘USP [INTU : Part B, April-11, Ans, : Refer Q.23, Q.24 and Q.25. | 5.6 Travelling Salesperson Prob Q.29 Discuss briefly the solution to the salesperson problem using dynamic p Can it be solved by using divide method ? GSP [INTU : Part B, May-0 Ans, : Travelling salesperson problem Problem Description Let G be directed graph denoted by (V, denotes set of vertices and E denotes The edges are given along with their c cost Cy > 0 for all i and j. If there is no i and j then Cy = =. A tour for the giaph should be vertices should be visited only once an tour is sum of cost of edges on # traveling salesperson problem is to minimum cost. id to solve Dynamic programming is . ending at Vip, alll the intermediate © {Vier -VG.49} once, formula can be used to obtain mum cost tour. i S) = min (d{i, i] + Cost(, jeSandies. der one example to understand g of TSP using dynamic programming Soi) g salesperson problem can not be solved "and conquer because it is based on ‘optimality. In divide and conquer | given problem is divided into then simply the solutions of these ‘combined together to obtain the en problem. en diagraph, obtain optimum cost [INT : Part B, May-17, Marks 5] io eal __Dynamic Programming ‘Ans. : The distance matrix can be given Wyo BAe from 4 # First we will select any arbitrary vertex say select 1. Now process for intermediate sets with increasing size, Stop 1: Let S= then, Cost (2,61) = d@,1)=5 Cost (3,6 1) = d, 1)=6 Cost (4,91) = d(4, 1)=8 That means we have obtained dist (2, 1), dist (3, 1) and dist (4, 1). Step 2: Candidate (S) = 1 Apply the formula, Cost (i, $) = min {dfi, j]+Cost (j, S-(]} Hence from vertex 2 to 1, vertex 3 to 1 and vertex 4 to 1 by considering intermediate path lengths we will calculate total optimum cost. Cost (2 {3}, 1) = d @, 3) + Cost (3, @ 1) = 946 Cost (2, (3},1) = 15 Cost (2, (4), 1) = d (2, 4) + Cost (4, 6 1) =10+8 Cost (2 {4}, 1) = 18 Cost (3, (21) = dB, 2) + Cost 2, 61) = 13+5 Cost (3, (2), 1) = 18 Cost (3, (41, 1) = dG, 4) + Cost (4, 6 1) = 12+8 Cost (3, (41, 1) = 20 Cost (4, (2), 1) = d (4,2) + Cost (2, 1) = 845 13 d(4, 3) + Cost 3, @ 1) 946 15 Cost (4, (21) = Cost (4, (3), 1) Cost (4, (3), 1) step 3: Consider candidate (S) = 2 Design and Analysis of Algorithms af Cost (2, (3, 4}, 1) = min {[d(2, 3) + Cost (3, {4}, 1)], [42 4) + Cost (4, (31, 1) = min {(9 + 20], [10+15)) Cost (2, 3, 4},1) = 25 Cost (3, {2, 4], 1) = min {[d(3, 2) + Cost (2, {4}, 1)], [d(3, 4) + Cost (4, {2}, 1))} = min ({13+18), [12+13)) B = min {[d(4, 2) + Cost (2, {3}, 1) [d(4, 3) + Cost (3, (2), 1)]} min {[8 + 15], [9 + 18]} Cost (4, (2, 3),1) = 23 Step 4: Consider candidate (5) = 3. ie. Cost (1, (2 3, 4}) but as we have chosen vertex 1 initially the cycle should be completed ie. starting and ending vertex should be 1. «We will compute, Cost (3, {2, 4), 1) Cost (4, {2, 3}, 1) {[4(1,2) + Cost(2,(3,41,1)], Cost (1, {2 3, 4}, 1) = minj {d(1, 3)+Cost(3, 2,4},1)], [a 4) + Cost(4, (2,3),1)] = min ([10 + 25}, [15 + 25] (20 + 23)) = 35 Thus the optimal tour is of path length 35. Consider step 4 now, from vertex 1 we obtain the ‘optimum path as d(1, 2). Hence select vertex 2. Now consider step 3, in which from vertex 2 we obtain optimum cost from d(2, 4). Hence select vertex 4. ‘Now in step 2 we get remaining vertex 3 as d(4, 3) is optimum. Hence optimal tour is 1, 2, 4, 3, 1. 5.7 Reliability Design Q.31 Derive the mathematical formulation in reliability design. C&[INTU : Part B, Dec.-12, Marks 7] ‘Ans, : When devices are connected together then it is a necessity that each device should. work properly. The probability that device 1! will work properly is called reliability of that device. TECHNICAL PUBLICATIONS". An up trust fr kromedge 5-27 Let 1, be the reliability of device D, then entire system is 1. It may happen reliability of individual device is very reliability of entire system may not be ‘obtain the good performance from entire can duplicate individual devices and them in a series. To do so, we will attach Gircuits. The job of switching circuit is to | which device in a group is working Le] L®] [°] a) [>] [J [°] ‘Sage ‘Sage 2 ‘Sog23 Fig. Q.31.1 Multiple devices connected Let m, be the copies of device D, then (1 the probability that all m, have a the stage reliability is 1 - (1 - )™. We reliability of stage i by 6;(m,). Hence i(m) = 1-(0-1)™ In reliability design problem we Maximize reliability [] 6,(m;) which i Isisn to) Cim; FA t an be discarded * C1) = {0.930} | Sh = Si with ina,je2 | 17908, meoja2 (m) = 1-G-1 9% = 1-G-09)? rs | Computation ot | a) | 4 (m;) = 0.99 | and C;m, = Cy*m, =30°2 | Gm, = 60 8} = {(0.99, 60)} ees s! ={(08,30).(099,60)} 205+ 30— (30+ 15+ 20))/30 = 2.33 | Stop 2: pea = (30+ 15+20) S} = Sj with i=2, j-1 }+ 15- (30+ 15+ 20))/15 = 708 ‘Computation of S| 20, CG; = (C+ Cp +Ca) : : m= m=) = - 15 + 20))/20 = ha 4(m) = (1-G-5)™) E c 3) > 1-Q-5)@ I eespenos = 1-(1-08)! 4 (mi) = 08 Bel and choose m,,. Let us start and Gm, = Coma Il include {(reliability, cost)) | = 15"1 Gm = 15 1, s?, S? and so on Design and Analysis of Algorithms For computing the terms in S} we will use terms in 3 | se Using ple (09, 30) Using tuple (0.98, 60) | ja] | Reliability = 09 *o,(m;) _ Reiblity = 099° (m4) T14(m,)- 09°08 isi'sn | | Cost Cost = 60 +15 Sem; = 90415 = =75 We get (reliability, cossf] | We get (reliability, co | Pes =07, = (0792, 75) = {(0.72,45), (0.792,75)} Now compute S? = Sj with i=2, j=2. R= 708 m =m emi) = (1-G-n)™) = 1-(@-08)? si(mj) = 096 and Cm = Cm) = 15 *2 Ceram 20: For computing the terms in S3 we will use terms in a . Using tuple (09, 30 a eet | i) Reliability computation | Reliability computation | Tae)- 09*0.96 Elan) cee = 0864, 9504 |i) Cost computation | Tem, = 30+30 a -0 90 We get (reliability, cost) | We get (reliability, cost) = (0864, 60) | = (0.9504, 90) | | Cost computation | | | | SF recrnucA PUBLICATIONS”. Anup tt inode 5-29 ‘Dynamic S}_= {(0.864, 60), (0.9504, 90)} Now compute S? ye S| withi=2, j=3 = 08 m,=j=3 1-0-5)" =1-(-n)™ 1-(1-08)? 0.992 Gt*m = 15*3 im, = 45 For computing the terms in S3 we si “sg tuple (0.9, 30) No i) Reliability computation Rel Tla(m,)-09%0992 | [eG | | || i | | | | | a eS eas on | || | Cost computation | Cost Tem, = +45 | Vey 7 | We get (0.8928, 75) | We $3 = {(0.8928, 75), (105, Eliminate because | s SE = {(0.8928, 75)} 8 fst st 3} | -. (an (0.9504, 90), (0.8928, From this we need to (0.9504, 90) due to dominance $= (0.72, 45),(0.864, Hence ___ Pymamic Programming rs with i=3 ;_ ett Computation of 88} 53 Ss =(-1,)™ (1-1, )™3 -(1-05)! g the terms is S? we will use terms in $2. Using tuple (0.72, 45) Using (0.864, 60) Using (0.8928, 75) =072*05 1) T]o,(m,) = 0864 * 05 = 036 = 0432 = 0.4464 Dem, - 45+20 Sci, = 60 + 20 TCim; = 75 +20 - -80 =95 = 6 We get (0.36, 65) We get (0.432, 80) We get (04464, 95) ne Tla(m,)=072"075 Tl (mu) = 864° 078 054 ote Tem, = +00 Tem rorw = 8 = 100, ‘We get (0.54, 85) ‘We get (0.648, 100) Sf + Cask a) (ti aye AD Eliminate because it exceeds 100 % Sy = {(0-54, 85), (0.648, 100)} Now compute S} S3- Ss with i= 3, j=3. argos m = my=j=3 (mj) = 1-0-1 )™ = 1-(@-05)"3 = 1-@-05)° %(m)) = 0.875 and Cym, = Cyms = 20°3 Cm, = For computing the terms in $3, we will use terms in $*. Using (0.864, 60) Tla(m) -072-0875 Tho(e,) = 0840075 063 = 0.756 Tem, = 60+ 0 120 Hence there will not be any term for $3 S? » {s?, 33} S= {(0-36, 65), (0.432, 80), (0.4464, 95), (0.54, 85), (0.648, 100)} the reliability des ‘ign 0.648 and cost 199,” ° 00, computed (0.864, 60) from (0.9, (0.9, 30) is present in Sl ie, =1,j=1. Then mj =1. get m,=1, m; = 2 and m,=2 to reliability design. stages of a system with tg = 0.2 and C, = 30, C, = 20 the total cost of system is = 2, ug = 3, ug = 2. Find the WGP [INTU : Part B, May-13, Marks 7] = ry = 03, r= 05, 15-02 dC = 30 GG) = {0359} S| with i= 1 andj"? 170. 5-32 , ™ * my efg } Su) = Taq a yet = 1-4-03) $i(m;) = and Cim, = Cm, = S} = {051,60} | aie {03 59,051, 69} | O51 CG * my =30"2 60 Step 2: Computation of S? m= = 05 simi) = (mi) = Cm = and Cm; = Using tuple (0.3, 30) | Using tuple (0.51, 60) Reliability | | Tl o.(m,)=05*6,(m,) \ (mj) = 03 * @ (m,) | St sn | | E 100 %. one * = {(0.080, 0), (0576, 100) (0.0525,120)} - From this we need to eliminate (0.525, 120) and (0.576, 100) as, maximum allowed cost is 80. S* = {(0.030, 0)} From $° tuple is (0.030, 80). This tuple is actually obtained from $3. © SP with i= 3, j= 1, my =j=1. Therefore set mri We have computed (0.030, 80) from (0.15, 50) But (0.15, 50) € S? originally. ae ; $2 with i=2,j=1,m,=j=1. have compute (0.15, 50) from (0.3, 30) t (0.3, 30) € St with i= 1, j= 1, my =j=1 Set m,=1— ve get my = 1, m= 1 and ms = 1 as 2 soln ty design. Multiple Choice Questions Which method can be used when the solution to a problem can not be viewed as the result of a sequence of decisions ? OS [INTU = Nov.-09] [a| Greedy method [pl Divide and conquer Dynamic Programming 4 a | ee state and decision are, the remaining decisions must constitute an optimal decision sequence. OS [INTU : Nov.-09] Qi Q2 a3

You might also like