Skip to contents

Constructs symmetric k-nearest neighbor graphs for several values of k while performing one bundled ANN search for observations, or one exact ranking of supplied distances, at the largest requested value. The ordered non-self neighbor matrix is sliced for each graph, so lower-k graphs use exactly the same neighbor ranking as the largest graph.

Usage

create.sknn.graphs(
  X,
  k.values,
  ...,
  input.type = c("observations", "distances")
)

Arguments

X

Numeric observation matrix/data frame, or a symmetric numeric distance matrix or dist object when input.type = "distances".

k.values

Strictly increasing integer vector of neighborhood sizes, each between 1 and n - 1.

...

Named arguments forwarded to create.sknn.graph(). The k and knn.index arguments cannot be supplied. neighbor.method must be "ann" for observations or "exact" for distances if supplied.

input.type

Input interpretation. Defaults to "observations" for backward compatibility; symmetry never triggers automatic detection.

Value

A list of class "sknn_graphs" with components graphs, a named list of "sknn_graph" objects, and k_statistics, a data frame of edge and component counts. Attributes record the requested values and number of vertices.

Examples

set.seed(1)
X <- matrix(rnorm(60), ncol = 3)
graphs <- create.sknn.graphs(
  X,
  k.values = c(2, 4),
  graph.detail = "minimal"
)
graphs$k_statistics
#>   idx k n_edges n_edges_before_pruning n_edges_after_pruning
#> 2   1 2      27                     27                    27
#> 4   2 4      55                     55                    55
#>   n_components_before n_components_after n_mst_edges_added
#> 2                   2                  2                 0
#> 4                   1                  1                 0