Making an algorithm more efficient

algorithmsefficiency

I have a question about the efficiency of an algorithm:

You have a list of x,y points

Now I want to get all the points that are up to 5 units away from a reference x,y point

How do you calculate it in the most efficient way?

I just go over each point and calculate distance, and if that distance is less than 5, I add it to the answer

If you know how this algorithm is called – I'd be happy if you post that too.

Best Answer

While you cannot avoid having to check every point, there are techniques you can use to check quickly or check only once.

For example, most points you can tell if they are too far away without calculating the exact distance. For example, if I have a point whose x component is 100 away from the point of reference, you don't have to calculate distance. You already know that the point is too far away. Likewise if the x and y deltas are less than 5*2*sqrt(2) away from the point of reference, you know they are close enough. See the diagram below for a visual example:

Diagram describing algorithm

If P is your point of origin and the circle represents the farthest away that a point can be from point P (in your case radius of 5 units), then A and C represents places where points are definitely out or definitely in. Therefore, you can optimize your algorithm by checking x and y deltas. Only points which lie in the gray area B should have their distance calculated because you cannot know without performing a more sophisticated calculation.

Also, it would not hurt to sort your points by distance to P. If you have to perform this calculation often, you could optimize calculation by sorting once, then finding the farthest point within 5 units from P. Unless point positions change, you know that all points after that point must be closer.

I hope that helps!