#graph #cycle #detect #graph-algorithms #petgraph

graph-cycles

Detect all cycles in a petgraph graph

3 releases (breaking)

Uses new Rust 2024

0.3.0 Jun 5, 2025
0.2.0 Feb 28, 2025
0.1.0 Jan 13, 2023

#137 in Math

Download history 1353/week @ 2025-02-23 3508/week @ 2025-03-02 2541/week @ 2025-03-09 1754/week @ 2025-03-16 2238/week @ 2025-03-23 3164/week @ 2025-03-30 2177/week @ 2025-04-06 2849/week @ 2025-04-13 2894/week @ 2025-04-20 5005/week @ 2025-04-27 2892/week @ 2025-05-04 2647/week @ 2025-05-11 2296/week @ 2025-05-18 2201/week @ 2025-05-25 1959/week @ 2025-06-01 2302/week @ 2025-06-08

8,949 downloads per month
Used in 24 crates (2 directly)

MIT/Apache

10KB
160 lines

graph-cycles

Find all cycles in a graph

A naive implementation of Johnson's algorithm to find all cycles in a graph. Based on petgraph.

Example

The triangle graph has exactly one cycle, namely the full graph itself.

use graph_cycles::Cycles;
use petgraph::graph::Graph;

let g = Graph::<(), ()>::from_edges([(0, 1), (1, 2), (2, 0)]);

// find all cycles
let cycles = g.cycles();
assert_eq!(cycles.len(), 1);
assert_eq!(cycles[0].len(), 3);

// print each cycle in turn
g.visit_all_cycles(|_g, c| {
   println!("Found new cycle with vertices {c:?}");
});

Caveats

This crate is essentially untested. Tests are welcome!

References

Donald B. Johnson, Finding all the elementary circuits of a directed graph, SIAM Journal on Computing, 1975.

License: MIT or Apache-2.0

Dependencies

~3.5MB
~49K SLoC