petgraph/
lib.rs

1//! `petgraph` is a graph data structure library.
2//!
3//! Graphs are collections of nodes, and edges between nodes. `petgraph`
4//! provides several [graph types](index.html#graph-types) (each differing in the
5//! tradeoffs taken in their internal representation),
6//! [algorithms](./algo/index.html#functions) on those graphs, and functionality to
7//! [output graphs](./dot/struct.Dot.html) in
8//! [`graphviz`](https://www.graphviz.org/) format. Both nodes and edges
9//! can have arbitrary associated data, and edges may be either directed or undirected.
10//!
11//! # Example
12//!
13//! ```rust
14//! use petgraph::graph::{NodeIndex, UnGraph};
15//! use petgraph::algo::{dijkstra, min_spanning_tree};
16//! use petgraph::data::FromElements;
17//! use petgraph::dot::{Dot, Config};
18//!
19//! // Create an undirected graph with `i32` nodes and edges with `()` associated data.
20//! let g = UnGraph::<i32, ()>::from_edges(&[
21//!     (1, 2), (2, 3), (3, 4),
22//!     (1, 4)]);
23//!
24//! // Find the shortest path from `1` to `4` using `1` as the cost for every edge.
25//! let node_map = dijkstra(&g, 1.into(), Some(4.into()), |_| 1);
26//! assert_eq!(&1i32, node_map.get(&NodeIndex::new(4)).unwrap());
27//!
28//! // Get the minimum spanning tree of the graph as a new graph, and check that
29//! // one edge was trimmed.
30//! let mst = UnGraph::<_, _>::from_elements(min_spanning_tree(&g));
31//! assert_eq!(g.raw_edges().len() - 1, mst.raw_edges().len());
32//!
33//! // Output the tree to `graphviz` `DOT` format
34//! println!("{:?}", Dot::with_config(&mst, &[Config::EdgeNoLabel]));
35//! // graph {
36//! //     0 [label="\"0\""]
37//! //     1 [label="\"0\""]
38//! //     2 [label="\"0\""]
39//! //     3 [label="\"0\""]
40//! //     1 -- 2
41//! //     3 -- 4
42//! //     2 -- 3
43//! // }
44//! ```
45//!
46//! # Graph types
47//!
48//! * [`Graph`](./graph/struct.Graph.html) -
49//!   An adjacency list graph with arbitrary associated data.
50//! * [`StableGraph`](./stable_graph/struct.StableGraph.html) -
51//!   Similar to `Graph`, but it keeps indices stable across removals.
52//! * [`GraphMap`](./graphmap/struct.GraphMap.html) -
53//!   An adjacency list graph backed by a hash table. The node identifiers are the keys
54//!   into the table.
55//! * [`MatrixGraph`](./matrix_graph/struct.MatrixGraph.html) -
56//!   An adjacency matrix graph.
57//! * [`CSR`](./csr/struct.Csr.html) -
58//!   A sparse adjacency matrix graph with arbitrary associated data.
59//!
60//! ### Generic parameters
61//!
62//! Each graph type is generic over a handful of parameters. All graphs share 3 common
63//! parameters, `N`, `E`, and `Ty`. This is a broad overview of what those are. Each
64//! type's documentation will have finer detail on these parameters.
65//!
66//! `N` & `E` are called *weights* in this implementation, and are associated with
67//! nodes and edges respectively. They can generally be of arbitrary type, and don't have to
68//! be what you might conventionally consider weight-like. For example, using `&str` for `N`
69//! will work. Many algorithms that require costs let you provide a cost function that
70//! translates your `N` and `E` weights into costs appropriate to the algorithm. Some graph
71//! types and choices do impose bounds on `N` or `E`.
72//! [`min_spanning_tree`](./algo/fn.min_spanning_tree.html) for example requires edge weights that
73//! implement [`PartialOrd`](https://doc.rust-lang.org/stable/core/cmp/trait.PartialOrd.html).
74//! [`GraphMap`](./graphmap/struct.GraphMap.html) requires node weights that can serve as hash
75//! map keys, since that graph type does not create standalone node indices.
76//!
77//! `Ty` controls whether edges are [`Directed`](./enum.Directed.html) or
78//! [`Undirected`](./enum.Undirected.html).
79//!
80//! `Ix` appears on graph types that use indices. It is exposed so you can control
81//! the size of node and edge indices, and therefore the memory footprint of your graphs.
82//! Allowed values are `u8`, `u16`, `u32`, and `usize`, with `u32` being the default.
83//!
84//! ### Shorthand types
85//!
86//! Each graph type vends a few shorthand type definitions that name some specific
87//! generic choices. For example, [`DiGraph<_, _>`](./graph/type.DiGraph.html) is shorthand
88//! for [`Graph<_, _, Directed>`](graph/struct.Graph.html).
89//! [`UnMatrix<_, _>`](./matrix_graph/type.UnMatrix.html) is shorthand for
90//! [`MatrixGraph<_, _, Undirected>`](./matrix_graph/struct.MatrixGraph.html). Each graph type's
91//! module documentation lists the available shorthand types.
92//!
93//! # Crate features
94//!
95//! * **serde-1** -
96//!   Defaults off. Enables serialization for ``Graph, StableGraph, GraphMap`` using
97//!   [`serde 1.0`](https://crates.io/crates/serde). May require a more recent version
98//!   of Rust than petgraph alone.
99//! * **graphmap** -
100//!   Defaults on. Enables [`GraphMap`](./graphmap/struct.GraphMap.html).
101//! * **stable_graph** -
102//!   Defaults on. Enables [`StableGraph`](./stable_graph/struct.StableGraph.html).
103//! * **matrix_graph** -
104//!   Defaults on. Enables [`MatrixGraph`](./matrix_graph/struct.MatrixGraph.html).
105//! * **rayon** -
106//!   Defaults off. Enables parallel versions of iterators and algorithms using
107//!   [`rayon`](https://docs.rs/rayon/latest/rayon/) crate. Requires the `std` feature.
108//! * **std** -
109//!   Defaults on. Enables the Rust Standard Library. Disabling the `std` feature makes it possible to use `petgraph` in `no_std` contexts.
110//! * **generate** -
111//!   Defaults off. Enables graph generators.
112//! * **unstable** -
113//!   Defaults off. Enables unstable crate features (currently onle `generate`).
114//! * **dot_parser** -
115//!   Defaults off. Enables building [`Graph`](./graph/struct.Graph.html) and [`StableGraph`](./stable_graph/struct.StableGraph.html) from [DOT/Graphviz](https://www.graphviz.org/doc/info/lang.html) descriptions. Imports can be made statically or dynamically (i.e. at compile time or at runtime).
116//!
117#![doc(html_root_url = "https://docs.rs/petgraph/0.4/")]
118#![no_std]
119
120extern crate alloc;
121
122#[cfg(any(feature = "std", test))]
123extern crate std;
124
125extern crate fixedbitset;
126#[cfg(feature = "graphmap")]
127extern crate indexmap;
128
129#[cfg(feature = "serde-1")]
130extern crate serde;
131#[cfg(feature = "serde-1")]
132#[macro_use]
133extern crate serde_derive;
134
135#[cfg(all(feature = "serde-1", test))]
136extern crate itertools;
137
138#[doc(no_inline)]
139pub use crate::graph::Graph;
140
141pub use crate::Direction::{Incoming, Outgoing};
142
143#[macro_use]
144mod macros;
145mod scored;
146
147// these modules define trait-implementing macros
148#[macro_use]
149pub mod visit;
150#[macro_use]
151pub mod data;
152
153pub mod acyclic;
154pub mod adj;
155pub mod algo;
156pub mod csr;
157pub mod dot;
158#[cfg(feature = "generate")]
159pub mod generate;
160pub mod graph6;
161mod graph_impl;
162#[cfg(feature = "graphmap")]
163pub mod graphmap;
164mod iter_format;
165mod iter_utils;
166#[cfg(feature = "matrix_graph")]
167pub mod matrix_graph;
168#[cfg(feature = "quickcheck")]
169mod quickcheck;
170#[cfg(feature = "serde-1")]
171mod serde_utils;
172mod traits_graph;
173pub mod unionfind;
174mod util;
175
176pub mod operator;
177pub mod prelude;
178
179/// `Graph<N, E, Ty, Ix>` is a graph datastructure using an adjacency list representation.
180pub mod graph {
181    pub use crate::graph_impl::{
182        edge_index, node_index, DefaultIx, DiGraph, Edge, EdgeIndex, EdgeIndices, EdgeReference,
183        EdgeReferences, EdgeWeightsMut, Edges, EdgesConnecting, Externals, Frozen, Graph,
184        GraphError, GraphIndex, IndexType, Neighbors, Node, NodeIndex, NodeIndices, NodeReferences,
185        NodeWeightsMut, UnGraph, WalkNeighbors,
186    };
187}
188
189#[cfg(feature = "stable_graph")]
190pub use crate::graph_impl::stable_graph;
191
192// Index into the NodeIndex and EdgeIndex arrays
193/// Edge direction.
194#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Ord, Eq, Hash)]
195#[repr(usize)]
196#[cfg_attr(
197    feature = "serde-1",
198    derive(serde_derive::Serialize, serde_derive::Deserialize)
199)]
200pub enum Direction {
201    /// An `Outgoing` edge is an outward edge *from* the current node.
202    Outgoing = 0,
203    /// An `Incoming` edge is an inbound edge *to* the current node.
204    Incoming = 1,
205}
206
207impl Direction {
208    /// Return the opposite `Direction`.
209    #[inline]
210    pub fn opposite(self) -> Direction {
211        match self {
212            Outgoing => Incoming,
213            Incoming => Outgoing,
214        }
215    }
216
217    /// Return `0` for `Outgoing` and `1` for `Incoming`.
218    #[inline]
219    pub fn index(self) -> usize {
220        (self as usize) & 0x1
221    }
222}
223
224#[doc(hidden)]
225pub use crate::Direction as EdgeDirection;
226
227/// Marker type for a directed graph.
228#[derive(Clone, Copy, Debug)]
229#[cfg_attr(
230    feature = "serde-1",
231    derive(serde_derive::Serialize, serde_derive::Deserialize)
232)]
233pub enum Directed {}
234
235/// Marker type for an undirected graph.
236#[derive(Clone, Copy, Debug)]
237#[cfg_attr(
238    feature = "serde-1",
239    derive(serde_derive::Serialize, serde_derive::Deserialize)
240)]
241pub enum Undirected {}
242
243/// A graph's edge type determines whether it has directed edges or not.
244pub trait EdgeType {
245    fn is_directed() -> bool;
246}
247
248impl EdgeType for Directed {
249    #[inline]
250    fn is_directed() -> bool {
251        true
252    }
253}
254
255impl EdgeType for Undirected {
256    #[inline]
257    fn is_directed() -> bool {
258        false
259    }
260}
261
262/// Convert an element like `(i, j)` or `(i, j, w)` into
263/// a triple of source, target, edge weight.
264///
265/// For `Graph::from_edges` and `GraphMap::from_edges`.
266pub trait IntoWeightedEdge<E> {
267    type NodeId;
268    fn into_weighted_edge(self) -> (Self::NodeId, Self::NodeId, E);
269}
270
271impl<Ix, E> IntoWeightedEdge<E> for (Ix, Ix)
272where
273    E: Default,
274{
275    type NodeId = Ix;
276
277    fn into_weighted_edge(self) -> (Ix, Ix, E) {
278        let (s, t) = self;
279        (s, t, E::default())
280    }
281}
282
283impl<Ix, E> IntoWeightedEdge<E> for (Ix, Ix, E) {
284    type NodeId = Ix;
285    fn into_weighted_edge(self) -> (Ix, Ix, E) {
286        self
287    }
288}
289
290impl<Ix, E> IntoWeightedEdge<E> for (Ix, Ix, &E)
291where
292    E: Clone,
293{
294    type NodeId = Ix;
295    fn into_weighted_edge(self) -> (Ix, Ix, E) {
296        let (a, b, c) = self;
297        (a, b, c.clone())
298    }
299}
300
301impl<Ix, E> IntoWeightedEdge<E> for &(Ix, Ix)
302where
303    Ix: Copy,
304    E: Default,
305{
306    type NodeId = Ix;
307    fn into_weighted_edge(self) -> (Ix, Ix, E) {
308        let (s, t) = *self;
309        (s, t, E::default())
310    }
311}
312
313impl<Ix, E> IntoWeightedEdge<E> for &(Ix, Ix, E)
314where
315    Ix: Copy,
316    E: Clone,
317{
318    type NodeId = Ix;
319    fn into_weighted_edge(self) -> (Ix, Ix, E) {
320        self.clone()
321    }
322}