apaGraph.kNN.Build(Distances, OutputType, DoAddUnconnectedOnly)
=apaGraph.kNN.Build(Distances, OutputType, DoAddUnconnectedOnly)Result
Returns the edge list of the k-nearest-neighbor (kNN) graph at the smallest k at which the graph is connected, or that k. Each row of the edge list contains node i, node j (i < j) and the distance d(i, j).
Syntax
Distances: n x n distance matrix
OutputType: optional; 0 to return the edge list, 1 to return the k at which the graph first becomes connected; default: 0
DoAddUnconnectedOnly: optional; TRUE: from round k = 2 on, the k-th nearest neighbor of a node is only linked if the two nodes are not yet connected through a path; FALSE: all kNN edges; default: FALSE
Algorithm
The nearest neighbors of each node are sorted once by distance; ties are broken by the lower node index. Starting at k = 1, each round adds the k-th nearest neighbor of every node as an undirected edge, i.e. nodes i and j are linked if j is among the k nearest neighbors of i or i is among the k nearest neighbors of j. With DoAddUnconnectedOnly = TRUE, an edge from round k = 2 on is only added if its two nodes were not connected through a path after round k - 1; edges within an existing component are skipped. The connected components are tracked with a union-find structure, and the procedure stops at the first k for which a single component remains.
Remarks
Connected means that every node can reach every other node through a path, not that every pair of nodes is linked directly. The graph is always connected at k = n - 1. For n = 1, the k returned is 0 and the edge list is a row of #N/A. The edge list at the returned k equals the result of apaGraph.kNN.Build.K with that k and the same DoAddUnconnectedOnly. DoAddUnconnectedOnly does not change the returned k, only the number of edges.
Pressing Esc aborts the calculation; the function then returns "Calculation aborted by user."
