Question
programming in R Implement a greedy nearest-neighbour approach to find a suitable route for visiting all houses (pairs of coordinates generated) ( shortest route possible).
programming in R
Implement a greedy nearest-neighbour approach to find a suitable route for visiting all houses (pairs of coordinates generated) ( shortest route possible).
Starting at house 1, visit the house nearest to house 1 first. Then, each time move on to the nearest house not yet visited and, after visiting all houses, return to house 1.
Suppose house 1 is at point (0,1) .
Write a function called gp which will take the matrix of coordinates of the houses as argument and will return
the coordinates of the path found by the greedy algorithm.
( You can generate a set of coordinates of the houses using the function gen.h
gen.h=function(n=10) { while (TRUE) { r <- sqrt(runif(n))*0.9 phi <- runif(n) * 2 * pi houses <- cbind(x=r*sin(phi), y=r*cos(phi)) if (min(dist(houses))>0.15) return(houses) } }
houses <- gen.h ()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started