pub struct GraphMap<N, E, Ty> { /* private fields */ }
Expand description
GraphMap<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 existence in constant time.
GraphMap
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 aliases UnGraphMap
and DiGraphMap
for convenience.
GraphMap
does not allow parallel edges, but self loops are allowed.
Depends on crate feature graphmap
(default).
Implementations§
source§impl<N, E, Ty> GraphMap<N, E, Ty>
impl<N, E, Ty> GraphMap<N, E, Ty>
sourcepub fn with_capacity(nodes: usize, edges: usize) -> GraphMap<N, E, Ty>
pub fn with_capacity(nodes: usize, edges: usize) -> GraphMap<N, E, Ty>
Create a new GraphMap
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 is_directed(&self) -> bool
pub fn is_directed(&self) -> bool
Whether the graph has directed edges.
sourcepub fn from_edges<I>(iterable: I) -> GraphMap<N, E, Ty>
pub fn from_edges<I>(iterable: I) -> GraphMap<N, E, Ty>
Create a new GraphMap
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.
use petgraph::graphmap::UnGraphMap;
// Create a new undirected GraphMap.
// Use a type hint to have `()` be the edge weight type.
let gr = UnGraphMap::<_, ()>::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 remove_node(&mut self, n: N) -> bool
pub fn remove_node(&mut self, n: N) -> bool
Return true
if node n
was removed.
Computes in O(V) time, due to the removal of edges with other nodes.
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)
.
// Create a GraphMap with directed edges, and add one edge to it
use petgraph::graphmap::DiGraphMap;
let mut g = DiGraphMap::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 remove_edge(&mut self, a: N, b: N) -> Option<E>
pub fn remove_edge(&mut self, a: N, b: N) -> Option<E>
Remove edge from a
to b
from the graph and return the edge weight.
Return None
if the edge didn’t exist.
// Create a GraphMap with undirected edges, and add and remove an edge.
use petgraph::graphmap::UnGraphMap;
let mut g = UnGraphMap::new();
g.add_edge("x", "y", -1);
let edge_data = g.remove_edge("y", "x");
assert_eq!(edge_data, Some(-1));
assert_eq!(g.edge_count(), 0);
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, a: N) -> Edges<'_, N, E, Ty> ⓘ
pub fn edges(&self, a: 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, N, &E)
.
sourcepub fn edges_directed(
&self,
a: N,
dir: Direction
) -> EdgesDirected<'_, N, E, Ty> ⓘ
pub fn edges_directed( &self, a: N, dir: Direction ) -> EdgesDirected<'_, N, E, Ty> ⓘ
Return an iterator of target nodes with an edge starting from a
,
paired with their respective edge weights.
Directed
,Outgoing
: All edges froma
.Directed
,Incoming
: All edges toa
.Undirected
,Outgoing
: All edges connected toa
, witha
being the source of each edge.Undirected
,Incoming
: All edges connected toa
, witha
being the target of each edge.
Produces an empty iterator if the node doesn’t exist.
Iterator element type is (N, 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.
sourcepub fn all_edges(&self) -> AllEdges<'_, N, E, Ty> ⓘ
pub fn all_edges(&self) -> AllEdges<'_, N, E, Ty> ⓘ
Return an iterator over all edges of the graph with their weight in arbitrary order.
Iterator element type is (N, N, &E)
sourcepub fn all_edges_mut(&mut self) -> AllEdgesMut<'_, N, E, Ty> ⓘ
pub fn all_edges_mut(&mut self) -> AllEdgesMut<'_, N, E, Ty> ⓘ
Return an iterator over all edges of the graph in arbitrary order, with a mutable reference to their weight.
Iterator element type is (N, N, &mut E)
sourcepub fn into_graph<Ix>(self) -> Graph<N, E, Ty, Ix>where
Ix: IndexType,
pub fn into_graph<Ix>(self) -> Graph<N, E, Ty, Ix>where
Ix: IndexType,
Return a Graph
that corresponds to this GraphMap
.
- Note that node and edge indices in the
Graph
have nothing in common with theGraphMap
s node weightsN
. The node weightsN
are used as node weights in the resultingGraph
, too. - Note that the index type is user-chosen.
Computes in O(|V| + |E|) time (average).
Panics if the number of nodes or edges does not fit with the resulting graph’s index type.
sourcepub fn from_graph<Ix>(graph: Graph<N, E, Ty, Ix>) -> GraphMap<N, E, Ty>
pub fn from_graph<Ix>(graph: Graph<N, E, Ty, Ix>) -> GraphMap<N, E, Ty>
Creates a GraphMap
that corresponds to the given Graph
.
Warning: Nodes with the same weight are merged and only the last parallel edge
is kept. Node and edge indices of the Graph
are lost. Only use this function
if the node weights are distinct and there are no parallel edges.
Computes in O(|V| + |E|) time (average).
Trait Implementations§
source§impl<N, E, Ty> Build for GraphMap<N, E, Ty>
impl<N, E, Ty> Build for GraphMap<N, E, Ty>
fn add_node( &mut self, weight: <GraphMap<N, E, Ty> as Data>::NodeWeight ) -> <GraphMap<N, E, Ty> as GraphBase>::NodeId
source§fn add_edge(
&mut self,
a: <GraphMap<N, E, Ty> as GraphBase>::NodeId,
b: <GraphMap<N, E, Ty> as GraphBase>::NodeId,
weight: <GraphMap<N, E, Ty> as Data>::EdgeWeight
) -> Option<<GraphMap<N, E, Ty> as GraphBase>::EdgeId>
fn add_edge( &mut self, a: <GraphMap<N, E, Ty> as GraphBase>::NodeId, b: <GraphMap<N, E, Ty> as GraphBase>::NodeId, weight: <GraphMap<N, E, Ty> as Data>::EdgeWeight ) -> Option<<GraphMap<N, E, Ty> as GraphBase>::EdgeId>
None
.source§impl<N, E, Ty> Data for GraphMap<N, E, Ty>
impl<N, E, Ty> Data for GraphMap<N, E, Ty>
type NodeWeight = N
type EdgeWeight = E
source§impl<N, E, Ty> EdgeCount for GraphMap<N, E, Ty>
impl<N, E, Ty> EdgeCount for GraphMap<N, E, Ty>
source§fn edge_count(&self) -> usize
fn edge_count(&self) -> usize
source§impl<N, E, Ty> EdgeIndexable for GraphMap<N, E, Ty>
impl<N, E, Ty> EdgeIndexable for GraphMap<N, E, Ty>
source§impl<N, E, Ty, Item> Extend<Item> for GraphMap<N, E, Ty>
impl<N, E, Ty, Item> Extend<Item> for GraphMap<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> FromElements for GraphMap<N, E, Ty>
impl<N, E, Ty> FromElements for GraphMap<N, E, Ty>
fn from_elements<I>(iterable: I) -> GraphMap<N, E, Ty>where
GraphMap<N, E, Ty>: Sized,
I: IntoIterator<Item = Element<<GraphMap<N, E, Ty> as Data>::NodeWeight, <GraphMap<N, E, Ty> as Data>::EdgeWeight>>,
source§impl<N, E, Ty, Item> FromIterator<Item> for GraphMap<N, E, Ty>
impl<N, E, Ty, Item> FromIterator<Item> for GraphMap<N, E, Ty>
Create a new GraphMap
from an iterable of edges.
source§impl<N, E, Ty> GetAdjacencyMatrix for GraphMap<N, E, Ty>
impl<N, E, Ty> GetAdjacencyMatrix for GraphMap<N, E, Ty>
The GraphMap
keeps an adjacency matrix internally.
source§impl<N, E, Ty> Index<(N, N)> for GraphMap<N, E, Ty>
impl<N, E, Ty> Index<(N, N)> for GraphMap<N, E, Ty>
Index GraphMap
by node pairs to access edge weights.
source§impl<N, E, Ty> IndexMut<(N, N)> for GraphMap<N, E, Ty>
impl<N, E, Ty> IndexMut<(N, N)> for GraphMap<N, E, Ty>
Index GraphMap
by node pairs to access edge weights.
source§impl<'a, N, E, Ty> IntoEdgeReferences for &'a GraphMap<N, E, Ty>
impl<'a, N, E, Ty> IntoEdgeReferences for &'a GraphMap<N, E, Ty>
type EdgeRef = (N, N, &'a E)
type EdgeReferences = AllEdges<'a, N, E, Ty>
fn edge_references( self ) -> <&'a GraphMap<N, E, Ty> as IntoEdgeReferences>::EdgeReferences
source§impl<'a, N, E, Ty> IntoEdgesDirected for &'a GraphMap<N, E, Ty>
impl<'a, N, E, Ty> IntoEdgesDirected for &'a GraphMap<N, E, Ty>
type EdgesDirected = EdgesDirected<'a, N, E, Ty>
fn edges_directed( self, a: <&'a GraphMap<N, E, Ty> as GraphBase>::NodeId, dir: Direction ) -> <&'a GraphMap<N, E, Ty> as IntoEdgesDirected>::EdgesDirected
source§impl<'a, N, E, Ty> IntoNeighbors for &'a GraphMap<N, E, Ty>
impl<'a, N, E, Ty> IntoNeighbors for &'a GraphMap<N, E, Ty>
source§impl<'a, N, E, Ty> IntoNeighborsDirected for &'a GraphMap<N, E, Ty>
impl<'a, N, E, Ty> IntoNeighborsDirected for &'a GraphMap<N, E, Ty>
type NeighborsDirected = NeighborsDirected<'a, N, Ty>
fn neighbors_directed( self, n: N, dir: Direction ) -> <&'a GraphMap<N, E, Ty> as IntoNeighborsDirected>::NeighborsDirected
source§impl<'a, N, E, Ty> IntoNodeIdentifiers for &'a GraphMap<N, E, Ty>
impl<'a, N, E, Ty> IntoNodeIdentifiers for &'a GraphMap<N, E, Ty>
type NodeIdentifiers = NodeIdentifiers<'a, N, E, Ty>
fn node_identifiers( self ) -> <&'a GraphMap<N, E, Ty> as IntoNodeIdentifiers>::NodeIdentifiers
source§impl<'a, N, E, Ty> IntoNodeReferences for &'a GraphMap<N, E, Ty>
impl<'a, N, E, Ty> IntoNodeReferences for &'a GraphMap<N, E, Ty>
type NodeRef = (N, &'a N)
type NodeReferences = NodeReferences<'a, N, E, Ty>
fn node_references( self ) -> <&'a GraphMap<N, E, Ty> as IntoNodeReferences>::NodeReferences
source§impl<N, E, Ty> NodeIndexable for GraphMap<N, E, Ty>
impl<N, E, Ty> NodeIndexable for GraphMap<N, E, Ty>
impl<N, E, Ty> NodeCompactIndexable for GraphMap<N, E, Ty>
Auto Trait Implementations§
impl<N, E, Ty> RefUnwindSafe for GraphMap<N, E, Ty>
impl<N, E, Ty> Send for GraphMap<N, E, Ty>
impl<N, E, Ty> Sync for GraphMap<N, E, Ty>
impl<N, E, Ty> Unpin for GraphMap<N, E, Ty>
impl<N, E, Ty> UnwindSafe for GraphMap<N, E, Ty>
Blanket Implementations§
source§impl<T, U> AsBindGroupShaderType<U> for T
impl<T, U> AsBindGroupShaderType<U> for T
source§fn as_bind_group_shader_type(&self, _images: &RenderAssets<Image>) -> U
fn as_bind_group_shader_type(&self, _images: &RenderAssets<Image>) -> U
T
ShaderType
for self
. When used in AsBindGroup
derives, it is safe to assume that all images in self
exist.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
source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
source§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
source§impl<T> FromWorld for Twhere
T: Default,
impl<T> FromWorld for Twhere
T: Default,
source§fn from_world(_world: &mut World) -> T
fn from_world(_world: &mut World) -> T
Self
using data from the given World
.