Graaf

Work with directed graphs in Rust.
Table of Contents
Installation
Add the following to your Cargo.toml:
[]
= "0.64.5"
Digraph Types
Graaf provides three representations of directed graphs.
- The Adjacency List type represents unweighted sparse digraphs.
- The Adjacency Matrix type represents unweighted dense digraphs.
- The Weighted Adjacency List type represents weighted sparse digraphs.
These types eagerly implement digraph operations and digraph algorithms.
Creating Digraphs
The gen module provides four digraph generators.
- The
Completetrait generates a digraph in which an arc connects every ordered pair of distinct vertices. - The
Cycletrait generates a digraph with a cycle of a given length. - The
Emptytrait generates a digraph with no arcs. - The
RandomTournamenttrait generates a random digraph in which an arc connects every unordered pair of distinct vertices.
| Generator | Adj. List | Adj. Matrix | Weighted Adj. List |
|---|---|---|---|
Complete |
Yes | Yes | No |
Cycle |
Yes | Yes | No |
Empty |
Yes | Yes | Yes |
RandomTournament |
Yes | Yes | No |
Operations
The op module provides digraph operation traits. The digraph types implement these traits. One can implement these traits for custom digraph types. Operations form the foundation for algorithms.
Basic Operations
Individual digraph types implement the basic operations.
- The
AddArcWeightedtrait adds an arc to a weighted digraph. - The
AddArctrait adds an arc to an unweighted digraph. - The
ArcWeighttrait returns the weight of an arc. - The
ArcsWeightedtrait returns the arcs and their weights in a digraph. - The
Arcstrait returns the arcs in a digraph. - The
Conversetrait returns the converse of a digraph. - The
HasArctrait checks if an arc exists in a digraph. - The
Indegreetrait returns the indegree of a vertex. - The
IsSimpletrait checks if a digraph contains no loops or parallel arcs. - The
Ordertrait returns the number of vertices. - The
OutNeighborsWeightedtrait returns the weighted out-neighbors of a vertex. - The
OutNeighborstrait returns the out-neighbors of a vertex. - The
Outdegreetrait returns the outdegree of a vertex. - The
RemoveArctrait removes an arc from a digraph. - The
Sizetrait returns the number of arcs in a digraph. - The
Verticestrait returns the vertices in a digraph.
| Operation | Adj. List | Adj. Matrix | Weighted Adj. List |
|---|---|---|---|
AddArcWeighted |
No | No | Yes |
AddArc |
Yes | Yes | No |
ArcWeight |
Yes | Yes | Yes |
ArcsWeighted |
Yes | Yes | Yes |
Arcs |
Yes | Yes | Yes |
Converse |
Yes | Yes | Yes |
HasArc |
Yes | Yes | Yes |
Indegree |
Yes | Yes | Yes |
IsSimple |
Yes | Yes | Yes |
Order |
Yes | Yes | Yes |
OutNeighborsWeighted |
Yes | Yes | Yes |
OutNeighbors |
Yes | Yes | Yes |
Outdegree |
Yes | Yes | Yes |
RemoveArc |
Yes | Yes | Yes |
Size |
Yes | Yes | Yes |
Vertices |
Yes | Yes | Yes |
Extended Operations
The extended traits derive their implementation from the basic operations.
Degreereturns the degree of a vertex.HasEdgechecks if an edge exists in a digraph.InNeighborsreturns the in-neighbors of a vertex.IsBalancedchecks if a digraph is balanced.IsCompletechecks if a digraph is complete.IsIsolatedchecks if a vertex is isolated.IsOrientedchecks if a digraph is oriented.IsPendantchecks if a vertex is a pendant.IsRegularchecks if a digraph is regular.IsSemicompletechecks if a digraph is semicomplete.IsSubdigraphchecks if a digraph is a subdigraph of another digraph.IsSuperdigraphchecks if a digraph is a superdigraph of another digraph.IsSymmetricchecks if a digraph is symmetric.IsWalkchecks if a sequence of vertices is a walk in a digraph.
| Operation | Adj. List | Adj. Matrix | Weighted Adj. List |
|---|---|---|---|
Degree |
Yes | Yes | Yes |
HasEdge |
Yes | Yes | Yes |
InNeighbors |
Yes | Yes | Yes |
IsBalanced |
Yes | Yes | Yes |
IsComplete |
Yes | Yes | Yes |
IsIsolated |
Yes | Yes | Yes |
IsOriented |
Yes | Yes | Yes |
IsPendant |
Yes | Yes | Yes |
IsRegular |
Yes | Yes | Yes |
IsSemicomplete |
Yes | Yes | Yes |
IsSubdigraph |
Yes | Yes | Yes |
IsSuperdigraph |
Yes | Yes | Yes |
IsSymmetric |
Yes | Yes | Yes |
IsWalk |
Yes | Yes | Yes |
Algorithms
The algo module provides digraph algorithms.
Bellman-Ford-Moore
bellman_ford_moore finds the shortest paths in a weighted digraph with negative weights.
single_source_distancesfinds the shortest distances from one source vertex to all other vertices.
Breadth-First Search (BFS)
bfs explores the vertices of an unweighted digraph in order of their distance from a source.
These functions start from one or more source vertices and allow a custom step function, target predicate, distance array, breadth-first tree, and queue value, where applicable.
distancesfinds the shortest distances to all other vertices.predecessorsfinds the predecessors of the vertices on the shortest paths.shortest_pathfinds the shortest path to a target vertex.
These functions start from one source vertex.
single_source_distancesfinds the distances to all other vertices.single_source_predecessorsfinds the predecessors on the shortest paths.single_pair_shortest_pathfinds the shortest path between two vertices.
Depth-First Search (DFS)
dfs explores the vertices of an unweighted digraph in order of their depth from a source.
dfsatraverses the digraph, collecting an acyclic ordering and the times of each vertex's first and last visit.dfsa_predecessorscollects an acyclic ordering, the predecessors, and the times of each vertex's first and last visit.acyclic_orderinggenerates an acyclic ordering of the vertices.
Dijkstra's Algorithm
dijkstra finds the shortest paths from one or more source vertices in a weighted digraph.
These functions start from one or more source vertices and allow a custom step function, target predicate, distance array, and heap value, where applicable.
distancesfinds the shortest distances to all other vertices.predecessorsfinds the predecessors of the vertices on the shortest paths.shortest_pathfinds the shortest path to a target vertex.
These functions start from one source vertex.
single_source_distancesfinds the shortest distances to all other vertices.single_source_predecessorsfinds the predecessors of the vertices on the shortest paths.single_pair_shortest_pathfinds the shortest path between two vertices.
Floyd-Warshall
floyd_warshall finds the shortest paths between all pairs of vertices in a weighted digraph.
distancesfinds the shortest distances between all pairs of vertices.
Breadth-First Tree
A BreadthFirstTree is the result of a breadth-first search.
searchfinds the path to a target vertex.search_byfinds the path to a vertex that satisfies a predicate.
These functions produce a BreadthFirstTree.
bfs::single_source_predecessorsbfs::predecessorsdijkstra::single_source_predecessorsdijkstra::predecessors
Distance Matrix
A DistanceMatrix contains the shortest distances between all pairs of
vertices in a digraph.
centerfinds the center of the digraph.diameterfinds the diameter of the digraph.eccentricitiesreturns the eccentricities of the vertices.is_connectedchecks if the digraph is connected.
Project goals
- A flexible API for digraph operations
- A comprehensive set of algorithms
- Generators for common digraphs
- Competitive performance
- Full documentation
- Extensive property tests
- Complete unit test and benchmark coverage
Changelog
See CHANGELOG.md for a list of changes.
License
Licensed under Apache License, Version 2.0 or MIT license at your option.