Skip to content

web search done right

stop shortest path calculation when distance is too large

Mikkel Denkerdd40f362a703
diff --git a/core/Cargo.toml b/core/Cargo.toml
index 7bcac14f..3ed652fa 100644
--- a/core/Cargo.toml
+++ b/core/Cargo.toml
@@ -83,4 +83,8 @@ harness = false
[[bench]]
name = "harmonic-centrality"
+harness = false
+
+[[bench]]
+name = "approximate-harmonic"
harness = false
\ No newline at end of file
diff --git a/core/benches/approximate-harmonic.rs b/core/benches/approximate-harmonic.rs
new file mode 100644
index 00000000..b8a26353
--- /dev/null
+++ b/core/benches/approximate-harmonic.rs
@@ -0,0 +1,20 @@
+use criterion::{criterion_group, criterion_main, Criterion};
+use cuely::webgraph::{
+ centrality::approximate_harmonic::ApproximatedHarmonicCentrality, WebgraphBuilder,
+};
+
+const WEBGRAPH_PATH: &str = "data/webgraph";
+
+pub fn criterion_benchmark(c: &mut Criterion) {
+ let webgraph = WebgraphBuilder::new(WEBGRAPH_PATH).with_host_graph().open();
+ c.bench_function("Approximated harmonic centrality calculation", |b| {
+ b.iter(|| {
+ for _ in 0..10 {
+ ApproximatedHarmonicCentrality::new(&webgraph);
+ }
+ })
+ });
+}
+
+criterion_group!(benches, criterion_benchmark);
+criterion_main!(benches);
diff --git a/core/src/webgraph/mod.rs b/core/src/webgraph/mod.rs
index 43544ff9..391a6ee1 100644
--- a/core/src/webgraph/mod.rs
+++ b/core/src/webgraph/mod.rs
@@ -221,6 +221,7 @@ impl<S: Store> Webgraph<S> {
F1: Fn(NodeID) -> Vec<Edge>,
F2: Fn(&Edge) -> NodeID,
{
+ const MAX_DIST: usize = 5;
let source_id = store.node2id(&source);
if source_id.is_none() {
return HashMap::new();
@@ -236,6 +237,11 @@ impl<S: Store> Webgraph<S> {
while let Some(state) = queue.pop() {
let (cost, v) = state.0;
+
+ if cost >= MAX_DIST {
+ continue;
+ }
+
let current_dist = distances.get(&v).unwrap_or(&usize::MAX);
if cost > *current_dist {
@@ -244,9 +250,15 @@ impl<S: Store> Webgraph<S> {
for edge in node_edges(v) {
if cost + 1 < *distances.get(&edge_node(&edge)).unwrap_or(&usize::MAX) {
- let next = cmp::Reverse((cost + 1, edge_node(&edge)));
+ let d = cost + 1;
+
+ if d > MAX_DIST {
+ continue;
+ }
+
+ let next = cmp::Reverse((d, edge_node(&edge)));
queue.push(next);
- distances.insert(edge_node(&edge), cost + 1);
+ distances.insert(edge_node(&edge), d);
}
}
}