


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



Greedy algorithms always choose the intuitively “best” option at each decision point, and (unlike backtracking) do not consider other alternatives. The prob- lem is that while this powerful intuition is powerful, it is often wrong – just as in life, acting in one’s immediate best interest is not always the best longer-term strategy. Therefore it is especially important to prove that a greedy algorithm finds the best solution. Luckily, these proofs typically follow a standard form, which we will discuss today.
Instance: a set of events E = {Ei = (si, fi)} where si < fi.
Solution form: A subset S ⊂ E.
Constraints: No two events in S intersect.
Objective: Maximize the number of events, |S|.
At each decision, a backtracking algorithm tries each feasible choice and returns the best solution. A greedy algorithm, on the other hand, tries only the (locally) best option, ignoring locally less-appealing choices. The first hard part of coming up with a greedy algorithm is finding the right definition of “locally best” – often a naive approach will fail where a more sophisticated version of local optimality will work. To show that a greedy algorithm works, we must show that choosing greedily will do no worse than searching exhaustively. This is the second hard part of coming up with a greedy algorithm – many problems have greedy “solutions” that are as appealing and natural as they are wrong.
Example: Job (interval) scheduling
Given a list of jobs with start- and finish-times, maximize the number of non- overlapping jobs performed. We can imagine several definitions of local opti- mality, choosing at each step the job with:
None of definitions 1-3 works; to see this, we can come up with counterexamples for each. For example, maybe there is a short duration job that intersects with two long duration jobs, one intersecting the first part, the other the second part: e.g., (1,9),(7,11),(10,18). The first algorithm would take the short middle job over the two longer jobs. The first job to start might also be the last to end, so the third algorithm is not always optimal. For the second, consider 3 jobs from (1,3), 5 from (2,6), one from (4,8), 3 from (7,10), one from (9,12) , 5 from (11,15), and 3 from (14,16). The number of conflicts for each category are 7,8,8,4,8,8,7, so the second algorithm would pick one of the middle 3 jobs, from (7,10) first. This gives a schedule with 3 jobs, wheras there is a scheudle with 4 jobs, one from each of the first, third, fifth, and seventh categories. Algorithm 4 succeeds in these cases, but we need to prove that it works for all cases.
Algorithm
The right heuristic is to always choose the event with the earliest end-time. Here is a backtracking algorithm of Schedule that considers both options for this choice:
1 Schedule(E) 2 e <- event minimizing finish(e) 3 s1 <- Schedule(E - overlap(e)) + { e } 4 s2 <- Schedule(E - e) 5 if |s1| < |s2| 6 return s 7 else 8 return s
The greedy version differs by never considering s2 on line 4, always returning s1.
Example
(1,3),(2,4),(3,6),(0,6),(5,7),(5,8),(9,10) S2: + + + A: + - (-) B: + - (-) S1: + + +
Here S2’s first move is non-greedy. The transformation in our proof replaces (2,4) with (1,3) (line A), then performs another greedy transformation (line B), yielding an equal-sized schedule S1.
Implementation
A naive implementation of the above approach, searching the entire list at each step for the next choice, then for overlapping events, would take O(n^2 ). An efficient implementation instead sorts the list by finish time, and skips over overlapping events as it traverses this sorted list:
1 Schedule2(E) 2 E <- sort_by_finish(E) 3 S <- E[1] 4 f <- finish(E[1]) 5 for i = 2 .. n 6 if start(E[i]) > f 7 S <- S + E[i] 8 f <- finish(E[i])
The “modify-the-solution” approach used above can be applied to prove the correctness of many greedy algorithms. In general, we proceed by the following steps: