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
| Property | AdjacencyList | AdjacencyMatrix |
|---|---|---|
| Space | O(V + E) | O(V²) |
add_edge | O(1) | O(1) |
contains_edge | O(deg) | O(1) |
neighbors | O(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>
impl<N, W> AdjacencyMatrix<N, W>
Sourcepub fn directed() -> Self
pub fn directed() -> Self
Creates an empty directed adjacency-matrix graph.
§Examples
use graph_core::{AdjacencyMatrix, Graph};
let g: AdjacencyMatrix<()> = AdjacencyMatrix::directed();
assert!(g.is_empty());Sourcepub fn undirected() -> Self
pub fn undirected() -> Self
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)); // symmetricSourcepub fn is_directed(&self) -> bool
pub fn is_directed(&self) -> bool
Returns true if this graph is directed.
Sourcepub fn edge_weight(&self, from: NodeId, to: NodeId) -> Option<&W>
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: Clone, W: Clone> Clone for AdjacencyMatrix<N, W>
impl<N: Clone, W: Clone> Clone for AdjacencyMatrix<N, W>
Source§fn clone(&self) -> AdjacencyMatrix<N, W>
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)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl<N, W: Clone> Graph for AdjacencyMatrix<N, W>
impl<N, W: Clone> Graph for AdjacencyMatrix<N, W>
Source§type NeighborIter<'a> = NeighborIter<'a, W>
where
Self: 'a
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
fn add_node(&mut self, data: N) -> NodeId
Adds a node with the given data and returns its
NodeId.Source§fn remove_node(&mut self, id: NodeId) -> Option<N>
fn remove_node(&mut self, id: NodeId) -> Option<N>
Removes the node at
id and all edges incident to it. Read moreSource§fn node_count(&self) -> usize
fn node_count(&self) -> usize
Returns the number of nodes.
Source§fn edge_count(&self) -> usize
fn edge_count(&self) -> usize
Returns the number of directed edges.
Source§fn contains_node(&self, id: NodeId) -> bool
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
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
fn degree(&self, id: NodeId) -> usize
Returns the out-degree of node
id (number of outgoing edges). Read moreAuto Trait Implementations§
impl<N, W> Freeze for AdjacencyMatrix<N, W>
impl<N, W> RefUnwindSafe for AdjacencyMatrix<N, W>where
N: RefUnwindSafe,
W: RefUnwindSafe,
impl<N, W> Send for AdjacencyMatrix<N, W>
impl<N, W> Sync for AdjacencyMatrix<N, W>
impl<N, W> Unpin for AdjacencyMatrix<N, W>
impl<N, W> UnwindSafe for AdjacencyMatrix<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
Mutably borrows from an owned value. Read more