mas01mc@516: /* mas01mc@516: * MultiProbe C++ class mas01mc@516: * mas01mc@516: * Given a LSH structure, perform lookup by probing nearby hash-function locations mas01mc@516: * mas01mc@516: * Implementation using C++ STL mas01mc@516: * mas01mc@516: * Reference: mas01mc@516: * Qin Lv, William Josephson, Zhe Wang, Moses Charikar and Kai Li, mas01mc@516: * "Multi-Probe LSH: Efficient Indexing for High-Dimensional Similarity mas01mc@516: * Search", Proc. Intl. Conf. VLDB, 2007 mas01mc@516: * mas01mc@516: * mas01mc@516: * Copyright (C) 2009 Michael Casey, Dartmouth College, All Rights Reserved mas01mc@516: * License: GNU Public License 2.0 mas01mc@516: * mas01mc@516: */ mas01mc@516: mas01mc@516: #include mas01mc@516: #include mas01mc@516: #include mas01mc@516: #include mas01mc@516: mas01mc@516: using namespace std ; mas01mc@516: mas01mc@516: typedef set perturbation_set ; mas01mc@516: typedef vector vector_of_perturbation_set ; mas01mc@516: typedef pair min_heap_element ; mas01mc@516: typedef vector vector_of_min_heap_element ; mas01mc@516: typedef priority_queue< mas01mc@516: min_heap_element, mas01mc@516: vector_of_min_heap_element, mas01mc@516: greater mas01mc@516: > min_heap_of_perturbation_set ; mas01mc@516: typedef pair > sorted_distance_functions ; mas01mc@516: mas01mc@516: bool operator>(const min_heap_element& a, const min_heap_element& b){ mas01mc@516: return a.second > b.second; mas01mc@516: } mas01mc@516: mas01mc@516: bool operator<(const min_heap_element& a, const min_heap_element& b){ mas01mc@516: return a.second < b.second; mas01mc@516: } mas01mc@516: mas01mc@516: bool operator>(const sorted_distance_functions& a, const sorted_distance_functions& b){ mas01mc@516: return a.second > b.second; mas01mc@516: } mas01mc@516: mas01mc@516: bool operator<(const sorted_distance_functions& a, const sorted_distance_functions& b){ mas01mc@516: return a.second < b.second; mas01mc@516: } mas01mc@516: mas01mc@516: class MultiProbe{ mas01mc@516: protected: mas01mc@516: min_heap_of_perturbation_set* minHeap; mas01mc@516: vector_of_perturbation_set* outSets; mas01mc@516: sorted_distance_functions distFuns; mas01mc@516: public: mas01mc@516: MultiProbe(); mas01mc@516: ~MultiProbe(); mas01mc@516: mas01mc@516: // perturbation set operations mas01mc@516: perturbation_set& shift(perturbation_set); mas01mc@516: perturbation_set& expand(perturbation_set); mas01mc@516: double score(perturbation_set&); mas01mc@516: bool valid(perturbation_set&); mas01mc@516: mas01mc@516: // generation of perturbation sets mas01mc@516: void generatePerturbationSets(int, vector&); mas01mc@516: sorted_distance_functions& makeSortedDistFuns(vector &); mas01mc@516: }; mas01mc@516: mas01mc@516: /* NOTES: mas01mc@516: mas01mc@516: Reference: mas01mc@516: Qin Lv, William Josephson, Zhe Wang, Moses Charikar and Kai Li, mas01mc@516: "Multi-Probe LSH: Efficient Indexing for High-Dimensional Similarity mas01mc@516: Search", Proc. Intl. Conf. VLDB, 2007 mas01mc@516: mas01mc@516: i = 1..M (number of hash functions used) mas01mc@516: f_i(q) = a_i * q + b_i // projection to real line mas01mc@516: h_i(q) = floor( ( a_i * q + b_i ) / w ) // hash slot mas01mc@516: delta \in {-1, +1} mas01mc@516: x_i( delta ) = distance of query to left or right boundary mas01mc@516: Delta = [delta_1 delta_2 ... delta_M] mas01mc@516: score(Delta) = sum_{i=1}_M x_i(delta_i).^2 mas01mc@516: mas01mc@516: z = sort(x(delta_i), increasing) // i = 1..2M delta={+1,-1} mas01mc@516: p_j = (i, delta) if z_j == x_i(delta) mas01mc@516: mas01mc@516: A_k is index into p_j mas01mc@516: mas01mc@516: Multi-probe algorithm (after Lv et al. 2007) mas01mc@516: --------------------------------------------- mas01mc@516: mas01mc@516: A0 = {1} mas01mc@516: minHeap.insert(A0, score(A0)) mas01mc@516: mas01mc@516: for i = 1 to T do // T = number of perturbation sets mas01mc@516: repeat mas01mc@516: Ai = minHeap.extractMin() mas01mc@516: As = shift(Ai) mas01mc@516: minHeap.insert(As, score(As)) mas01mc@516: Ae = expand(Ai) mas01mc@516: minHeap.insert(Ae, score(Ae)) mas01mc@516: until valid(Ai) mas01mc@516: output Ai mas01mc@516: end for mas01mc@516: mas01mc@516: */