This repository contains an implementation of Kosaraju's algorithm in Rust. Kosaraju's algorithm is used to find the strongly connected components (SCCs) of a directed graph.
Kosaraju's algorithm works in two main phases:
- Perform a Depth-First Search (DFS) on the original graph to determine the finish order of nodes.
- Transpose the graph (reverse the direction of all edges) and perform DFS in the order of decreasing finish times to identify SCCs.
- Efficiently finds all strongly connected components in a directed graph.
- Uses Rust's standard library collections for graph representation.
- Demonstrates the use of DFS and graph transposition.
- Rust and Cargo installed on your system. You can download them from rust-lang.org.
-
Clone the repository:
git clone https://github.com/yourusername/kosaraju-rust.git cd kosaraju-rust
-
Build the project:
cargo build
-
Run the project:
cargo run
You can modify the main
function in src/main.rs
to test the algorithm with different graphs. Here is an example of how to define a graph and run the algorithm:
fn main() {
let mut graph = HashMap::new();
graph.insert(1, vec![2]);
graph.insert(2, vec![3]);
graph.insert(3, vec![1]);
graph.insert(3, vec![4]);
graph.insert(4, vec![5]);
graph.insert(5, vec![4]);
let scc = kosaraju(&graph);
println!("Strongly Connected Components: {:?}", scc);
}