## Regular example with obvious solution (minimum distance 48)
m <- 10 # Generate some points in the plane
z <- cbind(c(rep(0,m), rep(2,m), rep(5,m), rep(7,m)), rep(seq(0,m-1),4))
n <- nrow(z)
d <- compute_distance_matrix(z)
b <- build_tour_nn(d, n, 1)
b$distance # Distance 50
plot_tour(z,b)
b <- build_tour_nn(d, n, 5)
b$distance # Distance 52.38
plot_tour(z,b)
## Random points
set.seed(1)
n <- 25
z <- cbind(runif(n,min=1,max=10),runif(n,min=1,max=10))
d <- compute_distance_matrix(z)
b <- build_tour_nn(d, n, 1)
b$distance # Distance 46.4088
plot_tour(z,b)
b <- build_tour_nn(d, n, 9)
b$distance # Distance 36.7417
plot_tour(z,b)
Run the code above in your browser using DataLab