Hammered distance between N points in a 2-D plane

Given n number of point in 2-d plane followed by Xi, Yi describing n points. The task is to calculate the hammered distance of n points.
Note: Hammered distance is the sum of the square of the shortest distance between every pair of the point.

Examples:

Input: n = 3
0 1
0 0
1 0
Output: 4

Input: n = 4
1 0
2 0
3 0
4 0

Output: 20

Basic Approach:As we have to find out sum of square of shortest distance among all the pairs.So, we can take every possible pair and calculate the sum of square of distance.



// Pseudo code to find hammered-distance using above approach.
//this will store hammered distance
Distance=0
for(int i=0;i<n;i++)
{
    for(int j=i+1;j<n;j++)
    {
         //shortest distance between point i and j.
         Distance+=(x[i]-x[j])^2+(y[i]-y[j])^2
     }
}

Its time complexity will be O(n^2).

Efficient Approach: This problem can be solved in time complexity of O(N).

     \begin{document} $$Sum=\sum_{i=1}^{n} \sum_{j=1}^{i-1} \left(X_j-X_i \right)^2+\left(Y_j-Y_i \right)^2$$ We can solve separtely for x and y coordinates. For X: $$Sum_x=\sum_{i=1}^{n} \sum_{j=1}^{i-1} \left(X_j-X_i \right)^2$$ $$Sum_x= \sum_{i=1}^{n} \sum_{j=1}^{i-1} \left(X_j^2+X_i^2 -2 \cdot X_i \cdot X_j \right)$$ Now expand the summation part, We can write this equation as- $$Sum_x=\sum_{i=1}^{n} \left( (i-1)*X_i^2+\sum_{j=1}^{i-1}X_j^2-2 \cdot X_i\cdot \sum_{j=1}^{i-1} X_j \right)$$ $\sum_{j=1}^{i-1}X_j$ This is commulative sum of square of points upto i-1.So, this can be calculated in linear time. Similarly, This can also be calculated in linear time. $2 \cdot X_i\cdot \sum_{j=1}^{i-1} X_j$ \end{document}



Below is the implementation of above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of above approach
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
  
// Function calculate cummalative sum
// of x, y, x^2, y^2 coordinates.
ll cumm(vector<ll>& x, vector<ll>& y,
        vector<ll>& cummx, vector<ll>& cummy,
        vector<ll>& cummx2, vector<ll>& cummy2, ll n)
{
    for (int i = 1; i <= n; i++) {
        cummx[i] = cummx[i - 1] + x[i];
        cummy[i] = cummy[i - 1] + y[i];
        cummx2[i] = cummx2[i - 1] + x[i] * x[i];
        cummy2[i] = cummy2[i - 1] + y[i] * y[i];
    }
}
  
// Function ot calculate the hammered distance
int calHammeredDistance(int n, vector<ll>& x, vector<ll>& y)
{
    // cummx conatins cummulative sum of x
    // cummy conatins cummulative sum of y
    vector<ll> cummx(n + 1, 0), cummy(n + 1, 0);
  
    // cummx2 conatins cummulative sum of x^2
    // cummy2 conatins cummulative sum of y^2
    vector<ll> cummx2(n + 1, 0), cummy2(n + 1, 0);
  
    // calculate cummalative of x
    //, y, x^2, y^2, because these terms
    // required in formula to reduce complexity.
  
    // this function calculate all required terms.
    cumm(x, y, cummx, cummy, cummx2, cummy2, n);
  
    // hdx calculate hammer distance for x coordinate
    // hdy calculate hammer distance for y coordinate
    ll hdx = 0, hdy = 0;
  
    for (int i = 1; i <= n; i++) {
  
        // came from formula describe in explanation
        hdx += (i - 1) * x[i] * x[i] + cummx2[i - 1]
               - 2 * x[i] * cummx[i - 1];
  
        // came from formula describe in explanation
        hdy += (i - 1) * y[i] * y[i] + cummy2[i - 1]
               - 2 * y[i] * cummy[i - 1];
    }
  
    // total is the sum of both x and y.
    ll total = hdx + hdy;
    return total;
}
  
// Driver code
int main()
{
    // number of points
    int n = 3;
  
    // x contains the x coordinates
    // y conatins the y coordinates
    vector<ll> x(n + 1), y(n + 1);
    x = { 0, 0, 1 };
    y = { 1, 0, 0 };
  
    cout << calHammeredDistance(n, x, y);
  
    return 0;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the 
# above approach 
  
# Function calculate cummalative sum 
# of x, y, x^2, y^2 coordinates. 
def cumm(x, y, cummx, cummy, 
               cummx2, cummy2, n): 
  
    for i in range(1, n+1): 
        cummx[i] = cummx[i - 1] + x[i] 
        cummy[i] = cummy[i - 1] + y[i] 
        cummx2[i] = cummx2[i - 1] + x[i] * x[i] 
        cummy2[i] = cummy2[i - 1] + y[i] * y[i] 
  
# Function ot calculate the 
# hammered distance 
def calHammeredDistance(n, x, y): 
  
    # cummx conatins cummulative sum of x 
    # cummy conatins cummulative sum of y 
    cummx = [0] * (n + 1)
    cummy = [0] * (n + 1
  
    # cummx2 conatins cummulative sum of x^2 
    # cummy2 conatins cummulative sum of y^2 
    cummx2 = [0] * (n + 1)
    cummy2 = [0] * (n + 1
  
    # calculate cumulative of x , y, x^2, y^2, 
    # because these terms are required in the
    # formula to reduce complexity. 
  
    # This function calculate all required terms. 
    cumm(x, y, cummx, cummy, cummx2, cummy2, n) 
  
    # hdx calculate hammer distance for x coordinate 
    # hdy calculate hammer distance for y coordinate 
    hdx, hdy = 0, 0
  
    for i in range(1, n + 1): 
  
        # came from formula describe in explanation 
        hdx += ((i - 1) * x[i] * x[i] + cummx2[i - 1] -
                             2 * x[i] * cummx[i - 1])
  
        # came from formula describe in explanation 
        hdy += ((i - 1) * y[i] * y[i] + cummy2[i - 1] - 
                             2 * y[i] * cummy[i - 1])
      
    # total is the sum of both x and y. 
    total = hdx + hdy 
    return total 
  
# Driver Code
if __name__ == "__main__":
  
    # number of points 
    n = 3
  
    # x contains the x coordinates 
    # y conatins the y coordinates 
    x = [0, 0, 1, 0
    y = [1, 0, 0, 0
  
    print(calHammeredDistance(n, x, y)) 
  
# This code is contributed by Rituraj Jain

chevron_right


Output:

2


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


1


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