AdjacencyList

Struct AdjacencyList 

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

A sparse graph representation backed by an adjacency list.

Node data is stored in a Vec<N>. Outgoing edges for each node are stored in a Vec<Vec<(NodeId, W)>> indexed by node position.

§Directed vs Undirected

Use AdjacencyList::directed or AdjacencyList::undirected. For an undirected graph, add_edge(u, v, w) inserts both v into u’s list and u into v’s list automatically.

§Complexity

OperationTime
add_nodeO(1) amortised
add_edgeO(1) amortised
neighborsO(out-degree)
contains_edgeO(out-degree)
node_countO(1)
edge_countO(1)

§Examples

use graph_core::{AdjacencyList, Graph};

let mut g: AdjacencyList<&str, f64> = AdjacencyList::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.0).unwrap();
g.add_edge(b, c, 2.0).unwrap();

assert_eq!(g.node_count(), 3);
assert_eq!(g.edge_count(), 2);
assert!(g.contains_edge(a, b));
assert!(!g.contains_edge(c, a));

Implementations§

Source§

impl<N, W> AdjacencyList<N, W>

Source

pub fn directed() -> Self

Creates an empty directed adjacency-list graph.

§Examples
use graph_core::{AdjacencyList, Graph};

let g: AdjacencyList<()> = AdjacencyList::directed();
assert!(g.is_empty());
Source

pub fn undirected() -> Self

Creates an empty undirected adjacency-list graph.

§Examples
use graph_core::{AdjacencyList, Graph};

let mut g: AdjacencyList<()> = AdjacencyList::undirected();
let u = g.add_node(());
let v = g.add_node(());
g.add_edge(u, v, 1.0).unwrap();

// Both directions exist.
assert!(g.contains_edge(u, v));
assert!(g.contains_edge(v, u));
Source

pub fn is_directed(&self) -> bool

Returns true if this graph is directed.

§Examples
use graph_core::AdjacencyList;

let d: AdjacencyList<()> = AdjacencyList::directed();
assert!(d.is_directed());

let u: AdjacencyList<()> = AdjacencyList::undirected();
assert!(!u.is_directed());
Source§

impl<N, W> AdjacencyList<N, W>

Source

pub fn node_data(&self, id: NodeId) -> Option<&N>

Returns a reference to the data stored at id, or None if not found.

§Examples
use graph_core::{AdjacencyList, Graph};

let mut g: AdjacencyList<&str> = AdjacencyList::directed();
let id = g.add_node("hello");
assert_eq!(g.node_data(id), Some(&"hello"));
Source

pub fn node_data_mut(&mut self, id: NodeId) -> Option<&mut N>

Returns a mutable reference to the data stored at id, or None if not found.

§Examples
use graph_core::{AdjacencyList, Graph};

let mut g: AdjacencyList<u32> = AdjacencyList::directed();
let id = g.add_node(1);
*g.node_data_mut(id).unwrap() = 42;
assert_eq!(g.node_data(id), Some(&42));

Trait Implementations§

Source§

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

Source§

fn clone(&self) -> AdjacencyList<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 AdjacencyList<N, W>

Source§

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

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

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

Source§

type NodeData = N

Data stored at each node (e.g. String, (), coordinates).
Source§

type Weight = W

Edge-weight type (e.g. f64, u32, ()).
Source§

type NodeIter<'a> = NodeIter where Self: 'a

Iterator over all NodeIds in the graph.
Source§

type NeighborIter<'a> = NeighborIter<'a, W> where Self: 'a

Iterator over (neighbour_id, &weight) pairs for one node.
Source§

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

Adds a node with the given data and returns its NodeId.
Source§

fn add_edge( &mut self, from: NodeId, to: NodeId, weight: W, ) -> Result<(), GraphError>

Adds a directed edge from from to to with weight. Read more
Source§

fn remove_node(&mut self, id: NodeId) -> Option<N>

Removes the node at id and all edges incident to it. Read more
Source§

fn node_count(&self) -> usize

Returns the number of nodes.
Source§

fn edge_count(&self) -> usize

Returns the number of directed edges.
Source§

fn contains_node(&self, id: NodeId) -> bool

Returns true if the graph contains a node with this id.
Source§

fn contains_edge(&self, from: NodeId, to: NodeId) -> bool

Returns true if there is a directed edge from from to to.
Source§

fn degree(&self, id: NodeId) -> usize

Returns the out-degree of node id (number of outgoing edges). Read more
Source§

fn nodes(&self) -> NodeIter

Returns an iterator over all NodeIds in the graph.
Source§

fn neighbors(&self, id: NodeId) -> NeighborIter<'_, W>

Returns an iterator over (neighbour_id, &weight) for all outgoing edges of id. Read more
Source§

fn all_edges(&self) -> Vec<Edge<Self::Weight>>
where Self::Weight: Clone,

Returns all edges in the graph as Edge<W> values. Read more
Source§

fn is_empty(&self) -> bool

Returns true if the graph has no nodes. Read more

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<N, W> UnwindSafe for AdjacencyList<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.