This problem is a variant of the maximum weighted interval planning algorithm. The DP algorithm has polynomial complexity O(N*log(N)) with the space O(N) for the naive problem and O(2^G * N * logn(N)) complexity with the space O(2^G * N) for this variation problems, where G , N represents the total number of groups / subsets (5 here) and intervals, respectively.
If x_i does not represent intervals, then the problem is in NP, as other solutions have proved.
Let me first explain the dynamic programming solution for planning the maximum weighted interval, and then solve the change problem.
- We are given the starting and ending points of the intervals. Let
start(i) , end(i) , weight(i) be the start, end point, the length of the interval interval i respectively. - Sort intervals based on ascending start point order.
- Let the sorted order of intervals
1, 2, ... N - Let
next(i) represent the next interval that does not overlap with interval i . - Allows you to define subtask
S(i) as the maximum weighted interval only taking into account tasks i, i+1, ... N S(1) is a solution that considers all tasks from 1,2,... N and returns the maximum weighted interval.- Now let's define
S(i) recursively.
.
S(i) = weight(i) if(i==N) // last job = max(weight(i)+S(next(i)), S(i+1)
The complexity of this solution is O(N*log(N) + N) . N*log(N) to find next(i) for all tasks and N to solve subtasks. Space O(N) for saving solutions to subtasks.
Now let's resolve the change to this problem.
- Let's look at all the intervals in X. Each interval belongs to one of the sets S_1, ... S_5.
- Let
start(i) , end(i) , weight(i) , subset(i) be the start, end point, interval length, subset of interval i respectively. - Sort intervals based on ascending start point order.
- Let the sorted order of intervals
1, 2, ... N - Let
next(i) represent the next interval that does not overlap with interval i . - Allows you to define subtask
S(i, pending) as the maximum weighted interval only taking into account tasks i, i+1, ... N , and pending is a list of subsets from which we must choose one interval each. S(1, {S_1,...S_5}) is a solution that considers all tasks 1,...N , selects one interval for each of S_1,...S_5 and returns the maximum weighted interval.- Now let's recursively define
S(i) as follows.
.
S(i, pending) = 0 if(pending==empty_set) // possible combination = -inf if(i==N && pending!={group(i)}) // incorrect combination = S(i+1, pending) if(group(i) not element of pending) = max(weight(i)+S(next(i), pending-group(i)), S(i+1, pending)
Please note that I may have missed some basic cases.
The complexity of this algorithm is O(2^G * N * logn(N)) with the space O(2^G * N) . 2^G * N represents the size of the subtask.
As an estimate, for small values ββof G<=10 and high values ββof N>=100000 this algorithm works quite quickly. For average values G>=20 , N<=10000 must also be low for this algorithm to converge. And for large values G>=40 , the algorithm does not converge.