strongly-connected-components
Decomposes a graph into Strongly Connected Components and sorts them in topological order.
Installation
Add this to your Cargo.toml:
[]
= "0.1"
Usage
1. Define the graph structure
// ┌───┐ ┌───┐ ┌───┐
// │ a ├─►│ b ├─►│ c │
// └───┘ └─▲─┘ └─┬─┘
// └──────┘
let mut graph = new;
let a: Node = graph.new_node;
let b: Node = graph.new_node;
let c: Node = graph.new_node;
graph.new_edge;
graph.new_edge;
graph.new_edge;
2. Run the algorithm and create the SccDecomposition
let decomp: SccDecomposition = graph.find_sccs;
// There are 2 SCCs in the example graph: `[a]` and `[b, c]`.
assert_eq!;
3. Check whether two nodes belong to the same SCC
let a_scc: &Scc = decomp.scc_of_node;
let b_scc: &Scc = decomp.scc_of_node;
let c_scc: &Scc = decomp.scc_of_node;
// Node `a` belongs to a different SCC than `b` and `c`.
assert_ne!;
assert_ne!;
// Nodes `b` and `c` belong to the same SCC.
assert_eq!;
4. List all nodes belonging to an SCC
assert_eq!;
let a_scc_all: = a_scc.iter_nodes.collect;
assert_eq!;
assert_eq!;
let b_scc_all: = b_scc.iter_nodes.collect;
assert_eq!;
5. List SCCs in topological order
let sccs: = decomp.iter_sccs.collect;
assert_eq!;
6. List nodes in topological order
let nodes: = decomp.iter_nodes.collect;
// Either of those would be a valid order,
// because `b` and `c` belong to the same SCC.
assert!;
API Overview
| Type | Description |
|---|---|
Graph |
The graph structure. Create nodes with new_node() and edges with new_edge(). |
Node |
A node in the graph. |
Scc |
A single strongly connected component. Iterate over nodes with iter_nodes(). |
SccDecomposition |
The result of running graph.find_sccs(). Query SCCs and iterate in topological order. |
License
This project is licensed under the MIT License - see the LICENSE file for details.