Struct modular_decomposition::MDTree

source ·
pub struct MDTree<NodeId: Copy + PartialEq> { /* private fields */ }
Expand description

A modular decomposition tree. The tree contains at least one node.

Implementations§

source§

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

source

pub fn node_count(&self) -> usize

Return the number of nodes in the modular decomposition tree.

source

pub fn root(&self) -> ModuleIndex

Return the root node 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 nodes.

source

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

Return an iterator for the children of a node.

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)
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
fn main() {
    let graph = Graph::from_edges([(0, 1), (1, 2), (2, 3)]);
    let tree = modular_decomposition(&graph).map(|tree| tree.into_digraph()).unwrap_or_default();
    println!("{:?}", Dot::with_config(&tree, &[EdgeNoLabel]));

    let mut factorizing_permutation = Vec::new();
    let root = tree.externals(Incoming).next().unwrap();
    let mut dfs = Dfs::new(&tree, root);
    while let Some(node) = dfs.next(&tree) {
        if let ModuleKind::Node(u) = tree[node] {
            factorizing_permutation.push(u);
        }
    }
    let factorizing_permutation: Vec<_> = factorizing_permutation.to_vec();
    println!("{:?}", factorizing_permutation);
}

Trait Implementations§

source§

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

source§

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

Returns a copy 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> 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,

§

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

§

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

§

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