How to Compute Minkowski, Euclidean and CityBlock Distance in C++?


Given a number of coordinates (could be any dimensions, but for simplicity we use 2 dimension X/Y coordinates for demonstration), you can get the ‘central’ point by averaging all coordinates:

tex_974b1df50f9f40cfbf40a70cb200056c How to Compute Minkowski, Euclidean and CityBlock Distance in C++? c / c++ math
tex_c7d8e88e8d76dca1e1abf58ba2deb343 How to Compute Minkowski, Euclidean and CityBlock Distance in C++? c / c++ math

where n is the number of X/Y points. The C/C++ code is simple as the formula:

1
2
3
4
5
6
7
8
void GetXY(double *x, double *y, int n, double *ax, double *ay) {    
    ax = 0;
    ay = 0;
    for (int i = 0; i < n; i ++) {
        ax += (x[i] / n);
        ay += (y[i] / n);
    }
}
void GetXY(double *x, double *y, int n, double *ax, double *ay) {    
    ax = 0;
    ay = 0;
    for (int i = 0; i < n; i ++) {
        ax += (x[i] / n);
        ay += (y[i] / n);
    }
}

In the machine learning K-means algorithm where the 'distance' is required before the candidate cluttering point is moved to the 'central' point. To compute the distance, wen can use following three methods: Minkowski, Euclidean and CityBlock Distance.

Minkowski Distance

The Minkowski Distance can be computed by the following formula, the parameter tex_3ae7d06c160d08e65eaca60d50bdfa77 How to Compute Minkowski, Euclidean and CityBlock Distance in C++? c / c++ math can be arbitary.

tex_27aa0210836aef89b91234bc3970855c How to Compute Minkowski, Euclidean and CityBlock Distance in C++? c / c++ math

So, translating into C/C++ code with help of this easiest power function:

1
2
3
4
5
6
7
double GetMinkowskiDistance(double *x, double *y, int n, double r) {
    double sum = 0;
    for (int i = 0; i < n; i ++) {
        sum += power(x[i] - y[i], r);
    }
    return power(sum, 1.0 / r);
}
double GetMinkowskiDistance(double *x, double *y, int n, double r) {
    double sum = 0;
    for (int i = 0; i < n; i ++) {
        sum += power(x[i] - y[i], r);
    }
    return power(sum, 1.0 / r);
}

Euclidean Distance

Euclidean Distance is the case when tex_0f00defb17f63a3e3d48a97608078dd8 How to Compute Minkowski, Euclidean and CityBlock Distance in C++? c / c++ math .

CityBlock Distance

CityBlock Distance is the case when tex_6a09b8aaf50dffa01b114e8e2ba22bc3 How to Compute Minkowski, Euclidean and CityBlock Distance in C++? c / c++ math .

Other Distances Formula

When tex_3ae7d06c160d08e65eaca60d50bdfa77 How to Compute Minkowski, Euclidean and CityBlock Distance in C++? c / c++ math approaches infinity, we obtain the Chebyshev distance. If you visualize all these methods with different value of tex_3ae7d06c160d08e65eaca60d50bdfa77 How to Compute Minkowski, Euclidean and CityBlock Distance in C++? c / c++ math , you could see that how the 'central' point is approached.

distance-of-minkowski-distance How to Compute Minkowski, Euclidean and CityBlock Distance in C++? c / c++ math

distance-of-minkowski-distance

--EOF (The Ultimate Computing & Technology Blog) --

GD Star Rating
loading...
636 words
Last Post: How to Compute the Power of Arbitrary Base without Loops in C/C++?
Next Post: How to Generate 100K Test Data to MySQL Database?

The Permanent URL is: How to Compute Minkowski, Euclidean and CityBlock Distance in C++?

Leave a Reply