algorithm

Does greedily removing intervals with most conflicts solve interval scheduling?


We can solve the scheduling problem, in which we must select the largest set of continuous intervals that do no overlap, with a greedy algorithm: we just keep picking the intervals that end the earliest: http://en.wikipedia.org/wiki/Interval_scheduling

Apparently, greedily picking the intervals with fewest conflicts does not work.

I was wondering if putting all the intervals in one big set and then greedily removing the interval with the most number of conflicts left (until the intervals have no conflicts) works. I can envision implementing this greedy algorithm with a priority queue: every time we remove the interval X with greatest conflicts from the priority queue, we update the other intervals that used to conflict with interval X so that the other intervals now are marked as having 1 less conflict.

Does this work? I'm trying to come up with a counterexample to disprove it and can't.


Solution

  • Here is a counterexample. The idea is to drop a required interval on the very first pick. The number of conflicts is on the right.

            ====    2
           ----     3
          ----      3
        ====        4
      ----          3
     ----           3
    ====            2
    

    Obviously, we will want to pick the three bold (====) intervals and drop the four thin (----) intervals. There is no other way to obtain three non-intersecting intervals.

    By the way, you may find the TopCoder tutorial on greedy problems interesting since it starts with a discussion of several approaches on the same problem.