Nearest Neighbor algorithm for the Travelling Salesman Problem
The 2-column vector contains cities' coordinates. The algorithm generates the optimal path to visit all the cities exactly once, and return to the starting city.
Do for all the cities:
1. select a city as current city.
2. find out the shortest edge connecting the current city and an unvisited city.
3. set the new city as current city.
4. mark the previous current city as visited.
5. if all the cities are visited, then terminate.
6. Go to step 2.
The algorithm has its limitations, and based on the cities' distribution on the map it may or may not give the optimal solution.
It resolves the problem of the equally distant cities, that some other submitted files had.
Cite As
ajevtic (2024). Nearest Neighbor algorithm for the Travelling Salesman Problem (https://www.mathworks.com/matlabcentral/fileexchange/25542-nearest-neighbor-algorithm-for-the-travelling-salesman-problem), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Platform Compatibility
Windows macOS LinuxCategories
- AI, Data Science, and Statistics > Statistics and Machine Learning Toolbox > Cluster Analysis and Anomaly Detection > Nearest Neighbors >
- MATLAB > Mathematics > Graph and Network Algorithms > Shortest Path > Traveling Salesman (TSP) >
- Mathematics and Optimization > Optimization Toolbox > Linear Programming and Mixed-Integer Linear Programming > Problem-Based Linear Programming >
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.