Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2bc2f33

Browse files
committed
Fill in README example
1 parent 7bd49db commit 2bc2f33

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,19 @@ operations:
2323
Usage:
2424

2525
```go
26-
hnsw.Graph
26+
g := hnsw.NewGraph[hnsw.Vector]()
27+
g.Add(
28+
hnsw.MakeVector("1", []float32{1, 1, 1}),
29+
hnsw.MakeVector("2", []float32{1, -1, 0.999}),
30+
hnsw.MakeVector("3", []float32{1, 0, -0.5}),
31+
)
32+
33+
neighbors := g.Search(
34+
[]float32{0.5, 0.5, 0.5},
35+
1,
36+
)
37+
fmt.Printf("best friend: %v\n", neighbors[0].Embedding())
38+
// Output: best friend: [1 1 1]
2739
```
2840

2941
## Performance

example/readme/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ func main() {
1010
g := hnsw.NewGraph[hnsw.Vector]()
1111
g.Add(
1212
hnsw.MakeVector("1", []float32{1, 1, 1}),
13-
hnsw.MakeVector("2", []float32{1, 0.999, 0.999}),
13+
hnsw.MakeVector("2", []float32{1, -1, 0.999}),
1414
hnsw.MakeVector("3", []float32{1, 0, -0.5}),
1515
)
1616

1717
neighbors := g.Search(
18-
[]float32{1, 1, 1},
18+
[]float32{0.5, 0.5, 0.5},
1919
1,
2020
)
21-
fmt.Printf("Neighbors: %v\n", neighbors)
21+
fmt.Printf("best friend: %v\n", neighbors[0].Embedding())
2222
}

graph.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ func (g *Graph[T]) Add(nodes ...T) {
344344
}
345345
}
346346

347+
// Search finds the k nearest neighbors to the target node.
347348
func (h *Graph[T]) Search(near Embedding, k int) []T {
348349
if len(near) != h.dims {
349350
panic(fmt.Sprint("embedding dimension mismatch: ", len(near), " != ", h.dims))

0 commit comments

Comments
 (0)