|
| template<typename Node , typename WeightFunc > |
| double | calculate_total_distance (const std::vector< Node > &path, WeightFunc &&weight) |
| | Compute the total length of a Hamiltonian path/cycle.
|
| |
| template<typename Node , typename WeightFunc > |
| auto | detail::prim_mst (size_t n, WeightFunc &&weight) -> std::vector< std::pair< Node, Node > > |
| | Prim's MST for dense (complete) graphs - O(n^2).
|
| |
| template<typename Node > |
| auto | detail::find_odd_degree_nodes (const std::vector< std::pair< Node, Node > > &mst_edges, size_t n) -> std::vector< Node > |
| | Collect vertices with odd degree in the MST.
|
| |
| template<typename Node , typename WeightFunc > |
| auto | detail::greedy_min_weight_matching (const std::vector< Node > &odd_nodes, WeightFunc &&weight) -> std::vector< std::pair< Node, Node > > |
| | Greedy minimum-weight perfect matching - O(k^2 log k).
|
| |
| template<typename Node > |
| auto | detail::build_multigraph (size_t n, const std::vector< std::pair< Node, Node > > &mst_edges, const std::vector< std::pair< Node, Node > > &matching_edges) -> std::vector< std::multiset< Node > > |
| | Build a multigraph adjacency list (MST + matching).
|
| |
| template<typename Node > |
| auto | detail::hierholzer (std::vector< std::multiset< Node > > adj, Node start) -> std::vector< std::pair< Node, Node > > |
| | Hierholzer's algorithm - find an Eulerian circuit.
|
| |
| template<typename Node > |
| auto | detail::shortcut_eulerian (const std::vector< std::pair< Node, Node > > &eulerian_circuit) -> std::vector< Node > |
| | Convert an Eulerian circuit to a Hamiltonian cycle by skipping repeated vertices (shortcut).
|
| |
| template<typename Graph , typename WeightFunc > |
| auto | christofides_tsp (const Graph &G, WeightFunc &&weight) -> std::vector< typename Graph::node_t > |
| | Solve Metric TSP using the Christofides approximation algorithm.
|
| |
| template<typename Graph , typename WeightFunc > |
| auto | two_opt (std::vector< typename Graph::node_t > path, const Graph &G, WeightFunc &&weight) -> std::vector< typename Graph::node_t > |
| | Refine a TSP tour using 2-opt neighbourhood search.
|
| |
| template<typename Graph , typename WeightFunc > |
| auto | solve_christofides_2opt_tsp (const Graph &G, WeightFunc &&weight) -> std::vector< typename Graph::node_t > |
| | Solve Metric TSP using Christofides followed by 2-Opt refinement.
|
| |
Traveling Salesman Problem (TSP) approximation algorithms.
Implements the Christofides algorithm (3/2-approximation for Metric TSP) and the 2-Opt local search heuristic for tour refinement.
All functions accept a callable weight(u, v) -> double for edge weights, making them agnostic to the underlying graph representation.
Algorithm summary (Christofides, 1976):
- Compute a Minimum Spanning Tree (MST).
- Find odd-degree vertices in the MST.
- Compute a Minimum Weight Perfect Matching on odd vertices.
- Superimpose MST and matching -> an Eulerian multigraph.
- Find an Eulerian circuit (Hierholzer).
- Shortcut repeated vertices -> Hamiltonian cycle.
Combining with 2-Opt refinement typically yields near-optimal tours for moderate-size metric instances.