|
| 1 | +//! Hamiltonian Path problem implementation. |
| 2 | +//! |
| 3 | +//! The Hamiltonian Path problem asks whether a graph contains a simple path |
| 4 | +//! that visits every vertex exactly once. |
| 5 | +
|
| 6 | +use crate::registry::{FieldInfo, ProblemSchemaEntry}; |
| 7 | +use crate::topology::{Graph, SimpleGraph}; |
| 8 | +use crate::traits::{Problem, SatisfactionProblem}; |
| 9 | +use crate::variant::VariantParam; |
| 10 | +use serde::{Deserialize, Serialize}; |
| 11 | + |
| 12 | +inventory::submit! { |
| 13 | + ProblemSchemaEntry { |
| 14 | + name: "HamiltonianPath", |
| 15 | + module_path: module_path!(), |
| 16 | + description: "Find a Hamiltonian path in a graph", |
| 17 | + fields: &[ |
| 18 | + FieldInfo { name: "graph", type_name: "G", description: "The underlying graph G=(V,E)" }, |
| 19 | + ], |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +/// The Hamiltonian Path problem. |
| 24 | +/// |
| 25 | +/// Given a graph G = (V, E), determine whether G contains a Hamiltonian path, |
| 26 | +/// i.e., a simple path that visits every vertex exactly once. |
| 27 | +/// |
| 28 | +/// # Representation |
| 29 | +/// |
| 30 | +/// A configuration is a sequence of `n` vertex indices representing a vertex |
| 31 | +/// ordering (permutation). Each position `i` in the configuration holds the |
| 32 | +/// vertex visited at step `i`. A valid solution must be a permutation of |
| 33 | +/// `0..n` where consecutive entries are adjacent in the graph. |
| 34 | +/// |
| 35 | +/// The search space has `dims() = [n; n]` (each position can hold any of `n` |
| 36 | +/// vertices), so brute-force enumerates `n^n` configurations. Only `n!` |
| 37 | +/// permutations can satisfy the constraints, but the encoding avoids complex |
| 38 | +/// variable-domain schemes and matches the problem's natural formulation. |
| 39 | +/// |
| 40 | +/// # Type Parameters |
| 41 | +/// |
| 42 | +/// * `G` - Graph type (e.g., SimpleGraph) |
| 43 | +/// |
| 44 | +/// # Example |
| 45 | +/// |
| 46 | +/// ``` |
| 47 | +/// use problemreductions::models::graph::HamiltonianPath; |
| 48 | +/// use problemreductions::topology::SimpleGraph; |
| 49 | +/// use problemreductions::{Problem, Solver, BruteForce}; |
| 50 | +/// |
| 51 | +/// // Path graph: 0-1-2-3 |
| 52 | +/// let graph = SimpleGraph::new(4, vec![(0, 1), (1, 2), (2, 3)]); |
| 53 | +/// let problem = HamiltonianPath::new(graph); |
| 54 | +/// |
| 55 | +/// let solver = BruteForce::new(); |
| 56 | +/// let solution = solver.find_satisfying(&problem); |
| 57 | +/// assert!(solution.is_some()); |
| 58 | +/// ``` |
| 59 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 60 | +#[serde(bound(deserialize = "G: serde::Deserialize<'de>"))] |
| 61 | +pub struct HamiltonianPath<G> { |
| 62 | + graph: G, |
| 63 | +} |
| 64 | + |
| 65 | +impl<G: Graph> HamiltonianPath<G> { |
| 66 | + /// Create a new Hamiltonian Path problem from a graph. |
| 67 | + pub fn new(graph: G) -> Self { |
| 68 | + Self { graph } |
| 69 | + } |
| 70 | + |
| 71 | + /// Get a reference to the underlying graph. |
| 72 | + pub fn graph(&self) -> &G { |
| 73 | + &self.graph |
| 74 | + } |
| 75 | + |
| 76 | + /// Get the number of vertices in the underlying graph. |
| 77 | + pub fn num_vertices(&self) -> usize { |
| 78 | + self.graph.num_vertices() |
| 79 | + } |
| 80 | + |
| 81 | + /// Get the number of edges in the underlying graph. |
| 82 | + pub fn num_edges(&self) -> usize { |
| 83 | + self.graph.num_edges() |
| 84 | + } |
| 85 | + |
| 86 | + /// Check if a configuration is a valid Hamiltonian path. |
| 87 | + pub fn is_valid_solution(&self, config: &[usize]) -> bool { |
| 88 | + is_valid_hamiltonian_path(&self.graph, config) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +impl<G> Problem for HamiltonianPath<G> |
| 93 | +where |
| 94 | + G: Graph + VariantParam, |
| 95 | +{ |
| 96 | + const NAME: &'static str = "HamiltonianPath"; |
| 97 | + type Metric = bool; |
| 98 | + |
| 99 | + fn variant() -> Vec<(&'static str, &'static str)> { |
| 100 | + crate::variant_params![G] |
| 101 | + } |
| 102 | + |
| 103 | + fn dims(&self) -> Vec<usize> { |
| 104 | + let n = self.graph.num_vertices(); |
| 105 | + vec![n; n] |
| 106 | + } |
| 107 | + |
| 108 | + fn evaluate(&self, config: &[usize]) -> bool { |
| 109 | + is_valid_hamiltonian_path(&self.graph, config) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +impl<G: Graph + VariantParam> SatisfactionProblem for HamiltonianPath<G> {} |
| 114 | + |
| 115 | +/// Check if a configuration represents a valid Hamiltonian path in the graph. |
| 116 | +/// |
| 117 | +/// A valid Hamiltonian path is a permutation of the vertices such that |
| 118 | +/// consecutive vertices in the permutation are adjacent in the graph. |
| 119 | +pub(crate) fn is_valid_hamiltonian_path<G: Graph>(graph: &G, config: &[usize]) -> bool { |
| 120 | + let n = graph.num_vertices(); |
| 121 | + if config.len() != n { |
| 122 | + return false; |
| 123 | + } |
| 124 | + |
| 125 | + // Check that config is a valid permutation of 0..n |
| 126 | + let mut seen = vec![false; n]; |
| 127 | + for &v in config { |
| 128 | + if v >= n || seen[v] { |
| 129 | + return false; |
| 130 | + } |
| 131 | + seen[v] = true; |
| 132 | + } |
| 133 | + |
| 134 | + // Check consecutive vertices are adjacent |
| 135 | + for i in 0..n.saturating_sub(1) { |
| 136 | + if !graph.has_edge(config[i], config[i + 1]) { |
| 137 | + return false; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + true |
| 142 | +} |
| 143 | + |
| 144 | +// Use Bjorklund (2014) O*(1.657^n) as best known for general undirected graphs |
| 145 | +crate::declare_variants! { |
| 146 | + HamiltonianPath<SimpleGraph> => "1.657^num_vertices", |
| 147 | +} |
| 148 | + |
| 149 | +#[cfg(test)] |
| 150 | +#[path = "../../unit_tests/models/graph/hamiltonian_path.rs"] |
| 151 | +mod tests; |
0 commit comments