pub struct MatrixGraph<N, E, Ty = Directed, Null: Nullable<Wrapped = E> = Option<E>, Ix = u16> { /* private fields */ }
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 and E 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 to Option<E>). You may specify NotZero<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 to DefaultIx).

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

Create a new MatrixGraph with estimated capacity for nodes.

Remove all nodes and 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.

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 from a.
  • Undirected: All edges from or to a.

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 from a.
  • Undirected: All edges connected to a.

Produces an empty iterator if the node doesn’t exist.
Iterator element type is Edges<E, Ix>.

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),
]);

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 from a.
  • Incoming: All edges to a.

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 from a.
  • Incoming: All edges to a.

Produces an empty iterator if the node a doesn’t exist.
Iterator element type is EdgeReference<E, Ix>.

Create a new MatrixGraph with directed 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.

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

Add or update the edge from a to b. Return the id of the affected edge. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Create a new empty MatrixGraph.

Returns the “default value” for a type. Read more

Return the number of edges in the graph.

The associated adjacency matrix type

Create the adjacency matrix

Return true if there is an edge from a to b, false otherwise. Read more

node identifier

edge identifier

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.

The returned type after indexing.

Performs the indexing (container[index]) operation. Read more

Index the MatrixGraph by NodeIndex to access node weights.

Panics if the node doesn’t exist.

The returned type after indexing.

Performs the indexing (container[index]) operation. Read more

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.

Performs the mutable indexing (container[index]) operation. Read more

Index the MatrixGraph by NodeIndex to access node weights.

Panics if the node doesn’t exist.

Performs the mutable indexing (container[index]) operation. Read more

Return an iterator of the neighbors of node a.

Return an upper bound of the node indices in the graph (suitable for the size of a bitmap). Read more

Convert a to an integer index.

Convert i to a node index. i must be a valid value in the graph.

The associated map type

Create a new visitor map

Reset the visitor map (and resize to new size of graph if needed)

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.