t00340020120134062t0034-pert 11 & 12-greedy methods(1)

Upload: richmafia21

Post on 08-Mar-2016

218 views

Category:

Documents


0 download

DESCRIPTION

greedy

TRANSCRIPT

  • Session 11 & 12 Greedy MethodsCourse: T0034 Algorithm Design & AnalysisYear: 2013

  • Bina NusantaraSOLUTION OF PROBLEMSOne of the functions of the algorithm is to solve problems/issues.

    A problem could be:Having a single set of solutionsHaving multiple sets of solutions that are equally trueHaving multiple sets of solutions with different levels of truthDo not have a set of solutions that "total" true, but there are several sets of solutions close to the truthDo not have the set of solutions at all

  • Bina NusantaraPROBLEM SOLVING TECHNIQUESSome of the popular techniques in problem solving:Greedy methodDynamic ProgrammingData compressionBacktrackingBranch and Bound

  • Bina NusantaraGREEDY METHOD Greedy means rapacious = serakah.

    Algorithms often perform some calculation phase before finding a final solution.

    Sometimes these stages produced a number of elements that will be used to formulate the final solution.

    Greedy method will "ignore" a complete calculation in the search for solutions, replacing it with using those elements to calculate the solution more quickly.

    Very useful if the search for solutions 100% take too long rather than waste time trying to calculate the "best" solution, it is better saved by finding a "good enough" solution.

  • Bina NusantaraUNIVERSAL SOLUTIONA problem has:Universal solutionall the possible solutions both right and wrongFeasible solutionall possible correct solutions, but different levels of truthObjective functionthe function to measure the solution which is more correct (better)Optimal solutionthe most correct solution (best)

  • Bina NusantaraKNAPSACK PROBLEMA thief entered a house. He was carrying a bag with maximum capacity 20 kg. At the house there are items A, B, C, D, E. A weighed 7 kg, valued at $ 2975 B weighed 3 kg, valued at $ 1230 C weighed 9 kg, valued at $ 3870 D weighed 2 kg, valued at $ 840 E weighed 5 kg, valued at $ 2250What items to bring the thief for getting maximum benefit? (must not exceed the capacity of his bag of 20 kg)

  • Bina NusantaraVARIATION OF KNAPSACK PROBLEMFractional Knapsack ProblemItems should be brought in part (in fractional units). 0/1 Knapsack ProblemEach item is only available 1 unit, take it or leave it. Bounded Knapsack ProblemEach item is available in N units (limited number). Unbounded Knapsack ProblemEach item is available in more than one unit, the number is unlimited.

  • Bina NusantaraCALCULATION FEASIBLE SOLUTIONIf the above example is the case of 0/1 Knapsack, its feasible solutions:

    ItemWeightValueItemWeightValue{}00{A,B,C}198075{A}72975{A,B,D}125045{B}31230{A,B,E}156455{C}93870{A,C,D}187685{D}2840{A,C,E}219095{E}52250{A,D,E}146065{A,B}104205{B,C,D}145940{A,C}166845{B,C,E}177350{A,D}93815{B,D,E}104320{A,E}125225{C,D,E}166960{B,C}125100{A,B,C,D}218915{B,D}52070{A,B,C,E}2410325{B,E}83480{A,B,D,E}177295{C,D}114710{A,C,D,E}239935{C,E}146120{B,C,D,E}198190{D,E}73090{A,B,C,D,E}2711165

  • Bina NusantaraSOLUTION0/1 Knapsack ProblemCan not be solved by Greedy MethodWhy?

    Unbounded Knapsack ProblemCan be solved by Greedy Method.Calculate the value per kg.Take the highest value.Take the item with the highest value as much as possible (note the limit on the weight and the amount of availability of goods).If there are remaining weight, use of goods with the highest value and so on.Calculate the complexity of the algorithm!

    Fractional Unbounded Knapsack ProblemCan be solved by Greedy MethodCalculate the value per kg.Take the highest value.Enter the item with the highest value as much as possible (note the limit on the weight and the amount of availability of goods).Calculate the complexity of the algorithm!

  • Bina NusantaraUNBOUNDED KNAPSACK PROBLEM (1)Step 1: calculate the value per kgItem A weighing 7 kg, value $2,975 value per kg = $425Item B weighing 3 kg, value $1230 value per kg = $410Item C weighing 9 kg, value $3,870 value per kg = $430Item D weighing 2 kg, value $840 value per kg = $420Item E weighing 5 kg, value $2250 value per kg = $450

    Step 2: take the highest valueThe highest value is $ 450 per kg, mean item E will be taken.

    Step 3: Take the item with the highest value as much as possibleMaximum weight that can be taken is 20 kg, item E are very large (unlimited), means that the thief was able to bring home 4 pieces of item E. The gains is 4 x 2250 = $ 9000

    Step 4: if there is residual weight, take the item with second highest value and so on.In the above case, there is no rest for 4 pieces of heavy item E already has a total weight of 20 kg, equal to the maximum weight limit.

  • Bina NusantaraUNBOUNDED KNAPSACK PROBLEM (2)If maximum weight is changed to 23 kg Step 1: calculate the value per kgItem A weighing 7 kg, value $2,975 value per kg = $425Item B weighing 3 kg, value $1230 value per kg = $410Item C weighing 9 kg, value $3,870 value per kg = $430Item D weighing 2 kg, value $840 value per kg = $420Item E weighing 5 kg, value $2250 value per kg = $450

    Step 2: take the highest valueThe highest value is $ 450 per kg, mean item E will be taken.

    Step 3: Take the item with the highest value as much as possibleMaximum weight that can be taken is 20 kg, item E are very large (unlimited), means that the thief was able to bring home 4 pieces of item E. The gains is 4 x 2250 = $ 9000

    Step 4: if there is residual weight, take the item with second highest value and so on.Item C (the second highest value) can not be taken due to exceeding the weight limitItem A (the third highest value) can not be taken due to exceeding the weight limitItem D (the fourth highest value) were taken in increments of 1 pieces, total weight is now 22 kgThe remaining weight of 1 kg, there are no other items that can be brought4 items E and 1 D of goods worth $ 9,420

    If the thief was carrying four E and a B, he can earn $ 10,320 Greedy method is not optimal

  • Bina NusantaraFRACTIONAL KNAPSACK PROBLEMMaximum weight 23 kg Step 1: calculate the value per kgItem A weighing 7 kg, value $2,975 value per kg = $425Item B weighing 3 kg, value $1230 value per kg = $410Item C weighing 9 kg, value $3,870 value per kg = $430Item D weighing 2 kg, value $840 value per kg = $420Item E weighing 5 kg, value $2250 value per kg = $450

    Step 2: take the highest valueThe highest value is $ 450 per kg, mean item E will be taken. Step 3: take the item with the highest value as much as possibleMaximum weight that can be taken is 23 kg, item E is available in a number of denominations, meaning the thief can bring home the goods 4.6 E. The advantage obtained is 4.6 x 2250 = $ 10,350 This solution the Greedy method produces an optimal solution!

  • Bina NusantaraJOB SEQUENCING WITH DEADLINES

    A simple form of scheduling problems.There are a number of jobs to be done.Job will generate profits if successfully completed before the deadline (deadline).

  • Bina NusantaraCASE EXAMPLEn=4; (p1,p2,p3,p4)=(110,47,25,20); (d1,d2,d3,d4)=(2,1,2,1)

    d1,d2,d3,d4 deadlinep1,p2,p3,p4 profit

    There are 4 jobs.Job 1 can generate profits 110 if completed by the deadline in the second order (sequence).Job 2 can generate profits 47 if completed by the deadline in the first order (sequence).Job 3 can generate profits 25 if completed by the deadline in the second order (sequence).Job 4 can generate profits 20 if completed by the deadline in the first order (sequence).

    Dengan teknik biasa (complete) :(1)sequence (1)profit 110(2)sequence (2)profit 47(3)sequence (3)profit 25(4)sequence (4)profit 20(1, 2)sequence (2, 1)profit 157(1, 3)sequence (1, 3) or (3, 1)profit 135(1, 4)sequence (4, 1)profit 130(2, 3)sequence (2, 3)profit 72(3, 4)sequence (4, 3)profit 45

  • Bina NusantaraUSING GREEDY METOHD

    JobConsiderationActionJob 1Can be assigned to [1,2], accept{1}Job 2Cant fit, reject{1}Job 3Can be assigned to [1,2], accept{1,3}Job 4Cant fit, reject

  • GRAPH vs TREEA tree can be described as a specialized case of graph with no self loops and circuits.There are no loops in a tree whereas a graph can have loops.There are three sets in a graph i.e. edges, vertices and a set that represents their relation while a tree consists of nodes that are connected to each other. These connections are referred to as edges.In tree there are numerous rules spelling out how connections of nodes can occur whereas graph has no rules dictating the connection among the nodes.

  • Bina NusantaraSPANNING TREESpanning Tree is a Tree that created from Graph by eliminating some its edge. This tree should contain all the nodes owned Graph.

  • Bina NusantaraMINIMUM SPANNING TREEA single graph can have many different spanning trees.If a Weighted Graph is changed to Spanning Tree, every combination of the Tree can be created to have sum of different weight.Problem in Minimum Spanning Tree (MST) is to get tree (as from a Weighted Graph) with minimum sum of the weight.

  • Bina NusantaraMST USING METODE GREEDYPrim-Dijkstra algorithmwas invented by Robert C. Prim in 1957 and Edsger Dijkstra in 1959.Kruskal algorithmwas invented by Joseph Kruskal in 1956.

  • Bina NusantaraPRIM-DIJKSTRA ALGORITHMSteps:Determine the initial node, assume all the edges are black.All edges are connected to the initial node, select the edge with the smallest weight.Mark the selected edge with the color green.If there is a second edge node was already in the green path, mark the edges with red color (because if it is selected it will form a round path violate the requirements of tree)Specify the nodes that are in the green path as an active node.Compare all edges are connected to active nodes (only black edge), select the smallest weightMark the selected edge with the color greenRepeat from step-4 until all the nodes crossed by the green path.When all nodes have been crossed by the green path, then the formed green path is a searched MST.

  • Bina NusantaraKRUSKAL ALGORITHMSteps:Assume all edges are black.Compare the weights all the black edge, select the edge with smallest weight (the shortest arc).Mark the selected edge with the color green.If there is an edge that both nodes are in the green path, mark the edge with red color (because if it is selected it will form a cycle path that violate the requirements of tree).Repeat step-2 until all nodes are crossed by the green path.When all nodes are crossed by the green path, then the made green path is MST that searched.

  • Bina Nusantara

  • Bina NusantaraEXAMPLE MST PROBLEM

  • Bina NusantaraSHORTEST PATHIn a graph each edge has weight, the shortest path between 2 nodes can be search by Greedy Method.

    Suppose we want to find the shortest path (shortest path) from node A to node F, how is it calculated with the method Greedy?

  • Bina NusantaraSHORTEST PATH USING GREEDY METHOD Steps:Departing from the initial node.Select the edge that has the smallest weight of node.Advanced to the destination node.Repeat from step 2 until the destination node.

  • Bina NusantaraEXAMPLE PROBLEM OF SHORTEST PATH

  • Bina NusantaraOPTIMAL SOLUTION?Is Greedy Method for Shortest Path problem is really the best solution?

    Just compare the following solution:

    Greedy method produces a pretty good solution, but not the best solution.Discuss why is that?

  • Bina NusantaraEXERCISESA thief entered a house. He was carrying a bag that is only fit to carry 14 kg of goods. Inside the house there are items A, B, C, D, E.Item A weighs 4 kg, value $980Item B weighs 5 kg, value $1275Item C weighs 2 kg, value $480Item D weighs 6 kg, value $1410Item E weighs 3 kg, value $750All goods in powder form, so it could take some (not to be taken in units of round). Decide which items should be taken of the thief and the amounts for maximum results!

    There is a problem of sequencing jobs as follows: n=6 (p1,p2,p3,p4,p5,p6)=(65,75,45,30,35,70) (d1,d2,d3,d4,d5,d6)=(2,1,1,2,3,2)Specify the jobs to be taken based on the method Greedy!

  • Bina NusantaraEXERCISECreate a Minimum Spanning Tree using Prim-Dijkstra algorithm and Kruskal algorithm.

    Determine Shortest Path from node A to node F using Greedy Method!

    Discuss: why Greedy Method violate to get the best solution!

  • Bina NusantaraREVIEWSOLUTION OF PROBLEMSPROBLEM SOLVING TECHNIQUESGREEDY METHOD UNIVERSAL SOLUTIONKNAPSACK PROBLEMVARIATION OF KNAPSACK PROBLEMCALCULATION FEASIBLE SOLUTIONUNBOUNDED KNAPSACK PROBLEM (1)UNBOUNDED KNAPSACK PROBLEM (2)FRACTIONAL KNAPSACK PROBLEMJOB SEQUENCING WITH DEADLINES

  • Bina NusantaraREVIEWGRAPH vs TREESPANNING TREEMINIMUM SPANNING TREEMST USING METODE GREEDY

  • Bina NusantaraBooks ReferencesReferences:Computer Algorithms / C++Ellis Horowitz, Sartaj Sahni, Sanguthevar Rajasekaran.Computer Science Press. (1998)Introduction to AlgorithmsThomas H Cormen, Charles E Leiserson, Ronald L.3nd Edition. The MIT Press. New York. (2009)Algoritma Itu MudahRobert Setiadi.PT Prima Infosarana Media, Kelompok Gramedia.Jakarta. (2008)

  • Bina Nusantara

    END