3

The details are a bit cringe, fair warning lol:

I want to set up meters on the floor of my building to catch someone; assume my floor is a number line from 0 to length L. The specific type of meter I am designing has a radius of detection that is 4.7 meters in the -x and +x direction (diameter of 9.4 meters of detection). I want to set them up in such a way that if the person I am trying to find steps foot anywhere in the floor, I will know. However, I can't just setup a meter anywhere (it may annoy other residents); therefore, there are only n valid locations that I can setup a meter. Additionally, these meters are expensive and time consuming to make, so I would like to use as few as possible.

For simplicity, you can assume the meter has 0 width, and that each valid location is just a point on the number line aformentioned. What is a greedy algorithm that places as few meters as possible, while being able to detect the entire hallway of length L like I want it to, or, if detecting the entire hallway is not possible, will output false for the set of n locations I have (and, if it isn't able to detect the whole hallway, still uses as few meters as possible while attempting to do so)?

Edit: some clarification on being able to detect the entire hallway or not

John Adams
  • 109
  • 10
  • Can you give an example for your problem? It's kind of hard for me to understand it. – lier wu Oct 08 '20 at 00:50
  • Are you just wanting to find the longest interval cover? Where the intersection will only count as once? – lier wu Oct 08 '20 at 00:53
  • @lierwu Imagine you have a list of n valid places to put a meter (n_1, n_2,n_3,n_4). I know n_1 is 6 ft from the end of the hallway, n_2 is 20 ft from the end of the hallway, n_3 is 18ft, n_4 is 2 ft. Say that if I place a meter at n_2; the position of its left most edge of range of detection (the negative x direction) is s = 20 - 4.7 = 15.3 ft from the end of the hallway. The right most edge is f = 20 + 4.7 = 24.7 ft from the end of the hallway. Do this for the other positions, and you will get the positions of the extrema of each meter's detection range along the number line mentioned. – John Adams Oct 08 '20 at 00:56
  • @lierwu I don't want any intersections between the detection of each meter when I place them in the hallway; I don't want to use more meters than necessary; I want to be able to detect the whole hallway if possible (given n possible locations), or if not possible, for the algorithm to return false so I can try a different combination of valid locations. – John Adams Oct 08 '20 at 01:00
  • @lierwu So, I guess I would want to return the smallest set of valid locations while still detecting the whole hallway, which is why I asked for a minimally optimal solution (as opposed to the largest set that you typically see with other greedy algorithms) – John Adams Oct 08 '20 at 01:02
  • So... In your case, the (n_1, n_2,n_3,n_4) won't cover the entire hallway? – lier wu Oct 08 '20 at 01:02
  • @lierwu they could, but if I put a meter at each of these, it might be too many meters. They are valid locations where I could put a meter without annoying other residents, but I don't have to put a meter at each of these locations if I don't want to, and still be able to detect the whole hallway. – John Adams Oct 08 '20 at 01:04
  • I don't want to use more meters than necessary;" so in that case, do you want true or false? Since you also said: "What is a greedy algorithm that places as few meters as possible while being able to detect the entire hallway of length L..." – lier wu Oct 08 '20 at 01:08
  • And you said it would cove the entire hallway, but I don't think it would cove the position that is 13ft away from the end of the hallway..... Do you really want to cover the "Entire" hallway? – lier wu Oct 08 '20 at 01:11
  • @lierwu I want to return true if the algorithm places as few meters as possible while detecting the entire length of the hallway, and false if is not possible to detect the entire length of the hallway given the set of n valid locations to place a meter. – John Adams Oct 08 '20 at 01:12
  • @lierwu "And you said it would cove the entire hallway, but I don't think it would cove the position that is 13ft away from the end of the hallway..... Do you really want to cover the "Entire" hallway?" So, in this case the algorithm would return false because the given set of n locations doesn't work. – John Adams Oct 08 '20 at 01:13
  • From your simplification, it should be enough to check if the distance between any two valid locations is greater than 9.4m, and if so, then it is not possible to cover the entire hallway. If you are using circles, then all you need are the center coordinates and use the euclidean distance formula to calculate this. – smac89 Oct 08 '20 at 07:17

2 Answers2

0

To prove that your solution is optimal if it is found, you merely have to prove that it finds the lexicographically last optimal solution.

And you do that by induction on the size of the lexicographically last optimal solution. The case of a zero length floor and no monitor is trivial. Otherwise you demonstrate that you found the first element of the lexicographically last solution. And covering the rest of the line with the remaining elements is your induction step.

Technical note, for this to work you have to be allowed to place monitoring stations outside of the line.

btilly
  • 43,296
  • 3
  • 59
  • 88
0

Given:

  • L (hallway length)
  • a list of N valid positions to place a meter (p_0 ... p_N-1) of radius 4.7

You can determine in O(N) either a valid and minimal ("good") covering of the whole hallway or a proof that no such covering exists given the constraints as follows (pseudo-code):

// total = total length; 
// start = current starting position, initially 0
// possible = list of possible meter positions
// placed = list of (optimal) meter placements, initially empty
boolean solve(float total, float start, List<Float> possible, List<Float> placed):

   if (total-start <= 0): 
        return true; // problem solved with no additional meters - woo!
   else:
        Float next = extractFurthestWithinRange(start, possible, 4.7);
        if (next == null):
             return false; // no way to cover end of hall: report failure
        else:
             placed.add(next); // placement decided
             return solve(total, next + 4.7, possible, placed); 

Where extractFurthestWithinRange(float start, List<Float> candidates, float range) returns null if there are no candidates within range of start, or returns the last position p in candidates such that p <= start + range -- and also removes p, and all candidates c such that p >= c.

The key here is that, by always choosing to place a meter in the next position that a) leaves no gaps and b) is furthest from the previously-placed position we are simultaneously creating a valid covering (= no gaps) and an optimal covering (= no possible valid covering could have used less meters - because our gaps are already as wide as possible). At each iteration, we either completely solve the problem, or take a greedy bite to reduce it to a (guaranteed) smaller problem.

Note that there can be other optimal coverings with different meter positions, but they will use the exact same number of meters as those returned from this pseudo-code. For example, if you adapt the code to start from the end of the hallway instead of from the start, the covering would still be good, but the gaps could be rearranged. Indeed, if you need the lexicographically minimal optimal covering, you should use the adapted algorithm that places meters starting from the end:

// remaining = length (starts at hallway length)
// possible = positions to place meters at, starting by closest to end of hallway
// placed = positions where meters have been placed
boolean solve(float remaining, List<Float> possible, Queue<Float> placed):

   if (remaining <= 0): 
        return true; // problem solved with no additional meters - woo!
   else:
        // extracts points p up to and including p such that p >= remaining - range
        Float next = extractFurthestWithinRange2(remaining, possible, 4.7);
        if (next == null):
             return false; // no way to cover start of hall: report failure
        else:
             placed.add(next); // placement decided
             return solve(next - 4.7, possible, placed); 
tucuxi
  • 17,561
  • 2
  • 43
  • 74
  • "...an optimal covering (= no possible valid covering could have used less meters - because our gaps are already as wide as possible)" How can it be as wide as possible if we are allowed to pick an element p that is >= remaining - range, as is stated in the second codeblock? Aren't we always picking the element in possible that is closest to the previously placed element then? – John Adams Oct 09 '20 at 19:47
  • You don't pick just *any* element that is `>= remaining - range` - you pick the *furthest* one that satisfies this condition. That is, the one that leaves *the largest gap* (but that is still within range from the previous meter, because otherwise we would not cover the whole hall) – tucuxi Oct 12 '20 at 22:11