pub struct MDTree<NodeId: Copy + PartialEq> { /* private fields */ }
Expand description
A modular decomposition tree.
The tree is non-empty and rooted.
A node in the tree is a strong module of the original graph.
The modules have a type: Prime
, Series
, or Parallel
for inner nodes and Node(_)
for leaf nodes.
The nodes at the leaves are exactly the nodes of the original graph.
The leaves of a subtree rooted at a module are the set of nodes of the original graph that are a strong module. They can be obtained by MDTree::nodes.
Implementations§
Source§impl<NodeId: Copy + PartialEq> MDTree<NodeId>
impl<NodeId: Copy + PartialEq> MDTree<NodeId>
Sourcepub fn is_cograph(&self) -> bool
pub fn is_cograph(&self) -> bool
Returns whether the original graph is a cograph.
Source§impl<NodeId: Copy + PartialEq> MDTree<NodeId>
impl<NodeId: Copy + PartialEq> MDTree<NodeId>
Sourcepub fn twins(&self) -> impl Iterator<Item = Vec<NodeId>> + '_
pub fn twins(&self) -> impl Iterator<Item = Vec<NodeId>> + '_
Returns an iterator for the set of nodes of the original graph that are twins.
Two nodes are twins when they have the same neighborhood excluding themselves, i.e. N(u) \ {v} = N(v) \ {u}.
Twins are either true or false twins. See MDTree::true_twins and MDTree::false_twins.
use petgraph::graph::{NodeIndex, UnGraph};
use modular_decomposition::modular_decomposition;
// a K_2 + 2 K_1
let graph = UnGraph::<(), ()>::from_edges([(2, 3)]);
let md = modular_decomposition(&graph)?;
let mut twins: Vec<_> = md.twins().collect();
twins.iter_mut().for_each(|nodes| nodes.sort());
twins.sort();
assert_eq!(twins, [[NodeIndex::new(0), NodeIndex::new(1)], [NodeIndex::new(2), NodeIndex::new(3)]]);
Sourcepub fn true_twins(&self) -> impl Iterator<Item = Vec<NodeId>> + '_
pub fn true_twins(&self) -> impl Iterator<Item = Vec<NodeId>> + '_
Returns an iterator for the set of nodes of the original graph that are true twins.
Two nodes are true twins when they have the same closed neighborhood, i.e. N(u) = N(v).
use petgraph::graph::{NodeIndex, UnGraph};
use modular_decomposition::modular_decomposition;
// a K_2 + 2 K_1
let graph = UnGraph::<(), ()>::from_edges([(2, 3)]);
let md = modular_decomposition(&graph)?;
let mut twins: Vec<_> = md.true_twins().collect();
twins.iter_mut().for_each(|nodes| nodes.sort());
twins.sort();
assert_eq!(twins, [[NodeIndex::new(2), NodeIndex::new(3)]]);
Sourcepub fn false_twins(&self) -> impl Iterator<Item = Vec<NodeId>> + '_
pub fn false_twins(&self) -> impl Iterator<Item = Vec<NodeId>> + '_
Returns an iterator for the set of nodes of the original graph that false twins.
Two nodes are false twins when they have the same open neighborhood, i.e. N(u) u {u} = N(v) u {v}.
use petgraph::graph::{NodeIndex, UnGraph};
use modular_decomposition::modular_decomposition;
// a K_2 + 2 K_1
let graph = UnGraph::<(), ()>::from_edges([(2, 3)]);
let md = modular_decomposition(&graph)?;
let mut twins: Vec<_> = md.false_twins().collect();
twins.iter_mut().for_each(|nodes| nodes.sort());
twins.sort();
assert_eq!(twins, [[NodeIndex::new(0), NodeIndex::new(1)]]);
Source§impl<NodeId: Copy + PartialEq> MDTree<NodeId>
impl<NodeId: Copy + PartialEq> MDTree<NodeId>
Sourcepub fn strong_module_count(&self) -> usize
pub fn strong_module_count(&self) -> usize
Return the number of strong modules in the modular decomposition tree.
These are all the nodes of the tree.
Sourcepub fn root(&self) -> ModuleIndex
pub fn root(&self) -> ModuleIndex
Return the root module index.
Sourcepub fn module_kind(&self, module: ModuleIndex) -> Option<&ModuleKind<NodeId>>
pub fn module_kind(&self, module: ModuleIndex) -> Option<&ModuleKind<NodeId>>
Access the ModuleKind of a module.
If the module does not exist, return None.
Sourcepub fn module_kinds(&self) -> impl Iterator<Item = &ModuleKind<NodeId>>
pub fn module_kinds(&self) -> impl Iterator<Item = &ModuleKind<NodeId>>
Return an iterator yielding references to ModuleKinds for all modules.
Sourcepub fn children(
&self,
module: ModuleIndex,
) -> impl Iterator<Item = ModuleIndex> + '_
pub fn children( &self, module: ModuleIndex, ) -> impl Iterator<Item = ModuleIndex> + '_
Return an iterator for the children of a module.
Sourcepub fn nodes(&self, module: ModuleIndex) -> impl Iterator<Item = NodeId> + '_
pub fn nodes(&self, module: ModuleIndex) -> impl Iterator<Item = NodeId> + '_
Sourcepub fn into_digraph(self) -> DiGraph<ModuleKind<NodeId>, ()>
pub fn into_digraph(self) -> DiGraph<ModuleKind<NodeId>, ()>
Convert to DiGraph.
This allows the use of petgraph algorithms. Use ModuleIndex::index and petgraph::graph::NodeIndex::new to convert the root index.
use petgraph::graph::{NodeIndex, UnGraph};
use petgraph::visit::Dfs;
use modular_decomposition::{modular_decomposition, ModuleKind};
let graph = UnGraph::<(), ()>::from_edges([(0, 2), (1, 2), (2, 3), (3, 4), (3, 5)]);
let md = modular_decomposition(&graph)?;
let root = NodeIndex::new(md.root().index());
let digraph = md.into_digraph();
let mut dfs = Dfs::new(&digraph, root);
let mut node_order = vec![];
while let Some(node) = dfs.next(&digraph) { node_order.push(*digraph.node_weight(node).unwrap()); }
let expected_node_order = [
ModuleKind::Prime,
ModuleKind::Node(NodeIndex::new(2)),
ModuleKind::Parallel,
ModuleKind::Node(NodeIndex::new(0)),
ModuleKind::Node(NodeIndex::new(1)),
ModuleKind::Node(NodeIndex::new(3)),
ModuleKind::Parallel,
ModuleKind::Node(NodeIndex::new(4)),
ModuleKind::Node(NodeIndex::new(5)),
];
assert_eq!(node_order, expected_node_order);
Examples found in repository?
72fn main() {
73 let graph = Graph::from_edges([(0, 1), (1, 2), (2, 3)]);
74 let tree = modular_decomposition(&graph).map(|tree| tree.into_digraph()).unwrap_or_default();
75 println!("{:?}", Dot::with_config(&tree, &[EdgeNoLabel]));
76
77 let mut factorizing_permutation = Vec::new();
78 let root = tree.externals(Incoming).next().unwrap();
79 let mut dfs = Dfs::new(&tree, root);
80 while let Some(node) = dfs.next(&tree) {
81 if let ModuleKind::Node(u) = tree[node] {
82 factorizing_permutation.push(u);
83 }
84 }
85 let factorizing_permutation: Vec<_> = factorizing_permutation.to_vec();
86 println!("{:?}", factorizing_permutation);
87}