The problem was the follow: this algorithm use "Iterators" on the vector, as follows:
Iterator i1=layer.iterator();
while(i1.hasNext()){
i1.next();
Iterator i2=layer.iterator();
while(i2.hasNext()){
i2.next();
// calculate Euclidean distance
}
}
but it has a problem in optimization terms, because the distance between point #1 and point #2 is the same that between point #2 and point #1, i.e.,an optimized algorithm only need compute the superior diagonal of the matrix, beacuse the matrix of distances is simetric.
In above sample, with optimized algorithm, we need 3 iterations on the vector and symmetrize the matrix; in the other hand without optimization we need 9 iterations: n^2.
If we assume that the objects in the vector are unique, the best solution in order of do not lose the semantic of Iterators is the use the follows code:
Iterator
while (i1.hasNext()) {
Integer ii1 = (Integer) i1.next();
Iterator
while (i2.hasNext()){
i2.next();
}
}
If we use two index in order to follow the loops, we can use the follows code:
int i=0;
Iterator
while (i1.hasNext()) {
c=0:
Integer ii1 = (Integer) i1.next();
Iterator
while (i2.hasNext()){
i2.next();
long distance = ....
matrix[i][j] = distance;
matrix[j][i] = distance;
c++;
}
i++;
}
In this way we can reduce the computing time in n/2.