0

Let's say we have a set of intervals [s1,e1],[s2,e2]...[sn,en]

I would like to find the subset of non-overlapping intervals and has the maximum aggregate time.

Actually I'm looking for a greedy solution. Does it exist or not?

Jun
  • 426
  • 1
  • 7
  • 16
  • 1
    This is not too hard using dynamic programming. Hint: how much can you aggregate until each `en`? – John Dvorak Jul 07 '13 at 19:59
  • 4
    This is a very common problem indeed. Are you sure you have tried enough? – Fallen Jul 07 '13 at 20:00
  • 1
    possible duplicate of [Algorithm to find the maximum sum in a sequence of overlapping intervals](http://stackoverflow.com/questions/3243234/algorithm-to-find-the-maximum-sum-in-a-sequence-of-overlapping-intervals) – David Eisenstat Jul 07 '13 at 20:05
  • What do you mean by "greedy solution"? If you mean, "always take the leftmost available interval", then the algorithm is pretty obvious. – John Dvorak Jul 07 '13 at 20:12
  • Hey I missed something. DP will work for sure. I'm trying to find out if there is any greedy solution, because there exist one for the maximum number of intervals. However maximum time is quite different than maximum number of intervals. – Jun Jul 07 '13 at 20:14
  • once again, define "greedy". Just taking the leftmost available interval won't work. Nor will taking the longest one. – John Dvorak Jul 07 '13 at 20:17
  • 1
    **NO** there is no greedy solution for this problem – Fallen Jul 07 '13 at 20:18
  • @JanDvorak I believe greedy is defined in any algorithm textbook. I'm asking is there any local optimal choice, like you said, leftmost or longest neither works. – Jun Jul 07 '13 at 20:20
  • 1
    See the duplicate. Since the uniqueness problem is a subproblem for this, there is no algorithm better than `th(n log n)`. – John Dvorak Jul 07 '13 at 20:27

1 Answers1

1

"Greedy" is not a formal term, but for the purpose of this question, let's define the class of greedy algorithms to be those that impose an a priori total order on intervals (i.e., independent of the input) and repeatedly extend the partial solution by the maximum available interval. Consider the inputs

[0,2],[1,4],[3,5]
[0,2],[1,4]
[1,4],[3,5].

There are three possibilities for the maximum interval among [0,2],[1,4],[3,5]. If [0,2] or [3,5] is maximum, then the greedy algorithm answers incorrectly for the second or third input respectively. If [1,4] is maximum, then the greedy algorithm answers incorrectly for the first input.

David Eisenstat
  • 64,237
  • 7
  • 60
  • 120
  • An O(N^2) greedy approach will work for these 3 inputs. like, for first case, we'll start taking the first interval [0,2] then ignore 2nd (because it intersects with [0,2]) and take the 3rd. at the next step we'll take the second and compare and we'll get that the [0,2]+[3,5] interval is the best For the 2nd input, we'll start with [0,2]. It intersects with [1,4]. So again we start with [1,4]. And we find that the best is [1,4]. Same for the 3rd input. but this time we'll find that the first one is better. There is **no** greedy solution for this problem but these cases seem to be weak. – Fallen Jul 08 '13 at 11:00
  • @MuhammedHedayet That's not a "greedy" algorithm. I have no idea what greedy means for you and thus no idea why you're so sure that no greedy algorithm exists. – David Eisenstat Jul 08 '13 at 11:43
  • why that's not greedy? :) I'm starting from any interval and choosing the next interval greedily such that the next interval doesn't intersect with the previous one. **A greedy algorithm always makes the choice that looks best at the moment** I hope this answers why no greedy algorithm exists for this particular problem :) – Fallen Jul 08 '13 at 12:49
  • @MuhammedHedayet It's not a greedy algorithm by my definition, and yours isn't formal. – David Eisenstat Jul 08 '13 at 12:57
  • My one is copied from CLRS :) Still you believe it's not formal? ;-) – Fallen Jul 08 '13 at 12:58
  • @MuhammedHedayet Believe it or not, not everything in CLRS is a formal definition. – David Eisenstat Jul 08 '13 at 13:00
  • @MuhammedHedayet I have nothing else to say until you post an answer with a formal definition of a greedy algorithm and a proof that this problem has no correct greedy algorithm by that definition. – David Eisenstat Jul 08 '13 at 13:02
  • **A greedy algorithm is an algorithm that follows the problem solving heuristic of making the locally optimal choice at each stage with the hope of finding a global optimum.** This one is from Wikipedia. Actually, the definition from CLRS is correct. Greedy means this thing. If you have some other definition you believe which is formal please share :) – Fallen Jul 08 '13 at 13:03