Skip to content

Commit

Permalink
fix: set an implicit root in external build if parents are not set (#147
Browse files Browse the repository at this point in the history
)

Signed-off-by: usamoi <[email protected]>
  • Loading branch information
usamoi authored Dec 23, 2024
1 parent 1c00a5e commit 6335852
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/vchordrq/algorithm/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,29 @@ impl Structure {
vectors.insert(id, crate::projection::project(vector.as_borrowed().slice()));
}
});
if parents.len() >= 2 && parents.values().all(|x| x.is_none()) {
// if there are more than one vertexs and no edges,
// assume there is an implicit root
let n = parents.len();
let mut result = Vec::new();
result.push(Structure {
means: vectors.values().cloned().collect::<Vec<_>>(),
children: vec![Vec::new(); n],
});
result.push(Structure {
means: vec![{
// compute the vector on root, without normalizing it
let mut sum = vec![0.0f32; vector_options.dims as _];
for vector in vectors.values() {
f32::vector_add_inplace(&mut sum, vector);
}
f32::vector_mul_scalar_inplace(&mut sum, 1.0 / n as f32);
sum
}],
children: vec![(0..n as u32).collect()],
});
return result;
}
let mut children = parents
.keys()
.map(|x| (*x, Vec::new()))
Expand Down

0 comments on commit 6335852

Please sign in to comment.