mas01mc@516: /* mas01mc@516: * MultiProbe C++ class mas01mc@516: * mas01mc@518: * Given a vector of LSH boundary distances for a query, mas01mc@518: * 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@518: #ifndef __LSH_MULTIPROBE_ mas01mc@518: #define __LSH_MULTIPROBE_ mas01mc@518: mas01mc@516: #include mas01mc@516: #include mas01mc@516: #include mas01mc@516: #include mas01mc@517: #include mas01mc@517: #include mas01mc@516: mas01mc@518: using namespace std; mas01mc@516: mas01mc@516: typedef set perturbation_set ; mas01mc@518: mas01mc@518: typedef class MinHeapElement{ mas01mc@518: public: mas01mc@518: perturbation_set perturbs; mas01mc@519: float score; mas01mc@519: MinHeapElement(perturbation_set a, float s); mas01mc@518: virtual ~MinHeapElement(); mas01mc@518: } min_heap_element; mas01mc@518: mas01mc@518: typedef priority_queue, mas01mc@518: greater mas01mc@518: > min_heap_of_perturbation_set ; mas01mc@518: mas01mc@519: typedef pair > sorted_distance_functions ; mas01mc@516: mas01mc@516: class MultiProbe{ mas01mc@516: protected: mas01mc@516: min_heap_of_perturbation_set* minHeap; mas01mc@518: min_heap_of_perturbation_set* outSets; mas01mc@518: vector* distFuns; mas01mc@517: unsigned numHashBoundaries; mas01mc@516: mas01mc@518: // data structure initialization and cleanup mas01mc@518: void initialize(); mas01mc@518: void cleanup(); mas01mc@518: mas01mc@516: // perturbation set operations mas01mc@517: perturbation_set& shift(perturbation_set&); mas01mc@517: perturbation_set& expand(perturbation_set&); mas01mc@519: float score(perturbation_set&); mas01mc@516: bool valid(perturbation_set&); mas01mc@519: void makeSortedDistFuns(vector &); mas01mc@519: void makeSortedDistFuns(float* x, unsigned N); mas01mc@519: mas01mc@519: // perturbation set generation algorithm mas01mc@519: void algorithm1(unsigned T); mas01mc@518: mas01mc@518: public: mas01mc@518: MultiProbe(); mas01mc@518: ~MultiProbe(); mas01mc@516: mas01mc@516: // generation of perturbation sets mas01mc@519: void generatePerturbationSets(vector& vectorOfBounaryDistances, unsigned numSetsToGenerate); mas01mc@519: void generatePerturbationSets(float* arrayOfBoundaryDistances, unsigned numDistances, unsigned numSetsToGenerate); mas01mc@518: perturbation_set getNextPerturbationSet(); mas01mc@518: void dump(perturbation_set); mas01mc@518: size_t size(); // Number of perturbation sets are in the output queue mas01mc@518: bool empty(); // predicate for empty MultiProbe set mas01mc@519: int getIndex(perturbation_set::iterator it); // return index of hash function for given set entry mas01mc@519: int getBoundary(perturbation_set::iterator it); // return boundary {-1,+1} for given set entry mas01mc@516: }; mas01mc@516: mas01mc@518: 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: */ mas01mc@518: mas01mc@518: #endif // __LSH_MULTIPROBE_