Add Dijkstra variant without returning predecessors

This commit is contained in:
2026-04-22 15:50:45 +02:00
parent a250bde626
commit a939f9b889
2 changed files with 78 additions and 12 deletions
+49
View File
@@ -107,6 +107,55 @@ fn dijkstra() {
}
}
#[test]
fn dijkstra_distances_single_vertex() {
let mut graph = AppendGraph::new();
let v1 = graph.add_vertex();
let distances = algorithms::dijkstra_distances(&graph, v1);
assert_eq!(distances.len(), 1, "distances count must equal vertex count");
assert_eq!(distances[0], Some(0), "unexpected distance of source vertex");
}
#[test]
fn dijkstra_distances_disconnected() {
let mut graph = AppendGraph::new();
let v1 = graph.add_vertex();
graph.add_vertex();
let distances = algorithms::dijkstra_distances(&graph, v1);
assert_eq!(distances.len(), 2, "distances count must equal vertex count");
assert_eq!(distances[1], None, "unexpected distance of disconnected vertex");
}
#[test]
fn dijkstra_distances() {
let (graph, vertices) = make_test_graph();
let distances = algorithms::dijkstra_distances(&graph, vertices[0]);
let expected_distances_from_v0 = [
Some(0),
Some(1),
Some(2),
Some(2),
Some(2),
Some(3),
Some(3),
Some(3),
Some(3),
Some(4),
];
assert_eq!(
distances.len(),
graph.vertex_count(),
"distances count must equal vertex count"
);
for i in 0..graph.vertex_count() {
assert_eq!(
distances[i], expected_distances_from_v0[i],
"unexpected distance from {:?} to {:?}",
vertices[0], vertices[i]
);
}
}
fn make_test_graph() -> (AppendGraph, [<AppendGraph as GraphTopology>::Vertex; 10]) {
let mut graph = AppendGraph::new();
let vertices = core::array::from_fn::<_, 10, _>(|_| graph.add_vertex());