Trait ContractNodesUndirected

Source
pub trait ContractNodesUndirected: Data {
    // Required method
    fn contract_nodes<I>(
        &mut self,
        nodes: I,
        weight: Self::NodeWeight,
    ) -> Self::NodeId
       where I: IntoIterator<Item = Self::NodeId>;
}

Required Methods§

Source

fn contract_nodes<I>( &mut self, nodes: I, weight: Self::NodeWeight, ) -> Self::NodeId
where I: IntoIterator<Item = Self::NodeId>,

Substitute a set of nodes with a single new node.

The specified nodes are removed and replaced with a new node with the given weight. Any nodes not in the graph are ignored. It is valid for nodes to be empty, in which case the new node is added to the graph without edges.

The contraction may result in multiple edges between nodes if the underlying graph is a multi-graph. If this is not desired, use ContractNodesSimpleUndirected::contract_nodes_simple.

The NodeId of the newly created node is returned.

§Example
use petgraph::prelude::*;
use rustworkx_core::graph_ext::*;

// Performs the following transformation:
//      ┌─┐
//      │a│
//      └┬┘              ┌─┐
//       0               │a│
//      ┌┴┐              └┬┘
//      │b│               0
//      └┬┘              ┌┴┐
//       1      ───►     │m│
//      ┌┴┐              └┬┘
//      │c│               2
//      └┬┘              ┌┴┐
//       2               │d│
//      ┌┴┐              └─┘
//      │d│
//      └─┘
let mut dag: StableUnGraph<char, usize> = StableUnGraph::default();
let a = dag.add_node('a');
let b = dag.add_node('b');
let c = dag.add_node('c');
let d = dag.add_node('d');
dag.add_edge(a.clone(), b.clone(), 0);
dag.add_edge(b.clone(), c.clone(), 1);
dag.add_edge(c.clone(), d.clone(), 2);

let m = dag.contract_nodes([b, c], 'm');
assert_eq!(dag.edge_weight(dag.find_edge(a.clone(), m.clone()).unwrap()).unwrap(), &0);
assert_eq!(dag.edge_weight(dag.find_edge(m.clone(), d.clone()).unwrap()).unwrap(), &2);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<N, E> ContractNodesUndirected for GraphMap<N, E, Undirected>
where for<'a> N: NodeTrait + 'a, for<'a> E: Clone + 'a,

Source§

fn contract_nodes<I>(&mut self, nodes: I, obj: Self::NodeWeight) -> Self::NodeId
where I: IntoIterator<Item = Self::NodeId>,

Source§

impl<N, E, Ix> ContractNodesUndirected for StableGraph<N, E, Undirected, Ix>
where Ix: IndexType, E: Clone,

Source§

fn contract_nodes<I>(&mut self, nodes: I, obj: Self::NodeWeight) -> Self::NodeId
where I: IntoIterator<Item = Self::NodeId>,

Implementors§