Skip to main content

pathfinding/
lib.rs

1#![forbid(missing_docs)]
2//! # pathfinding
3//!
4//! [![Current Version](https://img.shields.io/crates/v/pathfinding.svg)](https://crates.io/crates/pathfinding)
5//! [![Documentation](https://docs.rs/pathfinding/badge.svg)](https://docs.rs/pathfinding)
6//! [![License: Apache-2.0/MIT](https://img.shields.io/crates/l/pathfinding.svg)](#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](undirected/cliques/index.html): 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//! ## Working with Graphs
49//!
50//! This library does not provide a fixed graph data structure. Instead, the algorithms accept
51//! a **successor function** that defines how to navigate from one node to its neighbors.
52//!
53//! For comprehensive examples showing how to represent graphs (adjacency lists, adjacency matrices,
54//! edge lists, etc.) and use them with various algorithms, see the
55//! [Graph Guide](https://github.com/evenfurther/pathfinding/blob/main/GRAPH_GUIDE.md).
56//!
57//! ## Example
58//!
59//! We will search the shortest path on a chess board to go from (1, 1) to (4, 6) doing only knight
60//! moves.
61//!
62//! ``` rust
63//! use pathfinding::prelude::bfs;
64//!
65//! #[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
66//! struct Pos(i32, i32);
67//!
68//! impl Pos {
69//!   fn successors(&self) -> Vec<Pos> {
70//!     let &Pos(x, y) = self;
71//!     vec![Pos(x+1,y+2), Pos(x+1,y-2), Pos(x-1,y+2), Pos(x-1,y-2),
72//!          Pos(x+2,y+1), Pos(x+2,y-1), Pos(x-2,y+1), Pos(x-2,y-1)]
73//!   }
74//! }
75//!
76//! static GOAL: Pos = Pos(4, 6);
77//! let result = bfs(&Pos(1, 1), |p| p.successors(), |p| *p == GOAL);
78//! assert_eq!(result.expect("no path found").len(), 5);
79//! ```
80//!
81//! ## Note on floating-point types
82//!
83//! Several algorithms require that the numerical types used to describe
84//! edge weights implement `Ord`. If you wish to use Rust built-in
85//! floating-point types (such as `f32`) that implement `PartialOrd`
86//! in this context, you can wrap them into compliant types using the
87//! [ordered-float](https://crates.io/crates/ordered-float) crate.
88//!
89//! The minimum supported Rust version (MSRV) is Rust 1.87.0.
90//!
91//! [A*]: https://en.wikipedia.org/wiki/A*_search_algorithm
92//! [BFS]: https://en.wikipedia.org/wiki/Breadth-first_search
93//! [Bidirectional search]: https://en.wikipedia.org/wiki/Bidirectional_search
94//! [Brent]: https://en.wikipedia.org/wiki/Cycle_detection#Brent's_algorithm
95//! [BronKerbosch]: https://en.wikipedia.org/wiki/Bron%E2%80%93Kerbosch_algorithm
96//! [Connected components]: https://en.wikipedia.org/wiki/Connected_component_(graph_theory)
97//! [DFS]: https://en.wikipedia.org/wiki/Depth-first_search
98//! [Dijkstra]: https://en.wikipedia.org/wiki/Dijkstra's_algorithm
99//! [Edmonds Karp]: https://en.wikipedia.org/wiki/Edmonds–Karp_algorithm
100//! [Floyd]: https://en.wikipedia.org/wiki/Cycle_detection#Floyd's_tortoise_and_hare
101//! [Fringe]: https://en.wikipedia.org/wiki/Fringe_search
102//! [IDA*]: https://en.wikipedia.org/wiki/Iterative_deepening_A*
103//! [IDDFS]: https://en.wikipedia.org/wiki/Iterative_deepening_depth-first_search
104//! [Kruskal]: https://en.wikipedia.org/wiki/Kruskal's_algorithm
105//! [Kuhn-Munkres]: https://en.wikipedia.org/wiki/Hungarian_algorithm
106//! [Prim]: https://en.wikipedia.org/wiki/Prim's_algorithm
107//! [Rust]: https://rust-lang.org/
108//! [Strongly connected components]: https://en.wikipedia.org/wiki/Strongly_connected_component
109//! [Topological sorting]: https://en.wikipedia.org/wiki/Topological_sorting
110//! [Yen]: https://en.wikipedia.org/wiki/Yen's_algorithm
111
112use deprecate_until::deprecate_until;
113pub use num_traits;
114
115pub mod directed;
116pub mod grid;
117pub mod kuhn_munkres;
118pub mod matrix;
119pub mod undirected;
120pub mod utils;
121
122mod noderefs;
123pub use noderefs::NodeRefs;
124
125use indexmap::{IndexMap, IndexSet};
126use rustc_hash::FxHasher;
127use std::hash::BuildHasherDefault;
128
129type FxIndexMap<K, V> = IndexMap<K, V, BuildHasherDefault<FxHasher>>;
130type FxIndexSet<K> = IndexSet<K, BuildHasherDefault<FxHasher>>;
131
132/// Export all public functions and structures for an easy access.
133pub mod prelude {
134    pub use crate::directed::astar::*;
135    pub use crate::directed::bfs::*;
136    pub use crate::directed::count_paths::*;
137    pub use crate::directed::cycle_detection::*;
138    pub use crate::directed::dfs::*;
139    pub use crate::directed::dijkstra::*;
140    pub use crate::directed::edmonds_karp::*;
141    pub use crate::directed::fringe::*;
142    pub use crate::directed::idastar::*;
143    pub use crate::directed::iddfs::*;
144    pub use crate::directed::strongly_connected_components::*;
145    pub use crate::directed::topological_sort::*;
146    pub use crate::directed::yen::*;
147    pub use crate::grid::*;
148    pub use crate::kuhn_munkres::*;
149    pub use crate::matrix::*;
150    pub use crate::undirected::cliques::*;
151    pub use crate::undirected::connected_components::*;
152    pub use crate::undirected::kruskal::*;
153    pub use crate::utils::*;
154}
155
156/// Deprecated: moved into the `directed` module.
157#[deprecate_until(
158    note = "use directed::cycle_detection or the prelude instead",
159    since = "4.3.1",
160    remove = "> 4.x"
161)]
162pub mod cycle_detection {
163    pub use crate::directed::cycle_detection::*;
164}