Skip to contents

knn_graph() converts a data matrix, a fastEmbedR_embedding, or a precomputed KNN object into a native undirected edge graph. When neighbors must be computed, the function uses fastEmbedR's existing native KNN route; it does not expose a second nearest-neighbor tuning API.

Usage

knn_graph(
  x,
  k = 30L,
  backend = NULL,
  metric = c("euclidean", "cosine", "correlation"),
  weight = c("snn", "distance", "binary"),
  mutual = FALSE,
  prune = 0,
  n.cores = NULL
)

Arguments

x

Numeric data matrix, a fastEmbedR_embedding, a list containing indices and distances, or an existing fastEmbedR_graph.

k

Number of non-self neighbors retained.

backend

KNN-search backend used only when x is data: "cpu", "cuda", or "metal".

metric

Distance metric used only when KNN must be computed: "euclidean", "cosine", or "correlation".

weight

Edge weighting: shared-neighbor Jaccard ("snn"), inverse distance ("distance"), or unit weights ("binary").

mutual

Keep only reciprocal KNN edges.

prune

Remove edges whose final weight is less than or equal to this non-negative threshold.

n.cores

CPU cores used by KNN search and graph construction.

Value

A compact fastEmbedR_graph list with from, to, weight, n_vertices, and construction metadata.

References

Blondel VD, Guillaume JL, Lambiotte R, Lefebvre E. Fast unfolding of communities in large networks. J Stat Mech. 2008;2008:P10008.

Examples

x <- scale(as.matrix(iris[, 1:4]))
graph <- knn_graph(x, k = 15, weight = "snn")
graph
#> fastEmbedR KNN graph
#>   vertices: 150
#>   edges: 1432
#>   weights: snn