forked from coder/hnsw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyzer.go
42 lines (35 loc) · 1.05 KB
/
analyzer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package hnsw
// Analyzer is a struct that holds a graph and provides
// methods for analyzing it. It offers no compatibility guarantee
// as the methods of measuring the graph's health with change
// with the implementation.
type Analyzer[T Embeddable] struct {
Graph *Graph[T]
}
func (a *Analyzer[T]) Height() int {
return len(a.Graph.layers)
}
// Connectivity returns the average number of edges in the
// graph for each non-empty layer.
func (a *Analyzer[T]) Connectivity() []float64 {
var layerConnectivity []float64
for _, layer := range a.Graph.layers {
if len(layer.nodes) == 0 {
continue
}
var sum float64
for _, node := range layer.nodes {
sum += float64(len(node.neighbors))
}
layerConnectivity = append(layerConnectivity, sum/float64(len(layer.nodes)))
}
return layerConnectivity
}
// Topography returns the number of nodes in each layer of the graph.
func (a *Analyzer[T]) Topography() []int {
var topography []int
for _, layer := range a.Graph.layers {
topography = append(topography, len(layer.nodes))
}
return topography
}