view multiprobe.h @ 516:2a7bad47a4a7 multiprobeLSH

New branch to implement multiprobe LSH. Copy of trunk:802. Added multiprobe.{cpp,h} source files.
author mas01mc
date Sat, 24 Jan 2009 19:51:46 +0000
parents
children 807c8be7dd45
line wrap: on
line source
/*
 * MultiProbe C++ class
 *
 * Given a LSH structure, perform lookup by probing nearby hash-function locations
 *
 * Implementation using C++ STL
 *
 * Reference:
 * Qin Lv, William Josephson, Zhe Wang, Moses Charikar and Kai Li,
 * "Multi-Probe LSH: Efficient Indexing for High-Dimensional Similarity
 * Search", Proc. Intl. Conf. VLDB, 2007
 *
 *
 * Copyright (C) 2009 Michael Casey, Dartmouth College, All Rights Reserved
 * License: GNU Public License 2.0
 *
 */

#include <functional>
#include <queue>
#include <vector>
#include <set>

using namespace std ;

typedef set<int > perturbation_set ;
typedef vector<perturbation_set > vector_of_perturbation_set ;
typedef pair<perturbation_set, float > min_heap_element ;
typedef vector<min_heap_element > vector_of_min_heap_element ;
typedef priority_queue<
  min_heap_element, 
  vector_of_min_heap_element, 
  greater<min_heap_element> 
  > min_heap_of_perturbation_set ;
typedef pair<double, pair<int, int> > sorted_distance_functions ;

bool operator>(const min_heap_element& a, const min_heap_element& b){
  return a.second > b.second;
}

bool operator<(const min_heap_element& a, const min_heap_element& b){
  return a.second < b.second;
}

bool operator>(const sorted_distance_functions& a, const sorted_distance_functions& b){
  return a.second > b.second;
}

bool operator<(const sorted_distance_functions& a, const sorted_distance_functions& b){
  return a.second < b.second;
}

class MultiProbe{
protected:
  min_heap_of_perturbation_set* minHeap;
  vector_of_perturbation_set* outSets;
  sorted_distance_functions distFuns;
public: 
  MultiProbe();
  ~MultiProbe();

  // perturbation set operations
  perturbation_set& shift(perturbation_set);
  perturbation_set& expand(perturbation_set);
  double score(perturbation_set&);
  bool valid(perturbation_set&);

  // generation of perturbation sets
  void generatePerturbationSets(int, vector<int>&);
  sorted_distance_functions& makeSortedDistFuns(vector<int> &);
};

/* NOTES:

Reference:
Qin Lv, William Josephson, Zhe Wang, Moses Charikar and Kai Li,
"Multi-Probe LSH: Efficient Indexing for High-Dimensional Similarity
Search", Proc. Intl. Conf. VLDB, 2007

  i = 1..M (number of hash functions used)
  f_i(q) = a_i * q + b_i // projection to real line
  h_i(q) = floor( ( a_i * q + b_i ) / w ) // hash slot  
  delta \in {-1, +1}
  x_i( delta ) = distance of query to left or right boundary
  Delta = [delta_1 delta_2 ... delta_M]
  score(Delta) = sum_{i=1}_M x_i(delta_i).^2
  
  z = sort(x(delta_i), increasing) // i = 1..2M delta={+1,-1}
  p_j = (i, delta) if z_j == x_i(delta) 
  
  A_k is index into p_j

  Multi-probe algorithm (after Lv et al. 2007)
  ---------------------------------------------

  A0 = {1}
  minHeap.insert(A0, score(A0))
  
  for i = 1 to T do   // T = number of perturbation sets
     repeat
        Ai = minHeap.extractMin()
	As = shift(Ai)
	minHeap.insert(As, score(As))
	Ae = expand(Ai)
	minHeap.insert(Ae, score(Ae))
     until valid(Ai)
     output Ai
  end for

 */