|
| 1 | +//! Optimal Linear Arrangement problem implementation. |
| 2 | +//! |
| 3 | +//! The Optimal Linear Arrangement problem asks whether there exists a one-to-one |
| 4 | +//! function f: V -> {0, 1, ..., |V|-1} such that the total edge length |
| 5 | +//! sum_{{u,v} in E} |f(u) - f(v)| is at most K. |
| 6 | +
|
| 7 | +use crate::registry::{FieldInfo, ProblemSchemaEntry}; |
| 8 | +use crate::topology::{Graph, SimpleGraph}; |
| 9 | +use crate::traits::{Problem, SatisfactionProblem}; |
| 10 | +use serde::{Deserialize, Serialize}; |
| 11 | + |
| 12 | +inventory::submit! { |
| 13 | + ProblemSchemaEntry { |
| 14 | + name: "OptimalLinearArrangement", |
| 15 | + module_path: module_path!(), |
| 16 | + description: "Find a vertex ordering on a line with total edge length at most K", |
| 17 | + fields: &[ |
| 18 | + FieldInfo { name: "graph", type_name: "G", description: "The undirected graph G=(V,E)" }, |
| 19 | + FieldInfo { name: "bound", type_name: "usize", description: "Upper bound K on total edge length" }, |
| 20 | + ], |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +/// The Optimal Linear Arrangement problem. |
| 25 | +/// |
| 26 | +/// Given an undirected graph G = (V, E) and a non-negative integer K, |
| 27 | +/// determine whether there exists a one-to-one function f: V -> {0, 1, ..., |V|-1} |
| 28 | +/// such that sum_{{u,v} in E} |f(u) - f(v)| <= K. |
| 29 | +/// |
| 30 | +/// This is the decision (satisfaction) version of the problem, following the |
| 31 | +/// Garey & Johnson formulation (GT42). |
| 32 | +/// |
| 33 | +/// # Representation |
| 34 | +/// |
| 35 | +/// Each vertex is assigned a variable representing its position in the arrangement. |
| 36 | +/// Variable i takes a value in {0, 1, ..., n-1}, and a valid configuration must be |
| 37 | +/// a permutation (all positions are distinct) with total edge length at most K. |
| 38 | +/// |
| 39 | +/// # Type Parameters |
| 40 | +/// |
| 41 | +/// * `G` - The graph type (e.g., `SimpleGraph`) |
| 42 | +/// |
| 43 | +/// # Example |
| 44 | +/// |
| 45 | +/// ``` |
| 46 | +/// use problemreductions::models::graph::OptimalLinearArrangement; |
| 47 | +/// use problemreductions::topology::SimpleGraph; |
| 48 | +/// use problemreductions::{Problem, Solver, BruteForce}; |
| 49 | +/// |
| 50 | +/// // Path graph: 0-1-2-3 with bound 3 |
| 51 | +/// let graph = SimpleGraph::new(4, vec![(0, 1), (1, 2), (2, 3)]); |
| 52 | +/// let problem = OptimalLinearArrangement::new(graph, 3); |
| 53 | +/// |
| 54 | +/// let solver = BruteForce::new(); |
| 55 | +/// let solution = solver.find_satisfying(&problem); |
| 56 | +/// assert!(solution.is_some()); |
| 57 | +/// ``` |
| 58 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 59 | +#[serde(bound(deserialize = "G: serde::Deserialize<'de>"))] |
| 60 | +pub struct OptimalLinearArrangement<G> { |
| 61 | + /// The underlying graph. |
| 62 | + graph: G, |
| 63 | + /// Upper bound K on total edge length. |
| 64 | + bound: usize, |
| 65 | +} |
| 66 | + |
| 67 | +impl<G: Graph> OptimalLinearArrangement<G> { |
| 68 | + /// Create a new Optimal Linear Arrangement problem. |
| 69 | + /// |
| 70 | + /// # Arguments |
| 71 | + /// * `graph` - The undirected graph G = (V, E) |
| 72 | + /// * `bound` - The upper bound K on total edge length |
| 73 | + pub fn new(graph: G, bound: usize) -> Self { |
| 74 | + Self { graph, bound } |
| 75 | + } |
| 76 | + |
| 77 | + /// Get a reference to the underlying graph. |
| 78 | + pub fn graph(&self) -> &G { |
| 79 | + &self.graph |
| 80 | + } |
| 81 | + |
| 82 | + /// Get the bound K. |
| 83 | + pub fn bound(&self) -> usize { |
| 84 | + self.bound |
| 85 | + } |
| 86 | + |
| 87 | + /// Get the number of vertices in the underlying graph. |
| 88 | + pub fn num_vertices(&self) -> usize { |
| 89 | + self.graph.num_vertices() |
| 90 | + } |
| 91 | + |
| 92 | + /// Get the number of edges in the underlying graph. |
| 93 | + pub fn num_edges(&self) -> usize { |
| 94 | + self.graph.num_edges() |
| 95 | + } |
| 96 | + |
| 97 | + /// Check if a configuration is a valid permutation with total edge length at most K. |
| 98 | + pub fn is_valid_solution(&self, config: &[usize]) -> bool { |
| 99 | + match self.total_edge_length(config) { |
| 100 | + Some(length) => length <= self.bound, |
| 101 | + None => false, |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /// Check if a configuration forms a valid permutation of {0, ..., n-1}. |
| 106 | + fn is_valid_permutation(&self, config: &[usize]) -> bool { |
| 107 | + let n = self.graph.num_vertices(); |
| 108 | + if config.len() != n { |
| 109 | + return false; |
| 110 | + } |
| 111 | + let mut seen = vec![false; n]; |
| 112 | + for &pos in config { |
| 113 | + if pos >= n || seen[pos] { |
| 114 | + return false; |
| 115 | + } |
| 116 | + seen[pos] = true; |
| 117 | + } |
| 118 | + true |
| 119 | + } |
| 120 | + |
| 121 | + /// Compute the total edge length for a given arrangement. |
| 122 | + /// |
| 123 | + /// Returns `None` if the configuration is not a valid permutation. |
| 124 | + pub fn total_edge_length(&self, config: &[usize]) -> Option<usize> { |
| 125 | + if !self.is_valid_permutation(config) { |
| 126 | + return None; |
| 127 | + } |
| 128 | + let mut total = 0usize; |
| 129 | + for (u, v) in self.graph.edges() { |
| 130 | + let fu = config[u]; |
| 131 | + let fv = config[v]; |
| 132 | + total += fu.abs_diff(fv); |
| 133 | + } |
| 134 | + Some(total) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +impl<G> Problem for OptimalLinearArrangement<G> |
| 139 | +where |
| 140 | + G: Graph + crate::variant::VariantParam, |
| 141 | +{ |
| 142 | + const NAME: &'static str = "OptimalLinearArrangement"; |
| 143 | + type Metric = bool; |
| 144 | + |
| 145 | + fn variant() -> Vec<(&'static str, &'static str)> { |
| 146 | + crate::variant_params![G] |
| 147 | + } |
| 148 | + |
| 149 | + fn dims(&self) -> Vec<usize> { |
| 150 | + let n = self.graph.num_vertices(); |
| 151 | + vec![n; n] |
| 152 | + } |
| 153 | + |
| 154 | + fn evaluate(&self, config: &[usize]) -> bool { |
| 155 | + self.is_valid_solution(config) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +impl<G: Graph + crate::variant::VariantParam> SatisfactionProblem for OptimalLinearArrangement<G> {} |
| 160 | + |
| 161 | +crate::declare_variants! { |
| 162 | + OptimalLinearArrangement<SimpleGraph> => "2^num_vertices", |
| 163 | +} |
| 164 | + |
| 165 | +#[cfg(test)] |
| 166 | +#[path = "../../unit_tests/models/graph/optimal_linear_arrangement.rs"] |
| 167 | +mod tests; |
0 commit comments