Skip to content

Commit

Permalink
added reverse
Browse files Browse the repository at this point in the history
  • Loading branch information
aaalgo committed Jun 16, 2016
1 parent 22d70ab commit 35cf123
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
48 changes: 48 additions & 0 deletions kgraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 () {
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down
8 changes: 7 additions & 1 deletion kgraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
}
};

Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.4
1.5

0 comments on commit 35cf123

Please sign in to comment.