1 unstable release

0.1.0 Jan 13, 2023

#1371 in Math

Download history 702/week @ 2023-12-15 333/week @ 2023-12-22 717/week @ 2023-12-29 734/week @ 2024-01-05 785/week @ 2024-01-12 869/week @ 2024-01-19 822/week @ 2024-01-26 920/week @ 2024-02-02 684/week @ 2024-02-09 908/week @ 2024-02-16 791/week @ 2024-02-23 1263/week @ 2024-03-01 1005/week @ 2024-03-08 706/week @ 2024-03-15 904/week @ 2024-03-22 818/week @ 2024-03-29

3,594 downloads per month
Used in 25 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

~3MB
~58K SLoC