1 unstable release

0.1.0 Jan 13, 2023

#1412 in Math

Download history 1845/week @ 2024-09-05 2330/week @ 2024-09-12 2578/week @ 2024-09-19 2158/week @ 2024-09-26 2213/week @ 2024-10-03 1796/week @ 2024-10-10 2806/week @ 2024-10-17 3034/week @ 2024-10-24 2319/week @ 2024-10-31 1863/week @ 2024-11-07 1725/week @ 2024-11-14 1527/week @ 2024-11-21 1516/week @ 2024-11-28 1264/week @ 2024-12-05 1583/week @ 2024-12-12 695/week @ 2024-12-19

5,378 downloads per month
Used in 29 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


lib.rs:

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.

The node identifier of the underlying graph

Dependencies

~3.5MB
~56K SLoC