#graph #graph-algorithms

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

#1429 in Math

Download history 2008/week @ 2025-10-18 2305/week @ 2025-10-25 2040/week @ 2025-11-01 1769/week @ 2025-11-08 1732/week @ 2025-11-15 3011/week @ 2025-11-22 2670/week @ 2025-11-29 1910/week @ 2025-12-06 1853/week @ 2025-12-13 1538/week @ 2025-12-20 744/week @ 2025-12-27 1320/week @ 2026-01-03 1646/week @ 2026-01-10 1321/week @ 2026-01-17 1009/week @ 2026-01-24 1346/week @ 2026-01-31

5,536 downloads per month
Used in 24 crates (4 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
~50K SLoC