pub struct Graph<N, W> { /* private fields */ }Expand description
A graph data structure supporting both directed and undirected graphs
Type parameters:
N: Node data typeW: Edge weight type
Implementations§
Source§impl<N, W> Graph<N, W>
impl<N, W> Graph<N, W>
Sourcepub fn graph_type(&self) -> GraphType
pub fn graph_type(&self) -> GraphType
Returns the type of the graph
Sourcepub fn node_count(&self) -> usize
pub fn node_count(&self) -> usize
Returns the number of nodes in the graph
Sourcepub fn edge_count(&self) -> usize
pub fn edge_count(&self) -> usize
Returns the number of edges in the graph
Sourcepub fn remove_node(&mut self, node_id: NodeId) -> Result<N, GraphError>
pub fn remove_node(&mut self, node_id: NodeId) -> Result<N, GraphError>
Removes a node and all its connected edges from the graph
Sourcepub fn get_node_mut(&mut self, node_id: NodeId) -> Option<&mut Node<N>>
pub fn get_node_mut(&mut self, node_id: NodeId) -> Option<&mut Node<N>>
Gets a mutable reference to a node by ID
Sourcepub fn add_edge(
&mut self,
source: NodeId,
target: NodeId,
weight: Option<W>,
) -> Result<EdgeId, GraphError>
pub fn add_edge( &mut self, source: NodeId, target: NodeId, weight: Option<W>, ) -> Result<EdgeId, GraphError>
Adds an edge between two nodes
Sourcepub fn remove_edge(&mut self, edge_id: EdgeId) -> Result<Edge<W>, GraphError>
pub fn remove_edge(&mut self, edge_id: EdgeId) -> Result<Edge<W>, GraphError>
Removes an edge from the graph
Sourcepub fn get_edge_mut(&mut self, edge_id: EdgeId) -> Option<&mut Edge<W>>
pub fn get_edge_mut(&mut self, edge_id: EdgeId) -> Option<&mut Edge<W>>
Gets a mutable reference to an edge by ID
Sourcepub fn nodes(&self) -> impl Iterator<Item = (&NodeId, &Node<N>)>
pub fn nodes(&self) -> impl Iterator<Item = (&NodeId, &Node<N>)>
Returns an iterator over all nodes
Sourcepub fn edges(&self) -> impl Iterator<Item = (&EdgeId, &Edge<W>)>
pub fn edges(&self) -> impl Iterator<Item = (&EdgeId, &Edge<W>)>
Returns an iterator over all edges
Sourcepub fn node_ids(&self) -> impl Iterator<Item = NodeId> + '_
pub fn node_ids(&self) -> impl Iterator<Item = NodeId> + '_
Returns an iterator over all node IDs
Sourcepub fn edge_ids(&self) -> impl Iterator<Item = EdgeId> + '_
pub fn edge_ids(&self) -> impl Iterator<Item = EdgeId> + '_
Returns an iterator over all edge IDs
Sourcepub fn neighbors(&self, node_id: NodeId) -> Option<Vec<NodeId>>
pub fn neighbors(&self, node_id: NodeId) -> Option<Vec<NodeId>>
Returns the neighbors of a node (outgoing edges for directed graphs)
Sourcepub fn predecessors(&self, node_id: NodeId) -> Option<Vec<NodeId>>
pub fn predecessors(&self, node_id: NodeId) -> Option<Vec<NodeId>>
Returns the predecessors of a node (incoming edges for directed graphs)
Sourcepub fn edges_of(&self, node_id: NodeId) -> Option<Vec<EdgeId>>
pub fn edges_of(&self, node_id: NodeId) -> Option<Vec<EdgeId>>
Returns the edges connected to a node
Sourcepub fn has_edge(&self, source: NodeId, target: NodeId) -> bool
pub fn has_edge(&self, source: NodeId, target: NodeId) -> bool
Checks if an edge exists between two nodes
Sourcepub fn get_edge_between(
&self,
source: NodeId,
target: NodeId,
) -> Option<&Edge<W>>
pub fn get_edge_between( &self, source: NodeId, target: NodeId, ) -> Option<&Edge<W>>
Gets the edge between two nodes if it exists
Sourcepub fn in_degree(&self, node_id: NodeId) -> Option<usize>
pub fn in_degree(&self, node_id: NodeId) -> Option<usize>
Returns the in-degree of a node (for directed graphs)
Sourcepub fn out_degree(&self, node_id: NodeId) -> Option<usize>
pub fn out_degree(&self, node_id: NodeId) -> Option<usize>
Returns the out-degree of a node (for directed graphs)
Sourcepub fn subgraph(&self, node_ids: &[NodeId]) -> Graph<N, W>
pub fn subgraph(&self, node_ids: &[NodeId]) -> Graph<N, W>
Creates a subgraph containing only the specified nodes and their connecting edges
Sourcepub fn reverse(&self) -> Graph<N, W>
pub fn reverse(&self) -> Graph<N, W>
Returns a reversed version of the graph (only meaningful for directed graphs)
Sourcepub fn to_undirected(&self) -> Graph<N, W>
pub fn to_undirected(&self) -> Graph<N, W>
Converts a directed graph to an undirected graph
Trait Implementations§
Auto Trait Implementations§
impl<N, W> Freeze for Graph<N, W>
impl<N, W> RefUnwindSafe for Graph<N, W>where
N: RefUnwindSafe,
W: RefUnwindSafe,
impl<N, W> Send for Graph<N, W>
impl<N, W> Sync for Graph<N, W>
impl<N, W> Unpin for Graph<N, W>
impl<N, W> UnwindSafe for Graph<N, W>where
N: UnwindSafe,
W: UnwindSafe,
Blanket Implementations§
Source§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,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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