diff --git a/src/generators.rs b/src/generators.rs index 5bd2a5e..5a628e4 100644 --- a/src/generators.rs +++ b/src/generators.rs @@ -44,6 +44,23 @@ where ) } +/// Generates a path graph. +/// +/// Generates a path with `n` edges in `graph` as a new connected component and returns the new +/// vertices *v0, v1, ..., vn* and edges *e0, e1, ..., e(n - 1)* in order, i.e. *e0 = (v0, v1)*, +/// *e1 = (v1, v2)*, etc. +/// +/// For `n == 0`, only a single vertex and no edges are added. +pub fn path(graph: &mut G, n: usize) -> (Vec, Vec) { + graph.reserve_vertices(n + 1); + graph.reserve_edges(n); + let vertices: Vec<_> = (0..n + 1).map(|_| graph.add_vertex()).collect(); + let edges = (0..n) + .map(|i| graph.add_edge(vertices[i], vertices[i + 1])) + .collect(); + (vertices, edges) +} + /// Generates a Petersen graph. /// /// Generates a [Petersen graph] in `graph` as a new connected component and returns the new