pathfinding/lib.rs
1#![forbid(missing_docs)]
2//! # pathfinding
3//!
4//! [](https://crates.io/crates/pathfinding)
5//! [](https://docs.rs/pathfinding)
6//! [](#license)
7//!
8//! This crate implements several pathfinding, flow, and graph algorithms in [Rust][Rust].
9//!
10//! ## Algorithms
11//!
12//! The algorithms are generic over their arguments.
13//!
14//! ### Directed graphs
15//!
16//! - [A*](directed/astar/index.html): find the shortest path in a weighted graph using an heuristic to guide the process ([⇒ Wikipedia][A*])
17//! - [BFS](directed/bfs/index.html): explore nearest successors first, then widen the search ([⇒ Wikipedia][BFS])
18//! - [Bidirectional search](directed/bfs/fn.bfs_bidirectional.html): simultaneously explore paths forwards from the start and backwards from the goal ([=> Wikipedia][Bidirectional search])
19//! - [Brent](directed/cycle_detection/index.html): find a cycle in an infinite sequence ([⇒ Wikipedia][Brent])
20//! - [DFS](directed/dfs/index.html): explore a graph by going as far as possible, then backtrack ([⇒ Wikipedia][DFS])
21//! - [Dijkstra](directed/dijkstra/index.html): find the shortest path in a weighted graph ([⇒ Wikipedia][Dijkstra])
22//! - [Edmonds Karp](directed/edmonds_karp/index.html): find the maximum flow in a weighted graph ([⇒ Wikipedia][Edmonds Karp])
23//! - [Floyd](directed/cycle_detection/index.html): find a cycle in an infinite sequence ([⇒ Wikipedia][Floyd])
24//! - [Fringe](directed/fringe/index.html): find the shortest path in a weighted graph using an heuristic to guide the process ([⇒ Wikipedia][Fringe])
25//! - [IDA*](directed/idastar/index.html): explore longer and longer paths in a weighted graph at the cost of multiple similar examinations ([⇒ Wikipedia][IDA*])
26//! - [IDDFS](directed/iddfs/index.html): explore longer and longer paths in an unweighted graph at the cost of multiple similar examinations ([⇒ Wikipedia][IDDFS])
27//! - [paths counting](directed/count_paths/index.html): count the paths to the destination in an acyclic graph
28//! - [strongly connected components](directed/strongly_connected_components/index.html): find strongly connected components in a directed graph ([⇒ Wikipedia][Strongly connected components])
29//! - [topological sorting](directed/topological_sort/index.html): find an acceptable topological order in a directed graph ([⇒ Wikipedia][Topological sorting])
30//! - [Yen](directed/yen/index.html): find k-shortest paths using Dijkstra ([⇒ Wikipedia][Yen])
31//!
32//! ### Undirected graphs
33//!
34//! - [connected components](undirected/connected_components/index.html): find disjoint connected sets of vertices ([⇒ Wikipedia][Connected components])
35//! - [Kruskal](undirected/kruskal/index.html): find a minimum-spanning-tree ([⇒ Wikipedia][Kruskal])
36//! - [Prim](undirected/prim/index.html): find a minimum-spanning-tree ([⇒ Wikipedia][Prim])
37//! - [cliques]: find maximum cliques in a graph ([= Wikipedia][BronKerbosch])
38//!
39//! ### Matching
40//!
41//! - [Kuhn-Munkres](kuhn_munkres/index.html) (Hungarian algorithm): find the maximum (or minimum) matching in a weighted bipartite graph ([⇒ Wikipedia][Kuhn-Munkres])
42//!
43//! ### Miscellaneous structures
44//!
45//! - A [`Grid`](grid/index.html) type representing a rectangular grid in which vertices can be added or removed, with automatic creation of edges between adjacent vertices.
46//! - A [`Matrix`](matrix/index.html) type to store data of arbitrary types, with neighbour-aware methods.
47//!
48//! ## Example
49//!
50//! We will search the shortest path on a chess board to go from (1, 1) to (4, 6) doing only knight
51//! moves.
52//!
53//! ``` rust
54//! use pathfinding::prelude::bfs;
55//!
56//! #[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
57//! struct Pos(i32, i32);
58//!
59//! impl Pos {
60//! fn successors(&self) -> Vec<Pos> {
61//! let &Pos(x, y) = self;
62//! vec![Pos(x+1,y+2), Pos(x+1,y-2), Pos(x-1,y+2), Pos(x-1,y-2),
63//! Pos(x+2,y+1), Pos(x+2,y-1), Pos(x-2,y+1), Pos(x-2,y-1)]
64//! }
65//! }
66//!
67//! static GOAL: Pos = Pos(4, 6);
68//! let result = bfs(&Pos(1, 1), |p| p.successors(), |p| *p == GOAL);
69//! assert_eq!(result.expect("no path found").len(), 5);
70//! ```
71//!
72//! ## Note on floating-point types
73//!
74//! Several algorithms require that the numerical types used to describe
75//! edge weights implement `Ord`. If you wish to use Rust built-in
76//! floating-point types (such as `f32`) that implement `PartialOrd`
77//! in this context, you can wrap them into compliant types using the
78//! [ordered-float](https://crates.io/crates/ordered-float) crate.
79//!
80//! The minimum supported Rust version (MSRV) is Rust 1.77.2.
81//!
82//! [A*]: https://en.wikipedia.org/wiki/A*_search_algorithm
83//! [BFS]: https://en.wikipedia.org/wiki/Breadth-first_search
84//! [Bidirectional search]: https://en.wikipedia.org/wiki/Bidirectional_search
85//! [Brent]: https://en.wikipedia.org/wiki/Cycle_detection#Brent's_algorithm
86//! [BronKerbosch]: https://en.wikipedia.org/wiki/Bron%E2%80%93Kerbosch_algorithm
87//! [Connected components]: https://en.wikipedia.org/wiki/Connected_component_(graph_theory)
88//! [DFS]: https://en.wikipedia.org/wiki/Depth-first_search
89//! [Dijkstra]: https://en.wikipedia.org/wiki/Dijkstra's_algorithm
90//! [Edmonds Karp]: https://en.wikipedia.org/wiki/Edmonds–Karp_algorithm
91//! [Floyd]: https://en.wikipedia.org/wiki/Cycle_detection#Floyd's_tortoise_and_hare
92//! [Fringe]: https://en.wikipedia.org/wiki/Fringe_search
93//! [IDA*]: https://en.wikipedia.org/wiki/Iterative_deepening_A*
94//! [IDDFS]: https://en.wikipedia.org/wiki/Iterative_deepening_depth-first_search
95//! [Kruskal]: https://en.wikipedia.org/wiki/Kruskal's_algorithm
96//! [Kuhn-Munkres]: https://en.wikipedia.org/wiki/Hungarian_algorithm
97//! [Prim]: https://en.wikipedia.org/wiki/Prim's_algorithm
98//! [Rust]: https://rust-lang.org/
99//! [Strongly connected components]: https://en.wikipedia.org/wiki/Strongly_connected_component
100//! [Topological sorting]: https://en.wikipedia.org/wiki/Topological_sorting
101//! [Yen]: https://en.wikipedia.org/wiki/Yen's_algorithm
102
103use deprecate_until::deprecate_until;
104pub use num_traits;
105
106pub mod directed;
107pub mod grid;
108pub mod kuhn_munkres;
109pub mod matrix;
110pub mod undirected;
111pub mod utils;
112
113use indexmap::{IndexMap, IndexSet};
114use rustc_hash::FxHasher;
115use std::hash::BuildHasherDefault;
116
117type FxIndexMap<K, V> = IndexMap<K, V, BuildHasherDefault<FxHasher>>;
118type FxIndexSet<K> = IndexSet<K, BuildHasherDefault<FxHasher>>;
119
120/// Export all public functions and structures for an easy access.
121pub mod prelude {
122 pub use crate::directed::astar::*;
123 pub use crate::directed::bfs::*;
124 pub use crate::directed::count_paths::*;
125 pub use crate::directed::cycle_detection::*;
126 pub use crate::directed::dfs::*;
127 pub use crate::directed::dijkstra::*;
128 pub use crate::directed::edmonds_karp::*;
129 pub use crate::directed::fringe::*;
130 pub use crate::directed::idastar::*;
131 pub use crate::directed::iddfs::*;
132 pub use crate::directed::strongly_connected_components::*;
133 pub use crate::directed::topological_sort::*;
134 pub use crate::directed::yen::*;
135 pub use crate::grid::*;
136 pub use crate::kuhn_munkres::*;
137 pub use crate::matrix::*;
138 pub use crate::undirected::cliques::*;
139 pub use crate::undirected::connected_components::*;
140 pub use crate::undirected::kruskal::*;
141 pub use crate::utils::*;
142}
143
144/// Deprecated: moved into the `directed` module.
145#[deprecate_until(
146 note = "use directed::cycle_detection or the prelude instead",
147 since = "4.3.1",
148 remove = "> 4.x"
149)]
150pub mod cycle_detection {
151 pub use crate::directed::cycle_detection::*;
152}