Permalink
Browse files

added reverse

  • Loading branch information...
aaalgo committed Jun 16, 2016
1 parent 22d70ab commit 35cf1235b26b0531259b273a7f46bfcf82bbbc04
Showing with 58 additions and 3 deletions.
  1. +1 −1 Makefile
  2. +1 −0 index.cpp
  3. +48 −0 kgraph.cpp
  4. +7 −1 kgraph.h
  5. +1 −1 version
@@ -14,7 +14,7 @@ NABO_LIBS+=-lnabo
COMMON=kgraph.o metric.o
HEADERS=kgraph.h kgraph-data.h
PROGS=resave index search prune split fvec2lshkit txt2lshkit
PROGS=resave index search prune split fvec2lshkit txt2lshkit reverse
EXTRA_PROGS=test
FLANN_PROGS=flann_index flann_search
NABO_PROGS=nabo_search
@@ -58,6 +58,7 @@ int main (int argc, char *argv[]) {
("delta", po::value(&params.delta)->default_value(default_delta), "")
("recall", po::value(&params.recall)->default_value(default_recall), "")
("prune", po::value(&params.prune)->default_value(default_prune), "")
("reverse", po::value(&params.reverse)->default_value(default_reverse), "")
("noise", po::value(&noise)->default_value(0), "noise")
("seed", po::value(&params.seed)->default_value(default_seed), "")
("dim,D", po::value(&D), "dimension, see format")
@@ -61,6 +61,10 @@ namespace kgraph {
return n1.dist < n2.dist;
}
static inline bool operator == (Neighbor const &n1, Neighbor const &n2) {
return n1.id == n2.id;
}
typedef vector<Neighbor> Neighbors;
// both pool and knn should be sorted in ascending order
@@ -241,6 +245,46 @@ namespace kgraph {
vector<vector<Neighbor>> graph;
bool no_dist; // Distance & flag information in Neighbor is not valid.
void reverse (int rev_k) {
if (rev_k == 0) return;
if (no_dist) throw runtime_error("Need distance information to reverse graph");
vector<vector<Neighbor>> ng; // new graph adds on original one
{
cerr << "Graph completion with reverse edges..." << endl;
ng = graph;
progress_display progress(graph.size(), cerr);
for (unsigned i = 0; i < graph.size(); ++i) {
auto const &v = graph[i];
unsigned K = M[i];
if (rev_k > 0) {
K = rev_k;
if (K > v.size()) K = v.size();
}
//if (v.size() < XX) XX = v.size();
for (unsigned j = 0; j < K; ++j) {
auto const &e = v[j];
auto re = e;
re.id = i;
ng[e.id].push_back(re);
}
++progress;
}
graph.swap(ng);
}
{
cerr << "Reranking edges..." << endl;
progress_display progress(graph.size(), cerr);
#pragma omp parallel for
for (unsigned i = 0; i < graph.size(); ++i) {
auto &v = graph[i];
std::sort(v.begin(), v.end());
v.resize(std::unique(v.begin(), v.end()) - v.begin());
M[i] = v.size();
#pragma omp critical
++progress;
}
}
}
public:
virtual ~KGraphImpl () {
}
@@ -523,6 +567,7 @@ namespace kgraph {
}
}
void prune2 () {
vector<vector<unsigned>> reverse(graph.size()); // reverse of new graph
vector<unsigned> new_L(graph.size(), 0);
@@ -875,6 +920,9 @@ namespace kgraph {
knn[k].dist = pool[k].dist;
}
}
if (params.reverse) {
reverse(params.reverse);
}
if (params.prune) {
prune(o, params.prune);
}
@@ -27,7 +27,12 @@ namespace kgraph {
PRUNE_LEVEL_1 = 1,
PRUNE_LEVEL_2 = 2
};
enum {
REVERSE_AUTO = -1,
REVERSE_NONE = 0,
};
static unsigned const default_prune = 0;
static int const default_reverse = REVERSE_NONE;
/// Verbosity control
/** Set verbosity = 0 to disable information output to stderr.
@@ -95,9 +100,10 @@ namespace kgraph {
float delta;
float recall;
unsigned prune;
int reverse;
/// Construct with default values.
IndexParams (): iterations(default_iterations), L(default_L), K(default_K), S(default_S), R(default_R), controls(default_controls), seed(default_seed), delta(default_delta), recall(default_recall), prune(default_prune) {
IndexParams (): iterations(default_iterations), L(default_L), K(default_K), S(default_S), R(default_R), controls(default_controls), seed(default_seed), delta(default_delta), recall(default_recall), prune(default_prune), reverse(default_reverse) {
}
};
@@ -1 +1 @@
1.4
1.5

0 comments on commit 35cf123

Please sign in to comment.