0% found this document useful (0 votes)
29 views10 pages

Daa (U2)

DAA unit 1 notes
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)
29 views10 pages

Daa (U2)

DAA unit 1 notes
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/ 10
& ‘l KHIADAR SiR 9966648277 GREEDY METHODS: - Greedy methods work in multiple phases, In each phase, a decision is made which appears to be good. The decision is made without considering the future consequences. It makes a decision in the hope that this decision will eventually lead to the right solution. If it is, then the algorithm is correct, otherwise the algorithm will produce a sub-optimal solution. General Algorithm: Greedy (A[] ,n) { //A\gorithm takes as input an array A of n elements. solution(]={}; //Initially empty for(i=O;iensi++) { //select an input from A =select(A) if(Feasible(x)) { solution=solution + x; remove(X) //Remove selected item from input } } return solution; } A selection function - Used to choose the best element to be added to the solution, A feasibility function - Indicates whether the selected element is valid or not. KNAPSACK PROBLEM: - Given a set of items, each with a weight and a value, we have to determine the number of each items to include in a collection so that the total weight is less than a given limit and the total value is a the maximum. possible, Let pli] be the profit corresponding to item i and wfi] be the weight of item i. Let x{i] be the fraction taken from item i, where iz1 to n. The problem is to find x{1.....n] so that x{1]p{1}+x{2Ip[2).......*x{n]pln} is moximum and x[1]w[1}+x{2]w[2}.....*x{nJw[n] « C. where ‘C’ is the maximum limit. Greedy Selection policy: Policy 1:- Choose the lightest (less weight) element and take of it as much as can fit in, Policy 2:- Choose the most profitable remaining item and take of it as much as can fit in. KHADAR SIR 9966648277 i iVwli]) and take Policy 3:- Choose the element with highest profit per weight (plil of it as much as can fit in. cy 2 does not gua! ytimal solution. n whereas Policy 3 rantee an optimal solutiot Policy 1 and Poli always gives an oP Example:- pl5 948 wl 1322 | C4 Pe The lightest element is Next lightest element is profit. So item 4 should be taken: x [4] = st the next lightest element i = 3. But its weight is ] units in the bag. So only 1 is left so we can take half o [3) = 1/2: Since the bag is full, x [2]= 0 Solution: x [1,2.3,4] = [1, 0, 1/2.1] And gain = (1 * 5)+(0* 9) + (1/2 * 4) + (1* 8) > Profit = 15 Policy The most profitable item is i=2. So take second item: x[2] = Next ere ae ee is 2 and the remaining capacity is 1. Since the capacity is filled, x[1] and x[3] will be 0. Solution: x[12,3,45(0,1,0,4] Profit = (0*5)+(1*9)+(0*4)+(1/2*8) Profit = 13 Policy 3:-i =1 2 py =5 9 4 8 Le so take the first item; x 0 -3 and 4, But out of two items 4 has the maximum 2 and we have already filled 3 item 3 to fill the bag: x Plywo=5 3 2 4 The highest profit per weight is 1, so take first item; x[1] =1; —_— =) Ae - / ¥ Seer Next highest profit per weight is is4: so take fourth item: x{4J=1: Next highest profit per weight is item 2. But its weight is 3 and we are left with only 1. So we can take 1/3 of item 2: x[2] = 1/3: Solution : x{1.2,3,4]=[1,1/3,0,1] Profit = (1*5)+-(1/3*9)+(0*4)+(1*8) Profit = 16 Algorithm Greedy_ Knapsack (w(J, plJ, x{I, C, n) { /*C is the maximum capacity, n is the number of items x] is solution vector, w{] is weight vector & pl] is profit vector, Items are sorted according to policy. */ for (iz0 , en i+) xf Usm; /*U is the unused capacity */ for (i= 0; ien&&w{ik=U; i++) £ x{iJ= 1 U=U-w[i]: } if (en) x{i] = U/wli} } JB SEQUENCING WITH DEADUNES: Problem: - There are n jobs, S = 2.3,4....n}. Each job i has a deadline di > 0 and a profit P20, At a time only one job can be executed and we need one unit of time to complete each job. The problem is to earn maximum profit by completing some jobs in given deadline. Algorithm:- Step 1: Sort P, into descending order. Step 2: Add the next job i to the solution set if i can be completed by its deadline. Step 3: Stop if all jobs are examined. Otherwise, go to step 2. Example:- Pil... 105, 15,20,1] d{t....n] = 1.3,22,3] Sort PL] Uh KHADAR SIR 9966648, ~_| Assign 1 to solution [1] 2__| Assign 2 to solution[1,2] Reject as deadline is crossed Assign 4 to solution [1,2,4] fe ae Reject as deadline is crossed Solution: - {1,2,4} and total profit = 20+15+5-40 SINGLE SOURCE SHORTEST PATH : - in a weighted directed graph G=(V,E) one verte is specified as the source. The problem is to find the cost of the shortest patl from source to every other vertex in V. To solve this problem we can use a greedy technique called as Dijkastra's algorithm. The algorithm works by maintaining a set S of vertices whose shortest distance from the source is already known, Initially, S contains only the source vertex, At each step we add a vertex v to S whose distance from the source is as short as possible. A shortest path from source to v that passes only through vertices in s is called as special path, At each step of the algorithm we use an array D to record the length of the shortest special path to each vertex in S. when $ includes all vertices, At this point D will contain the shortest distance from source to each vertex. DITKASTRA(V.ny) C J Tnitializing D For isl ton-l £ we w © V-S such that Dfw] is minimum S= Su (w) For (each vertex vin V-S) { Diviemin(dtv].dtw}ectw, wD); ) } ) Exampl KHADAR Sin 9966648277 consider the below graph Initially, adjacency matrix 2 2 3 4 a D{5}=100 A 0 10_ |e 30 | 100 | [2 |e Jo [50 [x [oo Add 2 to the solution since D[2] is smaller than [3 co |e lo eo |10 the rest 4 |e [= [20 {0 [60 Now; S=(1.2} S__|so_|er Jer Jo 0 Find the shortest path of 3,4,5 from source using 2 as intermediate vertex [3] = min {D[3],D[2}+ c[2,3} DI3}Fmin{ « 10+50}=60 DAF min(DL4].dl2}+c(2,4)} D[4}- min{30,10+ =} =30 Similarly, D5] = 100 Now D[2..5]={10,60,30,100} Remaining vertices which are not in 5, V-S=(3,4,5} Add 4 to the solution since D[4] is smaller than the rest Now , S=(1,2,4) Find the shortest path of 3, 5 from source using 4 as intermediate vertex D{3] =min {DI}, D4 }+C14,3), DI3Fmin(60,30+20}=50 D[B]emin(O[5],D(4hCl4 5), D[5}-min (100 30+60} =90 There-fore the array D will be , DI2......5]=[10,50,30,90] Remaining vertices which are not in S, V-S = {3,5} ‘Add 8 to the solution since D[3]is smaller than the rest Now, 5:(1.2,4,3) Find the shortest path of 5 from source using 3 as intermediate vertex D(SFmin (O151.0(3}+C13.5) DIB] = min(90,50+10) = 60 There-fore, D[2....5} = [10,50,30,60] Now ,D contains the cost of shortest paths from 1 to all other vertices —= GL oe BE KHADAR SIR 9966648277 MINIMUM SPANNING TREE The minimum spanning tree of a weight graph G, is a tree which contains all the vertices of G and does not contain a cycle, And The minimum spanning tree that 's formed from the graph should have minimum overall cost. ‘Minimum spanning Tree Example: Graph ‘Minimum spanning Tree of given Graph APPLICATIONS OF MINIMUM SPANNING TREE: By This technique we can reduce the cost in following 4 i. To link n stations using a communication network, To construct highways or railroads to several cities then we can use the concept of minimum spanning trees iii. Designing Local Area Networks. iv. To supply following for set of houses in . Electric Power Water Telephone lines Sewage lines PRIM'S ALGORITHM: . Take the least weighted edge from the graph and add it to the tree. If there ‘are more than one such edges, any one can be take from it. . As per the prim's algorithm, the next edge that should be added to the tree should satisfy the below two conditions: i. Anew edge can be added to an existing tree (the new edge must be adjacent to existing tree edges) . And it should not lead to a cycle. ii, The cost of the edge being added should be minimum from the available. edges that can be added. . The process will stop when all the vertices of graph are included in the tree. Example:- Construct the minimum spanning tree (MST) for the given graph using Prim's Algorithm- be ee a Le KHADAR SIR 9966648277 Solution- First start with the edge that has the least cost aoa Next possible edges are 6-5, and 1-2. But the edge with least cost is 6-5 and it does not form a cycle too, = So continuing similarly se eet Since all the vertices have been included in the MST, so we stop Now, Cost of Minimum Spanning Tree = Sum of all edge weights 310+ 25 +2212 + 16+ 14 = 99 units Example2: Using Primm's Algorithm, find the cost of minimum spanning tree (MST) of the given graph- 3S a KHADAR SIR 996664 Solution- Now, Cost of Minimum Spanning Tree um of all edge weights 2144424643410 6 units ALGORITHM: PRIMS-MST (6.E,V.n) { T=()_//Tritially empty tree while (T(¥) ¢) ‘ Heopity (hE) nextvertex = true: while (nextvertex) (uv) = del-heap (h): if(ueT 48 veTY, remove (x) else if (ueT I] veT)) Union (Tx) // Add x to T remove (XE) // Remove x from E nextvertex = false: } else if (== NULL) { Union (Tx): remove (x); nextvertex = false: KRUSKAL’S ALGORITHM: 4. Take the least weighted edge from the graph and add it to the tree. Tf there are more than one such edges, any one can be take from it, y In this algorithm, the next edge that should be added to the tree should satisfy the below two conditions: i, It should be of minimum cost in the remaining edges that can be added. ii, Tt should not forma cycle, It is not necessary that it should form a tree. 3. The process will stop when all the vertices of graph are included in the tree. Example: Construct the minimum spanning tree (MST) for the given graph using Kruskal's Algorithm- Fist start with the edge that has the least cost 10 ‘Add Next Edge with least cost Add next edge with least cost Similarly: Seas KHADAR SIR 9966548277 Since all the vertices have been connected / included in the MST, so we stop. Weight of the MST um of all edge weights 0+ 25+ 22+12+16 +14 = 99 units | ALGORITHM:- KRUSKALS- MST (6,E,V,n) i T=(}_ // Initially empty tree while (T(v)

You might also like