Struct MDTree

Source
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>

Source

pub fn is_cograph(&self) -> bool

Returns whether the original graph is a cograph.

Source§

impl<NodeId: Copy + PartialEq> MDTree<NodeId>

Source

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)]]);
Source

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)]]);
Source

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>

Source

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.

Source

pub fn root(&self) -> ModuleIndex

Return the root module index.

Source

pub fn module_kind(&self, module: ModuleIndex) -> Option<&ModuleKind<NodeId>>

Access the ModuleKind of a module.

If the module does not exist, return None.

Source

pub fn module_kinds(&self) -> impl Iterator<Item = &ModuleKind<NodeId>>

Return an iterator yielding references to ModuleKinds for all modules.

Source

pub fn children( &self, module: ModuleIndex, ) -> impl Iterator<Item = ModuleIndex> + '_

Return an iterator for the children of a module.

Source

pub fn nodes(&self, module: ModuleIndex) -> impl Iterator<Item = NodeId> + '_

Return the nodes of a module.

The nodes represent nodes of the original graph and not modules of the MDTree. These are the leaves of the subtree rooted at module in the MDTree.

Source

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?
examples/adj_vector_graph.rs (line 74)
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}

Trait Implementations§

Source§

impl<NodeId: Clone + Copy + PartialEq> Clone for MDTree<NodeId>

Source§

fn clone(&self) -> MDTree<NodeId>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<NodeId: Debug + Copy + PartialEq> Debug for MDTree<NodeId>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<NodeId> Freeze for MDTree<NodeId>

§

impl<NodeId> RefUnwindSafe for MDTree<NodeId>
where NodeId: RefUnwindSafe,

§

impl<NodeId> Send for MDTree<NodeId>
where NodeId: Send,

§

impl<NodeId> Sync for MDTree<NodeId>
where NodeId: Sync,

§

impl<NodeId> Unpin for MDTree<NodeId>
where NodeId: Unpin,

§

impl<NodeId> UnwindSafe for MDTree<NodeId>
where NodeId: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more