Download as pdf
Download as pdf
You are on page 1of 122
SUBJECT CODE: 410241 Choice Bosed Credit System SAVITRIBAL PHULE PUNE UNIVERSITY - 2019 SYLLABUS B.E. (Computer) Semester - VII DESIGN AND ANALYSIS OF ALGORITHMS (For END SEM Exam - 70 Marks) Mrs. Anuradha A. Puntambekar M.E. (Computer) Formerly Assistant Professor in PE.S. Modern College of Engineering, Pune. © Writeen by Popular Authors of Text Books of Technical Publications J Covers Entire Syllabus [2 Question - Answer Format © Exact Answers and Solutions © Chapterwise Solved SPPU Questions Dec. 2006 to March 2020 @ Solved Model Question Paper (As Per 2019 Pattern) “SOLVED SPPU QUESTION + May - 2017 + Dec, - 2016 *Dec.- 2017 +May-2018 | *Dec.-2019 » March - 2020 | ‘A Guide For Enginesring Students DESIGN AND ANALYSIS OF ALGORITHMS (For END SEM Exam - 70 Marks) SUBJECT CODE : 410241 B.E, (Computer Engineering) Semester - VII © Copyright with Technical Publications All publishing rights (printed and ebook,version) reserved with Technicel Publications No part ofthis book should be reproduced in any form, Electronic, Mechanical, Photocopy or any information storage and retrieval system without prior permission in writing, from Technical Publications, Pune. Published by : = TECHNICAL & - PUBLICATIONS perm Aa UpThust for Krowledgo Amit Residency, Office No.1, 412, Shaniwor Peth, Pune - 411080, MS, INDIA Ph.: +91-020-24495496/97 Email : info@technicalpublicotions.in Website : www:technicalpublicotions.in Printer : Yosinj Printers & Binders, SrNo. 10/1/A,Ghule Industal Estate, Nanded Village Road, Tak - Hoel, Dist. - Pune - 411041 ISBN 978-93-5585-239-7 _ 7189355 9789355859397 [1] (i) SYLLABUS Design and Analysis of Algorithms - (410241) Credit Examination Scheme : 03 End-Sem (Paper) : 70 Marks Unit I Greedy And Dynamic Programming algorithmic Strategy Greedy strategy : Principle, control abstraction, time analysis of control abstraction, knapsack problem, scheduling algorithms-Job scheduling and activity selection problem. : Dynamic Programming : Principle, control abstraction, time analysis of control abstraction, binomial coefficients, OBST, 0/1 knapsack, Chain Matrix multiplication. (Chapter - 3) Unit IV . Backtracking and Branch-n-Bound Backtracking : Principle, control abstraction, time analysis of control abstraction. 8-queen problem, graph coloring problem, sum of subsets problem. Branch-n-Bound : Principle, control abstraction, time analysis of control abstraction strategies - FIFO, LIFO and LC approaches, TSP, knapsack problem. (Chapter - 4) : Unit V Amortized Analysis Amortized Analysis ; Aggregate Analysis, Accounting Method, Potential Function method, Amortized analysis-binary counter, stack Time-Space tradeoff, Introduction to Tractable and Non-tractable Problems, Introduction to Randomized and Approximate algorithms, Embedded Algorithms : Embedded system scheduling (power optimized scheduling alg (Chapter - 5) Unit VI Multithreaded and Distributed Algorithms Multithreaded Algorithms - Introduction, Performance measures, Analyzing multithreaded algorithms, Parallel loops, Race conditions, Problem Solving using Multithreaded Algorithms - Multithreaded matrix multiplication, Multithreaded merge sort. Distributed Algorithms - Introduction, Distributed breadth first search, Distributed Minimum Spanning Tree. String Matching - Introduction, The Naive string matching algorithm, The Rabin-Karp algorithm. (Chapter - 6) ithm), sorting algorithm for embedded systems. TABLE OF CONTENTS Unit II Chapter-3 Greedy and Dynamic Programming — Algorithmic Strategy 8-1) to (8.34 3.1 Introduction to Greedy Strategy ... 3.2 Knapsack Problem 3,3 Scheduling Algorithms - Job Scheduling... 3.4 Activity Selection Problem... 3.5 Introduction to Dynamic Programming...... 3.6 Binomial Coefficients ..... 3.7 OBST... 3.8 The 0/1 Knapsack.. 3.9 Chain Matrix Multiplicatior Chapter - 4 Backtracking and Branch-n-Bound (4 - 1) to (4 - 43) 4.1 Principl 4.2 Control Abstraction and Time Analysis .... 4.3 Applications of Backtracking... 4.4 The 8-Queen Problem... 4.5 Graph Coloring Problem. (wy) 4,6 Sum of Subsets Problem 4.7 Principle and Control Abstraction 4.8 Strategies- FIFO, LIFO and LC Approaches .. 4.9 Concept of Bounding. 4.10 Traveling Sales Person Problem. 4.11 Knapsack Problem.... 4.12 Difference between Backtracking and Branch and Bound Techniques .. =o Chapter - 5 Amortized Analysis (5 - 1) to (5 - 14) 5.1 Amortized Analysis 5:2 Time-Space Trade-off.. 5.3 Introduction to Tractable and Non-tractable Problems... 5.4 Introduction to Randomized Algorithms. 5.5 Introduction to Approximate Algorithms... 5.6 Embedded Algorithms = Chapter - 6 Multithreaded and Distributed Algorithms - (6 - 1) to (6 = 18) 6.1 Multithreaded Algorithms. 6.2 Problem Solving using Multithreaded Algorithms 6.3 Distributed Algorithms... 6-10 6.4 String Matching. Solved Model Question Paper (M - 1) to (M- 2) Ww Greedy and Dynamic Programming Algorithmic Strategy 3.1: Introduction to Greedy Strategy Q1 What is Greedy strategy ? Ans. : © In Greedy technique, the solution is constructed through a sequence of steps, each expanding a partially constructed solution obtained so far, until a complete solution to the problem is reached. At each step the choice made should be, Choice of solution ——» Feasible - It should satisfy the problem's constraints, made ateach | _» Locally optimal - Among all feasible solutions the best step of choice is to be made. ‘blem solving in Greedy |__> |rrevocable - Once the particular choice is made then approach it should not get changed on subsequent ste In short, while making a choice there should be a Greed for the optimum solution. Q.2 What are the applications of Greedy strategy ? Ans, : 1) Knapsack problem 2)Job sequencing with deadlines 3) Minimum spanning trees 4) Single source shortest path algorithm. 5) Huffinan's Tree Q.3 What are the characteristics of greedy method ? 1&P[SPPU : Dec.-06,07, May-07,09 Marks 8) Greedy and Dynamic Design and Analysis of Algorithms 3-2 Programming Algorithmic Strategy Ans, : 1. Greedy choice property : By this property, a globally optimal solution can be arrived at by making a locally optimal choice. That means, for finding the solution to the problem. We solve the subproblems, and whichever choice. We find best for that subproblem is considered, Then solve the subproblem is considered. Then solve the subproblem arising after the choice is made. This choice may depend upon the previously made choices but it does not depend on any future choice. Thus in greedy method, greedy choices are made one after the another, reducing each given problem instance to smaller one. The greedy choice property brings efficiency in solving the problem with the help of subproblems. 2. Optimal substructure : A problem shows optimal substructure if an optimal solution to the problem -contains optimal solution to the sub-problems, In other words a problem has optimal substructure if the best next choice always leads to the optimal solution. 2: Knapsack Problem ‘Q.4 Write an algorithm for Knapsack problem using Greedy strategy. . OS [SPPU : May-17, Marks 6) Ans. : Algorithm Knapsack_Greedy(W,n) { //pli] contains the profits of i items such that 1 r ‘weight “=” A Guide for Engineering Students Greedy and Dy yn Design and Analysis of Algorithms 3-5 Programming Algorithmi¢ Stree, Method 1 : Select object with maximum profit. Object | x1 | x2 | x3 | x4 | x5 p | 10 | 20] 30 | 40 | so w | 20 | 30 | 66 | 40 | 60 Selected object | Profit x5 50 60 100 - 60 = 40 | x4 40 40 40-40 =0 Weight] Remaining weight Hence Maximum profit = 90 with solution (x5, x4) = (0,0,0,1,1) Method 2 : Select object with minimum weight. m = 100 Selected] Profit Weight Remaining weight object . xl 10 20 100 — 20 = 80 2 20 30 80 - 30 = 50 x4 40 40 # 50-40 = 10 * 50d = 8.33 60e4 = 10 10-100 Hence Maximum profit = 78.33 with solution (1,1,0,1,1/6) Method 3 : Select object with maximum profit/weight ratio. us ‘A Guide for Engineering Sauder | dg Design and Analysis of Algorithms 3-6 Greedy and Dynamic Programming Algorithmic Strategy Selected object Profit Weight | Remaining weight x4 40 40 100 - 40 = 60 Maximum profit = 90 with solution (0,0,0,1,1) This is a feasible solution. x5 50 60 6 0 Q.7 Consider the following instances of knapsack problem n = 3, M = 50, (PI, P2, P3) : (60, 100, 120) and (W1, W2, W3) : (10, 20, 30). Find the optimal solution using Greedy approach ? ES [SPU : Dec.-19, Marks 6] Ans. : We will use largest profit strategy to pick up the object. For that . P; purpose, we will compute Vv i Xj 1 2 3 B 60 100 120 W, 10 20 30 a 6 5 4 Wj Step 1: Select x; = 1 Total Profit = 60 Remaining Weight = M-W; Step 2: Now Total Profit = 60+100= 160 Remaining weight = 40-20 = 20 A Guide for Engineering Students ithms 3-7 Programming Geet 24 Dyna Design and Analysis of Algorithms gramming Algortnma gmc Ategy Step 3: ‘We can not select-items 3 as a whole. So, only select the Weight whi wvill fill up the knapsack ‘completely. Which ~ Take only wt 20 out of 30. “Remaining weight = 20-20= 0 20 = [:ost+ 2001230555) 50 i= (sons 1001+ 120 = 60+ 100+ 80 = 240 2 The optimal solution is ( 1, 2) 3.3 : Scheduling Algorithms - Job Scheduling Q8 Explain job-sequencing with deadlines. Solve the following instance : n = 5. (P, Py Py Py Pd = 20, 15, 10, 5, 1) (yy yy yy Ay hg) = (2, 2, 1, 3) 3) FRPESPPU: June-22, May-t3, Marks 10) Ans, 3 Stop 1: We will arrange the profits P; in descending order, along with corresponding deadlines, Deadline Step 2: — am array J { which stores the jobs, Initially 1 1 will ve ; 4 5 2 3 Pete tT ete ye fone a — Lajulie for Bnglnwset sw Greedy and Dynamic Design and Analysis of Algorithms 3-8 Programming Algorithmic Strategy Step 3: Step 4: Step 5: Step 6: Step 7: Step 8 : Add i Job in array J[ ] at index denoted by its deadline Dj, First job is P,, its deadline is 2. Hence insert P, in the array J[ ] at 2" index. 1 2 3 4 5 Py Next job is P; whose deadline i8 2 but J[2] is already occupied. We will search for empty location < J[2}. The J{1] is empty. Insert P; at J[1]. Next job is P; whose deadline is 1, But as J[1] is already occupied discard this job. Next job is P, with deadline 3. Insert Py at J[3). 1 2 3 4 5 P: Py Py Next job is Ps with deadline 3, But as there is empty slot at ob. <= J[3], we will discard th Thus the optimal sequence which we will obtain will be Py — Py = Py. The maximum profit will be 40, Q.9 Find the correct sequence for jobs using following instances, Job Nn 2 B MW JS Profit 20 1s 10 3 1 Deadline 2 2 ' 3 3 Ans. ¢ Stop 1: corresponding deadlines, Profit aly Deadline KOP[SPPU: Aprile19, Marks 5] We will arrange the profits Py iy descending onder, along with = : Greedy ana py, Design and Analysis of Algorithms 3-9 Programming Algorithmic mame 7 Stop 2: Create an array J.[ ] which stores the jobs. initially 5 [ yy be ‘ll 1 2 3 4 5 0 0 0 Lo 0 J6) Step 3: Add i Job in array J[ ] at index denoted by its deadline p, First job is P, its deadline is 2. , Hence insert P, in the array J[ ] at 2™ index. 1 2 3 4 ly || Step 4: Next job is P, whose deadline is 2 but J[2] is already occupied We will search for empty location < J[2]. The J[1] is empty. Insert P, at sti). Step 5: Next job is P; whose deadline is 1. But as J[l] is already occupied discard this job. Step 6 : Next job is P, with deadline 3. Insert P, at J(3) Step 7: Next job is P, with deadline 3. But as there is empty slot at <=J[3], we will discard this job. Step 8: Thus the optimal sequence which we will obtain will be P,-P,~P,, The maximum profit will be 40. Q.10 Write an algorithm for Job scheduling. Ans. : Algorithm Job_seq(D,J,n) { //Problem description : This algorithm is for job sequencing using Greedy method. //D\i) denotes i deadline where ents 4 Guide for Engineering Studer Greedy and Dynamic Design and Analysis of Algorithms 3-10 Programming Algorithmic Strategy //S|i] denotes i job // DiJlil) $ Disti+1)] /fnitially DI0|-0; J[0}-0; JI1}1; count<1; for —2 ton do - { tecount; while(D[J[t]] > Di) AND D[J[t]]!=1)) do t-t-1; if(D[J[t}] < Dl) AND(D[i] > t)) then { /Ansertion of i* feasible sequence into J array for s--count to (t+1) step -1 do Jis+1] — JIsl; Jit+ 1] count—count+1; end of if Mend of while return count; } The means quence of J will be inserted if and only if D{J[t]]!=t. This also hat the job J will be processed if it is in within the deadline. The computing time taken by above Job_seq algorithm is O(n’), because the basic operation of computing sequence in array J is within two nested for loops —= = : 3.4: Activity Selection Problem Q.11 Write greedy algorithm for activity selection problem. Give its time complexity. For following intervals, select the activities according to your algorithm. 1) (1-3), 10-2), 13(3-6) 14 (2-5), 15 (5-8), 1¢ (3-10), 15 (7-9). : A Gulde for Engineering Sudents d Greedy and 3-1 Programming Algorithmic sce tees i | Design and Analysis of Algorithms q ° i Aus. : | i / ee . Step 1: Sort the given activities in ascending order according to th ir finishing time. Step 2 : Select the first activity from sorted array and add it to soluti ion array. | Step 3: For next subsequent activity if start time of currently sclecteg - es o =. activity is greater than or equal to finish time of previous selected activity, then add it to solution array. a Step 4 : Select next activity. | Step 5 : Repeat step 3 and step 4 for all remaining activities. Time complexity is O(n log n). Example : | Step 1: We will sort the given activities in ascending order according to | their finish time. The sorted table is | Start Time Finish Time Activity Name 0 2 Ip 1 3 h 2 5 Is 3 6 Is 5 8 i - 7 9 I; 3 3 10 Ig Step 2 : Select first activity i.e. activity 12 to add it to solution array: Hence Solution = {I}. ill not Step 3 : Now select activity I,. If start (I, ) < finish (Iz. So W° a add I, to solution array. : Sr ‘A Guide for Engineer" ail it | Greedy and Dynamic Design’ and Analysis of Algorithms 3-12 Programming Algorithmic Strategy Step 4 : Now select activity I. Start (Iq) = Finish (4) = 2. Add I, to solution array. Solution = {I,T4] Step 5 : Select 13. Start (13) < Finish (15). Hence do not add it to solution array. Step 6 : Similarly activities 13, 15, I7 and Ig will not get added to solution array. Step 7 : Hence solution is (17,14). 3. introduction to Dynamic Programming Q.12 Name the elements of dynamic programming ? How does the dynamic programming solve the problem ? TG [SPU : Dec.-06, 07, May-07, 08, Marks 6, May-10, Marks 4] Ans. : Elements of Dynamic Programming : 1) Optimal substructure - The dynamic 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. Steps of Dynamic Programming : Dynamic programming design involves 4 major steps : 1. Characterize the structure of optimal solution, That means develop a mathematical notation that can express any solution and subsolution 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 subsolutions, using the mathematical notation of step 1. 4. Compute an optimal solution from computed information. A Guide for Engineering Students Greedy ana py, 3-13 Programming Algorithmic Strate egy Design and Analysis of Algorithms @.43 Write control abstraction (General Strategy Algorithm ¢ dynamic programming. O@>[SPPU : Aug.-15, In Sem, Ans. : Refer Q.12. Q.14 What is the "Principle of optimality" ? USP [SPPU : Dec.-06, 07, Marks q Marks 4) Ans, : © The dynamic programming algorithm obtains the solution Using principle of optimality. The principle of optimality states that “in an optimal sequence of decisions or choices, each subsequence must also be optimal.” # When it is not possible to apply the principle of optimality it is almost impossible to obtain the solution using the dynamic programming approach. For example : Finding of shortest path in a given graph uses the principle of optimality. Q.15 Enlist the applications of Dynamic programming. ‘Ans. : Dynamic programming is used for solving various problems such as Multistage Graph Traveling Salesman Problem Matrix Multiplication Problem Longest Common Subsequence Problem Maximum Flow Problem. All Pair Shortest Path Problem 16 Comparison of greedy approach and dynamics programming. . ES [SPPU : Dec.-18, Marks 6] 1. 2. 3. 4. 5. 6. Q Ans, : Dynamic programming Sr. No. Greedy method Greedy method is used for obtaining optimum solution. Dynamic programming is also for obtaining optimum solution. In Greedy method a set of feasible solutions and the picks up the optimum solution. There is no special set of feasible solutions in this method: A Guide for Engineering all SS, sen Design and Analysis of Algorithms 3-14 Greedy and Dynamic Programming Algorithmic Strategy 3. In Greedy method the optimum | Dynamic programming considers selection is without revising all possible sequences in order previously generated solutions, | to obtain the optimum solution. 4. In Greedy method there is no as such guarantee of getting optimum solution. It is guaranteed that the dynamic programming will generate optimal solution using principle of optimality. 3.6 : Binomial Coefficients Q.17 Explain with example, how to obtain Binomial coefficient using dynamic programming ? Ans. : © Computing binomial coefficient is a typical example of applying dynamic programming. © In mathematics, particularly in combinatorics, binomial coefficient is a coefficient of any of the terms in the expansion of (a+b)*. It is denoted by C(n, k) “(2) where (0 < k< n) e The formula for computing binomial coefficient is, = Ca-1,k-1)+Cm-1,) For example : Compute C (4, 2) n=4,k=2 C(4, 2) = C@-1,k-1)+C@~1,0 (4, 2) = CG, 1) + CG, 2) «= (QI7.1) As there afe two unknowns : C(3, 1) and C(3, 2) in above equation we will compute these sub instances of C(4, 2). : n=3,k=1 CG, 1) = C2, 0) + C2, 1) As C(n,0) = 1 We can write A Guide for Engineering Students \ * , Wreedy and Dyn Design and Analysis of Algorithms 3-15 Programming Algorithmic Stee CQ, 0) = 1 CG, 1) = 1+C, 1) ” Qura Hence let us compute C(2, 1). n=2,k=1 CQ, 1) = Ca-1,k-1)+Cm-1,) = CU, 0) + CU, 1) But as C(n, 0) = land C(n,n)=1 we get C(I, 0) = Land» (1, 1) =1 te C(2, 1) = C(I, 0) + C(I, 1) =141 CQ, 1) = 2 -(Q.173) Put this value in equation (3.6.2) and we get CG, 1).= 142 CG, 1) = 3 ~~ (Q174) Now to solve equation 1° we will first compute C(3, 2) with n = 3 and k=2. CG, 2) = Cn-1,k- 1) + C= 1, k) CG, 2) = C(2, 1) + C(2, 2) But as C(n, n) = C(2, 2) = 1, we will put values of C(2, 1) [obtained in equation (3.6.3) and C(2, 2) in C(3, 2) we get, CG, 2) = C(2, 1) + C(2, 2) = 241 CG, 2) = 3 van (QUT) Put equation (Q.17.4) and (Q.17.5) in equation (Q.17.1), then we get C4, 2) = 0G, 1) + €G, 2) =34+3 ; is A Guide for Engineering Suudet a . Greedy and Dynamic Design and Analysis of Algorithins 3-16 Programming Algorithmic Strategy C4, 2)= 6 is the final answer. While computing C(n,k) the smaller overlapping sequences get generated by C(n - I, k ~ 1) and C(n ~ 1, k). These overlapping, smaller instances of problem need to be solved first. The solutions which we obtained by solving these instances will ultimately generate the final solution. Thus for computing binomial coefficient dynamic programming is used. 3.7 : OBST Q.18 Write an algorithm for finding minimum cost binary search tree sing dynamic programming strategy. Show that the computing time of this algorithm is O(n2). 02 [SPPU : Dec.-06, May-07, 09, Marks 12] OR Write an algorithm for optimum binary search tree. GSP [SPPU : Dec.-15, End Sem, Marks 6] Ans. : Algorithm Wi(p,q,W.n) //p is an input array [1..n] //q is an input array (0..n] // Wii,j] will be output for this function such that W/0..n,0..n] { for ici to n do WIi,i] < alil; // initialize for me1 to n do { for 1-0 to n-m do { kCitm; WILK] CWIi,k-1] + pik] + alk); } write(W/[0:n});//print the values of W * For computing C;, ry following algorithm is used. Algorithm OBST(p,q,W,C,r) { /fcomputation of first, two rows ‘ A Guide for Engineering Smdents AL w Greedy and Dy, Design and Anaysis of Algoritims 3-17 Programming Algorithmig Sra for i=0 to n do Ci) <0; tli] <0; Ofi,i+1] — ali} +alit +pli+ 1); rit itt; “| 7 mé2tondo ‘ For <0 to n-m do { jcitm; ke-Min_Value(G,r,ij); //call for algortihm Min_Value // minimum value of C, is obtained for deciding value of k ClijlCWHij] + Clik-1) + Cl); rlijloks } } write(C[0:n],r[0:n]);//print values of C, and ry } Algorithm Min_Value(C,ri,j) minimum ©; // finding the range of k for (me+[ij-1] to r[i+1,j]) do { if(Clim-1]+C{m,j])< minimum then { minimum ¢ Clim-1]+C[m,j]; + kom; } ; tetum k//This k gives minimum value of C } Following algorithm is used for creating the tree Ty. ‘A Guide for Engineering Studen® Greedy and Dynamic Design and Analysts of Algorithms 3-18 Programming Algorithmic Strategy Algorithm build_tree(r,a,i,j) { T data < alk]; if G==i+1) return; T->left=build_tree(r,a,i.k-1); ‘T->right=build_tree(r,a,.k,j); } In order to obtain the optimal binary search tree we will follow : 1. Compute the weight using Wt function. 2. Compute C;, and 1; using OBST. 3. Build the tree using build_tee function. Analysis In given algorithm, the basic operation is computation of C[i, j]. In this computation j [SPPU : May-11, Marks 16, Aug.-15, In Sem, Marks 8] Ans, : For convenience we multiply the probabilities py and q; by 100. Hence p; = (50, 10, 5), qi = (15, 10, 5, 5) Wii = dir Wyis1 = 4i + Q(i+3) *PQisn) A Guide for Engineering Students lite I ' i Greedy and dyn eign and Ants of Agrins 3-19 PrgranningAeorime gM ey nin Tit Cy itl = qi tien *PCi+1) Wi = Wij-1+Pi +45 ny ok : min Gy tick sj Cary +Cay b+ Wy We will construct thie tables for values of W, C and r. Let, i=0 Woo = qo =15 When i= 1, Wy = qi =10 When i = 2, 1 Wa = q2=5 When i = 3, Wx = q3=5 When i = 0 andj -i= 1 then, Wo = d0+41+Pi = 15+10+50 Wo = 75 When i= 1 and j-i=2 then, Wiz = qi+q2+p2 = 10+5+10 Wy = 25 When i= 2 andj -i=3 Was = q2+q3+p3 = 54545 1s =2 = Wir +P +g) = Wo +p2+qa +1045 90 = s ‘A Guide for Engineering Sule" Greedy and Dynamic Design and Analysis of Algorithms 3-20 Programming Algorithmic Strategy Wi3_ = Wi +p3+q3 = 25+54+5 Wis = 35 Now, when i = 0, j—i=3 then Wo3s = Wo2+P3+43 = 90+5+5 Wos = 100 The table for W can be represented as We will compute for C and r Ci = 04, = 0 Hence Coy = 0, Cy =0, Cop =0, Cay =0 = 0, t11=0, 1) =0, 193 =0 = it disa + PGs and 4, je) =i+1 (i+ 1) PG+1) = qo*di+Pi = 15+10+50 = 75 my =it1=1 Cia = qitq2+P2 = 104+5+5 Cip = 20, ry =2 A Guide for Engineering Students Let, . oo” Greedy ana yy | Design and Analysis of Algorithms 3-21 Programming Algorithmic i ate, ey When C23 = G2 +43 +P3 = 54545 Co3 = 15, 193 =3 | Now we will compute Cjj and 11; for j- 122, min As Cy =< Sif Capa +Ca5 Wij Hence we will find k. For Cop we have i = 0 and j =2. For 151 to 4 ‘ -_ ist, be. for 19) to r, | we will compute minimum value of C; ia Let, toy =1 and ry) =2. Then we will assume k = compute C; ; value of C,,;. CT and k = 2 and then From these computations we will Pick-up the minimum withk=1, i=0, j=2 with k = 2, Gyx-1 + Cy Co = Cixy +O; i Coy + Cy 7 Co | Cy = 0+20 = 75+0 Coz = 20 Coz = 75 Clearly select Cgy =20 when k = 1. Now Cop = min{Cop }+ Wop = 20+90 Cw = 10 Now for Ci3_ we have i= 1, and j = 3. For r;;_1 to Ti+1j, be. for ry to 193 we will compute minimum value and 13 = 3, Then we will assume k = 2 and k = 3. then sender A Guide for Engineering Stud Greedy and Dynamic Design and Analysis of Algorithms 3 - 22 Programming Algorithmic Strategy withk=2, i=1, j with k= 3, i=1, j=3 Cy = Cyne + Cy; Cy = Cyn-1 4 Cay Ci3 = Cy + Cy Cis = Cp + Cog = 0+15 = 20+0 Cys =15 Cis = 20 Clearly select Cy3 =15 when k = 2 Now — Cyg = min (Cy3} + Wy3 = 15 +35 C3 = 50 Now, For Cog we have i = 0, and j = 3 For ¥; j-1 0 ty4 1, ie. for top to ry3 ie. with k= 1 tok=2, with k= 1, 1=0, j 3 withk=1, i=0, j=3 Cy = Cue + Cay q Cik-1+ CK j = Cy + Cis Cor + Cag = 0+15 = 75+15 Co3 = 15 Co3 = 90 Clearly select Co; = 15 when k = 1 Now Cos = min {Co3}+ Wo3 15 + 100 Cog = 115 " The Cj; and 1; table can be as given below, A Gitide for Engineering Students Greedy aul tyy teat Programming AMuontthon ‘ny Daigne anid dnaQyals Algortttuns Many ‘he tno My La a 10k Fy Me Fon 1 That monn OF Ie the yoo. The ny. 1 bovanies loll bila and ry become he MANE Ohi, Thy; Ville ok eh ‘ yy 0 bos empty felt ohlld mare he, ty th rlaht Finally the troo will be » programming with an GP [SPU : May-14, Marks 8] Q.20 Explain 0/1 Knapsack using dyna example, Ans, + Problem Description ; If we are given n objects and a Knapsack or a bag in which the object i that has weight w; is to be placed. The Knapsack has a capacity W. Then the profit that can be earned is pj The objective is to obtain filling of Knapsack with maximum profit eamed, 1 1 maximized )) px; subject to constraint)’ w,x; < W 0 a where 1 i Sn and n is total number of objects. And x, = 0 or 1 To solve Knapsack problem using dynamic programming we will write the recurrence relation as : RUS. 14] = maximum {table[i-1, j], v; + table[i-1, j-ws } 2W; if or table fi-1,j] if j nf, 4) — ude ‘A Guide for Engineerin& ‘Su yy Greedy and Dynamic Design and Analysis of Algorithms 3-30 Programming Algorithmic Strategy m[L1]+ m [2,3] + Py BPs | = 0 + 360+ (5-10-12) | min = 330 = 960 with k= 2 m({1, 3} = min m [1,2] + m[3,3]+ PoP; P3| = 150 +0 + ($-3-12) = 330 m (2,2]+ m[34] + P, P2 Py = 0 + 180 + (10-3-5) min = 330 . = 330 k=2 m[2, 4] = min m [2,3]+ m[44]+ Py P3 Py | = 360 + 0 + (10-12-50) = 6360 m [3,3] + m [4,5] + P2 Ps P| = 0 + 3000 + (3-12-50)] min = 930 = 4800 k=4 m[3,5] = min m [3A] + m [5,5] + P2 Py B} = 180+ 0+ (3-5+50) = 930 m [44] + m [5,6]+ P3 Py Ps | = 0 + 1500 + (12-5-6) | min = 1860 = 1860 k=4 m[4, 6] = min m[4,5]+ m[6,6]+ P3 Ps Pe | = 3000 + 0 + (12-50-6) LL = 6600 Now we will compute m[1,4], m[2, 5] and m[3, 6] A Guide for Engineering Students Design and Analysis of Algorithms m[1, 4] = min 3-31 m [1,1] + m [24] + PoP Ps —_ Greedy an id Programming Algorithmic onetic = 0 + 330 + (5-10-5) = 580 fm [1.2]+ m[34]+ Po P2 Pa = 150 + 180 + (5.3.5) = 405 m{1,3] + m[4,4] + Po P3 Py = 330 +0 + ($-12-5) = 630 m (2, 5] = min m [2,2]+ m[3,5] + Py P2 Bs = 0 + 930 + (10-3-50) = 2430 m([2,3]+ m[45]+ P P3 B5| = 360 + 3000 + (10-12-50) = 9360 m[24]+ m[5,5]+ P Py Ps min = 2439 = 330 + 0 + (10- 5.50) = 2830 m(3, 6] = min im [3,3] + m[4,6]+ P2 P3 Pe] = 0 + 1860 + (3- 12-6) min = 1770 = 2076 Ere lm [3,4] + m [5,6] + P2 Py Pe] = 180 + 1500 + (3-5-6) = 1770 m[35]+ m[66]+ Pz Ps] = 930 +0 + @-50-6) = 1830 nude A Guide for Engineering atl Greedy and Dynamic Design and Analysis of Algorithms 3-32 Programming Algorithmic Strategy m[1, 1}+ m[2, 5]+ Po FB} = 0+ 2430+ (5-10-50) = 4930 In[1, 2]+ m3, 5]+ Pp P; Ps} = 150 + 930 + (5-3-50) = 1830 jm{1, 3]+ ml4, 5]+ Py P3P3| = 330 + 3000 + (5-12-50) = 6330 m[1, 4]+ mf5, 5]+ Pp Py Ps] = 405 + 0 + (5-5-50) = 1655 iml[2, 2]+ m3, 6]+ P Py Pe = 0 + 1770 +.(10-3-6) = 1950 im[2, 3}+ mls, 6] +R P3 Pel = 360 + 1860 + (10-12-6) = 2940 m|[2, 6] = min m[2, 4] + m[5, 6]+ F PyPe] = 330 + 1500 + (10-5-6) = 2130 m[2, 5]+ m[6, 6]+ PF, Ps Pe] = 2430 + 0 + (10-50-6) = 5430 A Guide for Engineering Students ms 3-33 Design and Analysis of Algorith Gree. — Proj dy an gramming Algorihnd Dina Stra m[1, 1] + ml2, 6]+ PoP, Ps = 0 + 1950 + (5-10-6) = 2250 pala, 21+ m3, 61+ Po P2 Pe| = 150 + 1770 + (5-3-6) = 2010 mala, 31+ mba, 6]+ Po Ps Po| m{1.6] = 330 + 1860 + (5-126) = 2550 fi, 41+ mf5, 61+ Po Ps Ps = 405 + 1500 + (5.5.6) = 2055 mala, 51+ ml6, 6] + Po Ps Pe = 1655 + 0+ (5-506) 3155 min = 201 k=2 The s [i,j] 5 Greedy and Dynamic Design and Analysis of Algorithms 3-34 Programming Algorithmic Strategy The optimal parenthesization will be (Al *A2)*((A3 * Ad) #(A5 * 6)) The s[1, 6] = 2 -.(Al A2) (A3 Ad AS A6) The s[3, 6] = 4 “(Al A2) (3 Ad) (AS A6) ‘Thus we obtain optimal parenthesization as - (Al* A2) *(A3# Ad) *(A5 * A6) END....& A Gulde for Engineering Students unit Iv | Backtracking and Branch “n-Boun, d 4.1: Principle Q.1 Enlist the characteristics of backtracking strategy, CS [SPPU : May-o8, Marks 2) Ans. : ¢ In the backtracking method 1. The desired solution is expressible as an n tuple (x;, x; is chosen from some finite set S;. [SPPU : Dec.-13, Marks 8, Aug.-15, In Sem, Marks 6) Ans. : 4 queen's problem : Refer Q.11. Algorithm : Refer Q.9. A Guide for Engineering Students Design and Analysis of Algorithms 4-13 Bacleracking and Branchn y Boing Fi 9. O.141 State s Pace tree for 4-queen's problem io A Guide for Engineering smd Design and Analysis of Algorithms 4-14 Backtracking and Branch-n-Bound Q.13 For a feasible sequence (7, 5, 3, 1) solve 8 queen's problem using backtracking. GP [SPPU : May-14, Marks 10] Ans. : While placing the queen at next position we have to check whether the current position chosen is on the same diagonal of previous queen's position. If P; = G, j) and P, = (k, J) are two positions then P, and P, lie on the same diagonal ifi+j=k+/ ori-j=k—J, Let us put the given feasible sequence and try out remaining positions. ® 2@, 4@.6@.8 > Already occupied position. j 2 4 isfi 3 5]6]7]8 i 7)s[3fa 7)5)3)1)2 The remaining vacant position is 2. But 4 ~ 1 = 5-2. Not feasible. 7T{s[3aji]4 No conflict. 7/s[3]filafe (3, 3) = 6, 6) -. Conflict occurs. 7TIs|3lilals List ends and we can not place further queen's 7/5|3]1] 4 Hence backtrack. The next free slot 6 is tied. 7/5|3]1]6]2 Not feasible because (7, 1) = (2, 6) 1 +7 =6 +2. That is on same diagonal. Occupied are circled. 7\/s|3]1je]4]2 7|5|3|1| 6 | 4|2|8| Confict because (3, 3) = (8, 8) 71/5(3]}1])6]4)2 Hence backtrack. 7/s[3]ilel4a]s 6, 5) = (8, 7). Not feasible. 7Tls[3afilel4 Backtrack. 7/s[3fife[s 7/s[3filefsl[2 7|5|3|1] 6 | 8] 2] 4] List ends with solution of 8 queen's. A Guide for Engineering Students | 4-15 Backtracking and Branch, Design and Analysis of Algorithms omy Q.44 Generate at least 3 solutions for 5-queen's problem, SSP ISPPU : May-12, Marks 4 ‘Ans. : The solutions are as given below. 1 2 3 4 5 1] @ Solution 1 2 Q 0.35.24 3 Q 4 Q 5 Q 1 3 4 5 ; 2 Solution 2 > 2 @, 4, 1, 3, 5) 3 Q 4 Q a 1 2 3 4 . ; a a Solution 3 : Q (2, 5, 3, 1, 4) 3 4 5 4.5 : Graph Coloring Problem Q.15- Explain graph coloring problem with respect to backtracking. BSP [SPPU : Dec.-13, 14, Marks 8] Ans. : Graph coloring is a problem of coloring each vertex in graph in Such a way that no two adjacent vertices have same color and ytt mrcolors are used. This problem is also called m-coloring problem. If the degree of given graph is d then we can color it with d + 1 colors. Th least number of colors needed to color the graph is called its chromatic ynumber. For example : Consider a graph given in Fig, Q.15.1. “Students A Guide for Engineering Design and Analysis of Algorithms 4-16 Backtracking and Branch-n-Bound Blue Fig. Q.18.1 Graph and its coloring ‘As given in Fig. Q.15.1 we require three colors to color the graph. Hence the chromatic number of given graph is 3. We can use backtracking technique to solve the graph coloring problem as follows - Step 4: A Graph G consists of vertices from A to F, There are three colors used Red, Green and Blue. We will number them out. That meatis 1 indicates Red, 2 indicates Green and 3 indicates Blue color. A Guide for Engineering Students ©s Thus the graph coloring problem is solved, The state-space tree can drawn for better understanding of graph coloring technique Ute backtracking approach - i A Guide for Engineering Sete” . RARE HOE EEL RD Ae Sonate wid awaits ® y Kawe We Dave SANA QOH HNW!L Nad = | Qiae = 2 eat ke = & QMS Waite remerstve RRO OR Br RE RE Dae birch, etratggy Tamrate Che FARE CARMA af Fhe BRR, FTAA S NAHAE NORE AR DRAENOR BNR, NOS 8 OR Write ait athe He RRA CAR BRR RG Dackivackiye methods PIN fF Rigts, PA Gm, Rath, Bad Gam, Nee Q) Ags The algo 6 SFR) COE EERE A AARRRAE RRR, Gen_Ool_Vahry) Soe genenanigs Ode ANN, The BA Atgarithin Gr _ooteryk) JeTDe OFAN QU OA OD is aieeN by aeaceny Matty, SS Got AER Re DRDEENMEG MMe foes Design and Analysis of Algorithms 4-19 Backtracking od Bra me) //Bach vertex is picked up one by one for coloring i //x{k] indicates the legal color assigned to the vertex { i repeat { // produces possible colors assigned Gen_Col_Valua(t); TAKES O(n) tng if(x1k] = 0) then return; //Assignment of new color is not possible, if(k=n) then // all vertices of the graph are colored write(x[1:n)); //print color index else Gr_color(k+1) // choose next vertex | Funtil(false); } The algorithm used assigning the color is given as below Algorithm Gen_Col_Value(k) /x{k] indicates the legal color assigned to the vertex //€no color exists then x{k] = 0 { // xepeatedly find different colors repeat af lk] € (xfk]-+1) mod (m+1) ; //next color index when it is /eighest index {f(xIk] = 0) then //no new color is remaining return; for (j <1 ton) do { #/ Taking care of having different colors for adjacent // vertices by using twro conditions i) edge should be // present between two vertices 11%) adjacent vertices should not have same color AGI j]I=0) AND (xtk] > x{j))) then break; “A Guide for Engineers i Design and Analysis of Algorithms 4.29 Backtracking and Branch-n-Bound } /hi there is no new color remaining if (j= n+1) then return; juntil(false); } Time complexity : The Gr_color takes computing time of ¥ mi! . Hence total computing i=0 time for this algorithm is O(nm’), Q.17 Construct plannar graph for following map. Explain how to find m-coloring of this planar graph by using m-coloring _ backtracking algorithm. c&[sppu ; May-11, Marks 10) Aus. : Steps for graph coloring - Fig. Q.17.1 1. Number out each vertex (V0, V1, V2,...Vn-l) 2. Number out the available colors (C0,C1,C2,...Cn-1) 3. Start assigning Ci to each Vi. (2) While assigning the colors note NN / that two adjacent vertices should Yo not be colored by the same (s) color. And least number of colors should be used. Fig. Q.17.2 4. While assigning the appropriate color, just backtrack and change the color if two adjacent colors are getting the same. 4.6 : Sum of Subsets Problem 9.18 Explain in detail sum of subet problem using backtracking method with suitable example. S&P [SPPU + Aug.-15, In Sem, Marks 4] ft LS ee A Guide for Engineering Students Ay Design and Analysis of Agorihins 1 Sechrecking and Bran Chon, any Problem Statement Ani Z be a set of n positiv integers, then we h, peed cant equal to given positive integer d, O° to fg asi It is always convenient to sort the set's elements in ascending order, Ty is, $1 $ Sp So $5 Let us first write general algorithm for sum of subset Problem, Algorithm Let, S be a set of elements and d is the expected sum of subsets, Then. Step 1: Start with an empty set. Step 2: Add to the subset, the next element from the list, Stop 3: If the subset is having sum d then stop with that subset » solution, | Step 4: If the subset is not feasible or if we have reached the end of the set then backtrack through the subset until we find the most suitable value. Step 5; If the subset is feasible then repeat step 2. Step 6: If we have visited all the elements without finding a suitable subset and if backtracking is possible then stop witho solution, Example : Refer Q19. 49 Analyze sum of subset algorithm on data : M = 35 and DW = 7, 10,12, 18, 18, 20) it) W = (20, 18, 15, 12, 10,05 it) W = 45, 7,20, 5, 18, 10, 12) Are there any discernible differences in the computing time ? Uap [SPPU : May-t4, Mal aera — si ‘A Guide for Engineer"® rks 10) Ans, ; Design and Analysis of Algorithms 4.22 Backtracking and Branch-n-Bound 5,7, 10 22<35 | Add next element 5,7, 10, 12 34<35 | Add next element 5, 7, 10, 12, 15 49>35 | Sum excceds 2: Backtrack | 5, 7, 10, 15 37>35 | Backtrack 5,7, 12 24<35 | Select next element 3, 7, 12, 15 39>35 | Backtrack 3, 10 18<35 | Add next element r 5, 10, 12 27< 35 | Select next element 3, 10, 12, 15 42>35 | Backtrack 5, 10, 15 30 < 35 Select next element . 5, 10, 15, 18 48>35 | Backwack 5, 10, 18 33.<35 | Select next element 5, 10, 18, 20 53>35 | Backtrack 5, 10, 20 35 Solution is found The possible solutions are {5, 10, 20}, {15, 20} and {5, 12, 18}. The sequence (ii) W = {20, 18, 15, 12, 10, 7, 5} and (iii) W = {15, 7, 20, 5, 18, 10, 12} are not in non-decreasing order. The (ii) sequence is in decreasing order and (iii) is totally unsorted. For sum of subset problem, For efficient findings of solution the weights must be arranged in non-decreasing order. to show how this order is beneficial consider a scenario we have selected the elements {5, 7, 10, 12} which sums up 34. Now if we select the next element 15, then this sum will exceed the limit because 49 > 35. So we will simply backtrack. It is not even necessary to examine the Temaining elements {18, 20) of the list. If at all, the list is arranged according to decreasing order of the weights then we have to analyse all the elements of the given list. Similarly if the list is unsorted then each and every element needs to be analysed for required sum. Thus the Sequences (ii) and (iii) makes the computing time worst. A Guide for Engineering Students | Design and Analysis of Algorithms 4-23 Backtracking and Branch-n-Boung 4.7 : Principle and Control Abstraction Q.20 Write short note on - Branch and bound approach. SSP [SPPU : March-20, Marks 5) OR Explain branch and bound approach with suitable example, What are general characteristics of branch and bound ? OP [SPPU : May-19, Marks 8) Ans. : ¢ Branch and bound is a general algorithmic method for finding optimal ‘solutions of various optimization problems. © Branch and bounding method is a general optimization technique that applies where the greedy method and dynamic programming fail, However, it is much slower. © It often leads to exponential time complexities in the worst case. On the other hand, if applied carefully, it can lead to algorithms that run reasonably fast on average. © Branch and bound build the state space tree, and find the optimal solution quickly by pruning a few of the branches of the tree that do not satisfy the bound. e Branch and bound can be useful where some other optimization techniques like greedy or dynamic programming fail. For Example Consider the 4-queens problem using branch and bound method. In branch and bound method a bounding function is associated with each node. The | node with-optimum bounding function becomes an E-node. And children of such node get expanded. The state space tree for the same is as given below - In Fig. Q.20.1 based on bounding function the nodes will be selected and answer node can be obtained. The numbers that are written outside the node indicates the order in which evaluation of tree takes place Refer Fig. Q.20.1 on next page) A Guide for Engineering Studen® | i Design and Analysis of Algorithms 4-24 Backtracking and Branch-n-Bound Fig. Q.20.1 Portion of state space tree using 4-queens = A Guide for Engineering Students am oo” Design and Analysis of Algorithms 4-25 Backtracking and Branch-n-Boung Strategies- FIFO, LIFO and LC Approaches Q.21 Explain in detail control abstraction for LC search. OG [SPPU : Dec.-08, Marks 8; Dec.-10, May-13, Marks 6] + Ans. ; The control abstraction for Least cost search is given as follows. Algorithm LC_search() /itx is a state space tree and x be the node in the tr /7E represents the E-node /fuitialize the list of live nodes to empty { if( tr is answer node) then { write(tr);//output the answer node return; } Ectr //set the E-node repeat { for(each child x of E) do { if( is answer node) then { output the path from x to root; return; //the new live node x will be added in the list of live nodes Add_Node(x); X— parent < E;//pointing the path from x to root; } if(no more live nodes) then { write("Can not have answer nodell"); | return; | //& points to current E-node | ee A Guide for Engineering Studen® l Design and Analysis of Algorithms 4.26 Backtracking and Branch-n-Bound Ec Least_cost();//finds the least cost riode to set next E-node juntil(false); } In above algorithm if x is in tr then ¢(x) be the minimum cost answer node in tr. The algorithm uses two functions Least_cost and Add_Node() function to delete or add the live node from the list of live nodes. Using above algorithm we can obtain path from answer node to root, We can use parent to trace the parent of node x. Initially root is the E node. The for loop used in the algorithm examines all the children of E-node for obtaining the answer node. The function Least_cost() looks for the next possible E-node. Q.22 Write the control abstraction for LC-search. Explain how travelling salesperson problem is solved using LCBB, US [SPPU : May-14, Marks 8] Ans. : Control Abstraction for LC Search : Refer Q21. TSP using LCBB : Refer Q.25. Q.23 What is branch and bound method ? Explain FIFO branch and bound algorithm. US [SPPU : Dec.-14, Marks 8] Ans. : Branch and bound : Refer Q.20. FIFO branch and bound Algorithm : Algorithm FIFOBB() //tt is a state space tree and x be the node in the tr //E represents the E-node //initialize the list of live nodes to empty _ { if( tr is answer node) then { in(cost(tr] (upper(tr)+E)) write(tr);//output the answer node return; } Ectr //set the E-node repeat { A Guide for Engineering Students i . B Design and Analysis of Algorithms 4-27 Backtracking and Branch-n-Boung for(each child x of E) do { if(x is answer node) then { output the path from x to root} return; } //the new live node x will be added in the list of live /inodes Insert_Q(x); xparente E;//pointing the path from x to root; if(x is answer node) AND cost(x)

You might also like