rustworkx_core/lib.rs
1// Licensed under the Apache License, Version 2.0 (the "License"); you may
2// not use this file except in compliance with the License. You may obtain
3// a copy of the License at
4//
5// http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10// License for the specific language governing permissions and limitations
11// under the License.
12
13//! # rustworkx-core
14//!
15//! rustworkx-core is a graph algorithm crate built on top of [`petgraph`]. It offers
16//! a set of functions that are used in the larger rustworkx project but
17//! implemented in a generic manner for use by downstream rust projects.
18//!
19//! ## Usage
20//!
21//! First add this crate to your `Cargo.toml`:
22//!
23//! ```toml
24//! [dependencies]
25//! rustworkx-core = "0.11"
26//! ```
27//!
28//! Then in your code, it may be used something like this:
29//!
30//! ```rust
31//! use rustworkx_core::petgraph;
32//! use rustworkx_core::centrality::betweenness_centrality;
33//!
34//! let g = petgraph::graph::UnGraph::<i32, ()>::from_edges(&[
35//! (1, 2), (2, 3), (3, 4), (1, 4)
36//! ]);
37//! // Calculate the betweenness centrality
38//! let output = betweenness_centrality(&g, false, false, 200);
39//! assert_eq!(
40//! vec![Some(0.0), Some(0.5), Some(0.5), Some(0.5), Some(0.5)],
41//! output
42//! );
43//! ```
44//!
45//! ## Algorithm Modules
46//!
47//! The crate provides the following graph algorithm modules
48//!
49//! * [`centrality`](./centrality/index.html)
50//! * [`connectivity`](./connectivity/index.html)
51//! * [`max_weight_matching`](./max_weight_matching/index.html)
52//! * [`shortest_path`](./shortest_path/index.html)
53//! * [`token_swapper`](./token_swapper/index.html)
54//! * [`traversal`](./traversal/index.html)
55//! * [`generators`](./generators/index.html)
56//!
57//! ## Graph Extensions
58//!
59//! The crate also provides traits which extend `petgraph` types with
60//! additional methods, when imported.
61//!
62//! For example, the
63//! [`contract_nodes`][graph_ext::ContractNodesDirected::contract_nodes] method
64//! becomes available for applicable graph types when the following trait is
65//! imported:
66//!
67//! ```rust
68//! use petgraph::prelude::*;
69//! use rustworkx_core::graph_ext::ContractNodesDirected;
70//!
71//! let mut dag: StableDiGraph<char, usize> = StableDiGraph::default();
72//! let m = dag.contract_nodes([], 'm', true).unwrap();
73//! ```
74//!
75//! See the documentation of [`graph_ext`] for a full listing of the available
76//! extensions and their compatibility with petgraph types.
77//!
78//! ## Release Notes
79//!
80//! The release notes for rustworkx-core are included as part of the rustworkx
81//! documentation which is hosted at:
82//!
83//! <https://www.rustworkx.org/release_notes.html>
84
85use std::convert::Infallible;
86
87/// A convenient type alias that by default assumes no error can happen.
88///
89/// It can be used to avoid type annotations when the function you want
90/// to use needs a callback that returns [`Result`] but in your case no
91/// error can happen.
92pub type Result<T, E = Infallible> = core::result::Result<T, E>;
93pub mod err;
94
95pub mod bipartite_coloring;
96/// Module for centrality algorithms.
97pub mod centrality;
98/// Module for coloring algorithms.
99pub mod coloring;
100pub mod connectivity;
101/// Module for algorithms that work on DAGs.
102pub mod dag_algo;
103pub mod generators;
104pub mod graph_ext;
105pub mod line_graph;
106/// Module for maximum weight matching algorithms.
107pub mod max_weight_matching;
108pub mod planar;
109pub mod shortest_path;
110pub mod traversal;
111// These modules define additional data structures
112pub mod dictmap;
113pub mod distancemap;
114mod min_scored;
115/// Module for swapping tokens
116pub mod token_swapper;
117pub mod utils;
118
119pub mod steiner_tree;
120
121// re-export petgraph so there is a consistent version available to users and
122// then only need to require rustworkx-core in their dependencies
123pub use petgraph;