| create.sknn.graph | R Documentation |
Creates an undirected symmetric k-nearest neighbor (sKNN) graph from a numeric
data matrix. An edge between vertices i and j is present when either
j is among the k nearest neighbors of i, or i is among the k
nearest neighbors of j.
create.sknn.graph(
X,
k,
prune.edges = FALSE,
prune.method = c("none", "local.geodesic", "global.geodesic.ratio"),
max.path.edge.ratio.deviation.thld = 0.1,
path.edge.ratio.percentile = 0.5,
prune.tau = 1.05,
prune.local.k = NULL,
with.pruned.edge.stats = FALSE,
connect.components = FALSE,
connect.method = c("component.mst", "component.mst.ann", "global.mst"),
edge.weight = c("distance"),
neighbor.method = c("exact", "ann"),
ann.eps = 0,
bridge.k = NULL,
bridge.k.max = NULL,
bridge.growth = 2
)
X |
Numeric matrix or data frame with observations in rows. |
k |
Integer scalar. Number of non-self nearest neighbors. |
prune.edges |
Logical scalar. If |
prune.method |
Character scalar. |
max.path.edge.ratio.deviation.thld |
Numeric scalar in |
path.edge.ratio.percentile |
Numeric scalar in |
prune.tau |
Numeric scalar greater than 1. An edge may be pruned when the shortest retained local alternative path is at most this multiplicative factor times the direct edge length. |
prune.local.k |
Integer scalar or |
with.pruned.edge.stats |
Logical scalar. If |
connect.components |
Logical scalar. If |
connect.method |
Character scalar. |
edge.weight |
Character scalar. Currently only |
neighbor.method |
Character scalar. |
ann.eps |
Non-negative numeric scalar reserved for approximate ANN
search. Currently only |
bridge.k |
Integer scalar or |
bridge.k.max |
Integer scalar or |
bridge.growth |
Numeric scalar greater than 1. Multiplicative growth factor for bridge candidate neighborhoods. |
With no nearest-neighbor ties, the sKNN edge rule is equivalent to
d_{ij} \le \max(\sigma_i, \sigma_j),
where \sigma_i is the distance from point i to its k-th
non-self nearest neighbor.
If connect.components = TRUE, the graph is augmented with MST edges. The
default connect.method = "component.mst" first collapses each connected
component of the sKNN graph to one component vertex, weights each component
pair by the shortest data-space edge between them, and adds the minimum
spanning tree of this component graph. This adds exactly m - 1 bridge
edges when the original sKNN graph has m connected components.
connect.method = "component.mst.ann" uses ANN nearest-neighbor bridge
candidates first and automatically falls back to exact component MST when
the sparse candidate graph does not connect all components. The alternative
connect.method = "global.mst" unions the sKNN graph with the full
Euclidean MST on the data points.
neighbor.method = "exact" computes all pairwise distances and is the
deterministic reference implementation. neighbor.method = "ann" uses
the bundled ANN kd-tree for the Euclidean kNN search step; exact parity can
differ only in nearest-neighbor tie cases. Edge weights are always returned
as ordinary Euclidean distances. Exact component.mst repair uses exact
pairwise bridge distances; component.mst.ann uses ANN bridge candidates
before guaranteed exact fallback.
If prune.method = "local.geodesic" or prune.edges = TRUE, an
experimental local-geodesic pruning stage is applied after the sKNN support
is built and before optional MST connectivity repair. Candidate edges are
processed longest-first. For an edge (i, j), the edge is removed only when
the currently retained graph contains an alternative path from i to j,
restricted to the union of their local neighborhoods, whose length is at most
prune.tau times the direct edge length. prune.method = "global.geodesic.ratio" instead uses the whole graph as the alternative-path
search domain and removes long edges whose shortest alternative path is
within the configured geodesic-ratio threshold.
A list of class "sknn_graph" with adjacency lists, weights, edge
matrix, kNN index matrix, component diagnostics, and any MST edges added.
The graph lifecycle fields are:
The native sKNN graph before pruning and before optional MST component repair.
The graph after optional local geometric pruning and before optional MST component repair. If pruning is disabled, these fields are identical to the raw graph.
The native sKNN graph after MST component repair.
The prune-first branch after MST component repair.
The repair-first branch after applying local geometric pruning to the repaired raw graph.
The final graph after optional MST component repair. Downstream algorithms should use these fields unless they explicitly need a lifecycle diagnostic stage.
X <- rbind(c(0, 0), c(1, 0), c(10, 0), c(11, 0))
g <- create.sknn.graph(X, k = 1, connect.components = TRUE)
g$n_components_before
g$n_components_after
g$mst_edge_matrix
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.