Graph

Struct Graph 

Source
pub struct Graph<N, W> { /* private fields */ }
Expand description

A graph data structure supporting both directed and undirected graphs

Type parameters:

  • N: Node data type
  • W: Edge weight type

Implementations§

Source§

impl<N, W> Graph<N, W>
where N: Clone + Debug, W: Clone + Debug,

Source

pub fn new(graph_type: GraphType) -> Self

Creates a new empty graph

Source

pub fn graph_type(&self) -> GraphType

Returns the type of the graph

Source

pub fn node_count(&self) -> usize

Returns the number of nodes in the graph

Source

pub fn edge_count(&self) -> usize

Returns the number of edges in the graph

Source

pub fn is_empty(&self) -> bool

Checks if the graph is empty

Source

pub fn add_node(&mut self, data: N) -> NodeId

Adds a node to the graph and returns its ID

Source

pub fn remove_node(&mut self, node_id: NodeId) -> Result<N, GraphError>

Removes a node and all its connected edges from the graph

Source

pub fn get_node(&self, node_id: NodeId) -> Option<&Node<N>>

Gets a reference to a node by ID

Source

pub fn get_node_mut(&mut self, node_id: NodeId) -> Option<&mut Node<N>>

Gets a mutable reference to a node by ID

Source

pub fn add_edge( &mut self, source: NodeId, target: NodeId, weight: Option<W>, ) -> Result<EdgeId, GraphError>

Adds an edge between two nodes

Source

pub fn remove_edge(&mut self, edge_id: EdgeId) -> Result<Edge<W>, GraphError>

Removes an edge from the graph

Source

pub fn get_edge(&self, edge_id: EdgeId) -> Option<&Edge<W>>

Gets a reference to an edge by ID

Source

pub fn get_edge_mut(&mut self, edge_id: EdgeId) -> Option<&mut Edge<W>>

Gets a mutable reference to an edge by ID

Source

pub fn nodes(&self) -> impl Iterator<Item = (&NodeId, &Node<N>)>

Returns an iterator over all nodes

Source

pub fn edges(&self) -> impl Iterator<Item = (&EdgeId, &Edge<W>)>

Returns an iterator over all edges

Source

pub fn node_ids(&self) -> impl Iterator<Item = NodeId> + '_

Returns an iterator over all node IDs

Source

pub fn edge_ids(&self) -> impl Iterator<Item = EdgeId> + '_

Returns an iterator over all edge IDs

Source

pub fn neighbors(&self, node_id: NodeId) -> Option<Vec<NodeId>>

Returns the neighbors of a node (outgoing edges for directed graphs)

Source

pub fn predecessors(&self, node_id: NodeId) -> Option<Vec<NodeId>>

Returns the predecessors of a node (incoming edges for directed graphs)

Source

pub fn edges_of(&self, node_id: NodeId) -> Option<Vec<EdgeId>>

Returns the edges connected to a node

Source

pub fn has_edge(&self, source: NodeId, target: NodeId) -> bool

Checks if an edge exists between two nodes

Source

pub fn get_edge_between( &self, source: NodeId, target: NodeId, ) -> Option<&Edge<W>>

Gets the edge between two nodes if it exists

Source

pub fn degree(&self, node_id: NodeId) -> Option<usize>

Returns the degree of a node

Source

pub fn in_degree(&self, node_id: NodeId) -> Option<usize>

Returns the in-degree of a node (for directed graphs)

Source

pub fn out_degree(&self, node_id: NodeId) -> Option<usize>

Returns the out-degree of a node (for directed graphs)

Source

pub fn subgraph(&self, node_ids: &[NodeId]) -> Graph<N, W>

Creates a subgraph containing only the specified nodes and their connecting edges

Source

pub fn reverse(&self) -> Graph<N, W>

Returns a reversed version of the graph (only meaningful for directed graphs)

Source

pub fn to_undirected(&self) -> Graph<N, W>

Converts a directed graph to an undirected graph

Source

pub fn clear(&mut self)

Clears all nodes and edges from the graph

Trait Implementations§

Source§

impl<N: Clone, W: Clone> Clone for Graph<N, W>

Source§

fn clone(&self) -> Graph<N, W>

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<N: Debug, W: Debug> Debug for Graph<N, W>

Source§

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

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

impl<N, W> Default for Graph<N, W>
where N: Clone + Debug, W: Clone + Debug,

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<N, W> Freeze for Graph<N, W>

§

impl<N, W> RefUnwindSafe for Graph<N, W>

§

impl<N, W> Send for Graph<N, W>
where N: Send, W: Send,

§

impl<N, W> Sync for Graph<N, W>
where N: Sync, W: Sync,

§

impl<N, W> Unpin for Graph<N, W>
where N: Unpin, W: Unpin,

§

impl<N, W> UnwindSafe for Graph<N, W>
where N: UnwindSafe, W: 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> 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
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> 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

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

Source§

fn vzip(self) -> V

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

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,