(Latest Revision:
Feb 15, 2022)
[2022/02/15: fixed a typo - one of the intervals was missing from one of the lists]
[2022/02/14: I added some more detail to explain why the algorithm works.]
Detail of Work Done by The Interval Partitioning Algorithm
The List of Intervals Sorted by Increasing Start Time
(00, 05)
(01, 04)
(02, 09)
(03, 10)
(06, 22)
(07, 14)
(08, 13)
(11, 17)
(12, 18)
(15, 19)
(16, 23)
(20, 26)
(21, 25)
(24, 27)
After scheduling the first interval:
(00, 05) X
(01, 04)
(02, 09)
(03, 10)
(06, 22)
(07, 14)
(08, 13)
(11, 17)
(12, 18)
(15, 19)
(16, 23)
(20, 26)
(21, 25)
(24, 27)
Classroom A: (00, 05) <== PQ Top
Considering what to do with the second interval:
(00, 05) X
(01, 04) <== we now test whether start time 01 >= finish time 05. We know this second interval does not begin before the first, so we know it doesn't begin AND end before the first. The only way the second interval can be compatible with the first is if it begins and ends AFTER the first (except we can allow the second to start at the same time the first finishes). So this test: 01 >= 05 is definitive. The fact that it is false tells us that the second interval is NOT compatible with the first. (Just from knowing 01<05, we can be certain that the second interval overlaps the first.) Therefore we have to create a new classroom and schedule the second interval into it.
(02, 09)
(03, 10)
(06, 22)
(07, 14)
(08, 13)
(11, 17)
(12, 18)
(15, 19)
(16, 23)
(20, 26)
(21, 25)
(24, 27)
Classroom A: (00, 05) <== PQ Top
After scheduling the second interval:
(00, 05) X
(01, 04) X
(02, 09)
(03, 10)
(06, 22)
(07, 14)
(08, 13)
(11, 17)
(12, 18)
(15, 19)
(16, 23)
(20, 26)
(21, 25)
(24, 27)
Classroom B:(01, 04) <== PQ Top
Classroom A:(00, 05)
Here we are keeping the classrooms in a priority queue. The key value is the finish time of the interval most recently scheduled into the classroom. So now the classroom containing the second interval is the top priority item.
After scheduling the second interval:
(00, 05) X
(01, 04) X
(02, 09) <== Now as we consider where to schedule the third interval, we want to know if it is compatible or not with Classroom B or Classroom A. All we have to do is compare the start time (here 02) with the PQ Top key. Using the same reasoning as before, we know that it's impossible for the third interval to both begin and end before either the first or second intervals. The third interval is compatible with the first or second interval only if it begins and ends after one of them. When we check to see if 02>=04(PQ Top key), the outcome is definitive. 02<04, so the third interval is not compatible with (01, 04). We don't have to look at the other interval (00,05). We know that it finishes at least as late as (01, 04), so therefore we know "automatically" that 02 is also less than 05, and so (02, 09) is also incompatible with (00, 05). The one comparison tells us that we need another classroom for (02, 09). Notice that the test would have been definitive "the other way." Suppose the third interval was (04 4.75) instead of (02, 09). We'd know we could schedule that interval into the top priority classroom (after (01, 04)). In general, if the comparison returns TRUE, you know you can schedule the new interval into the top priority classroom. And if the comparison returns FALSE, then you can't schedule the new interval into ANY of the existing classrooms.
(03, 10)
(06, 22)
(07, 14)
(08, 13)
(11, 17)
(12, 18)
(15, 19)
(16, 23)
(20, 26)
(21, 25)
(24, 27)
Classroom B:(01, 04) <== PQ Top
Classroom A:(00, 05)
After scheduling the third interval:
(00, 05) X
(01, 04) X
(02, 09) X
(03, 10)
(06, 22)
(07, 14)
(08, 13)
(11, 17)
(12, 18)
(15, 19)
(16, 23)
(20, 26)
(21, 25)
(24, 27)
Classroom B: (01, 04) <== PQ Top (we only had to check B to know neither A nor B is compatible with (02, 09))
Classroom A: (00, 05)
Classroom C: (02, 09)
After scheduling the fourth interval:
(00, 05) X
(01, 04) X
(02, 09) X
(03, 10) X
(06, 22)
(07, 14)
(08, 13)
(11, 17)
(12, 18)
(15, 19)
(16, 23)
(20, 26)
(21, 25)
(24, 27)
Classroom B: (01, 04) <== PQ Top (we only had to check B to know neither A, B, nor C is compatible with (03, 10))
Classroom A: (00, 05)
Classroom C: (02, 09)
Classroom D: (03, 10)
After scheduling the fifth interval:
(00, 05) X
(01, 04) X
(02, 09) X
(03, 10) X
(06, 22) X
(07, 14)
(08, 13)
(11, 17)
(12, 18)
(15, 19)
(16, 23)
(20, 26)
(21, 25)
(24, 27)
Classroom A: (00, 05) <== PQ Top
Classroom C: (02, 09)
Classroom D: (03, 10)
Classroom B: (01, 04) (06, 22)
After scheduling the sixth interval:
(00, 05) X
(01, 04) X
(02, 09) X
(03, 10) X
(06, 22) X
(07, 14) X
(08, 13)
(11, 17)
(12, 18)
(15, 19)
(16, 23)
(20, 26)
(21, 25)
(24, 27)
Classroom C: (02, 09) <== PQ Top
Classroom D: (03, 10)
Classroom A: (00, 05) (07, 14)
Classroom B: (01, 04) (06, 22)
After scheduling the seventh interval:
(00, 05) X
(01, 04) X
(02, 09) X
(03, 10) X
(06, 22) X
(07, 14) X
(08, 13) X
(11, 17)
(12, 18)
(15, 19)
(16, 23)
(20, 26)
(21, 25)
(24, 27)
Classroom C: (02, 09) <== PQ Top (we only had to check C to know neither A, B, C, nor D is compatible with (08, 13))
Classroom D: (03, 10)
Classroom E: (08, 13)
Classroom A: (00, 05) (07, 14)
Classroom B: (01, 04) (06, 22)
After scheduling the eighth interval:
(00, 05) X
(01, 04) X
(02, 09) X
(03, 10) X
(06, 22) X
(07, 14) X
(08, 13) X
(11, 17) X
(12, 18)
(15, 19)
(16, 23)
(20, 26)
(21, 25)
(24, 27)
Classroom D: (03, 10) <== PQ Top
Classroom E: (08, 13)
Classroom A: (00, 05) (07, 14)
Classroom C: (02, 09) (11, 17)
Classroom B: (01, 04) (06, 22)
After scheduling the ninth interval:
(00, 05) X
(01, 04) X
(02, 09) X
(03, 10) X
(06, 22) X
(07, 14) X
(08, 13) X
(11, 17) X
(12, 18) X
(15, 19)
(16, 23)
(20, 26)
(21, 25)
(24, 27)
Classroom E: (08, 13) <== PQ Top
Classroom A: (00, 05) (07, 14)
Classroom C: (02, 09) (11, 17)
Classroom D: (03, 10) (12, 18)
Classroom B: (01, 04) (06, 22)
After scheduling the tenth interval:
(00, 05) X
(01, 04) X
(02, 09) X
(03, 10) X
(06, 22) X
(07, 14) X
(08, 13) X
(11, 17) X
(12, 18) X
(15, 19) X
(16, 23)
(20, 26)
(21, 25)
(24, 27)
Classroom A: (00, 05) (07, 14) <== PQ Top
Classroom C: (02, 09) (11, 17)
Classroom D: (03, 10) (12, 18)
Classroom E: (08, 13) (15, 19)
Classroom B: (01, 04) (06, 22)
After scheduling the eleventh interval:
(00, 05) X
(01, 04) X
(02, 09) X
(03, 10) X
(06, 22) X
(07, 14) X
(08, 13) X
(11, 17) X
(12, 18) X
(15, 19) X
(16, 23) X
(20, 26)
(21, 25)
(24, 27)
Classroom C: (02, 09) (11, 17) <== PQ Top
Classroom D: (03, 10) (12, 18)
Classroom E: (08, 13) (15, 19)
Classroom B: (01, 04) (06, 22)
Classroom A: (00, 05) (07, 14) (16, 23)
After scheduling the twelfth interval:
(00, 05) X
(01, 04) X
(02, 09) X
(03, 10) X
(06, 22) X
(07, 14) X
(08, 13) X
(11, 17) X
(12, 18) X
(15, 19) X
(16, 23) X
(20, 26) X
(21, 25)
(24, 27)
Classroom D: (03, 10) (12, 18) <== PQ Top
Classroom E: (08, 13) (15, 19)
Classroom B: (01, 04) (06, 22)
Classroom A: (00, 05) (07, 14) (16, 23)
Classroom C: (02, 09) (11, 17) (20, 26)
After scheduling the thirteenth interval:
(00, 05) X
(01, 04) X
(02, 09) X
(03, 10) X
(06, 22) X
(07, 14) X
(08, 13) X
(11, 17) X
(12, 18) X
(15, 19) X
(16, 23) X
(20, 26) X
(21, 25) X
(24, 27)
Classroom E: (08, 13) (15, 19) <== PQ Top
Classroom B: (01, 04) (06, 22)
Classroom A: (00, 05) (07, 14) (16, 23)
Classroom D: (03, 10) (12, 18) (21, 25)
Classroom C: (02, 09) (11, 17) (20, 26)
After scheduling the fourteenth (last) interval:
(00, 05) X
(01, 04) X
(02, 09) X
(03, 10) X
(06, 22) X
(07, 14) X
(08, 13) X
(11, 17) X
(12, 18) X
(15, 19) X
(16, 23) X
(20, 26) X
(21, 25) X
(24, 27) X
Classroom B: (01, 04) (06, 22) <== PQ Top
Classroom A: (00, 05) (07, 14) (16, 23)
Classroom D: (03, 10) (12, 18) (21, 25)
Classroom C: (02, 09) (11, 17) (20, 26)
Classroom E: (08, 13) (15, 19) (24, 27)
After putting the solution into alphabetical order, instead of priority order:
Classroom A: (00, 05) (07, 14) (16, 23)
Classroom B: (01, 04) (06, 22)
Classroom C: (02, 09) (11, 17) (20, 26)
Classroom D: (03, 10) (12, 18) (21, 25)
Classroom E: (08, 13) (15, 19) (24, 27)