Given
List<Point> cities = /* ... */ ;
double distance(Point a, Point b) { /* ... */ };
is there a single LINQ query that returns the travelling salesman shortest route by nearest neighbour algorithm as a List<int> of the indices of cities?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I don’t think you can do everything in a single query, some parts of the algorithms will have to be implemented separately.
Here’s a brute-force implementation that examines all city permutations and returns the shortest path that visits all the cities:
The
Permutationsextension method is defined as follows:Of course there are much better approaches to solve this problem, but this one works… Most of it is in a single query, the only parts that are implemented separately are not specific to the problem at hand and can be reused for other tasks.
EDIT: oops, I just realized I also used the non-standard
MinBymethod; you can find it in the MoreLinq project