Struct petgraph::stable_graph::StableGraph
[−]
[src]
pub struct StableGraph<N, E, Ty = Directed, Ix = DefaultIx> where Ix: IndexType { /* fields omitted */ }
StableGraph<N, E, Ty, Ix>
is a graph datastructure using an adjacency
list representation.
Depends on feature = "stable_graph"
.
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.
Methods
impl<N, E> StableGraph<N, E, Directed>
[src]
fn new() -> Self
Create a new StableGraph
with directed edges.
This is a convenience method. See StableGraph::with_capacity
or StableGraph::default
for a constructor that is generic in all the
type parameters of StableGraph
.
impl<N, E, Ty, Ix> StableGraph<N, E, Ty, Ix> where Ty: EdgeType, Ix: IndexType
[src]
fn with_capacity(nodes: usize, edges: usize) -> Self
Create a new StableGraph
with estimated capacity.
fn capacity(&self) -> (usize, usize)
Return the current node and edge capacity of the graph.
fn clear(&mut self)
Remove all nodes and edges
fn node_count(&self) -> usize
Return the number of nodes (vertices) in the graph.
Computes in O(1) time.
fn edge_count(&self) -> usize
Return the number of edges in the graph.
Computes in O(1) time.
fn is_directed(&self) -> bool
Whether the graph has directed edges or not.
fn add_node(&mut self, weight: N) -> NodeIndex<Ix>
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 Graph is at the maximum number of nodes for its index type.
fn remove_node(&mut self, a: NodeIndex<Ix>) -> Option<N>
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
, and including the edges with an
endpoint in the displaced node.
fn contains_node(&self, a: NodeIndex<Ix>) -> bool
fn add_edge(&mut self, a: NodeIndex<Ix>, b: NodeIndex<Ix>, weight: E) -> EdgeIndex<Ix>
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 Graph is at the maximum number of edges for its index
type.
Note: StableGraph
allows adding parallel (“duplicate”) edges.
fn remove_edge(&mut self, e: EdgeIndex<Ix>) -> Option<E>
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
conneced to the same endpoints as e
.
fn node_weight(&self, a: NodeIndex<Ix>) -> Option<&N>
Access the weight for node a
.
Also available with indexing syntax: &graph[a]
.
fn node_weight_mut(&mut self, a: NodeIndex<Ix>) -> Option<&mut N>
Access the weight for node a
, mutably.
Also available with indexing syntax: &mut graph[a]
.
fn node_indices(&self) -> NodeIndices<N, Ix>
Return an iterator over the node indices of the graph
fn edge_weight(&self, e: EdgeIndex<Ix>) -> Option<&E>
Access the weight for edge e
.
Also available with indexing syntax: &graph[e]
.
fn edge_weight_mut(&mut self, e: EdgeIndex<Ix>) -> Option<&mut E>
Access the weight for edge e
, mutably
Also available with indexing syntax: &mut graph[e]
.
fn edge_endpoints(&self, e: EdgeIndex<Ix>) -> Option<(NodeIndex<Ix>, NodeIndex<Ix>)>
Access the source and target nodes for e
.
fn find_edge(&self, a: NodeIndex<Ix>, b: NodeIndex<Ix>) -> Option<EdgeIndex<Ix>>
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).
fn neighbors(&self, a: NodeIndex<Ix>) -> Neighbors<E, Ix>
Return an iterator of all nodes with an edge starting from a
.
Undirected
: All edges from or toa
.Directed
: Outgoing edges froma
.
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.
fn neighbors_directed(&self, a: NodeIndex<Ix>, dir: EdgeDirection) -> Neighbors<E, Ix>
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).
Undirected
: All edges from or toa
.Directed
,Outgoing
: All edges froma
.Directed
,Incoming
: All edges 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.
fn neighbors_undirected(&self, a: NodeIndex<Ix>) -> Neighbors<E, Ix>
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).
Undirected
andDirected
: All edges from or 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.
fn from_edges<I>(iterable: I) -> Self where I: IntoIterator, I::Item: IntoWeightedEdge<E>, I::Item::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), ]);Run
fn extend_with_edges<I>(&mut self, iterable: I) where I: IntoIterator, I::Item: IntoWeightedEdge<E>, I::Item::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
impl<N, E, Ty, Ix> Debug for StableGraph<N, E, Ty, Ix> where N: Debug, E: Debug, Ty: EdgeType, Ix: IndexType
[src]
impl<N, E, Ty, Ix: IndexType> Clone for StableGraph<N, E, Ty, Ix> where N: Clone, E: Clone
[src]
The resulting cloned graph has the same graph indices as self
.
fn clone(&self) -> Self
Returns a copy of the value. Read more
fn clone_from(&mut self, rhs: &Self)
Performs copy-assignment from source
. Read more
impl<N, E, Ty, Ix> Index<NodeIndex<Ix>> for StableGraph<N, E, Ty, Ix> where Ty: EdgeType, Ix: IndexType
[src]
Index the StableGraph
by NodeIndex
to access node weights.
Panics if the node doesn't exist.
type Output = N
The returned type after indexing
fn index(&self, index: NodeIndex<Ix>) -> &N
The method for the indexing (Foo[Bar]
) operation
impl<N, E, Ty, Ix> IndexMut<NodeIndex<Ix>> for StableGraph<N, E, Ty, Ix> where Ty: EdgeType, Ix: IndexType
[src]
Index the StableGraph
by NodeIndex
to access node weights.
Panics if the node doesn't exist.
fn index_mut(&mut self, index: NodeIndex<Ix>) -> &mut N
The method for the indexing (Foo[Bar]
) operation
impl<N, E, Ty, Ix> Index<EdgeIndex<Ix>> for StableGraph<N, E, Ty, Ix> where Ty: EdgeType, Ix: IndexType
[src]
Index the StableGraph
by EdgeIndex
to access edge weights.
Panics if the edge doesn't exist.
type Output = E
The returned type after indexing
fn index(&self, index: EdgeIndex<Ix>) -> &E
The method for the indexing (Foo[Bar]
) operation
impl<N, E, Ty, Ix> IndexMut<EdgeIndex<Ix>> for StableGraph<N, E, Ty, Ix> where Ty: EdgeType, Ix: IndexType
[src]
Index the StableGraph
by EdgeIndex
to access edge weights.
Panics if the edge doesn't exist.
fn index_mut(&mut self, index: EdgeIndex<Ix>) -> &mut E
The method for the indexing (Foo[Bar]
) operation
impl<N, E, Ty, Ix> Default for StableGraph<N, E, Ty, Ix> where Ty: EdgeType, Ix: IndexType
[src]
Create a new empty StableGraph
.
impl<N, E, Ty, Ix> NodeIndexable for StableGraph<N, E, Ty, Ix> where Ty: EdgeType, Ix: IndexType
[src]
fn node_bound(&self) -> usize
Return an upper bound of the node indices in the graph