
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.

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.
4 comentarios:
Hey - I am certainly glad to find this. cool job!
Publicar un comentario