2

Let X be a set of n intervals on the real line. We say that a set P of points stabs X if every interval in X contains at least one point in P. Describe and analyze an efficient algorithm to compute the smallest set of points that stabs X . Assume that your input consists of two arrays XL [1 .. n] and XR[1..n], representing the left and right endpoints of the intervals in X.

Any suggestions where to start and how to solve it? Greedy algorithm? Huffman's?

user3450427
  • 57
  • 2
  • 4

3 Answers3

5

Here is what I can think of:

  1. sort intervals by end point
  2. go through each interval. If the interval is not covered(you only need to check the last added point with the current interval), place a point at the end point
aha
  • 4,314
  • 4
  • 23
  • 27
1

It's a greedy algorithm.

Starting at first x-coordinate, proceed until you reach the first end point. Mark that point, and all intervals that are stabbed by that point as stabbed. Repeat for all non-stabbed arrays until all are stabbed.

Proof :
Suppose there is an optimal solution that does not include the first end point. Then the first stab point is either before or after that end point. If it is after, then it wouldn't include the interval with the first end point, and therefore wouldn't be a solution.
So it must be before. Then, if the first stab point is before the first end point, then the solution including the first end point is also an optimal solution, as no other interval ends before then and therefore no intervals would not be skewered.

Sachith Muhandiram
  • 2,819
  • 10
  • 45
  • 94
ESCE
  • 123
  • 1
  • 6
0

Sort the intervals in non-decreasing order of the left point and let S be the set containing all XL and all XR values. Initially, we are to cover the minimum left point xmin. We must take one of the intervals that have xL ≤ xmin: we may as well take the one that has the largest xR. Repeat the process, updating xmin with the smallest value larger than this largest xR in S.