This problem is a variation of maximum weighted interval scheduling algorithm. The DP algorithm has polynomial complexity of O(N*log(N))
with O(N)
space for the naive problem, and O(2^G * N * logn(N))
complexity with O(2^G * N)
space for this variation problem, where G
, N
represent the total no of groups/subsets(5 here) & intervals respectively.
If x_i doesn't represent intervals, then the problem is in NP, which other solutions have proved.
First let me explain the dynamic programming solution for maximum weighted interval scheduling, and then solve the variation problem.
- We are given starting & ending points of the intervals. Let
start(i)
, end(i)
, weight(i)
be starting, ending point, interval length of the interval i
respectively.
- Sort the intervals based on increasing order of start point.
- Let the sorted order of intervals be
1, 2, ... N
.
- Let
next(i)
represent the next interval that doesn't overlap with interval i
.
- Lets define a subproblem
S(i)
to be the maximum weighted interval only considering jobs i, i+1, ... N
.
S(1)
is the solution, that considers all jobs from 1,2,... N
and returns the maximum weighted interval.
- Now lets define
S(i)
recursively.
.
S(i) = weight(i) if(i==N) // last job
= max(weight(i)+S(next(i)), S(i+1)
Complexity of this solution is O(N*log(N) + N)
. N*log(N)
for finding next(i)
for all jobs, and N
for solving the subproblems. Space is O(N)
for saving subproblem solutions.
Now, lets solve variation of this problem.
- Lets collectively 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 starting, ending point, interval length, subset of the interval i
respectively.
- Sort the intervals based on increasing order of start point.
- Let the sorted order of intervals be
1, 2, ... N
.
- Let
next(i)
represent the next interval that doesn't overlap with interval i
.
- Lets define a subproblem
S(i, pending)
to be the maximum weighted interval only considering jobs i, i+1, ... N
and pending
is a list of subsets from which we have to choose one interval each.
S(1, {S_1,...S_5})
is the solution, that considers all jobs 1,...N
, chooses one interval for each of S_1,...S_5
and returns the maximum weighted interval.
- Now lets define
S(i)
recursively 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)
Note that I may have missed some base cases.
Complexity of this algo is O(2^G * N * logn(N))
with O(2^G * N)
space. 2^G * N
represents the subproblem size.
As an estimate, for small values of G<=10
and high values of N>=100000
, this algo runs pretty quickly. For medium values of G>=20
, N<=10000
should be low as well for this algo to converge. And for high values of G>=40
, the algo doesn't converge.