Class TravelingSalesman
java.lang.Object
topics.greedy.tsp.TravelingSalesman
Traveling Salesman
Implements the "Nearest Neighbor" greedy strategy to approximate the shortest Hamiltonian cycle in a weighted graph.
The Greedy Trap
This algorithm is extremely fast O(N²), but it is sub-optimal. By picking the closest city at every step, the salesman often finds himself trapped at the end of the tour with no choice but to take an extremely expensive edge to return to the starting city.
- Author:
- vicegd
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionsolve(int[][] weights, int startNode) Executes the Nearest Neighbor heuristic. * @param weights Adjacency matrix of the graph.
-
Constructor Details
-
TravelingSalesman
public TravelingSalesman()
-
-
Method Details
-
solve
Executes the Nearest Neighbor heuristic. * @param weights Adjacency matrix of the graph.- Parameters:
startNode- The starting city index.- Returns:
- A solution object containing the path and the total cost.
-