Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 361 – Chapter 10 “Greedy algorithms” It’s a strategy of solving some problems –Need to make a series of choices –Each choice is made to maximize current.

Similar presentations


Presentation on theme: "CS 361 – Chapter 10 “Greedy algorithms” It’s a strategy of solving some problems –Need to make a series of choices –Each choice is made to maximize current."— Presentation transcript:

1 CS 361 – Chapter 10 “Greedy algorithms” It’s a strategy of solving some problems –Need to make a series of choices –Each choice is made to maximize current benefit, ignoring the future. –Often, some sorting is necessary. –How do we know a greedy technique works? We try to show that no other algorithm can achieve a better result. –Several examples

2 (1) Maximizing jobs Given a set of jobs, with specific start and finish times, what is the maximum # of jobs we can schedule? –Assume that you can’t do 2 jobs at the same time. –Assume time is represented as a whole number, and you may start a job as soon as another is finished. Try this: Sort list of jobs ascending by finish time. W = set of jobs we’ll do: initialize to empty. for j = 1 to n: if (job j doesn’t conflict with W) add j to W return W

3 Example Suppose we have this list of jobs, with (start, finish) times. j1=(1,4) j2=(3,5) j3=(0,6) j4=(4,7) j5=(3,8) j6=(5,9) j7=(6,10) j8=(8,11) Jobs are already sorted. Add j1 to W. We see that j2 and j3 conflict. (They don’t get reconsidered later.) Add j4 to W. We see that j5, j6 and j7 conflict. Finally, add j8 to W.

4 Optimal This greedy algorithm produces an optimal schedule. –Recall the goal is to maximize # jobs to do, given fixed start and finish times. –If the algorithm can schedule k jobs, no other algorithm (an optimal solution) would be able to give you k+1. Proof by contradiction –Let i 1, i 2, i 3, … i k be the set of jobs given by greedy algorithm. –Let j 1, j 2, j 3, … j m be the set of jobs in an optimal solution. (m > k) –Define r: The largest integer where first r jobs in each set match. –The corresponding #r+1 job in each list differ. In particular, j r+1 finishes after i r+1. –In the optimal solution, we can replace j r+1 with i r+1. The set of jobs is still legal and optimal. But we have a contradiction, because now the first r+1 jobs match.

5 (2) Minimizing rooms A conference consists of many events: lectures and workshops of various lengths. Like a job, each event has a known start and finish time. To simplify the model, again we’ll assume time is expressed in whole numbers. Produce a schedule that minimizes the number of rooms. –Assume that no 2 jobs may take place at same time in same place. –Inside a room, an event may begin as soon as another one ends. Similar to previous problem except we have to schedule all events. We are not necessarily interested if 1 room has a maximal number of events taking place inside.

6 Algorithm Sort list of events ascending by start time. numRooms = 0 for i = 1 to n: if event #i is compatible with some room r schedule event #i in room r. else ++numRooms schedule event #i in the new room return room list schedule Example e1(0,7) e2(0,3) e3(4,7) e4(4,10) e5(8,11) e6(8,11) e7(10,15) e8(12,15)

7 Optimal We can scan the list of events to see what the minimum number of rooms is. At each time t, see how many rooms are needed. Want to show the greedy algorithm will generate a schedule with this optimal number of rooms. Let d = # rooms the greedy algorithm schedules. Room #d is created because event #i conflicted with other d–1 rooms. Event #i and other d – 1 events at that time all finish after #i’s start time, or else there would have been no conflict. The other d – 1 events started before event #i. Therefore, at the time event #i starts, we indeed have d events taking place at the same time. So the optimal schedule must use d rooms.

8 (3) Fractional knapsack You discover a treasure chest. But you can only carry away some of the treasure. Each type of treasure has some weight and some value. –Subject to a total weight constraint, need to maximize total value of treasure to haul away. Formally: –For each item in some set, we have a value v i and a weight w i. We may select x i of the item. –Limits: total weight W you may take away, and available weight w i of each item. –We wish to maximize x i v i / w i. You can think of x i / w i as what proportion of all the w i you are taking. For example, chest may contain silver and gold; nickels and dimes, etc.

9 Algorithm We’re given L, list of items to loot. Sort L descending by v_i/w_i. This ratio is a rate, such as value per pound. w = 0 while w < weightLimit: x_i = Take as much of L[0] as you can. w += x_i If L[0] depleted, remove it from list. Not hard to see that there is no other way to return with more valuable loot given our weight limit. Final note: what is the complexity of the algorithms we’ve seen?

10 0/1 Knapsack problem A thief wants to rob a store. Item i has value v i and weight w i. Knapsack has max capacity W. Want to take as valuable a load as possible. Which items to take? “0/1” means each item is taken or not. No fractions. Greedy algorithm doesn’t work. For example (W=5): –Item 1 has value 3, weight 1 (ratio 3.0) –Item 2 has value 5, weight 2 (ratio 2.5) –Item 3 has value 6, weight 3 (ratio 2.0) –Taking items 1+2 (no room for #3): total value 3 + 5 = 8. –Taking items 1+3 (no room for #2): total value 3 + 6 = 9. –Taking items 2+3 (no room for #1): total value 5 + 6 = 11. –  Greedy algorithm actually gave “worst” solution.


Download ppt "CS 361 – Chapter 10 “Greedy algorithms” It’s a strategy of solving some problems –Need to make a series of choices –Each choice is made to maximize current."

Similar presentations


Ads by Google