Search in sources :

Example 1 with TiedTopBoundedHeap

use of de.lmu.ifi.dbs.elki.utilities.datastructures.heap.TiedTopBoundedHeap in project elki by elki-project.

the class SOD method getNearestNeighbors.

/**
 * Get the k nearest neighbors in terms of the shared nearest neighbor
 * distance.
 *
 * The query object is excluded from the knn list.
 *
 * FIXME: move this to the database layer.
 *
 * @param relation the database holding the objects
 * @param simQ similarity function
 * @param queryObject the query object for which the kNNs should be determined
 * @return the k nearest neighbors in terms of the shared nearest neighbor
 *         distance without the query object
 */
private DBIDs getNearestNeighbors(Relation<V> relation, SimilarityQuery<V> simQ, DBIDRef queryObject) {
    Heap<DoubleDBIDPair> nearestNeighbors = new TiedTopBoundedHeap<>(knn);
    for (DBIDIter iter = relation.iterDBIDs(); iter.valid(); iter.advance()) {
        if (DBIDUtil.equal(iter, queryObject)) {
            continue;
        }
        double sim = simQ.similarity(queryObject, iter);
        if (sim > 0.) {
            nearestNeighbors.add(DBIDUtil.newPair(sim, iter));
        }
    }
    // Collect DBIDs
    ArrayModifiableDBIDs dbids = DBIDUtil.newArray(nearestNeighbors.size());
    while (nearestNeighbors.size() > 0) {
        dbids.add(nearestNeighbors.poll());
    }
    return dbids;
}
Also used : TiedTopBoundedHeap(de.lmu.ifi.dbs.elki.utilities.datastructures.heap.TiedTopBoundedHeap) ArrayModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.ArrayModifiableDBIDs) DoubleDBIDPair(de.lmu.ifi.dbs.elki.database.ids.DoubleDBIDPair) DBIDIter(de.lmu.ifi.dbs.elki.database.ids.DBIDIter)

Aggregations

ArrayModifiableDBIDs (de.lmu.ifi.dbs.elki.database.ids.ArrayModifiableDBIDs)1 DBIDIter (de.lmu.ifi.dbs.elki.database.ids.DBIDIter)1 DoubleDBIDPair (de.lmu.ifi.dbs.elki.database.ids.DoubleDBIDPair)1 TiedTopBoundedHeap (de.lmu.ifi.dbs.elki.utilities.datastructures.heap.TiedTopBoundedHeap)1