Given n
lectures, each with start time and finish time, the problem is to assign all the lectures to rooms such that no two lectures occur at the same time in the same room. It is easy to design a greedy algorithm that sorts lectures by start time to minimize the number of rooms used. What if we process lectures by their finish time? Does the greedy algorithm still work? I tried a few examples, and did not find a counter example yet.
Greedy algorithm by Start Time
Sort all the lectures by start time in ascending order
Place all the used rooms in a priority queue. The priority of a room is based on the finish time of last lecture scheduled in the room.
Assign each lecture to the first available room in the queue. A room is available if the finish time of last lecture scheduled in the room is less than the start time of the current lecture (no overlap).
If none of rooms in the queue is available, add a new room, and insert the lecture into the new room.
The above greedy algorithm works. What if we change step 1, sort lectures by finish time, and keep other steps the same? Does it still work?
If we sort lectures by finish time in descending order, that is, we would process the last lecture first, that is a symmetric of the above greedy algorithm, I believe it definitely works.
But my question is, we sort lectures by finish time in ascending order, apply the greedy algorithm, does it work?