Optimum location of point to minimize total distance

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.
Example:

optimuml

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.

C++



filter_none

edit
close

play_arrow

link
brightness_4
code

//  C/C++ program to find optimum location and total cost
#include <bits/stdc++.h>
using namespace std;
#define sq(x) ((x)*(x))
#define EPS 1e-6
#define N 5
  
//  structure defining a point
struct point
{
    int x, y;
    point()  {}
    point(int x, int y) : x(x), y(y)   {}
};
  
//  structure defining a line of ax + by + c = 0 form
struct line
{
    int a, b, c;
    line(int a, int b, int c) : a(a), b(b), c(c) {}
};
  
//  method to get distance of point (x, y) from point p
double dist(double x, double y, point p)
{
    return sqrt(sq(x - p.x) + sq(y - p.y));
}
  
/*  Utility method to compute total distance all points
    when choose point on given line has x-cordinate
    value as X   */
double compute(point p[], int n, line l, double X)
{
    double res = 0;
  
    //  calculating Y of choosen point by line equation
    double Y = -1 * (l.c + l.a*X) / l.b;
    for (int i = 0; i < n; i++)
        res += dist(X, Y, p[i]);
  
    return res;
}
  
//  Utility method to find minimum total distance
double findOptimumCostUtil(point p[], int n, line l)
{
    double low = -1e6;
    double high = 1e6;
  
    // loop untill difference between low and high
    // become less than EPS
    while ((high - low) > EPS)
    {
        // mid1 and mid2 are representative x co-ordiantes
        // of search space
        double mid1 = low + (high - low) / 3;
        double mid2 = high - (high - low) / 3;
  
        //
        double dist1 = compute(p, n, l, mid1);
        double dist2 = compute(p, n, l, mid2);
  
        // if mid2 point gives more total distance,
        // skip third part
        if (dist1 < dist2)
            high = mid2;
  
        // if mid1 point gives more total distance,
        // skip first part
        else
            low = mid1;
    }
  
    // compute optimum distance cost by sending average
    // of low and high as X
    return compute(p, n, l, (low + high) / 2);
}
  
//  method to find optimum cost
double findOptimumCost(int points[N][2], line l)
{
    point p[N];
  
    //  converting 2D array input to point array
    for (int i = 0; i < N; i++)
        p[i] = point(points[i][0], points[i][1]);
  
    return findOptimumCostUtil(p, N, l);
}
  
