Rename 'weight' parameter in Dijkstra's algorithm to 'weights'

This commit is contained in:
2026-07-31 10:23:47 +02:00
parent 0169343ac8
commit afaf161344
+8 -8
View File
@@ -64,7 +64,7 @@ pub struct DijkstraResult<V: Copy> {
/// [Dijkstra's algorithm] with custom edge weights, returns minimum distances and predecessors.
///
/// Calculates the shortest paths from `source` to all vertices in `graph` with edge weights given
/// by `weight` function. Returns the distances from `source` to each vertex, and the predecessors
/// by `weights` function. Returns the distances from `source` to each vertex, and the predecessors
/// of each vertex on some shortest path from `source` to that vertex. Returns `None` for any vertex
/// not connected to `source`.
///
@@ -91,13 +91,13 @@ pub struct DijkstraResult<V: Copy> {
/// ```
///
/// [Dijkstra's algorithm]: https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
pub fn dijkstra<G, W>(graph: &G, source: G::Vertex, weight: W) -> DijkstraResult<G::Vertex>
pub fn dijkstra<G, W>(graph: &G, source: G::Vertex, weights: W) -> DijkstraResult<G::Vertex>
where
G: GraphTopology,
W: Fn(G::Edge) -> u32,
{
let mut predecessors = graph.vertex_map(None);
let distances = dijkstra_impl(graph, source, weight, |adjacent, predecessor| {
let distances = dijkstra_impl(graph, source, weights, |adjacent, predecessor| {
predecessors[adjacent] = Some(predecessor);
});
DijkstraResult {
@@ -109,7 +109,7 @@ where
/// [Dijkstra's algorithm] with custom edge weights, returns minimum distances.
///
/// Calculates the shortest paths from `source` to all vertices in `graph` with edge weights given
/// by `weight` function. Returns the distances from `source` to each vertex. Returns `None` for any
/// by `weights` function. Returns the distances from `source` to each vertex. Returns `None` for any
/// vertex not connected to `source`.
///
/// # Panics
@@ -137,13 +137,13 @@ where
pub fn dijkstra_distances<G, W>(
graph: &G,
source: G::Vertex,
weight: W,
weights: W,
) -> EntityMap<G::Vertex, Option<u32>>
where
G: GraphTopology,
W: Fn(G::Edge) -> u32,
{
dijkstra_impl(graph, source, weight, |_, _| {})
dijkstra_impl(graph, source, weights, |_, _| {})
}
/// [Dijkstra's algorithm] with unit edge weights, returns minimum distances and predecessors.
@@ -223,7 +223,7 @@ where
fn dijkstra_impl<G, W, F>(
graph: &G,
source: G::Vertex,
weight: W,
weights: W,
mut on_relax: F,
) -> EntityMap<G::Vertex, Option<u32>>
where
@@ -242,7 +242,7 @@ where
while let Some(v) = heap.pop() {
for incidence in graph.incidences(v.vertex) {
let new_distance = distances[v.vertex].unwrap() + weight(incidence.1);
let new_distance = distances[v.vertex].unwrap() + weights(incidence.1);
if match distances[incidence.0] {
None => true,
Some(old_distance) if old_distance > new_distance => true,