|
| 1 | +//! Reduction from HamiltonianCircuit to TravelingSalesman. |
| 2 | +//! |
| 3 | +//! The standard construction embeds the source graph into the complete graph on the |
| 4 | +//! same vertex set, assigning weight 1 to source edges and weight 2 to non-edges. |
| 5 | +//! The target optimum is exactly n iff the source graph contains a Hamiltonian circuit. |
| 6 | +
|
| 7 | +use crate::models::graph::{HamiltonianCircuit, TravelingSalesman}; |
| 8 | +use crate::reduction; |
| 9 | +use crate::rules::traits::{ReduceTo, ReductionResult}; |
| 10 | +use crate::topology::{Graph, SimpleGraph}; |
| 11 | + |
| 12 | +/// Result of reducing HamiltonianCircuit to TravelingSalesman. |
| 13 | +#[derive(Debug, Clone)] |
| 14 | +pub struct ReductionHamiltonianCircuitToTravelingSalesman { |
| 15 | + target: TravelingSalesman<SimpleGraph, i32>, |
| 16 | +} |
| 17 | + |
| 18 | +impl ReductionResult for ReductionHamiltonianCircuitToTravelingSalesman { |
| 19 | + type Source = HamiltonianCircuit<SimpleGraph>; |
| 20 | + type Target = TravelingSalesman<SimpleGraph, i32>; |
| 21 | + |
| 22 | + fn target_problem(&self) -> &Self::Target { |
| 23 | + &self.target |
| 24 | + } |
| 25 | + |
| 26 | + fn extract_solution(&self, target_solution: &[usize]) -> Vec<usize> { |
| 27 | + let graph = self.target.graph(); |
| 28 | + let n = graph.num_vertices(); |
| 29 | + if n == 0 { |
| 30 | + return vec![]; |
| 31 | + } |
| 32 | + |
| 33 | + let edges = graph.edges(); |
| 34 | + if target_solution.len() != edges.len() { |
| 35 | + return vec![0; n]; |
| 36 | + } |
| 37 | + |
| 38 | + let mut adjacency = vec![Vec::new(); n]; |
| 39 | + let mut selected_count = 0usize; |
| 40 | + for (idx, &selected) in target_solution.iter().enumerate() { |
| 41 | + if selected != 1 { |
| 42 | + continue; |
| 43 | + } |
| 44 | + let (u, v) = edges[idx]; |
| 45 | + adjacency[u].push(v); |
| 46 | + adjacency[v].push(u); |
| 47 | + selected_count += 1; |
| 48 | + } |
| 49 | + |
| 50 | + if selected_count != n || adjacency.iter().any(|neighbors| neighbors.len() != 2) { |
| 51 | + return vec![0; n]; |
| 52 | + } |
| 53 | + |
| 54 | + for neighbors in &mut adjacency { |
| 55 | + neighbors.sort_unstable(); |
| 56 | + } |
| 57 | + |
| 58 | + let mut order = Vec::with_capacity(n); |
| 59 | + let mut prev = None; |
| 60 | + let mut current = 0usize; |
| 61 | + |
| 62 | + for _ in 0..n { |
| 63 | + order.push(current); |
| 64 | + let neighbors = &adjacency[current]; |
| 65 | + let next = match prev { |
| 66 | + Some(previous) => { |
| 67 | + if neighbors[0] == previous { |
| 68 | + neighbors[1] |
| 69 | + } else { |
| 70 | + neighbors[0] |
| 71 | + } |
| 72 | + } |
| 73 | + None => neighbors[0], |
| 74 | + }; |
| 75 | + prev = Some(current); |
| 76 | + current = next; |
| 77 | + } |
| 78 | + |
| 79 | + order |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +#[reduction( |
| 84 | + overhead = { |
| 85 | + num_vertices = "num_vertices", |
| 86 | + num_edges = "num_vertices * (num_vertices - 1) / 2", |
| 87 | + } |
| 88 | +)] |
| 89 | +impl ReduceTo<TravelingSalesman<SimpleGraph, i32>> for HamiltonianCircuit<SimpleGraph> { |
| 90 | + type Result = ReductionHamiltonianCircuitToTravelingSalesman; |
| 91 | + |
| 92 | + fn reduce_to(&self) -> Self::Result { |
| 93 | + let num_vertices = self.num_vertices(); |
| 94 | + let target_graph = SimpleGraph::complete(num_vertices); |
| 95 | + let weights = target_graph |
| 96 | + .edges() |
| 97 | + .into_iter() |
| 98 | + .map(|(u, v)| if self.graph().has_edge(u, v) { 1 } else { 2 }) |
| 99 | + .collect(); |
| 100 | + let target = TravelingSalesman::new(target_graph, weights); |
| 101 | + |
| 102 | + ReductionHamiltonianCircuitToTravelingSalesman { target } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +#[cfg(feature = "example-db")] |
| 107 | +pub(crate) fn canonical_rule_example_specs() -> Vec<crate::example_db::specs::RuleExampleSpec> { |
| 108 | + use crate::export::SolutionPair; |
| 109 | + |
| 110 | + vec![crate::example_db::specs::RuleExampleSpec { |
| 111 | + id: "hamiltoniancircuit_to_travelingsalesman", |
| 112 | + build: || { |
| 113 | + let source = HamiltonianCircuit::new(SimpleGraph::cycle(4)); |
| 114 | + crate::example_db::specs::rule_example_with_witness::< |
| 115 | + _, |
| 116 | + TravelingSalesman<SimpleGraph, i32>, |
| 117 | + >( |
| 118 | + source, |
| 119 | + SolutionPair { |
| 120 | + source_config: vec![0, 1, 2, 3], |
| 121 | + target_config: vec![1, 0, 1, 1, 0, 1], |
| 122 | + }, |
| 123 | + ) |
| 124 | + }, |
| 125 | + }] |
| 126 | +} |
| 127 | + |
| 128 | +#[cfg(test)] |
| 129 | +#[path = "../unit_tests/rules/hamiltoniancircuit_travelingsalesman.rs"] |
| 130 | +mod tests; |
0 commit comments