|
| 1 | +use super::*; |
| 2 | +use crate::solvers::BruteForce; |
| 3 | +use crate::topology::Graph; |
| 4 | +use crate::traits::Problem; |
| 5 | +use crate::types::SolutionSize; |
| 6 | +use std::collections::HashSet; |
| 7 | + |
| 8 | +#[test] |
| 9 | +fn test_maximumclique_to_maximumindependentset_closed_loop() { |
| 10 | + // Path graph P4: vertices {0,1,2,3}, edges {(0,1),(1,2),(2,3)} |
| 11 | + // Maximum clique is any edge, size 2. |
| 12 | + // Complement has edges {(0,2),(0,3),(1,3)}, MIS of size 2. |
| 13 | + let source = MaximumClique::new( |
| 14 | + SimpleGraph::new(4, vec![(0, 1), (1, 2), (2, 3)]), |
| 15 | + vec![1i32; 4], |
| 16 | + ); |
| 17 | + let reduction = ReduceTo::<MaximumIndependentSet<SimpleGraph, i32>>::reduce_to(&source); |
| 18 | + let target = reduction.target_problem(); |
| 19 | + |
| 20 | + // Verify complement graph structure |
| 21 | + assert_eq!(target.graph().num_vertices(), 4); |
| 22 | + assert_eq!(target.graph().num_edges(), 3); // 4*3/2 - 3 = 3 |
| 23 | + |
| 24 | + let solver = BruteForce::new(); |
| 25 | + |
| 26 | + // Solve target (MIS on complement graph) |
| 27 | + let target_solutions = solver.find_all_best(target); |
| 28 | + assert!(!target_solutions.is_empty()); |
| 29 | + |
| 30 | + // Solve source directly |
| 31 | + let source_solutions: HashSet<Vec<usize>> = solver.find_all_best(&source).into_iter().collect(); |
| 32 | + assert!(!source_solutions.is_empty()); |
| 33 | + |
| 34 | + // Extract solutions and verify they are optimal for source |
| 35 | + for target_sol in &target_solutions { |
| 36 | + let source_sol = reduction.extract_solution(target_sol); |
| 37 | + assert!(source_solutions.contains(&source_sol)); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +#[test] |
| 42 | +fn test_maximumclique_to_maximumindependentset_triangle() { |
| 43 | + // Complete graph K3: all 3 edges present |
| 44 | + // Complement is empty graph (no edges) |
| 45 | + // MIS on empty graph = all vertices |
| 46 | + let source = MaximumClique::new( |
| 47 | + SimpleGraph::new(3, vec![(0, 1), (0, 2), (1, 2)]), |
| 48 | + vec![1i32; 3], |
| 49 | + ); |
| 50 | + let reduction = ReduceTo::<MaximumIndependentSet<SimpleGraph, i32>>::reduce_to(&source); |
| 51 | + let target = reduction.target_problem(); |
| 52 | + |
| 53 | + // Complement of K3 has no edges |
| 54 | + assert_eq!(target.graph().num_edges(), 0); |
| 55 | + |
| 56 | + let solver = BruteForce::new(); |
| 57 | + let target_solutions = solver.find_all_best(target); |
| 58 | + |
| 59 | + // MIS on empty graph is all vertices selected |
| 60 | + assert!(target_solutions |
| 61 | + .iter() |
| 62 | + .any(|s| s.iter().sum::<usize>() == 3)); |
| 63 | + |
| 64 | + // Extract solution: should be the full clique {0,1,2} |
| 65 | + let source_sol = reduction.extract_solution(&target_solutions[0]); |
| 66 | + assert!(matches!( |
| 67 | + source.evaluate(&source_sol), |
| 68 | + SolutionSize::Valid(3) |
| 69 | + )); |
| 70 | +} |
| 71 | + |
| 72 | +#[test] |
| 73 | +fn test_maximumclique_to_maximumindependentset_weights_preserved() { |
| 74 | + let source = MaximumClique::new(SimpleGraph::new(3, vec![(0, 1), (1, 2)]), vec![10, 20, 30]); |
| 75 | + let reduction = ReduceTo::<MaximumIndependentSet<SimpleGraph, i32>>::reduce_to(&source); |
| 76 | + let target = reduction.target_problem(); |
| 77 | + |
| 78 | + assert_eq!(target.weights().to_vec(), vec![10, 20, 30]); |
| 79 | +} |
| 80 | + |
| 81 | +#[test] |
| 82 | +fn test_maximumclique_to_maximumindependentset_empty_graph() { |
| 83 | + // Empty graph (no edges): complement is complete graph |
| 84 | + // Max clique in empty graph = any single vertex |
| 85 | + let source = MaximumClique::new(SimpleGraph::new(3, vec![]), vec![1i32; 3]); |
| 86 | + let reduction = ReduceTo::<MaximumIndependentSet<SimpleGraph, i32>>::reduce_to(&source); |
| 87 | + let target = reduction.target_problem(); |
| 88 | + |
| 89 | + // Complement of empty graph is K3 |
| 90 | + assert_eq!(target.graph().num_edges(), 3); |
| 91 | + |
| 92 | + let solver = BruteForce::new(); |
| 93 | + let target_solutions = solver.find_all_best(target); |
| 94 | + |
| 95 | + // MIS on K3 is any single vertex |
| 96 | + assert!(target_solutions |
| 97 | + .iter() |
| 98 | + .all(|s| s.iter().sum::<usize>() == 1)); |
| 99 | +} |
| 100 | + |
| 101 | +#[test] |
| 102 | +fn test_maximumclique_to_maximumindependentset_overhead() { |
| 103 | + // Verify overhead formula: complement edges = n*(n-1)/2 - m |
| 104 | + let source = MaximumClique::new( |
| 105 | + SimpleGraph::new(5, vec![(0, 1), (1, 2), (2, 3), (3, 4)]), |
| 106 | + vec![1i32; 5], |
| 107 | + ); |
| 108 | + let reduction = ReduceTo::<MaximumIndependentSet<SimpleGraph, i32>>::reduce_to(&source); |
| 109 | + let target = reduction.target_problem(); |
| 110 | + |
| 111 | + assert_eq!(target.graph().num_vertices(), 5); |
| 112 | + // 5*4/2 - 4 = 6 |
| 113 | + assert_eq!(target.graph().num_edges(), 6); |
| 114 | +} |
0 commit comments