Struct petgraph::stable_graph::StableGraph [−][src]
Expand description
StableGraph<N, E, Ty, Ix>
is a graph datastructure using an adjacency
list representation.
The graph does not invalidate any unrelated node or edge indices when items are removed.
StableGraph
is parameterized over:
- Associated data
N
for nodes andE
for edges, also called weights. The associated data can be of arbitrary type. - Edge type
Ty
that determines whether the graph edges are directed or undirected. - Index type
Ix
, which determines the maximum size of the graph.
The graph uses O(|V| + |E|) space, and allows fast node and edge insert and efficient graph search.
It implements O(e’) edge lookup and edge and node removals, where e’ is some local measure of edge count.
-
Nodes and edges are each numbered in an interval from 0 to some number m, but not all indices in the range are valid, since gaps are formed by deletions.
-
You can select graph index integer type after the size of the graph. A smaller size may have better performance.
-
Using indices allows mutation while traversing the graph, see
Dfs
. -
The
StableGraph
is a regular rust collection and isSend
andSync
(as long as associated dataN
andE
are). -
Indices don’t allow as much compile time checking as references.
Depends on crate feature stable_graph
(default). Stable Graph is still
missing a few methods compared to Graph. You can contribute to help it
achieve parity.
Implementations
Create a new StableGraph
with estimated capacity.
Return the current node and edge capacity of the graph.
Remove all edges
Return the number of nodes (vertices) in the graph.
Computes in O(1) time.
Return the number of edges in the graph.
Computes in O(1) time.
Whether the graph has directed edges or not.
Add a node (also called vertex) with associated data weight
to the graph.
Computes in O(1) time.
Return the index of the new node.
Panics if the StableGraph
is at the maximum number of nodes for
its index type.
Remove a
from the graph if it exists, and return its weight.
If it doesn’t exist in the graph, return None
.
The node index a
is invalidated, but none other.
Edge indices are invalidated as they would be following the removal of
each edge with an endpoint in a
.
Computes in O(e’) time, where e’ is the number of affected
edges, including n calls to .remove_edge()
where n is the number
of edges with an endpoint in a
.
Add an edge from a
to b
to the graph, with its associated
data weight
.
Return the index of the new edge.
Computes in O(1) time.
Panics if any of the nodes don’t exist.
Panics if the StableGraph
is at the maximum number of edges for
its index type.
Note: StableGraph
allows adding parallel (“duplicate”) edges.
Add or update an edge from a
to b
.
If the edge already exists, its weight is updated.
Return the index of the affected edge.
Computes in O(e’) time, where e’ is the number of edges
connected to a
(and b
, if the graph edges are undirected).
Panics if any of the nodes don’t exist.
Remove an edge and return its edge weight, or None
if it didn’t exist.
Invalidates the edge index e
but no other.
Computes in O(e’) time, where e’ is the number of edges
connected to the same endpoints as e
.
Access the weight for node a
.
Also available with indexing syntax: &graph[a]
.
Access the weight for node a
, mutably.
Also available with indexing syntax: &mut graph[a]
.
Return an iterator yielding immutable access to all node weights.
The order in which weights are yielded matches the order of their node indices.
Return an iterator yielding mutable access to all node weights.
The order in which weights are yielded matches the order of their node indices.
pub fn node_indices(&self) -> NodeIndices<'_, N, Ix>ⓘNotable traits for NodeIndices<'a, N, Ix>
impl<'a, N, Ix: IndexType> Iterator for NodeIndices<'a, N, Ix> type Item = NodeIndex<Ix>;
pub fn node_indices(&self) -> NodeIndices<'_, N, Ix>ⓘNotable traits for NodeIndices<'a, N, Ix>
impl<'a, N, Ix: IndexType> Iterator for NodeIndices<'a, N, Ix> type Item = NodeIndex<Ix>;
Return an iterator over the node indices of the graph
Access the weight for edge e
.
Also available with indexing syntax: &graph[e]
.
Access the weight for edge e
, mutably
Also available with indexing syntax: &mut graph[e]
.
Return an iterator yielding immutable access to all edge weights.
The order in which weights are yielded matches the order of their edge indices.
Return an iterator yielding mutable access to all edge weights.
The order in which weights are yielded matches the order of their edge indices.
Access the source and target nodes for e
.
pub fn edge_indices(&self) -> EdgeIndices<'_, E, Ix>ⓘNotable traits for EdgeIndices<'a, E, Ix>
impl<'a, E, Ix: IndexType> Iterator for EdgeIndices<'a, E, Ix> type Item = EdgeIndex<Ix>;
pub fn edge_indices(&self) -> EdgeIndices<'_, E, Ix>ⓘNotable traits for EdgeIndices<'a, E, Ix>
impl<'a, E, Ix: IndexType> Iterator for EdgeIndices<'a, E, Ix> type Item = EdgeIndex<Ix>;
Return an iterator over the edge indices of the graph
Lookup if there is an edge from a
to b
.
Computes in O(e’) time, where e’ is the number of edges
connected to a
(and b
, if the graph edges are undirected).
Lookup an edge from a
to b
.
Computes in O(e’) time, where e’ is the number of edges
connected to a
(and b
, if the graph edges are undirected).
Lookup an edge between a
and b
, in either direction.
If the graph is undirected, then this is equivalent to .find_edge()
.
Return the edge index and its directionality, with Outgoing
meaning
from a
to b
and Incoming
the reverse,
or None
if the edge does not exist.
Return an iterator of all nodes with an edge starting from a
.
Directed
: Outgoing edges froma
.Undirected
: All edges connected toa
.
Produces an empty iterator if the node doesn’t exist.
Iterator element type is NodeIndex<Ix>
.
Use .neighbors(a).detach()
to get a neighbor walker that does
not borrow from the graph.
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 connected toa
.
Produces an empty iterator if the node doesn’t exist.
Iterator element type is NodeIndex<Ix>
.
Use .neighbors_directed(a, dir).detach()
to get a neighbor walker that does
not borrow from the graph.
Return an iterator of all neighbors that have an edge between them and a
,
in either direction.
If the graph’s edges are undirected, this is equivalent to .neighbors(a).
Directed
andUndirected
: All edges connected toa
.
Produces an empty iterator if the node doesn’t exist.
Iterator element type is NodeIndex<Ix>
.
Use .neighbors_undirected(a).detach()
to get a neighbor walker that does
not borrow from the graph.
Return an iterator of all edges of a
.
Directed
: Outgoing edges froma
.Undirected
: All edges connected toa
.
Produces an empty iterator if the node doesn’t exist.
Iterator element type is EdgeReference<E, Ix>
.
Return an iterator of all edges of a
, in the specified direction.
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 a
doesn’t exist.
Iterator element type is EdgeReference<E, Ix>
.
Return an iterator over either the nodes without edges to them
(Incoming
) or from them (Outgoing
).
An internal node has both incoming and outgoing edges.
The nodes in .externals(Incoming)
are the source nodes and
.externals(Outgoing)
are the sinks of the graph.
For a graph with undirected edges, both the sinks and the sources are just the nodes without edges.
The whole iteration computes in O(|V|) time.
pub fn index_twice_mut<T, U>(
&mut self,
i: T,
j: U
) -> (&mut <Self as Index<T>>::Output, &mut <Self as Index<U>>::Output) where
Self: IndexMut<T> + IndexMut<U>,
T: GraphIndex,
U: GraphIndex,
pub fn index_twice_mut<T, U>(
&mut self,
i: T,
j: U
) -> (&mut <Self as Index<T>>::Output, &mut <Self as Index<U>>::Output) where
Self: IndexMut<T> + IndexMut<U>,
T: GraphIndex,
U: GraphIndex,
Index the StableGraph
by two indices, any combination of
node or edge indices is fine.
Panics if the indices are equal or if they are out of bounds.
Keep all nodes that return true
from the visit
closure,
remove the others.
visit
is provided a proxy reference to the graph, so that
the graph can be walked and associated data modified.
The order nodes are visited is not specified.
The node indices of the removed nodes are invalidated, but none other. Edge indices are invalidated as they would be following the removal of each edge with an endpoint in a removed node.
Computes in O(n + e’) time, where n is the number of node indices and
e’ is the number of affected edges, including n calls to .remove_edge()
where n is the number of edges with an endpoint in a removed node.
Keep all edges that return true
from the visit
closure,
remove the others.
visit
is provided a proxy reference to the graph, so that
the graph can be walked and associated data modified.
The order edges are visited is not specified.
The edge indices of the removed edes are invalidated, but none other.
Computes in O(e’‘) time, e’ is the number of affected edges,
including the calls to .remove_edge()
for each removed edge.
pub fn from_edges<I>(iterable: I) -> Self where
I: IntoIterator,
I::Item: IntoWeightedEdge<E>,
<I::Item as IntoWeightedEdge<E>>::NodeId: Into<NodeIndex<Ix>>,
N: Default,
pub fn from_edges<I>(iterable: I) -> Self where
I: IntoIterator,
I::Item: IntoWeightedEdge<E>,
<I::Item as IntoWeightedEdge<E>>::NodeId: Into<NodeIndex<Ix>>,
N: Default,
Create a new StableGraph
from an iterable of edges.
Node weights N
are set to default values.
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::stable_graph::StableGraph; let gr = StableGraph::<(), i32>::from_edges(&[ (0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3), ]);
Create a new StableGraph
by mapping node and
edge weights to new values.
The resulting graph has the same structure and the same
graph indices as self
.
Create a new StableGraph
by mapping nodes and edges.
A node or edge may be mapped to None
to exclude it from
the resulting graph.
Nodes are mapped first with the node_map
closure, then
edge_map
is called for the edges that have not had any endpoint
removed.
The resulting graph has the structure of a subgraph of the original graph. Nodes and edges that are not removed maintain their old node or edge indices.
pub fn extend_with_edges<I>(&mut self, iterable: I) where
I: IntoIterator,
I::Item: IntoWeightedEdge<E>,
<I::Item as IntoWeightedEdge<E>>::NodeId: Into<NodeIndex<Ix>>,
N: Default,
pub fn extend_with_edges<I>(&mut self, iterable: I) where
I: IntoIterator,
I::Item: IntoWeightedEdge<E>,
<I::Item as IntoWeightedEdge<E>>::NodeId: Into<NodeIndex<Ix>>,
N: Default,
Extend the graph from an iterable of edges.
Node weights N
are set to default values.
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.
Trait Implementations
Arbitrary
for StableGraph
creates a graph by selecting a node count
and a probability for each possible edge to exist.
The result will be simple graph or digraph, with possible self loops, no parallel edges.
The exact properties of the produced graph is subject to change.
Requires crate features "quickcheck"
and "stable_graph"
Add a new edge. If parallel edges (duplicate) are not allowed and
the edge already exists, return None
. Read more
fn update_edge(
&mut self,
a: Self::NodeId,
b: Self::NodeId,
weight: Self::EdgeWeight
) -> Self::EdgeId
fn update_edge(
&mut self,
a: Self::NodeId,
b: Self::NodeId,
weight: Self::EdgeWeight
) -> Self::EdgeId
Add or update the edge from a
to b
. Return the id of the affected
edge. Read more
The resulting cloned graph has the same graph indices as self
.
type NodeWeight = N
type EdgeWeight = E
Create a new empty StableGraph
.
impl<'de, N, E, Ty, Ix> Deserialize<'de> for StableGraph<N, E, Ty, Ix> where
Ty: EdgeType,
Ix: IndexType + Deserialize<'de>,
N: Deserialize<'de>,
E: Deserialize<'de>,
impl<'de, N, E, Ty, Ix> Deserialize<'de> for StableGraph<N, E, Ty, Ix> where
Ty: EdgeType,
Ix: IndexType + Deserialize<'de>,
N: Deserialize<'de>,
E: Deserialize<'de>,
Requires crate feature "serde-1"
Deserialize this value from the given Serde deserializer. Read more
Return the number of edges in the graph.
impl<N, E, Ty, Ix> EdgeIndexable for StableGraph<N, E, Ty, Ix> where
Ty: EdgeType,
Ix: IndexType,
impl<N, E, Ty, Ix> EdgeIndexable for StableGraph<N, E, Ty, Ix> where
Ty: EdgeType,
Ix: IndexType,
Return an upper bound of the edge indices in the graph (suitable for the size of a bitmap). Read more
Convert i
to an edge index. i
must be a valid value in the graph.
Convert a Graph
into a StableGraph
Computes in O(|V| + |E|) time.
The resulting graph has the same node and edge indices as the original graph.
Convert a StableGraph
into a Graph
Computes in O(|V| + |E|) time.
This translates the stable graph into a graph with node and edge indices in
a compact interval without holes (like Graph
s always are).
Only if the stable graph had no vacancies after deletions (if node bound was equal to node count, and the same for edges), would the resulting graph have the same node and edge indices as the input.
Performs the conversion.
impl<N, E, Ty, Ix> FromElements for StableGraph<N, E, Ty, Ix> where
Ty: EdgeType,
Ix: IndexType,
impl<N, E, Ty, Ix> FromElements for StableGraph<N, E, Ty, Ix> where
Ty: EdgeType,
Ix: IndexType,
fn from_elements<I>(iterable: I) -> Self where
Self: Sized,
I: IntoIterator<Item = Element<Self::NodeWeight, Self::EdgeWeight>>,
impl<N, E, Ty, Ix> GetAdjacencyMatrix for StableGraph<N, E, Ty, Ix> where
Ty: EdgeType,
Ix: IndexType,
impl<N, E, Ty, Ix> GetAdjacencyMatrix for StableGraph<N, E, Ty, Ix> where
Ty: EdgeType,
Ix: IndexType,
The adjacency matrix for Graph is a bitmap that’s computed by
.adjacency_matrix()
.
type AdjMatrix = FixedBitSet
type AdjMatrix = FixedBitSet
The associated adjacency matrix type
Create the adjacency matrix
Return true if there is an edge from a
to b
, false otherwise. Read more
type EdgeType = Ty
type EdgeType = Ty
The kind edges in the graph.
Index the StableGraph
by EdgeIndex
to access edge weights.
Panics if the edge doesn’t exist.
Index the StableGraph
by NodeIndex
to access node weights.
Panics if the node doesn’t exist.
Index the StableGraph
by EdgeIndex
to access edge weights.
Panics if the edge doesn’t exist.
Index the StableGraph
by NodeIndex
to access node weights.
Panics if the node doesn’t exist.
impl<'a, N: 'a, E: 'a, Ty, Ix> IntoEdgeReferences for &'a StableGraph<N, E, Ty, Ix> where
Ty: EdgeType,
Ix: IndexType,
impl<'a, N: 'a, E: 'a, Ty, Ix> IntoEdgeReferences for &'a StableGraph<N, E, Ty, Ix> where
Ty: EdgeType,
Ix: IndexType,
Create an iterator over all edges in the graph, in indexed order.
Iterator element type is EdgeReference<E, Ix>
.
type EdgeRef = EdgeReference<'a, E, Ix>
type EdgeReferences = EdgeReferences<'a, E, Ix>
impl<'a, N, E, Ty, Ix> IntoEdges for &'a StableGraph<N, E, Ty, Ix> where
Ty: EdgeType,
Ix: IndexType,
impl<'a, N, E, Ty, Ix> IntoEdges for &'a StableGraph<N, E, Ty, Ix> where
Ty: EdgeType,
Ix: IndexType,
impl<'a, N, E, Ty, Ix> IntoEdgesDirected for &'a StableGraph<N, E, Ty, Ix> where
Ty: EdgeType,
Ix: IndexType,
impl<'a, N, E, Ty, Ix> IntoEdgesDirected for &'a StableGraph<N, E, Ty, Ix> where
Ty: EdgeType,
Ix: IndexType,
type EdgesDirected = Edges<'a, E, Ty, Ix>
impl<'a, N, E: 'a, Ty, Ix> IntoNeighbors for &'a StableGraph<N, E, Ty, Ix> where
Ty: EdgeType,
Ix: IndexType,
impl<'a, N, E: 'a, Ty, Ix> IntoNeighbors for &'a StableGraph<N, E, Ty, Ix> where
Ty: EdgeType,
Ix: IndexType,
impl<'a, N, E: 'a, Ty, Ix> IntoNeighborsDirected for &'a StableGraph<N, E, Ty, Ix> where
Ty: EdgeType,
Ix: IndexType,
impl<'a, N, E: 'a, Ty, Ix> IntoNeighborsDirected for &'a StableGraph<N, E, Ty, Ix> where
Ty: EdgeType,
Ix: IndexType,
type NeighborsDirected = Neighbors<'a, E, Ix>
impl<'a, N, E: 'a, Ty, Ix> IntoNodeIdentifiers for &'a StableGraph<N, E, Ty, Ix> where
Ty: EdgeType,
Ix: IndexType,
impl<'a, N, E: 'a, Ty, Ix> IntoNodeIdentifiers for &'a StableGraph<N, E, Ty, Ix> where
Ty: EdgeType,
Ix: IndexType,
type NodeIdentifiers = NodeIndices<'a, N, Ix>
impl<'a, N, E, Ty, Ix> IntoNodeReferences for &'a StableGraph<N, E, Ty, Ix> where
Ty: EdgeType,
Ix: IndexType,
impl<'a, N, E, Ty, Ix> IntoNodeReferences for &'a StableGraph<N, E, Ty, Ix> where
Ty: EdgeType,
Ix: IndexType,
type NodeReferences = NodeReferences<'a, N, Ix>
impl<N, E, Ty, Ix> NodeIndexable for StableGraph<N, E, Ty, Ix> where
Ty: EdgeType,
Ix: IndexType,
impl<N, E, Ty, Ix> NodeIndexable for StableGraph<N, E, Ty, Ix> where
Ty: EdgeType,
Ix: IndexType,
Requires crate feature "serde-1"
type Map = FixedBitSet
type Map = FixedBitSet
The associated map type
Create a new visitor map
Auto Trait Implementations
impl<N, E, Ty, Ix> RefUnwindSafe for StableGraph<N, E, Ty, Ix> where
E: RefUnwindSafe,
Ix: RefUnwindSafe,
N: RefUnwindSafe,
Ty: RefUnwindSafe,
impl<N, E, Ty, Ix> Unpin for StableGraph<N, E, Ty, Ix> where
E: Unpin,
Ix: Unpin,
N: Unpin,
Ty: Unpin,
impl<N, E, Ty, Ix> UnwindSafe for StableGraph<N, E, Ty, Ix> where
E: UnwindSafe,
Ix: UnwindSafe,
N: UnwindSafe,
Ty: UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more