Travelling Salesman
The travelling salesman problem asks for the shortest tour through all cities. It is easy to state, NP-hard to solve exactly, and a benchmark for heuristics.
The greedy nearest-neighbor tour is a starting point. Each step applies a 2-opt improvement: remove two edges, reverse the segment between them, and keep the change if the tour gets shorter. The result is a local optimum, not necessarily the true shortest tour.
What you are seeing
A tour is a closed loop through every city exactly once.
Nearest neighbor builds a quick greedy tour by always visiting the closest unvisited city.
2-opt improves a tour by reversing segments whenever it removes two crossing edges.
Why it matters
TSP appears in routing, logistics, manufacturing, and DNA sequencing. It is the canonical example of a problem that is easy to verify but hard to optimize exactly.
Complexity & details
Brute force: O(n!). Nearest neighbor: O(n²). 2-opt improvement: O(n²) per pass with no optimality guarantee.
Frequently asked questions
Is nearest neighbor optimal?+
No. It can be much worse than the optimal tour. It is fast and simple, often used as a starting point for local search.
What does 2-opt do?+
It removes two edges, reverses the segment between them, and keeps the change if the tour gets shorter. Repeating until no improvement gives a local optimum.