Learn R Programming

cccd (version 1.6)

nng: Nearest Neighbor Graphs

Description

nearest neighbor, k-nearest neighbor, and mutual k-nearest neighbor (di)graphs.

Usage

nng(x = NULL, dx = NULL, k = 1, mutual = FALSE, method = NULL, 
    use.fnn = FALSE, algorithm = 'cover_tree')

Arguments

x

a data matrix. Either x or dx is required

dx

interpoint distance matrix

k

number of neighbors

mutual

logical. if true the neighbors must be mutual. See details.

method

the method used for the distance. See dist

use.fnn

logical. If TRUE, get.knn from the FNN package is used to obtain the neighbors.

algorithm

see get.knn.

Value

an object of class igraph with the extra attributes

layout

the x vectors.

k,mutual,p

arguments given to nn.

Details

a k-nearest neighbor graph is a digraph where each vertex is associated with an observation and there is a directed edge between the vertex and it's k nearest neighbors. A mutual k-nearest neighbor graph is a graph where there is an edge between x and y if x is one of the k nearest neighbors of y AND y is one of the k nearest neighbors of x.

References

D.J. Marchette, Random Graphs for Statistical Pattern Recognition, John Wiley & Sons, 2004.

See Also

dist get.knn

Examples

Run this code
# NOT RUN {
x <- matrix(runif(100),ncol=2)

G1 <- nng(x,k=1)
# }
# NOT RUN {
par(mfrow=c(2,2))
plot(G1)
# }
# NOT RUN {
G2 <- nng(x,k=2)
# }
# NOT RUN {
plot(G2)
# }
# NOT RUN {
G5 <- nng(x,k=5)
# }
# NOT RUN {
plot(G5)
# }
# NOT RUN {
G5m <- nng(x,k=5,mutual=TRUE)
# }
# NOT RUN {
plot(G5m)
par(mfrow=c(1,1))
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab