Here is an idea:
Consider the following graph: Create a node for each interval. If interval I1 and interval I2 do not overlap, and I1 does not overlap to I2, add a directed edge from node I1 to node I2. Please note that this graph is acyclic. Each node has a cost equal to the length of the corresponding interval.
Now the idea is to find the longest path in this graph that can be found in polynomial time for acyclic graphs (for example, using dynamic programming). The problem is that the costs are in nodes, not in edges. Here's the trick: divide each node v into v 'and v' '. All the edges in v will now be in v ', and all the remaining edges of v will now leave v' '. Then add the edge from v 'to v' 'with the node value, in this case, the length of the interval. All other edges will cost 0.
Well, if I'm not mistaken, the longest path on this graph will correspond to many disjoint intervals with a maximum sum.
source share