AdjacencyMatrix

Struct AdjacencyMatrix 

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

A dense graph representation backed by a 2-D adjacency matrix.

Edge existence and weight are stored in a Vec<Vec<Option<W>>> matrix where matrix[i][j] is Some(weight) if there is an edge from i to j, or None otherwise.

§When to prefer this over AdjacencyList

PropertyAdjacencyListAdjacencyMatrix
SpaceO(V + E)O(V²)
add_edgeO(1)O(1)
contains_edgeO(deg)O(1)
neighborsO(deg)O(V)

Use AdjacencyMatrix for dense graphs (many edges relative to V²) or when O(1) edge lookup is critical (e.g. Floyd-Warshall).

§Examples

use graph_core::{AdjacencyMatrix, Graph};

let mut g: AdjacencyMatrix<&str, u32> = AdjacencyMatrix::directed();
let a = g.add_node("A");
let b = g.add_node("B");
g.add_edge(a, b, 5).unwrap();

assert!(g.contains_edge(a, b));
assert!(!g.contains_edge(b, a));

Implementations§

Source§

impl<N, W> AdjacencyMatrix<N, W>

Source

pub fn directed() -> AdjacencyMatrix<N, W>

Creates an empty directed adjacency-matrix graph.

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

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

pub fn undirected() -> AdjacencyMatrix<N, W>

Creates an empty undirected adjacency-matrix graph.

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

let mut g: AdjacencyMatrix<()> = AdjacencyMatrix::undirected();
let u = g.add_node(());
let v = g.add_node(());
g.add_edge(u, v, 1.0).unwrap();
assert!(g.contains_edge(v, u)); // symmetric
Source

pub fn is_directed(&self) -> bool

Returns true if this graph is directed.

Source

pub fn edge_weight(&self, from: NodeId, to: NodeId) -> Option<&W>

Returns a reference to the weight of edge from → to, or None if no such edge exists.

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

let mut g: AdjacencyMatrix<()> = AdjacencyMatrix::directed();
let a = g.add_node(());
let b = g.add_node(());
g.add_edge(a, b, 3.0).unwrap();
assert_eq!(g.edge_weight(a, b), Some(&3.0));
assert_eq!(g.edge_weight(b, a), None);

Trait Implementations§

Source§

impl<N, W> Clone for AdjacencyMatrix<N, W>
where N: Clone, W: Clone,

Source§

fn clone(&self) -> AdjacencyMatrix<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, W> Debug for AdjacencyMatrix<N, W>
where N: Debug, W: Debug,

Source§

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

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

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

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 AdjacencyMatrix<N, W>: 'a

Iterator over all NodeIds in the graph.
Source§

type NeighborIter<'a> = NeighborIter<'a, W> where AdjacencyMatrix<N, W>: '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 AdjacencyMatrix<N, W>

§

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

§

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

§

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

§

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

§

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