I am trying to grasp the explanation of the closest pair point problem in various literatures. While the basic approach is the same in all of them, divide-solve-combine (divide and conquer), and get linear time merge (combine/conquer), the actual implementation is subtly different among the articles and books.
The linear-time-merge is key here which tries to limit the number of points to be compared.
The way the points are being considered in the Kleinberg book is a bit different the way points are considered in this Wikipedia article or Cormen book.
Anyway, for the later two, we find nice explanations for the number of points to be considered here, and here, including many others.
For the problem in hand, please take a look at these slides (slide 32) for the Kleinberg book. The claim of 11 point gap is in the same slide. A more detailed explanation can be found here in page 6, section 6.2.5.6.
However, in the same page of the above mentioned slides (slide 32), we find a claim something like, "Still true if we replace 12 with 7."
I have failed to find an explanation of the above claim.
Please see the figure below,
If we consider the red point to be compared with those in the right half, the points
in the right half should be in the shaded half circle. I have tried to put the extreme ones
in blue. Still they are |5-(-2)|+1 = 8 and |5-15|+1 = 11 apart.
What is it I might be missing here?
Actually you do not need to compute the distance for the lower half of the points, since in your range if you consider points sorted according to y-axis then you start from bottom and go up considering only the points in the region above it.
You can put 9 points on the grid actually. If (0,0) is center and assuming delta=1, you can have 9 points at (-1,-1), (-1,0),..., (1,1).
Proof that it is only at most 9:
Even at optimal packing, you can only have 3 layers of circles each with radius (1/2)with all the centers inside a 2X2 square.
So, the difference drops down to 8 after this. To get to seven you have to assume that it is not a special case (I forgot the technical term for it, but it is a popular assumption in computational geometry. It also states that 3 points cannot be on a same line. It is called "generality assumption" or something like that.
According to CLRS 33.4:
There're 2 points to the left of line l (see the most left 2 points), 2 points to the right of line l (see the most right 2 points), and 4 points on line l (both PL and PR are on the same points).
2 + 2 + 4 = 8
not including the self, so it's 8 - 1 = 7
e.g. we're counting the distance between PL (the upper point on l) and other points, let's do it counterclockwise:
the 1st point (PL) is the most left one,
2nd (PL) is bottom left,
3rd (PL) is the bottom point on l,
4th (PR) is also the bottom point on l,
5th (PR) is the bottom right one,
6th (PR) is the upper right one,
7th (PR) is the upper point on l.
Related
I'm trying to solve a programming task I've been given and I don't have the slightest idea how to do it.
This is the problem:
Skinny Pete is invited to a garden birthday party. He doesn’t really
like parties too much, but heard that the birthday cake is going to be
really amazing and he wouldn’t like to miss the chance to try it.
There is only one little problem. There is a sprinkler system
installed in the garden and by knowing his friends, there is a high
chance of someone turning it on as a party prank. Pete likes cake, but
really hates getting wet. Luckily he found a sketch of the garden that
has the location of the sprinklers and how far each one can sprinkle
water.
The garden looks like a rectangle that is open on one side and has the house in the opposite side.
The cake is going to be in the house.
The other two sides have fences so one can not enter through there, and the house does not have a back entrance. Pete is interested to
know if it is possible to enter the garden and get to the house
without any risk of getting wet.
For simplicity we can think that the map of the garden is in Cartesian
coordinate system.
The garden is a rectangle that has sides parallel to the axes and having its bottom left corner at the origin (0, 0).
The entrance to the garden is the left side and the the house is at the right side.
Sprinklers are represented as circles with a center and a radius. Stepping anywhere inside such a circle might get you wet.
For the purpose of this problem, and since Pete is so skinny, we can think of him as just a point travelling in the space, with real
numbers as coordinates.
Input Specifications First line of the standard input contains two
space separated integers H and W, the height and the width of the
garden.
Next line contains the number of sprinklers N. After that N lines
follow having three space separated integers each - Xi, Yi and Ri.
This a description of a sprinkler as a circle with center (Xi, Yi) and
radius Ri.
1 ≤ N ≤ 128
1 ≤ H, W ≤ 1024
0 ≤ Xi ≤ W
0 ≤ Yi ≤ H
1 ≤ Ri ≤ 1024
Output Specifications
Output a single line containing “CAKE” (without quotes) if it is
possible to get to the house without getting wet and “NO CAKE”(without
quotes) otherwise.
thanks in advance to helpers
Since you show no code and you only implicitly ask for help, I'll give a key idea and leave the mathematics and the implementation to you.
Skinny Pete can get the cake without getting wet unless there is a chain of sprinkler circles between the bottom and the top of the garden. In other words, we can assume that Pete succeeds. But look through all the circles. We see if any circle intersects the bottom edge of the garden--that is easy mathematics. If there is none, Pete really succeeds. If there is, see if there is another circle that intersects that first one, then if there is another that intersects the second, etc. Finally, you see if the last circle in this chain intersects the top edge of the garden. If there is any such chain of intersecting circles that also intersects top and bottom of the garden, poor Pete goes hungry. (Note that just one circle that intersects both top and bottom would also frustrate Pete--consider that to be a chain of one.)
Here is a diagram of the second example in your contest PDF, where you can see there is no chain of spanning circles so Pete succeeds.
And here is a diagram of the third example, where Pete fails. Note that there is a chain of four circles on the left, colored blue, that spans the garden.
Given that idea, there is an obvious O(N^2) algorithm to find all pairs of intersecting circles and an O(N) algorithm to find the circles intersecting the top and bottom sides of the garden. You could use a path-finding algorithm from graph theory to solve your problem. Think of the top and bottom sides and your circles as nodes in the graph, with two nodes connected with an edge if they intersect. You then search for a path between the nodes representing the top and bottom sides.
Good luck on figuring the mathematics, algorithm, and code.
At the moment I try to learn and understand geometric algorithms and I found the smallest-circle-problem quite interesting. I found many solutions and different algorithms including this one of which I'm not sure if it is Emo Welzl's.
However, I don't understand one specific (important) part:
You're given N points on a (XY)-Plane.
You order those points randomly
Choose 3 points and create the circle where they are on the circle boundary.
Get the next point and check if it is enclosed by the circle:
a) If it is enclosed, repeat 4 until there are no more points left.
b) If is not enclosed, create a new circle where the new point is on the circle boundary and still all other points are inside or on the circle.
Steps 1) to 4a) are simple, my problem is step 4b). How can I find this new circle? To me, it seems like this is the same problem just with a smaller (sub)set of points. (Divide-et-Impera)
I guessed I have to replace one of the 3 original points (the first 3 points, that made up the first circle) with the new point, but I'm not sure
if this really works...
From the 3 points A,B,C you can calculate the centre O of the circle: the point that is equidistant to those three. Its coordinates xO and yO are the means of xA,xB,xC and yA, yB, yC respectively.
Let's call D the 4th point, trace the circle of centre 0 and radius OD.
OD > OA (and OA=OB=OC) so A, B and C are in the circle.
EDIT
The solution I proposed above is not optimal.
I found a good explanation of Welzl's algorithm: see link
Of course, you can get his paper easily by looking on Google Scholar, but it is quite hard to read.
The basic principle is that in 4b) the circle is computed from all the possible circles having D on the boundary as well as two other points that were on the boundary before (or one point if that doesn't work and it will be diametrically opposed to D).
In
http://en.wikipedia.org/wiki/Closest_pair_of_points_problem
we can see that it mentions that is at most 6 points that is closest to the point on the other half, which can be represented as the graph below:
My question is for point P1 and Point P2, the distance to the red point will exceed sqrt(2)*d, why it is part of the solution? Why it is not at most 4 points that is closest to P rather than at most 6 points? Thanks.
P1 and P2 are not part of the solution, but they have to be examined on the way to the solution, because the algorithm examines all points in the box, and P1 and P2 are in the box.
Note that no such point as your Q can exist, because by hypothesis the minimum distance between points in the right-hand half of the diagram is d.
Edited to add: you seem to think that the Wikipedia article is making a claim like this:
There may be up to 6 points on the right side of the line that are within a distance d of P.
This claim would be false. But the article does not make such a claim. Instead, it makes two separate claims, both of which are true:
All the points on the right side of the line that are within a distance d of P are inside the box.
There may be up to 6 points in the box.
We are only counting the maximum number of points that can lie in the right d x 2d rectangle. Since any two points are constrained to have a minimum distance of d, we can place at most 6 points in the rectangle while satisfying this constraint, as shown in the figure.
Note that the points on the right side that are within d distance from P should all lie within a circular segment of a circle centered at P and whose radius is d. There can be at most 4 points in this segment. However, finding the number of points within a segment is more complicated than finding the number of points within a rectangle. So we use the rectangle instead and incur an extra cost of having to search for at most 2 additional points.
The bound is only important for complexity estimation. Code-wise, you may simply scan up and down within the distance dRmin. The bound here suggest that you'll at most see 6 points in each such scan, making this O(1).
So, if within δ * 2δ rectangle R, we only need to compare one point from the left side to 7 points on the right side. What I don't understand is, despite reading the proof, inside R we can fill as many points as we want inside the rectangle which may exceed the total number of 7. Imagine if we have δ = 2, a point p(1.2, 1.1) on the left side, and on the right side, we have a whole bunch of q, such as q(1.5, 1.7) , q(1.4, 1.3),.....how can only comparing 7 points detects the closest pair? I thought that we must compare every points within rectangle R if it is the case. Please help me.
There may only be 6 points inside your rectangle, since that's the maximum number of points that you can put in a rectangle with sides δ and 2δ maintaining the property that they are at least δ distant from each other.
The way to lay those 6 points is shown in the figure below:
You can check for yourself that there's way of putting another point inside the rectangle without violating the distance property. If you add more than 6 points, they would be less than δ apart, which is a contradiction, since δ is supposed to be the distance between the closest pair.
Since there may be a maximum of 6 points, testing 7 will guarantee that you find the solution.
I got figure 1 from these UCSB slides, which may be useful to you.
I am looking at the wikipedia entry for how to solve this. It lists five steps
1.Sort points along the x-coordinate
2.Split the set of points into two equal-sized subsets by a vertical line x = xmid
3.Solve the problem recursively in the left and right subsets. This will give the left-side and right-side minimal distances dLmin and dRmin respectively.
4.Find the minimal distance dLRmin among the pair of points in which one point lies on the left of the dividing vertical and the second point lies to the right.
5.The final answer is the minimum among dLmin, dRmin, and dLRmin.
The fourth step I am having trouble understanding. How do I choose what point to the left of the line to compare to a point right of the line. I know I am not supposed to compare all points, but I am unclear about how to choose points to compare. Please do not send me a link, I have searched, gone to numerous links, and have not found an explanation that helps me understand step 4.
Thanks
Aaron
The answer to your question was in the next paragraph of the wikipedia article:
It turns out that step 4 may be
accomplished in linear time. Again, a
naive approach would require the
calculation of distances for all
left-right pairs, i.e., in quadratic
time. The key observation is based on
the following sparsity property of the
point set. We already know that the
closest pair of points is no further
apart than dist = min(dLmin,dRmin).
Therefore for each point p of the left
of the dividing line we have to
compare the distances to the points
that lie in the rectangle of
dimensions (dist, 2 * dist) to the
right of the dividing line, as shown
in the figure. And what is more, this
rectangle can contain at most 6 points
with pairwise distances at least
dRmin. Therefore it is sufficient to
compute at most 6n left-right
distances in step 4. The recurrence
relation for the number of steps can
be written as T(n) = 2T(n / 2) + O(n),
which we can solve using the master
theorem to get O(n log n).
I don't think I can put it much clearer than they already have, but do you have any specific questions about this step of the algorithm?