Given a set of points as and a line as ax+by+c = 0. We need to find a point on given line for which sum of distances from given set of points is minimum.
In above figure optimum location of point of x - y - 3 = 0 line is (2, -1), whose total distance with other points is 20.77, which is minimum obtainable total distance.
If we take one point on given line at infinite distance then total distance cost will be infinite, now when we move this point on line towards given points the total distance cost starts decreasing and after some time, it again starts increasing which reached to infinite on the other infinite end of line so distance cost curve looks like a U-curve and we have to find the bottom value of this U-curve.
As U-curve is not monotonically increasing or decreasing we can’t use binary search for finding bottom most point, here we will use ternary search for finding bottom most point, ternary search skips one third of search space at each iteration, you can read more about ternary search here.
So solution proceeds as follows, we start with low and high initialized as some smallest and largest values respectively, then we start iteration, in each iteration we calculate two mids, mid1 and mid2, which represent 1/3rd and 2/3rd position in search space, we calculate total distance of all points with mid1 and mid2 and update low or high by comparing these distance cost, this iteration continues untill low and high become approximately equal.
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Rotation of a point about another point in C++
- Reflection of a point at 180 degree rotation of another point
- Shortest distance between a Line and a Point in a 3-D plane
- Perpendicular distance between a point and a Line in 2 D
- Distance between a point and a Plane in 3 D
- Shortest distance between a point and a circle
- Ratio of the distance between the centers of the circles and the point of intersection of two direct common tangents to the circles
- Ratio of the distance between the centers of the circles and the point of intersection of two transverse common tangents to the circles
- Sort an Array of Points by their distance from a reference Point
- Count paths with distance equal to Manhattan distance
- Distance of chord from center when distance between center and another equal length chord is given
- Count of different straight lines with total n points with m collinear
- Total area of two overlapping rectangles
- Total number of triangles formed when there are H horizontal and V vertical lines
- Total number of triplets (A, B, C) in which the points B and C are Equidistant to A
- Total number of unit cells covered by all given Rectangles
- Minimize steps defined by a string required to reach the destination from a given source
- Check whether a given point lies inside a triangle or not
- How to check if a given point lies inside or outside a polygon?
- Triangle with no point inside