problemreductions/models/graph/
maximum_clique.rs1use crate::registry::{FieldInfo, ProblemSchemaEntry};
7use crate::topology::{Graph, SimpleGraph};
8use crate::traits::{OptimizationProblem, Problem};
9use crate::types::{Direction, SolutionSize, WeightElement};
10use num_traits::Zero;
11use serde::{Deserialize, Serialize};
12
13inventory::submit! {
14 ProblemSchemaEntry {
15 name: "MaximumClique",
16 module_path: module_path!(),
17 description: "Find maximum weight clique in a graph",
18 fields: &[
19 FieldInfo { name: "graph", type_name: "G", description: "The underlying graph G=(V,E)" },
20 FieldInfo { name: "weights", type_name: "Vec<W>", description: "Vertex weights w: V -> R" },
21 ],
22 }
23}
24
25#[derive(Debug, Clone, Serialize, Deserialize)]
56pub struct MaximumClique<G, W> {
57 graph: G,
59 weights: Vec<W>,
61}
62
63impl<G: Graph, W: Clone + Default> MaximumClique<G, W> {
64 pub fn new(graph: G, weights: Vec<W>) -> Self {
66 assert_eq!(
67 weights.len(),
68 graph.num_vertices(),
69 "weights length must match graph num_vertices"
70 );
71 Self { graph, weights }
72 }
73
74 pub fn graph(&self) -> &G {
76 &self.graph
77 }
78
79 pub fn weights(&self) -> &[W] {
81 &self.weights
82 }
83
84 pub fn is_weighted(&self) -> bool
86 where
87 W: WeightElement,
88 {
89 !W::IS_UNIT
90 }
91
92 pub fn is_valid_solution(&self, config: &[usize]) -> bool {
94 is_clique_config(&self.graph, config)
95 }
96}
97
98impl<G: Graph, W: WeightElement> MaximumClique<G, W> {
99 pub fn num_vertices(&self) -> usize {
101 self.graph().num_vertices()
102 }
103
104 pub fn num_edges(&self) -> usize {
106 self.graph().num_edges()
107 }
108}
109
110impl<G, W> Problem for MaximumClique<G, W>
111where
112 G: Graph + crate::variant::VariantParam,
113 W: WeightElement + crate::variant::VariantParam,
114{
115 const NAME: &'static str = "MaximumClique";
116 type Metric = SolutionSize<W::Sum>;
117
118 fn variant() -> Vec<(&'static str, &'static str)> {
119 crate::variant_params![G, W]
120 }
121
122 fn dims(&self) -> Vec<usize> {
123 vec![2; self.graph.num_vertices()]
124 }
125
126 fn evaluate(&self, config: &[usize]) -> SolutionSize<W::Sum> {
127 if !is_clique_config(&self.graph, config) {
128 return SolutionSize::Invalid;
129 }
130 let mut total = W::Sum::zero();
131 for (i, &selected) in config.iter().enumerate() {
132 if selected == 1 {
133 total += self.weights[i].to_sum();
134 }
135 }
136 SolutionSize::Valid(total)
137 }
138}
139
140impl<G, W> OptimizationProblem for MaximumClique<G, W>
141where
142 G: Graph + crate::variant::VariantParam,
143 W: WeightElement + crate::variant::VariantParam,
144{
145 type Value = W::Sum;
146
147 fn direction(&self) -> Direction {
148 Direction::Maximize
149 }
150}
151
152fn is_clique_config<G: Graph>(graph: &G, config: &[usize]) -> bool {
154 let selected: Vec<usize> = config
156 .iter()
157 .enumerate()
158 .filter(|(_, &v)| v == 1)
159 .map(|(i, _)| i)
160 .collect();
161
162 for i in 0..selected.len() {
164 for j in (i + 1)..selected.len() {
165 if !graph.has_edge(selected[i], selected[j]) {
166 return false;
167 }
168 }
169 }
170 true
171}
172
173crate::declare_variants! {
174 MaximumClique<SimpleGraph, i32> => "1.1996^num_vertices",
175}
176
177#[cfg(test)]
186pub(crate) fn is_clique<G: Graph>(graph: &G, selected: &[bool]) -> bool {
187 assert_eq!(
188 selected.len(),
189 graph.num_vertices(),
190 "selected length must match num_vertices"
191 );
192
193 let selected_vertices: Vec<usize> = selected
195 .iter()
196 .enumerate()
197 .filter(|(_, &s)| s)
198 .map(|(i, _)| i)
199 .collect();
200
201 for i in 0..selected_vertices.len() {
203 for j in (i + 1)..selected_vertices.len() {
204 if !graph.has_edge(selected_vertices[i], selected_vertices[j]) {
205 return false;
206 }
207 }
208 }
209 true
210}
211
212#[cfg(test)]
213#[path = "../../unit_tests/models/graph/maximum_clique.rs"]
214mod tests;