I recently read about the Interval Scheduling algorithm in chapter 4 of Algorithm Design by Tardos and Kleinberg. The solution provided to the Interval Scheduling Problem was this:
Sort the n intervals based on their finishing time and store in array F
A = [] #list of intervals
S = array with property that S[i] contains the finishing time of the ith interval
i = 0
For j = 0 to n-1
if S[j] >= F[i] #this interval does not overlap with the ith interval
i = j
A.add(j)
This algorithm in C++/Java/Python can be found here: http://www.geeksforgeeks.org/greedy-algorithms-set-1-activity-selection-problem/
An extension of the problem, also known as the Interval Coloring Problem, can be explained as follows.
We are given a set of intervals, and we want to colour all intervals so that intervals given the same colour do not intersect and the goal is to try to minimize the number of colours used.
The algorithm proposed in the book was this:
Sort the intervals by their start times in a list I
n = len(I)
For j = 1 to n:
For each interval I[i] that precedes I[j] and overlaps it:
Exclude the label of I[i] from consideration for I[j]
The jth interval is assigned a nonexcluded label
However isn't this algorithm's running time O(n^2)? I thought of a better one, but I'm not sure if it is correct. Basically, we modify the algorithm for Interval Scheduling.
Sort the n intervals based on their finishing time and store in array F
A = []
x = 1
S = array with property that S[i] contains the finishing time of the ith interval
i = 0
while F is not empty:
new = []
For j = 0 to n-1
if S[j] >= F[i] #this interval does not overlap with the ith interval
i = j
A.add(F[j])
else:
new.add(F[j])
F = new
Recompute S
print x, A
A = []
x++
I might have a silly bug in the pseudocode but I hope you understand what my algorithm is trying to do. Basically I'm peeling off all possible colourings by repeatedly running the standard interval scheduling algorithm.
So, shouldn't my algorithm print all the various colourings of the intervals? Is this more efficient than the previous algorithm, if it is a correct algorithm? If it is not, could you please provide a counter-example? Thanks! (More about interval scheduling here: https://en.wikipedia.org/wiki/Interval_scheduling)