Add Dijkstra variant without returning predecessors
This commit is contained in:
+29
-12
@@ -26,20 +26,37 @@ pub struct DijkstraResult<V> {
|
|||||||
pub predecessors: Vec<Option<V>>,
|
pub predecessors: Vec<Option<V>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Maybe variant of Dijkstra's algorithm without predecessors?
|
|
||||||
// TODO: Replace Vec storage with VertexMap, this should make the bound on G::Vertex here obsolete.
|
|
||||||
pub fn dijkstra<G>(graph: &G, source: G::Vertex) -> DijkstraResult<G::Vertex>
|
pub fn dijkstra<G>(graph: &G, source: G::Vertex) -> DijkstraResult<G::Vertex>
|
||||||
where
|
where
|
||||||
G: GraphTopology,
|
G: GraphTopology,
|
||||||
G::Vertex: Into<usize>,
|
G::Vertex: Into<usize>,
|
||||||
{
|
{
|
||||||
let mut result = DijkstraResult {
|
let mut predecessors = vec![None; graph.vertex_count()];
|
||||||
distances: vec![None; graph.vertex_count()],
|
let distances = dijkstra_impl(graph, source, |neighbor, predecessor| {
|
||||||
predecessors: vec![None; graph.vertex_count()],
|
predecessors[neighbor.into()] = Some(predecessor);
|
||||||
};
|
});
|
||||||
|
DijkstraResult { distances, predecessors }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn dijkstra_distances<G>(graph: &G, source: G::Vertex) -> Vec<Option<u32>>
|
||||||
|
where
|
||||||
|
G: GraphTopology,
|
||||||
|
G::Vertex: Into<usize>,
|
||||||
|
{
|
||||||
|
dijkstra_impl(graph, source, |_, _| {})
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Replace Vec storage with VertexMap, this should make the bound on G::Vertex here obsolete.
|
||||||
|
fn dijkstra_impl<G, F>(graph: &G, source: G::Vertex, mut on_relax: F) -> Vec<Option<u32>>
|
||||||
|
where
|
||||||
|
G: GraphTopology,
|
||||||
|
G::Vertex: Into<usize>,
|
||||||
|
F: FnMut(G::Vertex, G::Vertex),
|
||||||
|
{
|
||||||
|
let mut distances = vec![None; graph.vertex_count()];
|
||||||
let mut heap = BinaryHeap::new();
|
let mut heap = BinaryHeap::new();
|
||||||
|
|
||||||
result.distances[source.into()] = Some(0);
|
distances[source.into()] = Some(0);
|
||||||
heap.push(DistanceOrderedVertex {
|
heap.push(DistanceOrderedVertex {
|
||||||
vertex: source,
|
vertex: source,
|
||||||
distance: 0,
|
distance: 0,
|
||||||
@@ -49,14 +66,14 @@ where
|
|||||||
for neighbor in graph.neighbors(v.vertex) {
|
for neighbor in graph.neighbors(v.vertex) {
|
||||||
// TODO: Add a way to provide custom edge weights for Dijkstra's algorithm.
|
// TODO: Add a way to provide custom edge weights for Dijkstra's algorithm.
|
||||||
let edge_weight = 1;
|
let edge_weight = 1;
|
||||||
let new_distance = result.distances[v.vertex.into()].unwrap() + edge_weight;
|
let new_distance = distances[v.vertex.into()].unwrap() + edge_weight;
|
||||||
if match result.distances[neighbor.into()] {
|
if match distances[neighbor.into()] {
|
||||||
None => true,
|
None => true,
|
||||||
Some(old_distance) if old_distance > new_distance => true,
|
Some(old_distance) if old_distance > new_distance => true,
|
||||||
_ => false,
|
_ => false,
|
||||||
} {
|
} {
|
||||||
result.distances[neighbor.into()] = Some(new_distance);
|
distances[neighbor.into()] = Some(new_distance);
|
||||||
result.predecessors[neighbor.into()] = Some(v.vertex);
|
on_relax(neighbor, v.vertex);
|
||||||
heap.push(DistanceOrderedVertex {
|
heap.push(DistanceOrderedVertex {
|
||||||
vertex: neighbor,
|
vertex: neighbor,
|
||||||
distance: new_distance,
|
distance: new_distance,
|
||||||
@@ -64,5 +81,5 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result
|
distances
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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]) {
|
fn make_test_graph() -> (AppendGraph, [<AppendGraph as GraphTopology>::Vertex; 10]) {
|
||||||
let mut graph = AppendGraph::new();
|
let mut graph = AppendGraph::new();
|
||||||
let vertices = core::array::from_fn::<_, 10, _>(|_| graph.add_vertex());
|
let vertices = core::array::from_fn::<_, 10, _>(|_| graph.add_vertex());
|
||||||
|
|||||||
Reference in New Issue
Block a user