|
| 1 | +//! SubgraphIsomorphism problem implementation. |
| 2 | +//! |
| 3 | +//! The Subgraph Isomorphism problem asks whether a "pattern" graph H can be |
| 4 | +//! found embedded within a "host" graph G as a subgraph — that is, whether |
| 5 | +//! there exists an injective mapping f: V(H) -> V(G) such that every edge |
| 6 | +//! {u,v} in H maps to an edge {f(u),f(v)} in G. |
| 7 | +
|
| 8 | +use crate::registry::{FieldInfo, ProblemSchemaEntry}; |
| 9 | +use crate::topology::{Graph, SimpleGraph}; |
| 10 | +use crate::traits::{Problem, SatisfactionProblem}; |
| 11 | +use serde::{Deserialize, Serialize}; |
| 12 | + |
| 13 | +inventory::submit! { |
| 14 | + ProblemSchemaEntry { |
| 15 | + name: "SubgraphIsomorphism", |
| 16 | + module_path: module_path!(), |
| 17 | + description: "Determine if host graph G contains a subgraph isomorphic to pattern graph H", |
| 18 | + fields: &[ |
| 19 | + FieldInfo { name: "graph", type_name: "SimpleGraph", description: "The host graph G = (V_1, E_1) to search in" }, |
| 20 | + FieldInfo { name: "pattern", type_name: "SimpleGraph", description: "The pattern graph H = (V_2, E_2) to find as a subgraph" }, |
| 21 | + ], |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +/// The Subgraph Isomorphism problem. |
| 26 | +/// |
| 27 | +/// Given a host graph G = (V_1, E_1) and a pattern graph H = (V_2, E_2), |
| 28 | +/// determine whether there exists an injective function f: V_2 -> V_1 such |
| 29 | +/// that for every edge {u,v} in E_2, {f(u), f(v)} is an edge in E_1. |
| 30 | +/// |
| 31 | +/// This is a satisfaction (decision) problem: the metric is `bool`. |
| 32 | +/// |
| 33 | +/// # Configuration |
| 34 | +/// |
| 35 | +/// A configuration is a vector of length |V_2| where each entry is a value |
| 36 | +/// in {0, ..., |V_1|-1} representing the host vertex that each pattern |
| 37 | +/// vertex maps to. The configuration is valid (true) if: |
| 38 | +/// 1. All mapped host vertices are distinct (injective mapping) |
| 39 | +/// 2. Every edge in the pattern graph maps to an edge in the host graph |
| 40 | +/// |
| 41 | +/// # Example |
| 42 | +/// |
| 43 | +/// ``` |
| 44 | +/// use problemreductions::models::graph::SubgraphIsomorphism; |
| 45 | +/// use problemreductions::topology::SimpleGraph; |
| 46 | +/// use problemreductions::{Problem, Solver, BruteForce}; |
| 47 | +/// |
| 48 | +/// // Host: K4 (complete graph on 4 vertices) |
| 49 | +/// let host = SimpleGraph::new(4, vec![(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)]); |
| 50 | +/// // Pattern: triangle (K3) |
| 51 | +/// let pattern = SimpleGraph::new(3, vec![(0,1),(0,2),(1,2)]); |
| 52 | +/// let problem = SubgraphIsomorphism::new(host, pattern); |
| 53 | +/// |
| 54 | +/// // Mapping [0, 1, 2] means pattern vertex 0->host 0, 1->1, 2->2 |
| 55 | +/// assert!(problem.evaluate(&[0, 1, 2])); |
| 56 | +/// |
| 57 | +/// let solver = BruteForce::new(); |
| 58 | +/// let solution = solver.find_satisfying(&problem); |
| 59 | +/// assert!(solution.is_some()); |
| 60 | +/// ``` |
| 61 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 62 | +pub struct SubgraphIsomorphism { |
| 63 | + /// The host graph G = (V_1, E_1). |
| 64 | + host_graph: SimpleGraph, |
| 65 | + /// The pattern graph H = (V_2, E_2). |
| 66 | + pattern_graph: SimpleGraph, |
| 67 | +} |
| 68 | + |
| 69 | +impl SubgraphIsomorphism { |
| 70 | + /// Create a new SubgraphIsomorphism problem. |
| 71 | + /// |
| 72 | + /// # Arguments |
| 73 | + /// * `host_graph` - The host graph to search in |
| 74 | + /// * `pattern_graph` - The pattern graph to find as a subgraph |
| 75 | + pub fn new(host_graph: SimpleGraph, pattern_graph: SimpleGraph) -> Self { |
| 76 | + Self { |
| 77 | + host_graph, |
| 78 | + pattern_graph, |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /// Get a reference to the host graph. |
| 83 | + pub fn host_graph(&self) -> &SimpleGraph { |
| 84 | + &self.host_graph |
| 85 | + } |
| 86 | + |
| 87 | + /// Get a reference to the pattern graph. |
| 88 | + pub fn pattern_graph(&self) -> &SimpleGraph { |
| 89 | + &self.pattern_graph |
| 90 | + } |
| 91 | + |
| 92 | + /// Get the number of vertices in the host graph. |
| 93 | + pub fn num_host_vertices(&self) -> usize { |
| 94 | + self.host_graph.num_vertices() |
| 95 | + } |
| 96 | + |
| 97 | + /// Get the number of edges in the host graph. |
| 98 | + pub fn num_host_edges(&self) -> usize { |
| 99 | + self.host_graph.num_edges() |
| 100 | + } |
| 101 | + |
| 102 | + /// Get the number of vertices in the pattern graph. |
| 103 | + pub fn num_pattern_vertices(&self) -> usize { |
| 104 | + self.pattern_graph.num_vertices() |
| 105 | + } |
| 106 | + |
| 107 | + /// Get the number of edges in the pattern graph. |
| 108 | + pub fn num_pattern_edges(&self) -> usize { |
| 109 | + self.pattern_graph.num_edges() |
| 110 | + } |
| 111 | + |
| 112 | + /// Check if a configuration represents a valid subgraph isomorphism. |
| 113 | + pub fn is_valid_solution(&self, config: &[usize]) -> bool { |
| 114 | + self.evaluate(config) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +impl Problem for SubgraphIsomorphism { |
| 119 | + const NAME: &'static str = "SubgraphIsomorphism"; |
| 120 | + type Metric = bool; |
| 121 | + |
| 122 | + fn dims(&self) -> Vec<usize> { |
| 123 | + let n_host = self.host_graph.num_vertices(); |
| 124 | + let n_pattern = self.pattern_graph.num_vertices(); |
| 125 | + |
| 126 | + if n_pattern > n_host { |
| 127 | + // No injective mapping possible: each variable gets an empty domain. |
| 128 | + vec![0; n_pattern] |
| 129 | + } else { |
| 130 | + vec![n_host; n_pattern] |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + fn evaluate(&self, config: &[usize]) -> bool { |
| 135 | + let n_pattern = self.pattern_graph.num_vertices(); |
| 136 | + let n_host = self.host_graph.num_vertices(); |
| 137 | + |
| 138 | + // If the pattern has more vertices than the host, no injective mapping exists. |
| 139 | + if n_pattern > n_host { |
| 140 | + return false; |
| 141 | + } |
| 142 | + |
| 143 | + // Config must have one entry per pattern vertex |
| 144 | + if config.len() != n_pattern { |
| 145 | + return false; |
| 146 | + } |
| 147 | + |
| 148 | + // All values must be valid host vertex indices |
| 149 | + if config.iter().any(|&v| v >= n_host) { |
| 150 | + return false; |
| 151 | + } |
| 152 | + |
| 153 | + // Check injectivity: all mapped host vertices must be distinct |
| 154 | + for i in 0..n_pattern { |
| 155 | + for j in (i + 1)..n_pattern { |
| 156 | + if config[i] == config[j] { |
| 157 | + return false; |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + // Check edge preservation: every pattern edge must map to a host edge |
| 163 | + for (u, v) in self.pattern_graph.edges() { |
| 164 | + if !self.host_graph.has_edge(config[u], config[v]) { |
| 165 | + return false; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + true |
| 170 | + } |
| 171 | + |
| 172 | + fn variant() -> Vec<(&'static str, &'static str)> { |
| 173 | + crate::variant_params![] |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +impl SatisfactionProblem for SubgraphIsomorphism {} |
| 178 | + |
| 179 | +crate::declare_variants! { |
| 180 | + SubgraphIsomorphism => "num_host_vertices ^ num_pattern_vertices", |
| 181 | +} |
| 182 | + |
| 183 | +#[cfg(test)] |
| 184 | +#[path = "../../unit_tests/models/graph/subgraph_isomorphism.rs"] |
| 185 | +mod tests; |
0 commit comments