#graph-algorithms #cycle #graph

graph-cycles

Detect all cycles in a petgraph graph

2 unstable releases

Uses new Rust 2024

0.2.0 Feb 28, 2025
0.1.0 Jan 13, 2023

#244 in Math

Download history 490/week @ 2024-12-21 1180/week @ 2024-12-28 2292/week @ 2025-01-04 2644/week @ 2025-01-11 1985/week @ 2025-01-18 2860/week @ 2025-01-25 2565/week @ 2025-02-01 2445/week @ 2025-02-08 1503/week @ 2025-02-15 1319/week @ 2025-02-22 3570/week @ 2025-03-01 2298/week @ 2025-03-08 2006/week @ 2025-03-15 2038/week @ 2025-03-22 3258/week @ 2025-03-29 1655/week @ 2025-04-05

9,249 downloads per month
Used in 24 crates (2 directly)

MIT/Apache

10KB
144 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.

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
~47K SLoC