algorithm - Covering all the numbers with the given intervals -


This algorithm is a question for the gurus: -)

Command / code> There may be a set of natural numbers interval which can overlap and be a list of N numbers.

I call the smallest subset of S. (Let's P), for example, there is at least one interval in our list N, P for each number which is included in the p over the allowance overlap is.

Trivial Example:

  S = {[1..4], [2..7], [3 .. 5], [8..15] , [9 .13]} N = [1, 4, 5] // so P = {[1..4], [2.7]}  

I think a dynamic The algorithm can not always work, so if someone is aware of the solution to this problem (or anybody who can be changed), then it would be good.

Thank you!

You can do this with a greedy algorithm.

Points in the order in N

For each point, if it is already covered by an interval then leave it.

Otherwise, consider the interval in which the point is involved, from these intervals, choose one that covers the most overlooked points. (This will be the interval with the highest end point.)

Example

  1. The first point is 1, covered by just 1. .
  2. The next point is 4, but it has already been covered.
  3. The next issue is covered by 5, 2.7 and 3.. Choose one of these to answer, in which all points of 2 points are included.

Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -