You are on page 1of 12
a a etnos Analysié-of Algorithm and Design 3-3 Groody Mothod t 3.3 Knapsack Problem Wis is he Krapeck probe cn be stated as follows. suppose there are n obj Each object i has some positive tx of ig tm object p x of weight wrand some profit value is associated wi FR is denoted a5 P. bb the ‘This Krupsuck carry at the most weight W. f While solving above mentioned Knapsack problem we Ts the capacity stew con Wien aes to solve this problem using Greedy approach our goa! iy im profit. a 1. Choose only those objects that give maximu 2. The total weight of selected objects should be = W: “And Then we can obtain the set of feasible solutions. In other words, oO C on) it of - n)Z coke tion (ry arm Where the Knapsack can carry the fraction Xi of an object i such that 0S x, $1 dnd = Consider that there are three items, Weight and profit value of each stem is Soom AIG?! to i fw] 1 18 30 vel 137 2 | 1 | 2 Fo wOI00) ap op | 1g < 207 sotution for the above given Knapsack prob ‘Also W=20. Obtain the given below. sible solutions are as olution : The fea: Xx | Xs Scanned with CamScanner * Algorithm 3.2 Algorithm Greedyknapsack(m, n) 77 pile nY and wf n] contain the Profits and weights respectively // of the n objects ordered such that PLI/wlil = pli + 1)/wii + 2), 1 2 3 4 //m is the knapsack size and x[1 : n] is the solution vector. Be 6 7 8 fori z= 1 ton do xfi] := 0.0; // Initialize x. Uiem; Store Ceracihy ~%» UV. fori :=1tondo t tokrng | 10 if (w[i] > U) then break; ia 13 HFG sn) then xii] = U/WLIl; seteep forcing Bt. - 14} oo % 4 a Scanned with CamScanner Example 2: Consider the following instance of the knapsack problem: n = 7, m = 15, (PryPzs-e----P7) = (10,5,15,7,6,18,3), and (Ws,W2,....,W7) = (2,3,5,7,1,4,1). Four feasible solutions are, po 2~2=) TPS BUHT=H4G PWS 74s oye Knapsack problem n = 7,m=15 neo rz) Te = ge s2 (D1, P2---P:) = (10, 5, 15, 7, 6, 18, 3) and (1, Wa, ..w) = (2,3,5,7,1,4, DD irst we find p, VP = 62XS42/2 = 3242333 ros 8 TO = IBGSBY E18 Optrne) SY? , Ch, 23,10, ND 1) 7 { t o T= OCK* >| HIN AN 0 a 1, 6, 3, 7,2, 4) we consider x; = 1, profit = 6 then we consider x, = 1 Total profit = 6+10=16 Weight uptil now = 1+2=3 X% = 1 Scanned with CamScanner Applications of spanning trees : ce ; sthms. 1. Spanning trees are very important in designing efficient routing a 2. Spanning trees have wide applications in many areas such as ne 3.4.1. Prim's Algorithm Let us understand the prim's algorithm with the help of example. Example : Consider the graph given below : Fig. 3.2 Graph Now we will consider all the vertices first, Then we will select an edge with minimum weight, The algonthay roceeds by selecting adjacent edges with minimum ‘weight: Care should be taken for not forming arcuik BIE Sate should be taken for not forming circuit. Step 7: @ @ © oO © Oo @ Total weight = 0 Total weight = 1 Scanned with CamScanner Step 3: a © @ ® Total weight = 33, Step 4: Total weight = 53 Step 5: @ @ 20 Total weight = 64 Scanned with CamScanner Step 6: Total weight = 78 Step 7: Tote: weight = 90 Scanned with CamScanner bee pres OF Fd Y B]p pg Pp, Pav Ts apy ten- d tWwle(ro df elas 7 tee T $ —Hhervte Arn hea Wiese te _# ae bor Cee te Ytte — BhS (2) te tree —Nro A A We leat ote , 4h 659) nop Scanned with CamScanner 3.4.2. Kruskal's Algorithm Kruskal’s algorithm is anothe algorithm is di algorithm alwa ¥ algorithm of obtaining minimum spanning tree. Th pvered by a second year graduate student Joseph Kruskal. In this the minimum cost edge has to be selected. But it is not necessary that seleSted_optimum edge =-atacenr Difference between Prim's and Kruskal's Algorithm __ Prim’s algorithm Kruskal's Algorithm This algorithm is for obtaining minimum This algorithm is for obtaining minimum. spanning tree by selecting the adjacent vertices| spanning tree but it is not necessary to choose of already selected vertices. adjacent vertices of already selected vertics. Let us understand this algorithm with the help of some example. Example : Consider the graph given below : Scanned with CamScanner First we will select all the vertices. Then an edge with optimum weight is selectea from heap, even though it is not adjacent to previously selected edge. Care should be taken for not forming circuit. Step 1: Oo ® o @ oO © Step 2: Total weight = 0 ©) © O O GO © Step 3: Total weight = 10 oO oe Total weight = 24 Scanned with CamScanner ° @) @ 7S tg / 3S 1 © Total weight = 33, Step 5: Step 6: Total weight = 67 Scanned with CamScanner Step 7: 20 Total weight = B42 Aten Weight = 90 Scanned with CamScanner ci ithe euskal Ge, ay 4 A atlile poo] eetges in Wee _T FY Sen — a — = _ me a ee Let w be “pewd adage ¥ Prom re oN 7 Adaw ia dye eT 2G AN creates a cy cle int), 7 “dischavde Abe edge we fae 4 y Aree | ___—__—— boo — _ — Ganite C4 hese lees them wo) ects DS » eves eige CVO) fo Et feseot eS re £4)! lr cate eye *O_ % thea

You might also like