|
| 1 | +//! Reduction from MinimumMaximalMatching (on a bipartite graph) to |
| 2 | +//! MaximumAchromaticNumber. |
| 3 | +//! |
| 4 | +//! Classical reduction of Yannakakis and Gavril (1980) establishing |
| 5 | +//! NP-completeness of Achromatic Number (G&J GT5). For a bipartite graph `G`, |
| 6 | +//! the identity `ach(complement(G)) = |V| - mm(G)` holds, where `mm(G)` is the |
| 7 | +//! minimum maximal matching size of `G`. The decision-version correspondence |
| 8 | +//! used in the reduction is `(G, K) -> (complement(G), |V| - K)`. |
| 9 | +
|
| 10 | +use crate::models::graph::{MaximumAchromaticNumber, MinimumMaximalMatching}; |
| 11 | +use crate::reduction; |
| 12 | +use crate::rules::traits::{ReduceTo, ReductionResult}; |
| 13 | +use crate::topology::{BipartiteGraph, Graph, SimpleGraph}; |
| 14 | +use std::collections::HashMap; |
| 15 | + |
| 16 | +/// Result of reducing `MinimumMaximalMatching<BipartiteGraph>` to |
| 17 | +/// `MaximumAchromaticNumber<SimpleGraph>`. |
| 18 | +/// |
| 19 | +/// Stores the target problem along with the source edge list (in unified vertex |
| 20 | +/// coordinates) so that `extract_solution` can map a target coloring back to a |
| 21 | +/// maximal matching of the source graph. |
| 22 | +#[derive(Debug, Clone)] |
| 23 | +pub struct ReductionMMMToAchromatic { |
| 24 | + target: MaximumAchromaticNumber<SimpleGraph>, |
| 25 | + /// Source edges in unified vertex coordinates, in the same order as |
| 26 | + /// `source.graph().edges()` (which determines the source `dims()`). |
| 27 | + source_edges: Vec<(usize, usize)>, |
| 28 | +} |
| 29 | + |
| 30 | +impl ReductionResult for ReductionMMMToAchromatic { |
| 31 | + type Source = MinimumMaximalMatching<BipartiteGraph>; |
| 32 | + type Target = MaximumAchromaticNumber<SimpleGraph>; |
| 33 | + |
| 34 | + fn target_problem(&self) -> &Self::Target { |
| 35 | + &self.target |
| 36 | + } |
| 37 | + |
| 38 | + /// Extract a maximal matching of the source graph from an achromatic |
| 39 | + /// coloring of `complement(G)`. |
| 40 | + /// |
| 41 | + /// Each color class of size exactly 2 corresponds to a clique-in-G of |
| 42 | + /// size 2, i.e., a single source edge. Marking those edges yields the |
| 43 | + /// maximal matching `M` with `|M| = |V| - k`, where `k` is the number of |
| 44 | + /// colors used. |
| 45 | + fn extract_solution(&self, target_solution: &[usize]) -> Vec<usize> { |
| 46 | + let num_source_edges = self.source_edges.len(); |
| 47 | + let mut source_config = vec![0usize; num_source_edges]; |
| 48 | + |
| 49 | + // Group vertices by color. |
| 50 | + let mut color_to_vertices: HashMap<usize, Vec<usize>> = HashMap::new(); |
| 51 | + for (vertex, &color) in target_solution.iter().enumerate() { |
| 52 | + color_to_vertices.entry(color).or_default().push(vertex); |
| 53 | + } |
| 54 | + |
| 55 | + // Build an edge lookup keyed by canonical (min, max) pairs. |
| 56 | + let mut edge_index: HashMap<(usize, usize), usize> = HashMap::new(); |
| 57 | + for (idx, &(u, v)) in self.source_edges.iter().enumerate() { |
| 58 | + let key = if u < v { (u, v) } else { (v, u) }; |
| 59 | + edge_index.insert(key, idx); |
| 60 | + } |
| 61 | + |
| 62 | + // Color classes of size 2 must be edges of G (cliques in G of size 2). |
| 63 | + for vertices in color_to_vertices.values() { |
| 64 | + if vertices.len() == 2 { |
| 65 | + let (a, b) = (vertices[0], vertices[1]); |
| 66 | + let key = if a < b { (a, b) } else { (b, a) }; |
| 67 | + if let Some(&idx) = edge_index.get(&key) { |
| 68 | + source_config[idx] = 1; |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + source_config |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +#[reduction( |
| 78 | + overhead = { |
| 79 | + num_vertices = "num_vertices", |
| 80 | + num_edges = "num_vertices * (num_vertices - 1) / 2 - num_edges", |
| 81 | + } |
| 82 | +)] |
| 83 | +impl ReduceTo<MaximumAchromaticNumber<SimpleGraph>> for MinimumMaximalMatching<BipartiteGraph> { |
| 84 | + type Result = ReductionMMMToAchromatic; |
| 85 | + |
| 86 | + fn reduce_to(&self) -> Self::Result { |
| 87 | + let n = self.graph().num_vertices(); |
| 88 | + let source_edges = self.graph().edges(); |
| 89 | + |
| 90 | + // Build adjacency lookup over unified coordinates. |
| 91 | + let source_edge_set: std::collections::HashSet<(usize, usize)> = source_edges |
| 92 | + .iter() |
| 93 | + .map(|&(u, v)| if u < v { (u, v) } else { (v, u) }) |
| 94 | + .collect(); |
| 95 | + |
| 96 | + // Complement graph: all non-edges of G become edges of H. |
| 97 | + let mut complement_edges = Vec::new(); |
| 98 | + for u in 0..n { |
| 99 | + for v in (u + 1)..n { |
| 100 | + if !source_edge_set.contains(&(u, v)) { |
| 101 | + complement_edges.push((u, v)); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + let target = MaximumAchromaticNumber::new(SimpleGraph::new(n, complement_edges)); |
| 107 | + |
| 108 | + ReductionMMMToAchromatic { |
| 109 | + target, |
| 110 | + source_edges, |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +#[cfg(feature = "example-db")] |
| 116 | +pub(crate) fn canonical_rule_example_specs() -> Vec<crate::example_db::specs::RuleExampleSpec> { |
| 117 | + use crate::export::SolutionPair; |
| 118 | + |
| 119 | + vec![crate::example_db::specs::RuleExampleSpec { |
| 120 | + id: "minimummaximalmatching_to_maximumachromaticnumber", |
| 121 | + build: || { |
| 122 | + // Path P4 as a bipartite graph: A = {v0, v2}, B = {v1, v3}. |
| 123 | + // |
| 124 | + // BipartiteGraph encoding (left_size = 2, right_size = 2): |
| 125 | + // left local 0 -> v0, local 1 -> v2 |
| 126 | + // right local 0 -> v1, local 1 -> v3 |
| 127 | + // edges (left_idx, right_idx): |
| 128 | + // (v0, v1) -> (0, 0) |
| 129 | + // (v1, v2) -> (1, 0) (v2 is left=1, v1 is right=0) |
| 130 | + // (v2, v3) -> (1, 1) |
| 131 | + // |
| 132 | + // Unified vertex labels: |
| 133 | + // 0 = v0 (left 0) |
| 134 | + // 1 = v2 (left 1) |
| 135 | + // 2 = v1 (right 0) |
| 136 | + // 3 = v3 (right 1) |
| 137 | + // |
| 138 | + // Unified edges from Graph::edges(): |
| 139 | + // (0, 2), (1, 2), (1, 3) |
| 140 | + // |
| 141 | + // mm(G) = 1, achieved by selecting the middle edge (v1, v2), |
| 142 | + // which is unified edge index 1 (i.e., (1, 2)). |
| 143 | + // So source_config = [0, 1, 0]. |
| 144 | + // |
| 145 | + // complement(G) edges: (0, 1), (0, 3), (2, 3). |
| 146 | + // |
| 147 | + // Achromatic 3-coloring of complement(G): |
| 148 | + // v0 (idx 0) -> color 0 |
| 149 | + // v2 (idx 1) -> color 1 |
| 150 | + // v1 (idx 2) -> color 1 (paired with v2 = G-edge (v1, v2)) |
| 151 | + // v3 (idx 3) -> color 2 |
| 152 | + // target_config = [0, 1, 1, 2]. |
| 153 | + let source = MinimumMaximalMatching::new(BipartiteGraph::new( |
| 154 | + 2, |
| 155 | + 2, |
| 156 | + vec![(0, 0), (1, 0), (1, 1)], |
| 157 | + )); |
| 158 | + crate::example_db::specs::rule_example_with_witness::< |
| 159 | + _, |
| 160 | + MaximumAchromaticNumber<SimpleGraph>, |
| 161 | + >( |
| 162 | + source, |
| 163 | + SolutionPair { |
| 164 | + source_config: vec![0, 1, 0], |
| 165 | + target_config: vec![0, 1, 1, 2], |
| 166 | + }, |
| 167 | + ) |
| 168 | + }, |
| 169 | + }] |
| 170 | +} |
| 171 | + |
| 172 | +#[cfg(test)] |
| 173 | +#[path = "../unit_tests/rules/minimummaximalmatching_maximumachromaticnumber.rs"] |
| 174 | +mod tests; |
0 commit comments