Closest pair of points - algorithm

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).

Related

Understanding algorithm for finding the smallest circle enclosing points

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).

How to convert relative gravitational force to coordinates

I have a question.
I have N objects and N x N matrix M. Each entry M(i, j) contains (a kind of) relative gravitational force indicating how strongly i pulls j toward it (or inversely pull it away from it).
I want to place these N objects on a two-dimensional R x R plane by assigning a coordinate to each object.
Is there an algorithm/method that does this? There must be some commonly methods used in astrophysics, physics, chemistry, etc.
Thank you for your help.
You are interested in assigning co-ordinates (xi,yi,zi) and mass (mi) to each object such that gravitational force is consistent, right?
Consider 8 points at a time. You have a total of 32 unknowns and 28 equations. You can assume that first point is at origin and second point on x axis. That means, you will have 28 unknown and 28 equations.
So, first device and algorithm to solve for 8 points at a time. Then incrementally add one point at each iteration.
===Walkthrough===
Consider you are given n points in D dimensions. You only have distances between the points, but not the co-ordinates. Goal is to find co-ordinates for each point.
If D=1, you need to consider only two (+1) points at a time. Place first point at origin. Place second point on the positive side of origin. You can place third point in relation to origin, but place it right or left of it depending on the distance to first point and so on...
If D=2, place point 1 at origin, point to on positive side on x axis, third point on positive side of y axis depending on distance. From fourth point onward, you can use any two placed points to place the next and use any other point to refine the options (there will be two options).
Similar with D=3. Place first three points on xy plane (z=0) for all three. Next, place 4th point ofn postive part of z axist. And so on.
Coming back to gravity:
Your problem is complicated because you cannot exactly place mass at the origin. So you would need more than 5 points to place them. As I have shown above, you need at most 8 points though.
In case your mass are all equal, you can calculate distance (~inverse of gravity) and apply the case when D=3.
The problem is, given that we know the n*n distances between n objects, how to obtain their positions?
1. Put the first one, say a, at (0,0)
2. Put the second one b at ( |b-a|, 0 )
3. For the third one c, it is at the one of the two intersections of the two circles:
|p-a|=|c-a| and |p-b|=|c-b|.
Solve this system of quadratic equations using the well-known formula, choose
either of the solutions as the position of c.
4. For any other points p, do the same thing as we're done for c, but choose one of the
two solutions that is consistent with the distance |p-c|. And check the distance
between p and all previous points. If the check fails, return with failure.

Placing a point to minimise distance from the farthest point

I have a question,
Given a set of points , how do you place a point with the constraint that the distance to the farthest point is as small as possible?.
This is in reference to this problem. I do not know how to proceed. Some pointers anyone?
Thanks
Check out this page. It describes several methods to do this.
http://www.personal.kent.edu/~rmuhamma/Compgeometry/MyCG/CG-Applets/Center/centercli.htm
In case the link above ever dies, here's the relevant part that describes the most straight-forward method:
An O(n2)-time Algorithm
Draw a circle at center, c, such that points of given set lie within the circle. Clearly, this circle can be made smaller (or else we have the solution).
Make a circle smaller by finding the point A farthest from the center of circler, and drawing a new circle with the same center and passing through the point A. These two steps produce a smaller enclosing circle. The reason that the new circle is smaller is that this new circle still contains all the points of given set, except now it passes through farthest point, x, rather than enclosing it.
If the circle passes through 2 or more points, proceed to step 4. Otherwise, make the circle smaller by moving the center towards point A, until the circle makes contact with another point B from the set.
At this stage, we a circle, C, that passes through two or more points of a given set. If the circle contains an interval (point-free interval) of arc greater than half the circle's circumference on which no points lie, the circle can be made smaller. Let D and E be the points at the ends of this point-free interval. While keeping D and E on the circle's boundary, reduce the diameter of the circle until we have either case (a) or case (b).
Case (a) The diameter is the distance DE.
We are done!
Case (b) The circle C touches another point from the set, F.
Check whether there exits point-free arc intervals of length more than half the circumference of C.
IF no such point-free arc intervals exit THEN
We are done!
Else
Goto step 4.
In this case, three points must lie on an arc less than half the circumference in length. We repeat step 4 on the outer two of the three points on the arc.
Another page here, with a sample applet:
http://www.sunshine2k.de/stuff/Java/Welzl/Welzl.html
You need to use the Voronoi diagram, possibibly the Farthest-Point Voronoi diagram, where the plane is divided into regions, where points in the same region have the same fartherst point
Update
You need to build a Farthest-Point voronoi diagram first, which is O(nlogn) time, and find the center of the smallest circle among all the vertices(if the circle is defined by three points) and all the edges(if the circle is defined by two points). The total time complexity of this approach is O(nlogn)
I just saw the Smallest circle problem wiki page, seems like there is a O(n) time algorithm. You can check it out if you care about the speed, otherwise never mind.

