R代写:CSC120-EuclideanMinimumWeightMatching

Requirement

You should use an R script for grouping up all dots in a vector including an even number of dots in this request, in a way that tries to minimize the sum of the distances between the dots in each of the pairs. This problem can arise in many contexts, including design of experiments in statistics. Doing this will provide practice in basic R scriptming facilities such as vectors and matrices, and use of if and for, in use of R’s plotting facilities, and in how to divide a script into several functions that can be understood and sometimes used separately.

The output of the script is a vector of pairs of input dots, with each of the input dots being in exactly one pair. The order of the pairs and the order of dots in each pair are not important. The objective is to try to find a vector of pairs for which the total of the distances from one dot in a pair to the other is small. Ideally, this total distance would be as small as possible, but the method used in this request is not guaranteed to find a vector of groupings that achieve this minimum, it just tries to make the total distance as small as it can.

The input for this problem is a vector of n dots in k-dimensional Euclidean space. The number of dots, n, will always be even. For the actual data you will work with, k is two, so the input is a vector of dots in the plane, but except for the 2-dimensional plotting function described below, your script should work for any dimensionality, k, and for any even number of dots, n.

We also must specify how a function to find the groupings will work. For this request, you should write a function called find_groupings that can be called with a single argument that is a matrix of dots, as specified above, and which will return as its value a matrix giving the pairs of dots that we hope have a small total distance. (Below, some additional optional arguments to find_groupings will be specified.)

In designing this script, we must decide how to represent the input (a vector of dots) as an R data structure. For this request, we will use an n by k matrix, in which each row represents a dot in k-dimensional Euclidean space. We must also decide how to represent the vector of pairs of dots that is the output. We will use an n=2 by 2 matrix for this, in which each row contains two integers between 1 and n, which index the rows for the two dots in the input matrix.