Minimum Sum of Euclidean Distances to all given Points
Last Updated :
13 May, 2024
Given a matrix mat[][] consisting of N pairs of the form {x, y} each denoting coordinates of N points, the task is to find the minimum sum of the Euclidean distances to all points.
Examples:
Input: mat[][] = { { 0, 1}, { 1, 0 }, { 1, 2 }, { 2, 1 }}
Output: 4
Explanation:
Average of the set of points, i.e. Centroid = ((0+1+1+2)/4, (1+0+2+1)/4) = (1, 1).
Euclidean distance of each point from the centroid are {1, 1, 1, 1}
Sum of all distances = 1 + 1 + 1 + 1 = 4
Input: mat[][] = { { 1, 1}, { 3, 3 }}
Output: 2.82843
Approach:
Since the task is to minimize the Euclidean Distance to all points, the idea is to calculate the Median of all the points. Geometric Median generalizes the concept of median to higher dimensions
Follow the steps below to solve the problem:
- Calculate the centroid of all the given coordinates, by getting the average of the points.
- Find the Euclidean distance of all points from the centroid.
- Calculate the sum of these distance and print as the answer.
Below is the implementation of above approach:
C++
// C++ Program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to calculate Euclidean distance
double find(double x, double y,
vector<vector<int> >& p)
{
double mind = 0;
for (int i = 0; i < p.size(); i++) {
double a = p[i][0], b = p[i][1];
mind += sqrt((x - a) * (x - a)
+ (y - b) * (y - b));
}
return mind;
}
// Function to calculate the minimum sum
// of the euclidean distances to all points
double getMinDistSum(vector<vector<int> >& p)
{
// Calculate the centroid
double x = 0, y = 0;
for (int i = 0; i < p.size(); i++) {
x += p[i][0];
y += p[i][1];
}
x = x / p.size();
y = y / p.size();
// Calculate distance of all
// points
double mind = find(x, y, p);
return mind;
}
// Driver Code
int main()
{
// Initializing the points
vector<vector<int> > vec
= { { 0, 1 }, { 1, 0 }, { 1, 2 }, { 2, 1 } };
double d = getMinDistSum(vec);
cout << d << endl;
return 0;
}
Java
// Java program to implement
// the above approach
class GFG{
// Function to calculate Euclidean distance
static double find(double x, double y,
int [][] p)
{
double mind = 0;
for(int i = 0; i < p.length; i++)
{
double a = p[i][0], b = p[i][1];
mind += Math.sqrt((x - a) * (x - a) +
(y - b) * (y - b));
}
return mind;
}
// Function to calculate the minimum sum
// of the euclidean distances to all points
static double getMinDistSum(int [][]p)
{
// Calculate the centroid
double x = 0, y = 0;
for(int i = 0; i < p.length; i++)
{
x += p[i][0];
y += p[i][1];
}
x = x / p.length;
y = y / p.length;
// Calculate distance of all
// points
double mind = find(x, y, p);
return mind;
}
// Driver Code
public static void main(String[] args)
{
// Initializing the points
int [][]vec = { { 0, 1 }, { 1, 0 },
{ 1, 2 }, { 2, 1 } };
double d = getMinDistSum(vec);
System.out.print(d + "\n");
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 program to implement
# the above approach
from math import sqrt
# Function to calculate Euclidean distance
def find(x, y, p):
mind = 0
for i in range(len(p)):
a = p[i][0]
b = p[i][1]
mind += sqrt((x - a) * (x - a) +
(y - b) * (y - b))
return mind
# Function to calculate the minimum sum
# of the euclidean distances to all points
def getMinDistSum(p):
# Calculate the centroid
x = 0
y = 0
for i in range(len(p)):
x += p[i][0]
y += p[i][1]
x = x // len(p)
y = y // len(p)
# Calculate distance of all
# points
mind = find(x, y, p)
return mind
# Driver Code
if __name__ == '__main__':
# Initializing the points
vec = [ [ 0, 1 ], [ 1, 0 ],
[ 1, 2 ], [ 2, 1 ] ]
d = getMinDistSum(vec)
print(int(d))
# This code is contributed by mohit kumar 29
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to calculate Euclidean distance
static double find(double x, double y,
int [,] p)
{
double mind = 0;
for(int i = 0; i < p.GetLength(0); i++)
{
double a = p[i,0], b = p[i,1];
mind += Math.Sqrt((x - a) * (x - a) +
(y - b) * (y - b));
}
return mind;
}
// Function to calculate the minimum sum
// of the euclidean distances to all points
static double getMinDistSum(int [,]p)
{
// Calculate the centroid
double x = 0, y = 0;
for(int i = 0; i < p.GetLength(0); i++)
{
x += p[i,0];
y += p[i,1];
}
x = x / p.Length;
y = y / p.Length;
// Calculate distance of all
// points
double mind = find(x, y, p);
return mind;
}
// Driver Code
public static void Main(String[] args)
{
// Initializing the points
int [,]vec = { { 0, 1 }, { 1, 0 },
{ 1, 2 }, { 2, 1 } };
int d = (int)getMinDistSum(vec);
Console.Write(d + "\n");
}
}
// This code is contributed by Rohit_ranjan
JavaScript
<script>
// JavaScript program for the above approach
// Function to calculate Euclidean distance
function find(x, y, p)
{
let mind = 0;
for(let i = 0; i < p.length; i++)
{
let a = p[i][0], b = p[i][1];
mind += Math.sqrt((x - a) * (x - a) +
(y - b) * (y - b));
}
return mind;
}
// Function to calculate the minimum sum
// of the euclidean distances to all points
function getMinDistSum(p)
{
// Calculate the centroid
let x = 0, y = 0;
for(let i = 0; i < p.length; i++)
{
x += p[i][0];
y += p[i][1];
}
x = x / p.length;
y = y / p.length;
// Calculate distance of all
// points
let mind = find(x, y, p);
return mind;
}
// Driver Code
// Initializing the points
let vec = [[ 0, 1 ], [ 1, 0 ],
[ 1, 2 ], [ 2, 1 ]];
let d = getMinDistSum(vec);
document.write(d);
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
CSES Solutions - Minimum Euclidean Distance Given a set of points in the two-dimensional plane, your task is to find the minimum Euclidean distance between two distinct points.The Euclidean distance of points (x1,y1) and (x2,y2) is sqrt( (x1-x2)2 + (y1-y2)2 )Example:Input: points = {{2, 1} ,{4, 4} ,{1, 2} ,{6, 3}};Output: 2Input: points = {{2
7 min read
Minimum lines to cover all points Given N points in 2-dimensional space, we need to print the count of the minimum number of lines which traverse through all these N points and which go through a specific (xO, yO) point also.Examples: If given points are (-1, 3), (4, 3), (2, 1), (-1, -2), (3, -3) and (xO, yO) point is (1, 0) i.e. ev
9 min read
Find a point such that sum of the Manhattan distances is minimized Given N points in K dimensional space where 2\leq N\leq 10^{5} and 1\leq K\leq 5 . The task is to determine the point such that the sum of Manhattan distances from this point to the N points is minimized. Manhattan distance is the distance between two points measured along axes at right angles. In a
5 min read
Find a point whose sum of distances from all given points on a line is K Given a sorted array arr[] consisting of N integers, representing points on a line and an integer K, the task is to find any point P between the first and last point such that the sum of distances of all given points from P is equal to K. If no such point exists, then print "-1". Examples: Input: ar
14 min read
Minimum time to reach given points on X-axis Given an array pos[] that represents N points on the X axis, a variable cur that represents the current position, and an array time. Where time[i] denotes the time taken to cover 1 unit of distance toward ith point, the task is to reach at any of the given points in minimum time and print the minimu
5 min read
Pairs with same Manhattan and Euclidean distance In a given Cartesian plane, there are N points. The task is to find the Number of Pairs of points(A, B) such that Point A and Point B do not coincide.Manhattan Distance and the Euclidean Distance between the points should be equal.Note: Pair of 2 points(A, B) is considered same as Pair of 2 points(B
10 min read