https://github.com/rust-cv/hnsw
HNSW ANN from the paper "Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs"
https://github.com/rust-cv/hnsw
Last synced: 2 months ago
JSON representation
HNSW ANN from the paper "Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs"
- Host: GitHub
- URL: https://github.com/rust-cv/hnsw
- Owner: rust-cv
- License: mit
- Created: 2019-07-21T03:52:31.000Z (almost 7 years ago)
- Default Branch: main
- Last Pushed: 2025-08-08T18:26:45.000Z (10 months ago)
- Last Synced: 2025-12-14T03:12:01.052Z (6 months ago)
- Language: Rust
- Homepage:
- Size: 160 KB
- Stars: 249
- Watchers: 4
- Forks: 21
- Open Issues: 20
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-vector-databases - HNSW (Rust) - A Rust implementation of the HNSW (Hierarchical Navigable Small World) approximate nearest neighbor search algorithm, useful for building high-performance, memory-safe vector search components in Rust-based AI and retrieval systems. ([Read more](/details/hnsw-rust.md)) `ANN` `Rust` `vector search` (Sdks & Libraries)
- awesome-rust-ai-libraries - hnsw - hnsw implements HNSW ANN for efficient approximate nearest neighbor search. Dead project. Score 14, 220 stars. ([Read more](/details/hnsw.md)) `Hnsw` `Ann` `Open Source` (Search)
README
# hnsw
[![Discord][dci]][dcl] [![Crates.io][ci]][cl] ![MIT/Apache][li] [![docs.rs][di]][dl] ![LoC][lo]
[ci]: https://img.shields.io/crates/v/hnsw.svg
[cl]: https://crates.io/crates/hnsw/
[li]: https://img.shields.io/crates/l/specs.svg?maxAge=2592000
[di]: https://docs.rs/hnsw/badge.svg
[dl]: https://docs.rs/hnsw/
[lo]: https://tokei.rs/b1/github/rust-cv/hnsw?category=code
[dci]: https://img.shields.io/discord/550706294311485440.svg?logo=discord&colorB=7289DA
[dcl]: https://discord.gg/d32jaam
Hierarchical Navigable Small World Graph for fast ANN search
Enable the `serde` feature to serialize and deserialize `HNSW`.
## Tips
A good default for M and M0 parameters is 12 and 24 respectively. According to the paper, M0 should always be double M,
but you can change both of them freely.
## Example
To see how this might be used with hamming space, see `tests/simple_discrete.rs`. To see how this might be used with euclidean space, see `tests/simple.rs`.
Note that the euclidean implementation in the test may have some numerical errors and fail to implement the triangle inequality, especially on high dimensionality. Use a [Kahan sum](https://en.wikipedia.org/wiki/Kahan_summation_algorithm) instead for proper usage. It also may not utilize SIMD, but using an array may help with that.
Please refer to the [`space` documentation](https://docs.rs/space/) for the trait and types regarding distance. It also contains special `Bits128` - `Bits4096` tuple structs that wrap an array of bytes and enable SIMD capability. Benchmarks provided use these SIMD impls.
## Benchmarks
Here is a recall graph that you can [compare to its alternatives](http://ann-benchmarks.com/sift-256-hamming_10_hamming.html):

For more benchmarks and how to benchmark, see [`benchmarks.md`](./benchmarks.md).
## Implementation
This is based on the paper ["Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs"](https://arxiv.org/pdf/1603.09320.pdf) by Yu. A. Malkov and D. A. Yashunin. This paper builds on the [original paper for NSW](http://www.iiis.org/CDs2011/CD2011IDI/ICTA_2011/PapersPdf/CT175ON.pdf). There are multiple papers written by the authors on NSW, which preceeded HNSW.
For more details about parameters and details of the implementation, see [`implementation.md`](./implementation.md).
## Credit
This is in no way a direct copy or reimplementation of [the original implementation](https://github.com/nmslib/hnswlib/blob/master/hnswlib/hnswalg.h). This was made purely based on [the paper](https://arxiv.org/pdf/1603.09320.pdf) without reference to the original headers. The paper is very well written and easy to understand, with some minor exceptions. Thank you to the authors for your valuable contribution.
## Questions? Contributions? Excited?
Please visit the [Rust CV Discord](https://discord.gg/d32jaam).