Change path graph size parameter by 1, add doc link and P(n) notation

This commit is contained in:
2026-10-18 11:57:51 +02:00
parent 421207a1cd
commit 026005fe46
+11 -8
View File
@@ -46,16 +46,19 @@ where
/// Generates a path graph. /// Generates a path graph.
/// ///
/// Generates a path with `n` edges in `graph` as a new connected component and returns the new /// Generates a [path graph] *P(n)* with `n` vertices and `n - 1` edges in `graph` as a new
/// vertices *v0, v1, ..., vn* and edges *e0, e1, ..., e(n - 1)* in order, i.e. *e0 = (v0, v1)*, /// connected component and returns the new vertices *v0, v1, ..., v(n - 1)* and edges
/// *e1 = (v1, v2)*, etc. /// *e0, e1, ..., e(n - 2)* in order, i.e. *e0 = (v0, v1)*, *e1 = (v1, v2)*, etc. and *v0* and
/// *v(n-1)* being the two terminal vertices
/// ///
/// For `n == 0`, only a single vertex and no edges are added. /// For `n == 0`, nothing is added. For `n == 1`, only a single vertex and no edges are added.
///
/// [Path graph]: https://en.wikipedia.org/wiki/Path_graph
pub fn path<G: GraphTopologyAddition>(graph: &mut G, n: usize) -> (Vec<G::Vertex>, Vec<G::Edge>) { pub fn path<G: GraphTopologyAddition>(graph: &mut G, n: usize) -> (Vec<G::Vertex>, Vec<G::Edge>) {
graph.reserve_vertices(n + 1); graph.reserve_vertices(n);
graph.reserve_edges(n); graph.reserve_edges(n - 1);
let vertices: Vec<_> = (0..n + 1).map(|_| graph.add_vertex()).collect(); let vertices: Vec<_> = (0..n).map(|_| graph.add_vertex()).collect();
let edges = (0..n) let edges = (0..n - 1)
.map(|i| graph.add_edge(vertices[i], vertices[i + 1])) .map(|i| graph.add_edge(vertices[i], vertices[i + 1]))
.collect(); .collect();
(vertices, edges) (vertices, edges)