//  Driver code to test above method
int main()
{
    line l(1, -1, -3);
    int points[N][2] = {{-3, -2}, {-1, 0}, {-1, 2},
                                   {1, 2}, {3, 4}};
    cout << findOptimumCost(points, l) << endl;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// A Java program to find optimum location
// and total cost
class GFG 
{
static double sq(double x)
{
    return((x) * (x));
}
static int EPS = (int) (1e-6) + 1;
static int N = 5;
  
// structure defining a point
static class point
{
    int x, y;
    point() {}
  
    public point(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
};
  
// structure defining a line of ax + by + c = 0 form
static class line
{
    int a, b, c;
  
    public line(int a, int b, int c) 
    {
        this.a = a;
        this.b = b;
        this.c = c;
    }
};
  
// method to get distance of point (x, y) from point p
static double dist(double x, double y, point p)
{
    return Math.sqrt(sq(x - p.x) + sq(y - p.y));
}
  
/* Utility method to compute total distance all points
    when choose point on given line has x-cordinate
    value as X */
static double compute(point p[], int n,
                      line l, double X)
{
    double res = 0;
  
    // calculating Y of choosen point by line equation
    double Y = -1 * (l.c + l.a * X) / l.b;
    for (int i = 0; i < n; i++)
        res += dist(X, Y, p[i]);
  
    return res;
}
  
// Utility method to find minimum total distance
static double findOptimumCostUtil(point p[], 
                                  int n, line l)
{
    double low = -1e6;
    double high = 1e6;
  
    // loop untill difference between low and high
    // become less than EPS
    while ((high - low) > EPS)
    {
        // mid1 and mid2 are representative x co-ordiantes
        // of search space
        double mid1 = low + (high - low) / 3;
        double mid2 = high - (high - low) / 3;
  
        double dist1 = compute(p, n, l, mid1);
        double dist2 = compute(p, n, l, mid2);
  
        // if mid2 point gives more total distance,
        // skip third part
        if (dist1 < dist2)
            high = mid2;
  
        // if mid1 point gives more total distance,
        // skip first part
        else
            low = mid1;
    }
  
    // compute optimum distance cost by sending average
    // of low and high as X
    return compute(p, n, l, (low + high) / 2);
}
  
// method to find optimum cost
static double findOptimumCost(int points[][], line l)
{
    point []p = new point[N];
  
    // converting 2D array input to point array
    for (int i = 0; i < N; i++)
        p[i] = new point(points[i][0], 
                         points[i][1]);
  
    return findOptimumCostUtil(p, N, l);
}
  
// Driver Code
public static void main(String[] args)
{
    line l = new line(1, -1, -3);
    int points[][] = {{-3, -2}, {-1, 0}, {-1, 2},
                                 {1, 2}, {3, 4}};
    System.out.println(findOptimumCost(points, l));
}
}
  
// This code is contributed by Rajput-Ji

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find optimum location
// and total cost
using System;
      
class GFG 
{
static double sq(double x)
{
    return((x) * (x));
}
  
static int EPS = (int) (1e-6) + 1;
static int N = 5;
  
// structure defining a point
public class point
{
    public int x, y;
    public point() {}
  
    public point(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
};
  
// structure defining a line 
// of ax + by + c = 0 form
public class line
{
    public int a, b, c;
  
    public line(int a, int b, int c) 
    {
        this.a = a;
        this.b = b;
        this.c = c;
    }
};
  
// method to get distance of
// point (x, y) from point p
static double dist(double x, 
                   double y, point p)
{
    return Math.Sqrt(sq(x - p.x) + 
                     sq(y - p.y));
}
  
/* Utility method to compute total distance 
of all points when choose point on 
given line has x-cordinate value as X */
static double compute(point []p, int n,
                      line l, double X)
{
    double res = 0;
  
    // calculating Y of choosen point
    // by line equation
    double Y = -1 * (l.c + l.a * X) / l.b;
    for (int i = 0; i < n; i++)
        res += dist(X, Y, p[i]);
  
    return res;
}
  
// Utility method to find minimum total distance
static double findOptimumCostUtil(point []p, 
                                  int n, line l)
{
    double low = -1e6;
    double high = 1e6;
  
    // loop untill difference between 
    // low and high become less than EPS
    while ((high - low) > EPS)
    {
        // mid1 and mid2 are representative 
        // x co-ordiantes of search space
        double mid1 = low + (high - low) / 3;
        double mid2 = high - (high - low) / 3;
  
        double dist1 = compute(p, n, l, mid1);
        double dist2 = compute(p, n, l, mid2);
  
        // if mid2 point gives more total distance,
        // skip third part
        if (dist1 < dist2)
            high = mid2;
  
        // if mid1 point gives more total distance,
        // skip first part
        else
            low = mid1;
    }
  
    // compute optimum distance cost by 
    // sending average of low and high as X
    return compute(p, n, l, (low + high) / 2);
}
  
// method to find optimum cost
static double findOptimumCost(int [,]points,
                                     line l)
{
    point []p = new point[N];
  
    // converting 2D array input to point array
    for (int i = 0; i < N; i++)
        p[i] = new point(points[i, 0], 
                         points[i, 1]);
  
    return findOptimumCostUtil(p, N, l);
}
  
// Driver Code
public static void Main(String[] args)
{
    line l = new line(1, -1, -3);
    int [,]points = {{-3, -2}, {-1, 0}, {-1, 2},
                                {1, 2}, {3, 4}};
    Console.WriteLine(findOptimumCost(points, l));
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Output:

20.7652

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 contribute@geeksforgeeks.org. 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.



My Personal Notes arrow_drop_up

Improved By : Rajput-Ji, 29AjayKumar

Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.