Learn R Programming

cccd (version 1.00.05)

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, p = 2)

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.
p
the value of p in the $L_p$ distance.

Value

  • an object of class igraph with the extra attributes
  • layoutthe x vectors.
  • k,mutual,parguments 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.

Examples

Run this code
x <- matrix(runif(100),ncol=2)

G1 <- nng(x,k=1)
par(mfrow=c(2,2))
plot(G1)

G2 <- nng(x,k=2)
plot(G2)

G5 <- nng(x,k=5)
plot(G5)

G5m <- nng(x,k=5,mutual=TRUE)
plot(G5m)

Run the code above in your browser using DataLab