data structures - Greedy Algorithm for Finding Min Set of Intervals that Overlap All Other Intervals -
i'm learning greedy algorithms , came across problem i'm not sure how tackle. given set of intervals (a,b) start time , end time b, give greedy algorithm returns minimum amount of intervals overlap every other interval in set. example if had:
(1,4) (2,3) (5,8) (6,9) (7,10)
i return (2,3) , (7,8) since these 2 intervals cover every interval in set. have right this:
- sort intervals increasing end time
- push interval smallest end time onto stack
- if interval (a,b) overlaps interval on top of stack (c,d) (so less d) if a<=c keep (c,d). else update interval on top of stack (a,d)
- if interval (a,b) not overlap interval on top of stack (c,d) push (a,b) onto stack
- at end stack contains desired intervals , should run in o(n) time
my question is: how algorithm greedy? i'm struggling concepts. maybe have right , maybe don't, if do, can't figure out greedy rule is/should be.
edit: valid point made below, should have been clearer about. (7,8) works instead of (1,10) (which covers everything) because every time in (7,8) in (5,8) (6,9) , (7,10). same (2,3), every time in there in (1,4) , (2,3). goal set of intervals such if looked @ possible times in set of intervals, each time in @ least 1 of original intervals.
a greedy algorithm 1 repeatedly chooses best incremental improvement, though might turn out sub-optimal in long run.
your algorithm doesn't seem greedy me. greedy algorithm problem be:
- find interval contained in largest number of intervals input set.
- remove intervals input set contain it.
- repeat until input set empty.
for example, first produce (7,8), because contained in 3 input intervals, reduce input set (1,4)(2,3), produce (2,3)
note algorithm doesn't produce optimal output input set: (0,4)(1,2)(1,4)(3,6)(3,7)(5,6)
it produces (3,4) first, since covered 4 input intervals, best answer (1,2)(5,6), covered 3 intervals each
Comments
Post a Comment