pub struct NamedGraph<NodeName, NodeData, EdgeData, Ty = Undirected>{ /* private fields */ }Expand description
Generic named graph wrapper (inspired by NamedGraphs.jl)
Provides a mapping between arbitrary node name types (NodeName) and internal NodeIndex. This allows using meaningful identifiers (coordinates, strings, etc.) instead of raw indices.
§Type Parameters
NodeName: Node name type (must be Clone, Hash, Eq, Send, Sync, Debug)NodeData: Node weight/data typeEdgeData: Edge weight/data typeTy: Edge type (Directed or Undirected, default: Undirected)
Implementations§
Source§impl<NodeName, NodeData, EdgeData, Ty> NamedGraph<NodeName, NodeData, EdgeData, Ty>
impl<NodeName, NodeData, EdgeData, Ty> NamedGraph<NodeName, NodeData, EdgeData, Ty>
Sourcepub fn with_capacity(nodes: usize, edges: usize) -> Self
pub fn with_capacity(nodes: usize, edges: usize) -> Self
Create a new NamedGraph with initial capacity.
Sourcepub fn add_node(
&mut self,
node_name: NodeName,
data: NodeData,
) -> Result<NodeIndex, String>
pub fn add_node( &mut self, node_name: NodeName, data: NodeData, ) -> Result<NodeIndex, String>
Add a node with the given name and data.
Returns an error if the node already exists.
Sourcepub fn node_index(&self, node_name: &NodeName) -> Option<NodeIndex>
pub fn node_index(&self, node_name: &NodeName) -> Option<NodeIndex>
Get the NodeIndex for a node name.
Sourcepub fn node_name(&self, node: NodeIndex) -> Option<&NodeName>
pub fn node_name(&self, node: NodeIndex) -> Option<&NodeName>
Get the node name for a NodeIndex.
Sourcepub fn rename_node(
&mut self,
old_name: &NodeName,
new_name: NodeName,
) -> Result<(), String>
pub fn rename_node( &mut self, old_name: &NodeName, new_name: NodeName, ) -> Result<(), String>
Rename an existing node without changing its data or incident edges.
Returns an error if the old node doesn’t exist or the new name is already in use.
Sourcepub fn node_data(&self, node_name: &NodeName) -> Option<&NodeData>
pub fn node_data(&self, node_name: &NodeName) -> Option<&NodeData>
Get a reference to the data of a node (by node name).
Sourcepub fn node_data_mut(&mut self, node_name: &NodeName) -> Option<&mut NodeData>
pub fn node_data_mut(&mut self, node_name: &NodeName) -> Option<&mut NodeData>
Get a mutable reference to the data of a node (by node name).
Sourcepub fn node_weight(&self, node: NodeIndex) -> Option<&NodeData>
pub fn node_weight(&self, node: NodeIndex) -> Option<&NodeData>
Get a reference to the data of a node (by NodeIndex).
Sourcepub fn node_weight_mut(&mut self, node: NodeIndex) -> Option<&mut NodeData>
pub fn node_weight_mut(&mut self, node: NodeIndex) -> Option<&mut NodeData>
Get a mutable reference to the data of a node (by NodeIndex).
Sourcepub fn add_edge(
&mut self,
n1: &NodeName,
n2: &NodeName,
weight: EdgeData,
) -> Result<EdgeIndex, String>where
EdgeData: Clone,
pub fn add_edge(
&mut self,
n1: &NodeName,
n2: &NodeName,
weight: EdgeData,
) -> Result<EdgeIndex, String>where
EdgeData: Clone,
Add an edge between two nodes.
Returns an error if either node doesn’t exist.
Sourcepub fn edge_weight(&self, n1: &NodeName, n2: &NodeName) -> Option<&EdgeData>
pub fn edge_weight(&self, n1: &NodeName, n2: &NodeName) -> Option<&EdgeData>
Get the weight of an edge between two nodes.
Sourcepub fn edge_weight_mut(
&mut self,
n1: &NodeName,
n2: &NodeName,
) -> Option<&mut EdgeData>
pub fn edge_weight_mut( &mut self, n1: &NodeName, n2: &NodeName, ) -> Option<&mut EdgeData>
Get a mutable reference to the weight of an edge between two nodes.
Sourcepub fn node_names(&self) -> Vec<&NodeName>
pub fn node_names(&self) -> Vec<&NodeName>
Get all node names.
Sourcepub fn node_count(&self) -> usize
pub fn node_count(&self) -> usize
Get the number of nodes.
Sourcepub fn edge_count(&self) -> usize
pub fn edge_count(&self) -> usize
Get the number of edges.
Sourcepub fn remove_node(&mut self, node_name: &NodeName) -> Option<NodeData>
pub fn remove_node(&mut self, node_name: &NodeName) -> Option<NodeData>
Remove a node and all its edges.
Returns the node data if the node existed.
Sourcepub fn remove_edge(&mut self, n1: &NodeName, n2: &NodeName) -> Option<EdgeData>
pub fn remove_edge(&mut self, n1: &NodeName, n2: &NodeName) -> Option<EdgeData>
Remove an edge between two nodes.
Returns the edge weight if the edge existed.
Sourcepub fn contains_node(&self, node: NodeIndex) -> bool
pub fn contains_node(&self, node: NodeIndex) -> bool
Check if a node exists in the internal graph.
Sourcepub fn graph(&self) -> &StableGraph<NodeData, EdgeData, Ty>
pub fn graph(&self) -> &StableGraph<NodeData, EdgeData, Ty>
Get a reference to the internal graph.
This allows direct access to petgraph algorithms that work with NodeIndex.
Sourcepub fn graph_mut(&mut self) -> &mut StableGraph<NodeData, EdgeData, Ty>
pub fn graph_mut(&mut self) -> &mut StableGraph<NodeData, EdgeData, Ty>
Get a mutable reference to the internal graph.
Warning: Directly modifying the internal graph can break the node-name-to-index mapping. Use the provided methods instead.
Sourcepub fn euler_tour_edges(
&self,
root: &NodeName,
) -> Option<Vec<(NodeIndex, NodeIndex)>>
pub fn euler_tour_edges( &self, root: &NodeName, ) -> Option<Vec<(NodeIndex, NodeIndex)>>
Perform an Euler tour traversal starting from the given root node.
The Euler tour visits each edge exactly twice (once in each direction), forming a closed walk that covers all edges. This is useful for sweep algorithms where we need to visit all nodes while maintaining the canonical center at a single point.
§Algorithm
Uses DFS with backtracking. When visiting a node:
- For each unvisited neighbor, record the edge and recurse
- When backtracking, record the edge back to the parent
§Returns
A vector of directed edges (from, to) in traversal order.
Each undirected edge appears twice: once as (u, v) and once as (v, u).
§Example
For a Y-shaped tree with root at center C:
A
|
B - C - DStarting from C, the tour might be:
[(C,A), (A,C), (C,B), (B,C), (C,D), (D,C)]
Sourcepub fn euler_tour_edges_by_index(
&self,
root: NodeIndex,
) -> Vec<(NodeIndex, NodeIndex)>
pub fn euler_tour_edges_by_index( &self, root: NodeIndex, ) -> Vec<(NodeIndex, NodeIndex)>
Perform an Euler tour traversal starting from the given root NodeIndex.
See Self::euler_tour_edges for details.
Sourcepub fn euler_tour_vertices(&self, root: &NodeName) -> Option<Vec<NodeIndex>>
pub fn euler_tour_vertices(&self, root: &NodeName) -> Option<Vec<NodeIndex>>
Perform an Euler tour traversal and return the vertex sequence.
This returns the sequence of vertices visited, including repeated visits. Each vertex appears multiple times based on its degree in the tree.
§Returns
A vector of NodeIndex in visitation order, or None if root doesn’t exist.
§Example
For a chain A - B - C starting from B:
- Edges:
[(B,A), (A,B), (B,C), (C,B)] - Vertices:
[B, A, B, C, B]
Sourcepub fn euler_tour_vertices_by_index(&self, root: NodeIndex) -> Vec<NodeIndex>
pub fn euler_tour_vertices_by_index(&self, root: NodeIndex) -> Vec<NodeIndex>
Perform an Euler tour traversal and return the vertex sequence by NodeIndex.
See Self::euler_tour_vertices for details.
Trait Implementations§
Source§impl<NodeName, NodeData, EdgeData, Ty> Clone for NamedGraph<NodeName, NodeData, EdgeData, Ty>
impl<NodeName, NodeData, EdgeData, Ty> Clone for NamedGraph<NodeName, NodeData, EdgeData, Ty>
Source§impl<NodeName, NodeData, EdgeData, Ty> Debug for NamedGraph<NodeName, NodeData, EdgeData, Ty>
impl<NodeName, NodeData, EdgeData, Ty> Debug for NamedGraph<NodeName, NodeData, EdgeData, Ty>
Auto Trait Implementations§
impl<NodeName, NodeData, EdgeData, Ty> Freeze for NamedGraph<NodeName, NodeData, EdgeData, Ty>
impl<NodeName, NodeData, EdgeData, Ty> RefUnwindSafe for NamedGraph<NodeName, NodeData, EdgeData, Ty>
impl<NodeName, NodeData, EdgeData, Ty> Send for NamedGraph<NodeName, NodeData, EdgeData, Ty>
impl<NodeName, NodeData, EdgeData, Ty> Sync for NamedGraph<NodeName, NodeData, EdgeData, Ty>
impl<NodeName, NodeData, EdgeData, Ty> Unpin for NamedGraph<NodeName, NodeData, EdgeData, Ty>
impl<NodeName, NodeData, EdgeData, Ty> UnsafeUnpin for NamedGraph<NodeName, NodeData, EdgeData, Ty>
impl<NodeName, NodeData, EdgeData, Ty> UnwindSafe for NamedGraph<NodeName, NodeData, EdgeData, Ty>
Blanket Implementations§
§impl<U> As for U
impl<U> As for U
§fn as_<T>(self) -> Twhere
T: CastFrom<U>,
fn as_<T>(self) -> Twhere
T: CastFrom<U>,
self to type T. The semantics of numeric casting with the as operator are followed, so <T as As>::as_::<U> can be used in the same way as T as U for numeric conversions. Read moreSource§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> DistributionExt for Twhere
T: ?Sized,
impl<T> DistributionExt for Twhere
T: ?Sized,
fn rand<T>(&self, rng: &mut (impl Rng + ?Sized)) -> Twhere
Self: Distribution<T>,
§impl<T> DistributionExt for Twhere
T: ?Sized,
impl<T> DistributionExt for Twhere
T: ?Sized,
fn rand<T>(&self, rng: &mut (impl Rng + ?Sized)) -> Twhere
Self: Distribution<T>,
§impl<T> DistributionExt for Twhere
T: ?Sized,
impl<T> DistributionExt for Twhere
T: ?Sized,
fn rand<T>(&self, rng: &mut (impl Rng + ?Sized)) -> Twhere
Self: Distribution<T>,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more