Cody

# Problem 44353. Group-wise Euclidean distance

Input:

• x —— An array of size n-by-d, where each row vector denotes a point in a d-dimensional space;
• g —— A grouping (index) vector g of size n-by-1, which divides the points in x into groups. Specifically, the rows in x corresponding to the same group index in g belong to the same group.

Output:

• y —— The group-wise Euclidean distance matrix associated with the points in x. Suppose that m = max(g), then y will be an m-by-m matrix, where each element y(i,j) is the Euclidean distance between group i and group j, which is defined as the minimum of the Euclidean distances between any points in group i and any other points in group j.

Example:

Example 1: n = 6, d = 1

```g = [2   1   3  2  1].';
x = [3  10  15  8  5].';
y = [0   2   5            % y(1,2) = y(2,1) = min(10-3,5-3,10-8,8-5) = 2
2   0   7            % y(1,3) = y(3,1) = min(15-10,15-5) = 5
5   7   0];          % y(2,3) = y(3,2) = min(15-3,15-8) = 7
```

Example 2: n = 3, d = 2

```g = [1 2 2].';
x = [0   0
5  12
3   4];
y = [0  5;
5  0];    % y(1,2) = y(2,1) = min(sqrt(5^2+12^2),sqrt(3^2+4^2)) = 5
```

Testing:

The test suite will focus mainly on the large-scale problem dimensions (e.g., large n and/or d). The purpose is to direct attention towards efficient runtime speed of execution. Note that your solution may run into a time-out error if it is not sufficiently efficient (which is why this problem falls into the Cody5:Hard category).

Scoring:

We have modified Cody's default size-based scoring function into a performance-based scoring system (implemented by our fellow Cody player LY Cao), in which the score of your submission equals 5 times the execution time of your solution (which reprents a score resolution of 0.2 seconds and allows for more room for performance improvement). Please ignore the code size and focus only on improving the code performance, as our test suite will reject any submissions running longer than 20 seconds (in contrast to Cody's default 40 seconds timeout limit).

Please be advised that an amazingly fast solution would earn a score < 5, meaning that it completes execution of all test cases within a second!

Update (11/21/2017): Additional test cases are added to ban cheater solutions (e.g., hard-coded submissions 1351541, 1351007, 1350563, 1349442, all came from Marco Tullio).

### Solution Stats

30.11% Correct | 69.89% Incorrect
Last solution submitted on May 23, 2019