Add Petersen graph generator

This commit is contained in:
2026-07-18 23:38:08 +02:00
parent ad9850800d
commit 8db7a86187
2 changed files with 66 additions and 0 deletions
+65
View File
@@ -0,0 +1,65 @@
use crate::traits::GraphTopology;
pub fn petersen<G>(
graph: &mut G,
) -> (
[<G as GraphTopology>::Vertex; 10],
[<G as GraphTopology>::Edge; 15],
)
where
G: GraphTopology,
{
const N: usize = 5;
const K: usize = 2;
let vertices = core::array::from_fn(|_| graph.add_vertex());
let edges = core::array::from_fn(|j| {
let i = j / 3;
match j % 3 {
0 => {
let outer = if i < N - 1 { i + 1 } else { 0 };
graph.add_edge(vertices[i], vertices[outer])
}
1 => {
let inner = if i < N - K { i + N + K } else { i + K };
graph.add_edge(vertices[i + N], vertices[inner])
}
_ => graph.add_edge(vertices[i], vertices[i + N]),
}
});
(vertices, edges)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::models::AppendGraph;
#[test]
fn petersen() {
let mut graph = AppendGraph::new();
let (vertices, edges) = super::petersen(&mut graph);
assert_eq!(graph.vertex_count(), 10, "unexpected number of added vertices");
assert_eq!(graph.edge_count(), 15, "unexpected number of added edges");
assert!(
graph.vertices().all(|v| graph.degree(v) == 3),
"all vertices should have degree 3"
);
assert_handles_in_graph(&graph, &vertices, &edges);
}
fn assert_handles_in_graph<G: GraphTopology>(
graph: &G,
vertices: &[G::Vertex],
edges: &[G::Edge],
) {
assert!(
vertices.iter().all(|&v| graph.vertices().any(|u| u == v)),
"all returned vertices should be in the graph"
);
assert!(
edges.iter().all(|&e| graph.edges().any(|f| f == e)),
"all returned edges should be in the graph"
);
}
}
+1
View File
@@ -128,6 +128,7 @@
//! [`GraphTopologyDeletion`]: crate::traits::GraphTopologyDeletion
pub mod algorithms;
pub mod generators;
pub mod maps;
pub mod models;
pub mod traits;