use crate::core::view::NodeId;
use crate::graph::graph::Graph;
use std::collections::HashSet;
use std::fmt::Debug;
use std::hash::Hash;
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, thiserror::Error)]
pub enum DominatingSetError {
#[error("Invalid neighbor node: node ID {0} not found")]
InvalidNeighborNode(NodeId),
}
impl From<crate::core::TraversalError> for DominatingSetError {
fn from(error: crate::core::TraversalError) -> Self {
match error {
crate::core::TraversalError::InvalidNode(id) => {
DominatingSetError::InvalidNeighborNode(id)
}
}
}
}
#[deprecated(
since = "3.0.0",
note = "superseded by `xgraph::core::DominatingSet`, which is implemented for every \
graph view and can also verify a candidate set with `is_dominating_set`"
)]
pub trait DominatingSetFinder {
fn find_dominating_set(&self) -> Result<HashSet<NodeId>, DominatingSetError>;
}
#[allow(deprecated)]
impl<W, N, E> DominatingSetFinder for Graph<W, N, E>
where
W: Copy + Default + PartialEq,
N: Clone + Eq + Hash + Debug,
E: Clone + Default + Debug,
{
fn find_dominating_set(&self) -> Result<HashSet<NodeId>, DominatingSetError> {
crate::core::DominatingSet::dominating_set(self).map_err(DominatingSetError::from)
}
}
#[cfg(test)]
#[allow(deprecated)]
mod tests {
use super::*;
fn create_test_graph(matrix: Vec<Vec<u32>>) -> Graph<u32, (), ()> {
Graph::from_adjacency_matrix(&matrix, false, (), ()).expect("Failed to create test graph")
}
#[test]
fn test_simple_dominating_set() {
let matrix = vec![
vec![0, 1, 1, 0],
vec![1, 0, 1, 1],
vec![1, 1, 0, 0],
vec![0, 1, 0, 0],
];
let graph = create_test_graph(matrix);
let ds = crate::core::DominatingSet::dominating_set(&graph).unwrap();
assert!(!ds.is_empty(), "Dominating set should not be empty");
assert!(ds.len() <= 2, "Dominating set size should be at most 2");
}
#[test]
fn test_complex_dominating_set() {
let matrix = vec![
vec![0, 1, 0, 1, 0],
vec![1, 0, 1, 0, 0],
vec![0, 1, 0, 1, 1],
vec![1, 0, 1, 0, 0],
vec![0, 0, 1, 0, 0],
];
let graph = create_test_graph(matrix);
let ds = crate::core::DominatingSet::dominating_set(&graph).unwrap();
assert!(!ds.is_empty(), "Dominating set should not be empty");
assert!(ds.len() <= 3, "Dominating set size should be at most 3");
}
}