I’m trying to determine the optimal solution for this tough problem. I’ve got a length (let’s say 11). So it’s a one dimensional space 0-10. Now I’ve got these intervals with same length (let’s assume 2 in this example). Now they’re randomly distributed (overlapping, or not). Let me draw an example:
Situation:
|00|01|02|03|04|05|06|07|08|09|10| <- Space (length = 11)
|-----|
|-----|
|-----|
|-----|
|-----|
|-----| <- single interval of length = 2
Now the solution needs to find the maximal number of intervals that can fit at once without overlap.
The solution is: 4 intervals
There are three results of 4 intervals:
|00|01|02|03|04|05|06|07|08|09|10|
|-----| |-----|-----| |-----| <- result 1
|-----| |-----| |-----| |-----| <- result 2
|-----| |-----|-----| |-----| <- result 3
But there are also two more constraints as well.
-
If there are more results (of best solution, in this case = 4), then the one with the least number of gaps.
-
If there are more results still the one with the highest minimal length of all its spaces. For example the one with spaces (of length) 2 & 3 has minimal length of space = 2, that is better than 1 & 4 where the minimal length of space is only 1.
So the result 2 has 4 “continual” chunks, the other two have only 3 so the refinement is:
|00|01|02|03|04|05|06|07|08|09|10|
|-----| |-----------| |-----| <- result 1
|-----| |-----------| |-----| <- result 3
Those two got same space distributions between them, so let’s take first one.
The result for the input set is:
Interval count : 4
Optimal solution: |-----| |-----------| |-----|
The algorithm has to work universally for all the space length (not only 11), all interval lengths (interval length is always <= space length) and any number of intervals.
Update:
Problematic scenario:
|00|01|02|03|04|05|06|07|08|09|
|-----|
|-----|
|-----|
|-----|
|-----|
This is a simple dynamic programming problem.
Let the total length be
Nand the length of a task beL.Let
F(T)be maximum number of tasks that can be selected from the sub interval(T, N), then at each unit time T, there are 3 possibilities:Case 1 is simple, we just have
F(T) = F(T + 1).In case 2/3, notice that selecting a task that start a
Tmeans we must reject all tasks that start while this task is running, i.e. betweenTandT + L. So we getF(T) = max(F(T + 1), F(T + L) + 1).Finally,
F(N) = 0. So you just start fromF(N)and work your way back toF(0).EDIT: This will give you the maximum number of intervals, but not the set that fulfils your 2 constraints. Your explanation of the constraints is unclear to me, so I’m not sure how to help you there. In particular, I can’t tell what constraint 1 means since all the solutions to your example set are apparently equal.
EDIT 2: Some further explanation as requested:
Consider your posted example, we have
N = 11andL = 2. There are tasks that start atT = 0, 3, 4, 5, 6, 9. Starting fromF(11) = 0and working backwards:F(11) = 0F(10) = F(11) = 0(Since no task starts atT = 10)F(9) = max(F(10), F(11) + 1) = 1Eventually we get to
F(0) = 4:EDIT 3: Well I was curious enough about this that I wrote a solution, so may as well post it. This will give you the set that has the most tasks, with the least number of gaps, and the smallest minimum gap. The output for the examples in the question is:
(0, 2) -> (4, 6) -> (6, 8) -> (9, 11)(0, 2) -> (4, 6) -> (8, 10)Obviously, I make no guarantees about correctness! 🙂
private class Task
{
public int Start { get; set; }
public int Length { get; set; }
public int End { get { return Start + Length; } }