Struct petgraph::matrix_graph::MatrixGraph [−][src]
pub struct MatrixGraph<N, E, Ty = Directed, Null: Nullable<Wrapped = E> = Option<E>, Ix = u16> { /* fields omitted */ }
Expand description
MatrixGraph<N, E, Ty, Null>
is a graph datastructure using an adjacency matrix
representation.
MatrixGraph
is parameterized over:
- Associated data
N
for nodes andE
for edges, called weights. The associated data can be of arbitrary type. - Edge type
Ty
that determines whether the graph edges are directed or undirected. - Nullable type
Null
, which denotes the edges’ presence (defaults toOption<E>
). You may specifyNotZero<E>
if you want to use a sentinel value (such as 0) to mark the absence of an edge. - Index type
Ix
that sets the maximum size for the graph (defaults toDefaultIx
).
The graph uses O(|V^2|) space, with fast edge insertion & amortized node insertion, as well as efficient graph search and graph algorithms on dense graphs.
This graph is backed by a flattened 2D array. For undirected graphs, only the lower triangular matrix is stored. Since the backing array stores edge weights, it is recommended to box large edge weights.
Implementations
impl<N, E, Ty: EdgeType, Null: Nullable<Wrapped = E>, Ix: IndexType> MatrixGraph<N, E, Ty, Null, Ix>
impl<N, E, Ty: EdgeType, Null: Nullable<Wrapped = E>, Ix: IndexType> MatrixGraph<N, E, Ty, Null, Ix>
Create a new MatrixGraph
with estimated capacity for nodes.
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.
Return 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 MatrixGraph is at the maximum number of nodes for its index type.
Remove a
from the graph.
Computes in O(V) time, due to the removal of edges with other nodes.
Panics if the node a
does not exist.
Update the edge from a
to b
to the graph, with its associated data weight
.
Return the previous data, if any.
Computes in O(1) time, best case. Computes in O(|V|^2) time, worst case (matrix needs to be re-allocated).
Panics if any of the nodes don’t exist.
Add an edge from a
to b
to the graph, with its associated
data weight
.
Computes in O(1) time, best case. Computes in O(|V|^2) time, worst case (matrix needs to be re-allocated).
Panics if any of the nodes don’t exist.
Panics if an edge already exists from a
to b
.
Note: MatrixGraph
does not allow adding parallel (“duplicate”) edges. If you want to avoid
this, use .update_edge(a, b, weight)
instead.
Remove the edge from a
to b
to the graph.
Panics if any of the nodes don’t exist.
Panics if no edge exists between a
and b
.
Return true if there is an edge between a
and b
.
Panics if any of the nodes don’t exist.
Access the weight for node a
.
Also available with indexing syntax: &graph[a]
.
Panics if the node doesn’t exist.
Access the weight for node a
, mutably.
Also available with indexing syntax: &mut graph[a]
.
Panics if the node doesn’t exist.
Access the weight for edge e
.
Also available with indexing syntax: &graph[e]
.
Panics if no edge exists between a
and b
.
Access the weight for edge e
, mutably.
Also available with indexing syntax: &mut graph[e]
.
Panics if no edge exists between a
and b
.
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 NodeIndex<Ix>
.
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 Edges<E, Ix>
.
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 MatrixGraph
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::matrix_graph::MatrixGraph; let gr = MatrixGraph::<(), i32>::from_edges(&[ (0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3), ]);
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.
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).
Outgoing
: All edges froma
.Incoming
: All edges toa
.
Produces an empty iterator if the node doesn’t exist.
Iterator element type is NodeIndex<Ix>
.
Return an iterator of all edges of a
, in the specified direction.
Outgoing
: All edges froma
.Incoming
: All edges toa
.
Produces an empty iterator if the node a
doesn’t exist.
Iterator element type is EdgeReference<E, Ix>
.
Create a new MatrixGraph
with undirected edges.
This is a convenience method. Use MatrixGraph::with_capacity
or MatrixGraph::default
for
a constructor that is generic in all the type parameters of MatrixGraph
.
Trait Implementations
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
type NodeWeight = N
type EdgeWeight = E
Create a new empty MatrixGraph
.
Return the number of edges in the graph.
impl<N, E, Ty: EdgeType, Null: Nullable<Wrapped = E>, Ix: IndexType> GetAdjacencyMatrix for MatrixGraph<N, E, Ty, Null, Ix>
impl<N, E, Ty: EdgeType, Null: Nullable<Wrapped = E>, Ix: IndexType> GetAdjacencyMatrix for MatrixGraph<N, E, Ty, Null, Ix>
type EdgeType = Ty
type EdgeType = Ty
The kind edges in the graph.
Index the MatrixGraph
by NodeIndex
pair to access edge weights.
Also available with indexing syntax: &graph[e]
.
Panics if no edge exists between a
and b
.
Index the MatrixGraph
by NodeIndex
to access node weights.
Panics if the node doesn’t exist.
Index the MatrixGraph
by NodeIndex
pair to access edge weights.
Also available with indexing syntax: &mut graph[e]
.
Panics if no edge exists between a
and b
.
Index the MatrixGraph
by NodeIndex
to access node weights.
Panics if the node doesn’t exist.
impl<'a, N, E, Ty: EdgeType, Null: Nullable<Wrapped = E>, Ix: IndexType> IntoEdgeReferences for &'a MatrixGraph<N, E, Ty, Null, Ix>
impl<'a, N, E, Ty: EdgeType, Null: Nullable<Wrapped = E>, Ix: IndexType> IntoEdgeReferences for &'a MatrixGraph<N, E, Ty, Null, Ix>
type EdgeReferences = EdgeReferences<'a, Ty, Null, Ix>
impl<'a, N, E, Null: Nullable<Wrapped = E>, Ix: IndexType> IntoEdgesDirected for &'a MatrixGraph<N, E, Directed, Null, Ix>
impl<'a, N, E, Null: Nullable<Wrapped = E>, Ix: IndexType> IntoEdgesDirected for &'a MatrixGraph<N, E, Directed, Null, Ix>
type EdgesDirected = Edges<'a, Directed, Null, Ix>
impl<'a, N, E: 'a, Ty: EdgeType, Null: Nullable<Wrapped = E>, Ix: IndexType> IntoNeighbors for &'a MatrixGraph<N, E, Ty, Null, Ix>
impl<'a, N, E: 'a, Ty: EdgeType, Null: Nullable<Wrapped = E>, Ix: IndexType> IntoNeighbors for &'a MatrixGraph<N, E, Ty, Null, Ix>
impl<'a, N, E: 'a, Null: Nullable<Wrapped = E>, Ix: IndexType> IntoNeighborsDirected for &'a MatrixGraph<N, E, Directed, Null, Ix>
impl<'a, N, E: 'a, Null: Nullable<Wrapped = E>, Ix: IndexType> IntoNeighborsDirected for &'a MatrixGraph<N, E, Directed, Null, Ix>
type NeighborsDirected = Neighbors<'a, Directed, Null, Ix>
impl<'a, N, E: 'a, Ty: EdgeType, Null: Nullable<Wrapped = E>, Ix: IndexType> IntoNodeIdentifiers for &'a MatrixGraph<N, E, Ty, Null, Ix>
impl<'a, N, E: 'a, Ty: EdgeType, Null: Nullable<Wrapped = E>, Ix: IndexType> IntoNodeIdentifiers for &'a MatrixGraph<N, E, Ty, Null, Ix>
type NodeIdentifiers = NodeIdentifiers<'a, Ix>
impl<'a, N, E, Ty: EdgeType, Null: Nullable<Wrapped = E>, Ix: IndexType> IntoNodeReferences for &'a MatrixGraph<N, E, Ty, Null, Ix>
impl<'a, N, E, Ty: EdgeType, Null: Nullable<Wrapped = E>, Ix: IndexType> IntoNodeReferences for &'a MatrixGraph<N, E, Ty, Null, Ix>
type NodeReferences = NodeReferences<'a, N, Ix>
impl<N, E, Ty: EdgeType, Null: Nullable<Wrapped = E>, Ix: IndexType> NodeIndexable for MatrixGraph<N, E, Ty, Null, Ix>
impl<N, E, Ty: EdgeType, Null: Nullable<Wrapped = E>, Ix: IndexType> NodeIndexable for MatrixGraph<N, E, Ty, Null, Ix>
Return an upper bound of the node indices in the graph (suitable for the size of a bitmap). Read more
Convert i
to a node index. i
must be a valid value in the graph.
type Map = FixedBitSet
type Map = FixedBitSet
The associated map type
Create a new visitor map
Auto Trait Implementations
impl<N, E, Ty, Null, Ix> RefUnwindSafe for MatrixGraph<N, E, Ty, Null, Ix> where
Ix: RefUnwindSafe,
N: RefUnwindSafe,
Null: RefUnwindSafe,
Ty: RefUnwindSafe,
impl<N, E, Ty, Null, Ix> Send for MatrixGraph<N, E, Ty, Null, Ix> where
Ix: Send,
N: Send,
Null: Send,
Ty: Send,
impl<N, E, Ty, Null, Ix> Sync for MatrixGraph<N, E, Ty, Null, Ix> where
Ix: Sync,
N: Sync,
Null: Sync,
Ty: Sync,
impl<N, E, Ty, Null, Ix> Unpin for MatrixGraph<N, E, Ty, Null, Ix> where
Ix: Unpin,
N: Unpin,
Null: Unpin,
Ty: Unpin,
impl<N, E, Ty, Null, Ix> UnwindSafe for MatrixGraph<N, E, Ty, Null, Ix> where
Ix: UnwindSafe,
N: UnwindSafe,
Null: UnwindSafe,
Ty: UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more