pub struct Graph<N, E, Ty = Directed> { /* private fields */ }
Expand description
Graph<N, E, Ty>
is a graph datastructure using an associative array
of its node weights N
.
It uses an combined adjacency list and sparse adjacency matrix representation, using O(|V| + |E|) space, and allows testing for edge existance in constant time.
§Graph
is parameterized over:
- Associated data
N
for nodes andE
for edges, called weights. - The node weight
N
must implementCopy
and will be used as node identifier, duplicated into several places in the data structure. It must be suitable as a hash table key (implementingEq + Hash
). The node type must also implementOrd
so that the implementation can order the pair (a
,b
) for an edge connecting any two nodesa
andb
. E
can be of arbitrary type.- Edge type
Ty
that determines whether the graph edges are directed or undirected.
You can use the type alias UndirectedGraph
for convenience.
Graph
does not allow parallel edges, but self loops are allowed.
Implementations§
Source§impl<N, E, Ty> Graph<N, E, Ty>
impl<N, E, Ty> Graph<N, E, Ty>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new Graph
instance.
§Examples
use safe_graph::Graph;
let graph: Graph<i32, f32> = Graph::new();
Sourcepub fn with_capacity(nodes: usize, edges: usize) -> Self
pub fn with_capacity(nodes: usize, edges: usize) -> Self
Create a new Graph
with estimated capacity.
Sourcepub fn capacity(&self) -> (usize, usize)
pub fn capacity(&self) -> (usize, usize)
Return the current node and edge capacity of the graph.
Sourcepub fn edge_key(a: N, b: N) -> (N, N)
pub fn edge_key(a: N, b: N) -> (N, N)
Use their natural order to map the node pair (a, b) to a canonical edge id.
Sourcepub fn is_directed(&self) -> bool
pub fn is_directed(&self) -> bool
Whether the graph has directed edges.
Sourcepub fn from_edges<I>(iterable: I) -> Self
pub fn from_edges<I>(iterable: I) -> Self
Create a new Graph
from an iterable of edges.
Node values are taken directly from the list.
Edge weights E
may either be specified in the list,
or they are filled with default values.
Nodes are inserted automatically to match the edges.
§Examples
use safe_graph::Graph;
// Create a new directed Graph.
// Use a type hint to have `()` be the edge weight type.
let gr = Graph::<_, ()>::from_edges(&[
(0, 1), (0, 2), (0, 3),
(1, 2), (1, 3),
(2, 3),
]);
Sourcepub fn node_count(&self) -> usize
pub fn node_count(&self) -> usize
Return the number of nodes in the graph.
Sourcepub fn edge_count(&self) -> usize
pub fn edge_count(&self) -> usize
Return the number of edges in the graph.
Sourcepub fn contains_node(&self, n: N) -> bool
pub fn contains_node(&self, n: N) -> bool
Return true
if the node is contained in the graph.
Sourcepub fn add_edge(&mut self, a: N, b: N, weight: E) -> Option<E>
pub fn add_edge(&mut self, a: N, b: N, weight: E) -> Option<E>
Add an edge connecting a
and b
to the graph, with associated
data weight
. For a directed graph, the edge is directed from a
to b
.
Inserts nodes a
and/or b
if they aren’t already part of the graph.
Return None
if the edge did not previously exist, otherwise,
the associated data is updated and the old value is returned
as Some(old_weight)
.
§Examples
// Create a Graph with directed edges, and add one edge to it
use safe_graph::Graph;
let mut g: Graph<_, _> = Graph::new();
g.add_edge("x", "y", -1);
assert_eq!(g.node_count(), 2);
assert_eq!(g.edge_count(), 1);
assert!(g.contains_edge("x", "y"));
assert!(!g.contains_edge("y", "x"));
Sourcepub fn contains_edge(&self, a: N, b: N) -> bool
pub fn contains_edge(&self, a: N, b: N) -> bool
Return true
if the edge connecting a
with b
is contained in the graph.
Sourcepub fn nodes(&self) -> Nodes<'_, N> ⓘ
pub fn nodes(&self) -> Nodes<'_, N> ⓘ
Return an iterator over the nodes of the graph.
Iterator element type is N
.
Sourcepub fn neighbors(&self, a: N) -> Neighbors<'_, N, Ty>
pub fn neighbors(&self, a: N) -> Neighbors<'_, N, Ty>
Return an iterator of all nodes with an edge starting from a
.
Directed
: Outgoing edges froma
.Undirected
: All edges from or toa
.
Produces an empty iterator if the node doesn’t exist.
Iterator element type is N
.
Sourcepub fn neighbors_directed(
&self,
a: N,
dir: Direction,
) -> NeighborsDirected<'_, N, Ty>
pub fn neighbors_directed( &self, a: N, dir: Direction, ) -> NeighborsDirected<'_, N, Ty>
Return an iterator of all neighbors that have an edge between them and
a
, in the specified direction.
If the graph’s edges are undirected, this is equivalent to .neighbors(a).
Directed
,Outgoing
: All edges froma
.Directed
,Incoming
: All edges toa
.Undirected
: All edges from or toa
.
Produces an empty iterator if the node doesn’t exist.
Iterator element type is N
.
Sourcepub fn edges(&self, from: N) -> Edges<'_, N, E, Ty> ⓘ
pub fn edges(&self, from: N) -> Edges<'_, N, E, Ty> ⓘ
Return an iterator of target nodes with an edge starting from a
,
paired with their respective edge weights.
Directed
: Outgoing edges froma
.Undirected
: All edges from or toa
.
Produces an empty iterator if the node doesn’t exist.
Iterator element type is (N, &E)
.
Sourcepub fn edge_weight(&self, a: N, b: N) -> Option<&E>
pub fn edge_weight(&self, a: N, b: N) -> Option<&E>
Return a reference to the edge weight connecting a
with b
, or
None
if the edge does not exist in the graph.
Sourcepub fn edge_weight_mut(&mut self, a: N, b: N) -> Option<&mut E>
pub fn edge_weight_mut(&mut self, a: N, b: N) -> Option<&mut E>
Return a mutable reference to the edge weight connecting a
with b
, or
None
if the edge does not exist in the graph.
Trait Implementations§
Source§impl<N, E, Ty, Item> Extend<Item> for Graph<N, E, Ty>
Extend the graph from an iterable of edges.
impl<N, E, Ty, Item> Extend<Item> for Graph<N, E, Ty>
Extend the graph from an iterable of edges.
Nodes are inserted automatically to match the edges.
Source§fn extend<I>(&mut self, iterable: I)where
I: IntoIterator<Item = Item>,
fn extend<I>(&mut self, iterable: I)where
I: IntoIterator<Item = Item>,
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl<N, E, Ty, Item> FromIterator<Item> for Graph<N, E, Ty>
Create a new Graph
from an iterable of edges.
impl<N, E, Ty, Item> FromIterator<Item> for Graph<N, E, Ty>
Create a new Graph
from an iterable of edges.