algorithm - Greedy Attempt for covering all the numbers with the given intervals -


After the

s has a set of intervals (n numbers of intervals) having two natural The numbers that can be found together and a list of n h numbers (containing n numbers of numbers)

I want to put the smallest subset of each number for such that SK (P) calls in our list N, at least one gap in P which is included in it Overlapping intervals in P is allowed.

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 similar), then it would be great. I am trying to make o (n ^ 2 solution)


Here is a greedy approach

  P = {} for each q: If in P / O (N) every q continues in S / O (N) in O / O (n) then q: I o / o (n) p. Ed (i) break  

but that is O (N ^ 4). O (N ^ 2) Any help to create a greedy approach would be great!

Thank you!


* Update: * I am making a mistake on this problem and I think I have an (O ^ 2) solution !!

Tell me if you think I am right!

  N = MergeSort (N) upper, lower = infinity, -1p = (Q> = bottom and q  

I think this should work !! I am trying to find a counter solution; But yes!

"post-text" itemprop = "text">

This problem is similar to the problem set, which is NP-complete (i.e., arguably no faster than the deficit) Why does it differently that the intervals always cover the adjacent elements (not the arbitrary subsets of N), which opens the way for faster solutions.

I think the solution proposed by Mike is good enough. But I think that I have quite a simple O (n ^ 2) greedy algo it starts like Mike (besides, I believe Mike's solution can be improved similarly):

  1. You sort your N numbers and sort them in sequence; COMPLEXITY O (N * lg N);

  2. Using binary search for each interval S [i], you identify the beginning and closing of the index of elements covered by SR [i]. Say, you put the number of this pair in the array cover, the difference between the two indices tells you how many elements you cover for simplicity, we put it COVER_COUNT array; COMPLEXITY O (N * lg N);

  3. You present the index point P, which shows which element in RII, your N is already covered. You set p = 0, which means that all elements of 0-th (excluded) are initially covered (i.e., no element); Complexity o (1) Apart from this, you introduce boolean array IS_INCLUDED, which shows that the interval S [i] is already included in your coverage set. Complexity O (n)

  4. < P> Then you start with 0-th element in zero and see if there is a gap between [0] and its maximum coverage is COVER_COUNT [i]. Imagine that this is the i-th interval. We again set it by IS_INCLUDED [i] and mark it as correct. Then you set [i] to end + 1, where the end [i] cover [i] is the end index in the pair (in fact now all the elements are covered [i]). After that, you know that you update all the elements in COVER_COUNT so that they can reflect that so far the elements covering each interval are not covered (this can be easily done in O time is). Then you follow the same steps for [P] and continue till p> = ELEM.length. It can be seen that the overall complexity is O (N ^ 2).

You end in O (N ^ 2) and in IS_INCLUDED is correct for the interval of S.

Let me know in the optimum cover set This solution seems right for you and if I am well-cited.

PS Just wanted to add that the optimumity of ythe solution found by the Elgo can be proved by induction and paradox. From paradox, it is easy to show that at least one optimal solution involves the longest interval of those elements which pile the [0] element, if so, by induction, we can show that in the algae every next element For, we can continue to follow the strategy of watching the interval which is the longest in relation to the number of remaining elements and which covers the left element still open on the left.


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? -