Minimum Sum of Euclidean Distances to all given Points | Python3 program to implement the above approach ; Function to calculate Euclidean distance ; Function to calculate the minimum sum of the euclidean distances to all points ; Calculate the centroid ; Calculate distance of all points ; Driver Code ; Initializing the points
from math import sqrt NEW_LINE def find ( x , y , p ) : NEW_LINE INDENT mind = 0 NEW_LINE for i in range ( len ( p ) ) : NEW_LINE INDENT a = p [ i ] [ 0 ] NEW_LINE b = p [ i ] [ 1 ] NEW_LINE mind += sqrt ( ( x - a ) * ( x - a ) + ( y - b ) * ( y - b ) ) NEW_LINE DEDENT return mind NEW_LINE DEDENT def getMinDistSum ( p ) : NEW_LINE INDENT x = 0 NEW_LINE y = 0 NEW_LINE for i in range ( len ( p ) ) : NEW_LINE INDENT x += p [ i ] [ 0 ] NEW_LINE y += p [ i ] [ 1 ] NEW_LINE DEDENT x = x // len ( p ) NEW_LINE y = y // len ( p ) NEW_LINE mind = find ( x , y , p ) NEW_LINE return mind NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT vec = [ [ 0 , 1 ] , [ 1 , 0 ] , [ 1 , 2 ] , [ 2 , 1 ] ] NEW_LINE d = getMinDistSum ( vec ) NEW_LINE print ( int ( d ) ) NEW_LINE DEDENT