Crates.io | toposort-scc |
lib.rs | toposort-scc |
version | 0.5.4 |
source | src |
created_at | 2020-07-23 19:08:50.090905 |
updated_at | 2020-07-24 15:10:42.862539 |
description | An implementation of Kahn's algorithm for topological sorting and Kosaraju's algorithm for strongly connected components |
homepage | |
repository | https://github.com/Ferdi265/toposort-scc |
max_upload_size | |
id | 268714 |
size | 43,577 |
toposort-scc
An implementation of Kahn's algorithm for topological sorting and Kosaraju's algorithm for strongly connected components.
IndexGraph
)O(V + E)
time and O(V)
additional space (Kahn's algorithm)O(V + E)
time and O(V)
additional space
(Kosaraju's algorithm).toposort()
and
.scc()
) or as a combined method (.toposort_or_scc()
) on IndexGraph
The id-arena
feature adds an additional wrapper type (ArenaGraph
) that
allows topological sorting and finding of strongly connected components on
arbitrary graph structures built with the id-arena
crate by creating a
proxy graph that is sorted and returning a list of indices into the original
graph.
This example creates an IndexGraph
of the example graph from the
Wikipedia page for
Topological sorting.
A copy of the graph with cycles in it is created to demonstrate finding of strongly connected components.
use toposort_scc::IndexGraph;
let g = IndexGraph::from_adjacency_list(&vec![
vec![3],
vec![3, 4],
vec![4, 7],
vec![5, 6, 7],
vec![6],
vec![],
vec![],
vec![]
]);
let mut g2 = g.clone();
g2.add_edge(0, 0); // trivial cycle [0]
g2.add_edge(6, 2); // cycle [2, 4, 6]
assert_eq!(g.toposort_or_scc(), Ok(vec![0, 1, 2, 3, 4, 5, 7, 6]));
assert_eq!(g2.toposort_or_scc(), Err(vec![vec![0], vec![4, 2, 6]]));
Documentation is provided via rustdoc, and can be built with cargo doc
, or
viewed online at
docs.rs/toposort-scc/.
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.