| Crates.io | graph-cycles |
| lib.rs | graph-cycles |
| version | 0.3.0 |
| created_at | 2023-01-13 13:01:46.915907+00 |
| updated_at | 2025-06-05 08:12:09.224253+00 |
| description | Detect all cycles in a petgraph graph |
| homepage | |
| repository | https://github.com/a-maier/graph-cycles |
| max_upload_size | |
| id | 757912 |
| size | 26,439 |
Find all cycles in a graph
A naive implementation of Johnson's algorithm to find all cycles in a graph. Based on petgraph.
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:?}");
});
This crate is essentially untested. Tests are welcome!
Donald B. Johnson, Finding all the elementary circuits of a directed graph, SIAM Journal on Computing, 1975.
License: MIT or Apache-2.0