Add generators::complete, without tests

This commit is contained in:
2026-10-18 11:58:26 +02:00
parent 026005fe46
commit 32fae0b24c
+22
View File
@@ -84,6 +84,28 @@ pub fn cycle<G: GraphTopologyAddition>(graph: &mut G, n: usize) -> (Vec<G::Verte
(vertices, edges)
}
/// Generates a complete graph.
///
/// Generates a [complete graph] *K(n)* with `n` vertices and `n * (n - 1) / 2` edges in `graph` as
/// a new connected component and returns the new vertices and edges.
///
/// For `n == 0`, nothing is added. For `n == 1`, only a single vertex is added.
///
/// [Complete graph]: https://en.wikipedia.org/wiki/Complete_graph
pub fn complete<G: GraphTopologyAddition>(graph: &mut G, n: usize) -> (Vec<G::Vertex>, Vec<G::Edge>) {
let capacity = n * (n - 1) / 2;
graph.reserve_vertices(n);
graph.reserve_edges(capacity);
let vertices: Vec<_> = (0..n).map(|_| graph.add_vertex()).collect();
let mut edges = Vec::with_capacity(capacity);
for (i, &u) in vertices.iter().enumerate() {
for &v in &vertices[i + 1..] {
edges.push(graph.add_edge(u, v));
}
}
(vertices, edges)
}
/// Generates a Petersen graph.
///
/// Generates a [Petersen graph] in `graph` as a new connected component and returns the new