10

Given a set of intervals [x,y] where 0 <= x,y <= 2000 how to find minimum number of points which can cover(i.e. Every interval should contain at least one point in resultant set of points) all intervals?

example:

Given Set of intervals:
    [2,5]
    [3,7]
    [7,10]

then answer should be 2 (minimum number of points required to cover all intervals) as points x=3,x=7 is one solution.

varsha
  • 1,620
  • 1
  • 16
  • 29
Parth
  • 188
  • 1
  • 1
  • 9
  • that's np-complete problem but you might want to use some greedy heuristics – mangusta Jan 03 '15 at 10:28
  • I tried to solve this using making ArrayLists for each of points between 0 and 2000 using deduction method. But i got stuck in between where i need to find minimal point set which cover 1 to n numbers – Parth Jan 03 '15 at 10:30
  • 1
    The answer is 2, not 3. You can pick 5 and 10. – kraskevich Jan 03 '15 at 10:36
  • 1
    I second user2040251. There are 3 solutions to the example 3,10; 4,10; 5,10. – Svea Jan 03 '15 at 10:41
  • 2
    oh actually i made a wrong analogy with set cover problem, ) that's not np-complete, it has optimal greedy solution, see the answer below – mangusta Jan 03 '15 at 10:41
  • 2
    looks simmilar to http://www.codechef.com/JAN15/problems/ONEKING – Neel Choudhury Jan 03 '15 at 12:32

1 Answers1

12

You can use a greedy algorithm:

  1. Sort all intervals by their end points(in increasing order).

  2. Iterate over a sorted array of intervals. When an interval is over, there are two options:

    1. It is already covered by some point. Nothing should be done in this case.
    2. It is not covered yet. Then the end point of this interval should be inserted into to the resulting set.

The resulting set generated by this algorithm is optimal.

kraskevich
  • 18,368
  • 4
  • 33
  • 45