Skip to main content

Graph

Struct Graph 

Source
pub struct Graph<N, W> {
    pub nodes: Vec<N>,
    pub edges: Vec<Edge<W>>,
    pub directedness: Directedness,
}
Expand description

A weighted graph over node labels N and edge weights W.

Algorithm-level node identity is the usize index into nodes; labels are payload and never affect correctness. Edge ids are stable. Multiedges and self-loops are representable. Undirected graphs store one record per edge; Graph::neighbors expands both directions.

§Examples

Build a small directed graph and read a node’s outgoing adjacencies:

use sim_lib_discrete_graph::{Directedness, Graph};

let mut g: Graph<&str, u64> = Graph::new(Directedness::Directed);
let a = g.add_node("a");
let b = g.add_node("b");
let c = g.add_node("c");
g.add_edge(a, b, 1).unwrap();
g.add_edge(a, c, 2).unwrap();

assert_eq!(g.node_count(), 3);
assert_eq!(g.edge_count(), 2);

let neighbors: Vec<usize> = g.neighbors(a).unwrap().iter().map(|n| n.node).collect();
assert_eq!(neighbors, vec![b, c]);

Fields§

§nodes: Vec<N>

Node labels, indexed by node id.

§edges: Vec<Edge<W>>

Edge records.

§directedness: Directedness

Whether edges are directed.

Implementations§

Source§

impl<N, W> Graph<N, W>

Source

pub fn new(directedness: Directedness) -> Self

An empty graph with the given directedness.

Source

pub fn with_nodes(nodes: Vec<N>, directedness: Directedness) -> Self

A graph seeded with nodes and no edges.

Source

pub fn node_count(&self) -> usize

Number of nodes.

Source

pub fn edge_count(&self) -> usize

Number of edge records (one per undirected edge).

Source

pub fn is_directed(&self) -> bool

Whether the graph is directed.

Source

pub fn add_node(&mut self, label: N) -> usize

Append a node label, returning its index.

Source

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

Append an edge, validating endpoints and assigning a fresh id.

Source

pub fn validate(&self) -> Result<(), GraphError>

Validate that edge ids match storage order and every endpoint is in range.

Public fields keep the value easy to encode, but id-indexed consumers require edges[i].id == i. Sparse, duplicate, or shuffled ids are rejected before bridge and certificate code indexes by edge id.

Source

pub fn neighbors(&self, node: usize) -> Result<Vec<Neighbor<'_, W>>, GraphError>

Outgoing adjacencies of node, in deterministic ascending neighbor order (ties broken by edge id). For undirected graphs both directions expand.

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 (const: unstable) · 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: PartialEq, W: PartialEq> PartialEq for Graph<N, W>

Source§

fn eq(&self, other: &Graph<N, W>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<N: PartialEq, W: PartialEq> StructuralPartialEq for Graph<N, W>

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

§

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