This preview shows pages 1–5. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 1.204 Lecture 10 Greedy algorithms: Knapsack (capital budgeting) Job scheduling Greedy method Local improvement method Does not look at problem globally Takes best immediate step to find a solution Useful in many cases where Objectives or constraints are uncertain, or An approximate answer is all thats required Generally O(n) complexity, easy to implement and interpret results Often requires sorting the data first, which is O(n lg n) In some cases, greedy algorithms provide optimal solutions (shortest paths, spanning trees, some job scheduling problems) In most cases they are approximate algorithms Sometimes used as a part of an exact algorithm (e.g., as a relaxation in an integer programming algorithm) 1 2 General greedy algorithm // Pseudocode public solution greedy(problem) { solution= empty set; problem.sort(); // Usually place elements in order for (element: problem) { if (element feasible and appears optimal) solution= union(solution, element); return solution; } Some greedy algorithms sort, some use a heap, some dont need to sort at all. Greedy knapsack problem We have n objects, each with weight w i and profit p i . The knapsack has capacity M M w t s x p i n i i < . . max The knapsack has capacity M. n i w p x M x w i i i i n i i < <= < , , 1 Greedy knapsack algorithm Algorithm chooses element with highest value/weight ratio first, the next highest second, and so on until it reaches the capacity of the knapsack. This is the same as a gradient or derivative method. Knapsack: integer or not? Let M= 1. Integer solution is {2, 3}, an unexpected result in some contexts. Greedy solution is {1, 98% of 2}. If problem has hard constraints, need integer solution. If constraints are fuzzy, greedy solution may be better. 3 Knapsack problems Truck packing: integer knapsack Packing problem in 2 and 3 dimensions is extension Packing problem in 2 and 3 dimensions is extension Investment program: Greedy knapsack at high level Can be integer knapsack at individual transaction level (Highway investment or telecom capital investment programs often handled as integer problem, with occasionally hardto interpret results) Used to train telecom execs for spectrum auction Interactions between projects: Greedy can be extended to handle interactions between small numbers of projects (that can be enumerated) Integer program handles this explicitly Greedy knapsack code, p.1 public class Knapsack { private static class Item implements Comparable { public double ratio; // Profit/weight ratio public int weight; public Item(double r, int w) { ratio = r; weight = w;...
View Full
Document
 Spring '10
 GeorgeKocur

Click to edit the document details