int main(int argc, char *argv[]) {
    if (argc != 2) {
        fprintf(stderr, "Usage: benchmark GRAPH\n");
        return 0;
    }
    
    PrunedHighwayLabeling phl;
    phl.ConstructLabel(argv[1]);
    phl.Statistics();
    
    int V = phl.NumVertices();
    vector <int> v(NumQueries);
    vector <int> w(NumQueries);
    for (int i = 0; i < NumQueries; i++) {
        v[i] = rand() % V;
        w[i] = rand() % V;
    }
    
    double start_time = GetTime();
    for (int i = 0; i < NumQueries; i++) phl.Query(v[i], w[i]);
    double end_time = GetTime();
    printf("Query Time : %lf usec\n", (end_time - start_time) * 1e6 / NumQueries);
    
    return 0;
}
Beispiel #2
0
void IER::getKNNsByPHLTravelTimes(StaticRtree& rtree, unsigned int k, NodeID queryNodeID, std::vector<NodeID>& kNNs, 
                                  std::vector<EdgeWeight>& kNNDistances, Graph& graph, PrunedHighwayLabeling& phl)
{
    // Retrieve kNN by euclidean distance
    std::vector<NodeID> euclideanKNNs;
    std::vector<EuclideanDistanceSqrd> euclideanKNNDistances;
    Coordinate queryNodeX, queryNodeY;
    graph.getCoordinates(queryNodeID,queryNodeX,queryNodeY);
    BinaryMinHeap<EuclideanDistanceSqrd,RtreeDataTuple> heap = rtree.getKNNs(k,queryNodeX,queryNodeY,euclideanKNNs,euclideanKNNDistances);
    // Note: We keep heap so that we incrementally retrieve further euclidean NNs

    // We compute the network distances to each of these
    BinaryMaxHeap<EdgeWeight,NodeID> knnCandidates;
    EdgeWeight spDist, Dk, minTimeByEuclid;
    for (std::size_t i = 0; i < euclideanKNNs.size(); ++i) {
        spDist = phl.Query(queryNodeID,euclideanKNNs[i]);
        knnCandidates.insert(euclideanKNNs[i],spDist);
    }

    // While the euclidean distance to the next euclidean nearest neigbour
    // is smaller than than network distance to the current kth neighbour
    // we can potentially find a closer nearest neighbour. Keep searching
    // until this lower bound exceed the kth neighbour network distance.
    Dk = knnCandidates.getMaxKey();
    NodeID nextEuclidNN;
    EuclideanDistanceSqrd currEuclidDistSqrd;
    while (true) {
        if (rtree.getNextNearestNeighbour(heap,queryNodeX,queryNodeY,nextEuclidNN,currEuclidDistSqrd)) {
            // Note: If there were less than k objects to begin with then getNextNearestNeighbour
            // would return false (as the heap is empty) and we not reach here
            minTimeByEuclid = std::ceil(std::sqrt(currEuclidDistSqrd)/graph.getMaxGraphSpeedByEdge()); // Floor as it's a lowerbound
            if (minTimeByEuclid < Dk) {
                spDist = phl.Query(queryNodeID,nextEuclidNN);
                if (spDist < Dk) {
                    // Only insert if it is a better kNN candidate
                    knnCandidates.insert(nextEuclidNN,spDist);
                    knnCandidates.extractMaxElement();
                    Dk = knnCandidates.getMaxKey();
                }
            } else {
                break;
            }
        } else {
            // This mean no nearest neighbours were found (we have reported
            // all objects) so we can stop the search
            break;
        }
    }

    knnCandidates.populateKNNs(kNNs,kNNDistances);
}