Nearest-neighbor algorithm with directions (left / right / top / bottom)

Having n random points in 2D geometry, for each point p I need to find 4 (or less if not exists) closest points (qa,qb,qc,qd), where qa is the closest left-top point, qb is the closest right-top point, qc is the closest left-bottom point and qd is the closest right-bottom point to point p. Having same x coordinate is considered as left, having same y coordinate is considered as bottom.
What would be the best data structure to store point coordinates and their nearest-neighbor references? What algorithm would be the fastest or the most performed?
Note: This issue is far more then nearest-neighbor algorithm, as for each point 4 neighbor points are needed.
You can try a space filling curve and a quadtree data structure. A space filling curve reduces the 2 dimension to 1 dimension and it works best with power of 2 grids. A quadtree divides the plane into 4 quads. A space filling curve is mathematical function taking 2 variables and gives 1 number as result. It can have also 3,4,5 variables but the most simple is with 2. Because it gives 1 number and takes 2 variables it can help for questions with 2 dimensions or more.
http://social.technet.microsoft.com/wiki/contents/articles/9694.tuning-spatial-point-data-queries-in-sql-server-2012.aspx
https://www.google.com/search?q=nearest+neigbor+search+space+filling+curve
Use a k-dim tree index (in this case k=2) so a quad tree. This should allow you to efficiently search the space to the left,right,up and down of your point. You can probably formulate a query in a dmbs for this but conceptually I would search the points own "quad" and then depending on the position of the point in the quad we can know if we found the nearest point in one direction or not. Then we know which quads to search for the rest of the points.
Since you are doing this for each point you know there exists symmetry i.e. point P1 has P2 as nearest left neighbor so P2 has P1 as nearest right neighbor. So update the point objects accordingly.

find a point non collinear with all other points in a plane

Given a list of N points in the plane in general position (no three are collinear), find a new point p that is not collinear with any pair of the N original points.
We obviously cannot search for every point in the plane, I started with finding the coincidence point of all the lines that can be formed with the given points, or making a circle with them something.. I dont have any clue how to check all the points.
Question found in http://introcs.cs.princeton.edu/java/42sort/
I found this question in a renowned algorithm book that means it is answerable, but I cannot think of an optimal solution, thats why I am posting it here so that if some one knows it he/she can answer it
The best I can come up with is an N^2 algorithm. Here goes:
Choose a tolerance e to control how close you're willing to come to a line formed from the points in the set.
Compute the convex hull of your set of points.
Choose a line L parallel to one of the sides of the convex hull, at a distance 3e outside the hull.
Choose a point P on L, so that P is outside the projection of the convex hull on L. The projection of the convex hull on L is an interval of L. P must be placed outside this interval.
Test each pair of points in the set. For a particular line M formed by the 2 test points intersects a disc of radius 2e around P, move P out further along L until M no longer intersects the disc. By the construction of L, there can be no line intersecting the disk parallel to L, so this can always be done.
If M crosses L beyond P, move P beyond that intersection, again far enough that M doesn't pass through the disc.
After all this is done, choose your point at distance e, on the perpendicular to L at P. It can be colinear with no line of the set.
I'll leave the details of how to choose the next position of P along L in step 5 to you,
There are some obvious trivial rejection tests you can do so that you do more expensive checks only with the test line M is "parallel enough" to L.
Finally, I should mention that it is probably possible to push P far enough out that numerical problems occur. In that case the best I can suggest is to try another line outside of the convex hull by a distance of at least 3e.
You can actually solved it using a simple O(nlogn) algorithm, which we will then improve to O(n). Name A the bottom most point (in case of tie choose the one that is has smaller x coordinate). You can now sort in clockwise order the rest of the points using the CCW. Now as you process each point from the sorted order you can see that between any two successive points having different angle with point A and the bottom axis (let these be U, V) there is no point having angle c, with U <= c <= V. So we can add any point in this section and it is guaranteed that it won’t be collinear with any other points from the set.
So, all you need is to find one pair of adjacent points and you are done. So, find the minimum and the second minimum angle with A (these should be different) in O(n) time and select any point in between them.

Resources