Chapter 7
■
Greed Is Good? prove It!
157
So, the greedy algorithm keeps up with the best, all the way to the end. However, this “keeping up” dealt only with
finishing times, not the number of intervals. We need to show that keeping up will yield an optimal solution, and we
can do so by contradiction: If the greedy algorithm is
not optimal, then
m >
k. For every
r,
including r =
k, we know
that
i
r
finishes at least as early as
j
r
. Because
m >
k, there must be an interval
j
r+1
that we didn’t use. This must start after
j
r
, and therefore after
i
r
, which means that we
could have—and, indeed,
would have
—included it. In other words, we
have a contradiction.
No
Worse Than Perfect
This is a technique I used in showing the greedy choice property for Huffman’s algorithm. It involves showing that you
can transform a hypothetical optimal solution to the greedy one, without reducing the quality. Kleinberg and Tardos
call this an
exchange argument. Let’s put a twist on the interval problem. Instead of having fixed starting and ending
times, we now have a
duration and a
deadline, and you’re free to schedule the intervals—let’s call them
tasks—as you
want, as long as they don’t overlap. You also
have a given starting time, of course.
However, any task that goes past its deadline incurs a penalty equal to its delay, and you want to minimize the
maximum of these delays. On the surface, this might seem like a rather complex scheduling problem (and, indeed,
many scheduling problems are really hard to solve). Surprisingly, though, you can find the optimum schedule through
a super-simple greedy strategy: Always perform the most urgent task. As is often the case for greedy algorithms, the
correctness proof is a bit tougher than the algorithm itself.
The greedy solution has no gaps in it. As soon as we’re done with one task, we start the next. There will also be
at least one optimal solution without gaps—if
we have an optimal solution with gaps, we can always close these up,
resulting in earlier finish times for the later tasks. Also, the greedy solution will have no
inversions (jobs scheduled
before other jobs with earlier deadlines). We can show that all solutions without gaps or inversions have the same
maximum delay. Two such solutions can differ only in the order of tasks with identical deadlines, and these must be
scheduled consecutively. Among the tasks in such a consecutive block, the maximum delay depends only on the last
task, and this delay doesn’t depend on the order of the tasks.
The only thing that remains to be proven is that there exists an optimal solution
without gaps or inversions,
because it would be equivalent to the greedy solution. This proof has three parts:
If the optimal solution has an inversion, there are two consecutive tasks where the first has a
•
later deadline than the second.
Switching these two removes one inversion.
•
Removing this inversion will not increase the maximum delay.
•
The first point should be obvious enough. Between two inverted tasks, there must be some point where the
deadlines start decreasing, giving us the two consecutive, inverted tasks. As for the second point, swapping the tasks
clearly removes one inversion, and no new inversions are created. The third point requires a little care. Swapping tasks
Do'stlaringiz bilan baham: