Skip to main content

NamedGraph

Struct NamedGraph 

Source
pub struct NamedGraph<NodeName, NodeData, EdgeData, Ty = Undirected>
where NodeName: Clone + Hash + Eq + Send + Sync + Debug, Ty: EdgeType,
{ /* 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 type
  • EdgeData: Edge weight/data type
  • Ty: Edge type (Directed or Undirected, default: Undirected)

Implementations§

Source§

impl<NodeName, NodeData, EdgeData, Ty> NamedGraph<NodeName, NodeData, EdgeData, Ty>
where NodeName: Clone + Hash + Eq + Send + Sync + Debug, Ty: EdgeType,

Source

pub fn new() -> Self

Create a new empty NamedGraph.

Source

pub fn with_capacity(nodes: usize, edges: usize) -> Self

Create a new NamedGraph with initial capacity.

Source

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.

Source

pub fn has_node(&self, node_name: &NodeName) -> bool

Check if a node exists.

Source

pub fn node_index(&self, node_name: &NodeName) -> Option<NodeIndex>

Get the NodeIndex for a node name.

Source

pub fn node_name(&self, node: NodeIndex) -> Option<&NodeName>

Get the node name for a NodeIndex.

Source

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.

Source

pub fn node_data(&self, node_name: &NodeName) -> Option<&NodeData>

Get a reference to the data of a node (by node name).

Source

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).

Source

pub fn node_weight(&self, node: NodeIndex) -> Option<&NodeData>

Get a reference to the data of a node (by NodeIndex).

Source

pub fn node_weight_mut(&mut self, node: NodeIndex) -> Option<&mut NodeData>

Get a mutable reference to the data of a node (by NodeIndex).

Source

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.

Source

pub fn edge_weight(&self, n1: &NodeName, n2: &NodeName) -> Option<&EdgeData>

Get the weight of an edge between two nodes.

Source

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.

Source

pub fn neighbors(&self, node_name: &NodeName) -> Vec<&NodeName>

Get all neighbors of a node.

Source

pub fn node_names(&self) -> Vec<&NodeName>

Get all node names.

Source

pub fn node_count(&self) -> usize

Get the number of nodes.

Source

pub fn edge_count(&self) -> usize

Get the number of edges.

Source

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.

Source

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.

Source

pub fn contains_node(&self, node: NodeIndex) -> bool

Check if a node exists in the internal graph.

Source

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.

Source

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.

Source

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:

  1. For each unvisited neighbor, record the edge and recurse
  2. 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 - D

Starting from C, the tour might be: [(C,A), (A,C), (C,B), (B,C), (C,D), (D,C)]

Source

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.

Source

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

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>
where NodeName: Clone + Hash + Eq + Send + Sync + Debug, NodeData: Clone, EdgeData: Clone, Ty: EdgeType,

Source§

fn clone(&self) -> Self

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<NodeName, NodeData, EdgeData, Ty> Debug for NamedGraph<NodeName, NodeData, EdgeData, Ty>
where NodeName: Clone + Hash + Eq + Send + Sync + Debug, NodeData: Debug, EdgeData: Debug, Ty: EdgeType,

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<NodeName, NodeData, EdgeData, Ty> Default for NamedGraph<NodeName, NodeData, EdgeData, Ty>
where NodeName: Clone + Hash + Eq + Send + Sync + Debug, Ty: EdgeType,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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>
where Ty: RefUnwindSafe, NodeName: RefUnwindSafe, NodeData: RefUnwindSafe, EdgeData: RefUnwindSafe,

§

impl<NodeName, NodeData, EdgeData, Ty> Send for NamedGraph<NodeName, NodeData, EdgeData, Ty>
where Ty: Send, NodeData: Send, EdgeData: Send,

§

impl<NodeName, NodeData, EdgeData, Ty> Sync for NamedGraph<NodeName, NodeData, EdgeData, Ty>
where Ty: Sync, NodeData: Sync, EdgeData: Sync,

§

impl<NodeName, NodeData, EdgeData, Ty> Unpin for NamedGraph<NodeName, NodeData, EdgeData, Ty>
where Ty: Unpin, NodeName: Unpin, NodeData: Unpin, EdgeData: Unpin,

§

impl<NodeName, NodeData, EdgeData, Ty> UnsafeUnpin for NamedGraph<NodeName, NodeData, EdgeData, Ty>

§

impl<NodeName, NodeData, EdgeData, Ty> UnwindSafe for NamedGraph<NodeName, NodeData, EdgeData, Ty>
where NodeName: UnwindSafe, Ty: UnwindSafe, NodeData: UnwindSafe, EdgeData: 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
§

impl<U> As for U

§

fn as_<T>(self) -> T
where T: CastFrom<U>,

Casts 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 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
§

impl<T> ByRef<T> for T

§

fn by_ref(&self) -> &T

§

impl<T> ByRef<T> for T

§

fn by_ref(&self) -> &T

§

impl<T> ByRef<T> for T

§

fn by_ref(&self) -> &T

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
§

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

§

fn rand<T>(&self, rng: &mut (impl Rng + ?Sized)) -> T
where Self: Distribution<T>,

§

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

§

fn rand<T>(&self, rng: &mut (impl Rng + ?Sized)) -> T
where Self: Distribution<T>,

§

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

§

fn rand<T>(&self, rng: &mut (impl Rng + ?Sized)) -> T
where Self: Distribution<T>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T, U> Imply<T> for U
where T: ?Sized, U: ?Sized,

§

impl<T> MaybeSend for T

§

impl<T> MaybeSendSync for T

§

impl<T> MaybeSync for T