-
Notifications
You must be signed in to change notification settings - Fork 4
Fix #165: Add MaximumIndependentSet to MaximumClique reduction #606
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
88ab777
feat: add MaximumIndependentSet to MaximumClique reduction (#165)
zazabap 5153bef
fix: update MIS→ILP path test to expect MaxClique intermediate
zazabap 3a22df6
chore: regenerate reduction_graph.json with MIS→MaxClique edge
zazabap 918736d
Merge remote-tracking branch 'origin/main' into issue-165-mis-to-maxc…
zazabap 9d826bc
fix: merge main, regenerate reduction graph, fix formatting
zazabap 8c45a48
Merge remote-tracking branch 'origin/main' into issue-165-mis-to-maxc…
zazabap 9f87871
fix: make MIS→ILP path test accept either MaxClique or MaxSetPacking
zazabap e40e081
fix: regenerate example JSON with consistent key ordering
zazabap File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
133
docs/paper/examples/maximumindependentset_to_maximumclique.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| { | ||
| "source": { | ||
| "problem": "MaximumIndependentSet", | ||
| "variant": { | ||
| "graph": "SimpleGraph", | ||
| "weight": "i32" | ||
| }, | ||
| "instance": { | ||
| "edges": [ | ||
| [ | ||
| 0, | ||
| 1 | ||
| ], | ||
| [ | ||
| 1, | ||
| 2 | ||
| ], | ||
| [ | ||
| 2, | ||
| 3 | ||
| ], | ||
| [ | ||
| 3, | ||
| 4 | ||
| ] | ||
| ], | ||
| "num_edges": 4, | ||
| "num_vertices": 5 | ||
| } | ||
| }, | ||
| "target": { | ||
| "problem": "MaximumClique", | ||
| "variant": { | ||
| "weight": "i32", | ||
| "graph": "SimpleGraph" | ||
| }, | ||
| "instance": { | ||
| "edges": [ | ||
| [ | ||
| 0, | ||
| 2 | ||
| ], | ||
| [ | ||
| 0, | ||
| 3 | ||
| ], | ||
| [ | ||
| 0, | ||
| 4 | ||
| ], | ||
| [ | ||
| 1, | ||
| 3 | ||
| ], | ||
| [ | ||
| 1, | ||
| 4 | ||
| ], | ||
| [ | ||
| 2, | ||
| 4 | ||
| ] | ||
| ], | ||
| "num_edges": 6, | ||
| "num_vertices": 5 | ||
| } | ||
| }, | ||
| "overhead": [ | ||
| { | ||
| "field": "num_vertices", | ||
| "expr": { | ||
| "Var": "num_vertices" | ||
| }, | ||
| "formula": "num_vertices" | ||
| }, | ||
| { | ||
| "field": "num_edges", | ||
| "expr": { | ||
| "Add": [ | ||
| { | ||
| "Mul": [ | ||
| { | ||
| "Mul": [ | ||
| { | ||
| "Var": "num_vertices" | ||
| }, | ||
| { | ||
| "Add": [ | ||
| { | ||
| "Var": "num_vertices" | ||
| }, | ||
| { | ||
| "Mul": [ | ||
| { | ||
| "Const": -1.0 | ||
| }, | ||
| { | ||
| "Const": 1.0 | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| "Pow": [ | ||
| { | ||
| "Const": 2.0 | ||
| }, | ||
| { | ||
| "Const": -1.0 | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| "Mul": [ | ||
| { | ||
| "Const": -1.0 | ||
| }, | ||
| { | ||
| "Var": "num_edges" | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| }, | ||
| "formula": "num_vertices * (num_vertices + -1 * 1) * 2^-1 + -1 * num_edges" | ||
| } | ||
| ] | ||
| } |
20 changes: 20 additions & 0 deletions
20
docs/paper/examples/maximumindependentset_to_maximumclique.result.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| { | ||
| "solutions": [ | ||
| { | ||
| "source_config": [ | ||
| 1, | ||
| 0, | ||
| 1, | ||
| 0, | ||
| 1 | ||
| ], | ||
| "target_config": [ | ||
| 1, | ||
| 0, | ||
| 1, | ||
| 0, | ||
| 1 | ||
| ] | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
examples/reduction_maximumindependentset_to_maximumclique.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| // # Independent Set to Clique Reduction | ||
| // | ||
| // ## Mathematical Equivalence | ||
| // S is an independent set in G iff S is a clique in the complement graph Ḡ. | ||
| // The reduction builds Ḡ by taking edges not in G. Solution extraction is | ||
| // identity: the same vertex set works for both problems. | ||
| // | ||
| // ## This Example | ||
| // - Instance: Path graph P5 (5 vertices, 4 edges) | ||
| // - Source MIS: max size 3 (e.g., {0, 2, 4}) | ||
| // - Target MaxClique on complement: max clique size 3 | ||
| // | ||
| // ## Output | ||
| // Exports `docs/paper/examples/maximumindependentset_to_maximumclique.json` and `.result.json`. | ||
|
|
||
| use problemreductions::export::*; | ||
| use problemreductions::prelude::*; | ||
| use problemreductions::topology::{Graph, SimpleGraph}; | ||
|
|
||
| pub fn run() { | ||
| // Path graph: 0-1-2-3-4 | ||
| let source = MaximumIndependentSet::new( | ||
| SimpleGraph::new(5, vec![(0, 1), (1, 2), (2, 3), (3, 4)]), | ||
| vec![1i32; 5], | ||
| ); | ||
|
|
||
| let reduction = ReduceTo::<MaximumClique<SimpleGraph, i32>>::reduce_to(&source); | ||
| let target = reduction.target_problem(); | ||
|
|
||
| println!("\n=== Problem Transformation ==="); | ||
| println!( | ||
| "Source: MaximumIndependentSet with {} vertices, {} edges", | ||
| source.graph().num_vertices(), | ||
| source.graph().num_edges() | ||
| ); | ||
| println!( | ||
| "Target: MaximumClique with {} vertices, {} edges (complement graph)", | ||
| target.num_vertices(), | ||
| target.num_edges() | ||
| ); | ||
|
|
||
| let solver = BruteForce::new(); | ||
| let target_solutions = solver.find_all_best(target); | ||
| println!("\n=== Solution ==="); | ||
| println!("Target solutions found: {}", target_solutions.len()); | ||
|
|
||
| let mut solutions = Vec::new(); | ||
| for target_sol in &target_solutions { | ||
| let source_sol = reduction.extract_solution(target_sol); | ||
| let size = source.evaluate(&source_sol); | ||
| assert!(size.is_valid()); | ||
| solutions.push(SolutionPair { | ||
| source_config: source_sol.clone(), | ||
| target_config: target_sol.clone(), | ||
| }); | ||
| } | ||
|
|
||
| let source_sol = reduction.extract_solution(&target_solutions[0]); | ||
| println!("Source IS solution: {:?}", source_sol); | ||
| let size = source.evaluate(&source_sol); | ||
| println!("Solution size: {:?}", size); | ||
| assert!(size.is_valid()); | ||
| println!("\nReduction verified successfully"); | ||
|
|
||
| // Export JSON | ||
| let source_edges = source.graph().edges(); | ||
| let target_edges = target.graph().edges(); | ||
| let source_variant = variant_to_map(MaximumIndependentSet::<SimpleGraph, i32>::variant()); | ||
| let target_variant = variant_to_map(MaximumClique::<SimpleGraph, i32>::variant()); | ||
| let overhead = lookup_overhead( | ||
| "MaximumIndependentSet", | ||
| &source_variant, | ||
| "MaximumClique", | ||
| &target_variant, | ||
| ) | ||
| .expect("MaximumIndependentSet -> MaximumClique overhead not found"); | ||
|
|
||
| let data = ReductionData { | ||
| source: ProblemSide { | ||
| problem: MaximumIndependentSet::<SimpleGraph, i32>::NAME.to_string(), | ||
| variant: source_variant, | ||
| instance: serde_json::json!({ | ||
| "num_vertices": source.graph().num_vertices(), | ||
| "num_edges": source.graph().num_edges(), | ||
| "edges": source_edges, | ||
| }), | ||
| }, | ||
| target: ProblemSide { | ||
| problem: MaximumClique::<SimpleGraph, i32>::NAME.to_string(), | ||
| variant: target_variant, | ||
| instance: serde_json::json!({ | ||
| "num_vertices": target.num_vertices(), | ||
| "num_edges": target.num_edges(), | ||
| "edges": target_edges, | ||
| }), | ||
| }, | ||
| overhead: overhead_to_json(&overhead), | ||
| }; | ||
|
|
||
| let results = ResultData { solutions }; | ||
| let name = "maximumindependentset_to_maximumclique"; | ||
| write_example(name, &data, &results); | ||
| } | ||
|
|
||
| fn main() { | ||
| run() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| //! Reduction from MaximumIndependentSet to MaximumClique via complement graph. | ||
| //! | ||
| //! An independent set in G corresponds to a clique in the complement graph Ḡ. | ||
| //! This is Karp's classical complement graph reduction. | ||
|
|
||
| use crate::models::graph::{MaximumClique, MaximumIndependentSet}; | ||
| use crate::reduction; | ||
| use crate::rules::traits::{ReduceTo, ReductionResult}; | ||
| use crate::topology::{Graph, SimpleGraph}; | ||
| use crate::types::WeightElement; | ||
|
|
||
| /// Result of reducing MaximumIndependentSet to MaximumClique. | ||
| #[derive(Debug, Clone)] | ||
| pub struct ReductionISToClique<W> { | ||
| target: MaximumClique<SimpleGraph, W>, | ||
| } | ||
|
|
||
| impl<W> ReductionResult for ReductionISToClique<W> | ||
| where | ||
| W: WeightElement + crate::variant::VariantParam, | ||
| { | ||
| type Source = MaximumIndependentSet<SimpleGraph, W>; | ||
| type Target = MaximumClique<SimpleGraph, W>; | ||
|
|
||
| fn target_problem(&self) -> &Self::Target { | ||
| &self.target | ||
| } | ||
|
|
||
| /// Solution extraction: identity mapping. | ||
| /// A vertex selected in the clique (target) is also selected in the independent set (source). | ||
| fn extract_solution(&self, target_solution: &[usize]) -> Vec<usize> { | ||
| target_solution.to_vec() | ||
| } | ||
| } | ||
|
|
||
| #[reduction( | ||
| overhead = { | ||
| num_vertices = "num_vertices", | ||
| num_edges = "num_vertices * (num_vertices - 1) / 2 - num_edges", | ||
| } | ||
| )] | ||
| impl ReduceTo<MaximumClique<SimpleGraph, i32>> for MaximumIndependentSet<SimpleGraph, i32> { | ||
| type Result = ReductionISToClique<i32>; | ||
|
|
||
| fn reduce_to(&self) -> Self::Result { | ||
| let n = self.graph().num_vertices(); | ||
| // Build complement graph edges | ||
| let mut complement_edges = Vec::new(); | ||
| for u in 0..n { | ||
| for v in (u + 1)..n { | ||
| if !self.graph().has_edge(u, v) { | ||
| complement_edges.push((u, v)); | ||
| } | ||
| } | ||
| } | ||
| let target = MaximumClique::new( | ||
| SimpleGraph::new(n, complement_edges), | ||
| self.weights().to_vec(), | ||
| ); | ||
| ReductionISToClique { target } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| #[path = "../unit_tests/rules/maximumindependentset_maximumclique.rs"] | ||
| mod tests; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
docs/src/reductions/reduction_graph.jsonfile needs to be regenerated to include the new MaximumIndependentSet → MaximumClique reduction edge. Runcargo run --example export_graphto update it.