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++
#include <bits/stdc++.h>
using namespace std;
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;
}
double getMinDistSum(vector<vector< int > >& p)
{
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();
double mind = find(x, y, p);
return mind;
}
int main()
{
vector<vector< int > > vec
= { { 0, 1 }, { 1, 0 }, { 1, 2 }, { 2, 1 } };
double d = getMinDistSum(vec);
cout << d << endl;
return 0;
}
|
Java
class GFG{
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;
}
static double getMinDistSum( int [][]p)
{
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;
double mind = find(x, y, p);
return mind;
}
public static void main(String[] args)
{
int [][]vec = { { 0 , 1 }, { 1 , 0 },
{ 1 , 2 }, { 2 , 1 } };
double d = getMinDistSum(vec);
System.out.print(d + "\n" );
}
}
|
Python3
from math import sqrt
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
def getMinDistSum(p):
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)
mind = find(x, y, p)
return mind
if __name__ = = '__main__' :
vec = [ [ 0 , 1 ], [ 1 , 0 ],
[ 1 , 2 ], [ 2 , 1 ] ]
d = getMinDistSum(vec)
print ( int (d))
|
C#
using System;
class GFG{
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;
}
static double getMinDistSum( int [,]p)
{
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;
double mind = find(x, y, p);
return mind;
}
public static void Main(String[] args)
{
int [,]vec = { { 0, 1 }, { 1, 0 },
{ 1, 2 }, { 2, 1 } };
int d = ( int )getMinDistSum(vec);
Console.Write(d + "\n" );
}
}
|
Javascript
<script>
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 getMinDistSum(p)
{
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;
let mind = find(x, y, p);
return mind;
}
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)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
11 May, 2022
Like Article
Save Article