#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

#1954 in Math

Download history 1795/week @ 2025-12-14 1489/week @ 2025-12-21 810/week @ 2025-12-28 1312/week @ 2026-01-04 1632/week @ 2026-01-11 1371/week @ 2026-01-18 942/week @ 2026-01-25 1523/week @ 2026-02-01 1945/week @ 2026-02-08 2408/week @ 2026-02-15 1848/week @ 2026-02-22 2905/week @ 2026-03-01 3567/week @ 2026-03-08 5579/week @ 2026-03-15 8461/week @ 2026-03-22 9208/week @ 2026-03-29

27,385 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

~3MB
~46K SLoC