Add generators::cycle, without tests

This commit is contained in:
2026-10-17 00:40:33 +02:00
parent ba3717f246
commit d1463544f7
+18
View File
@@ -61,6 +61,24 @@ pub fn path<G: GraphTopologyAddition>(graph: &mut G, n: usize) -> (Vec<G::Vertex
(vertices, edges) (vertices, edges)
} }
/// Generates a cycle graph.
///
/// Generates a cycle with `n` edges in `graph` as a new connected component and returns the new
/// vertices *v0, v1, ..., v(n - 1)* and edges *e0, e1, ..., e(n - 1)* in order, i.e.
/// *e0 = (v0, v1), e1 = (v1, v2), ..., e(n - 1) = (v(n - 1), v0)*.
///
/// For `n == 0`, nothing is added. For `n == 1`, a single vertex with a loop is added. For
/// `n == 2`, two vertices and two edges between them (multi-edge) are added.
pub fn cycle<G: GraphTopologyAddition>(graph: &mut G, n: usize) -> (Vec<G::Vertex>, Vec<G::Edge>) {
graph.reserve_vertices(n);
graph.reserve_edges(n);
let vertices: Vec<_> = (0..n).map(|_| graph.add_vertex()).collect();
let edges = (0..n)
.map(|i| graph.add_edge(vertices[i], vertices[if i < n - 1 { i + 1 } else { 0 }]))
.collect();
(vertices, edges)
}
/// Generates a Petersen graph. /// Generates a Petersen graph.
/// ///
/// Generates a [Petersen graph] in `graph` as a new connected component and returns the new /// Generates a [Petersen graph] in `graph` as a new connected component and returns the new