-
Notifications
You must be signed in to change notification settings - Fork 0
/
dijkstras_algorithm.rs
48 lines (41 loc) · 1.29 KB
/
dijkstras_algorithm.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use crate::extended_number::{ExtendedNumber, HasMaxValue};
use crate::graph::Graph;
/// calculate shortest path from `source` and return sum of weight. If can't reach from `source`, return infinity.
pub fn dijkstras_algorithm<
W: Default
+ HasMaxValue<S = W>
+ Into<ExtendedNumber<W>>
+ Clone
+ Copy
+ Ord
+ std::ops::Add<Output = W>,
>(
graph: &dyn Graph<Weight = W>,
source: usize,
) -> Vec<ExtendedNumber<W>> {
assert!(source < graph.size());
let mut dist = vec![Into::<ExtendedNumber<W>>::into(W::M); graph.size()];
dist[source] = Into::<ExtendedNumber<W>>::into(W::default());
let mut seen = vec![false; graph.size()];
let mut hq = std::collections::BinaryHeap::default();
hq.push((
std::cmp::Reverse(Into::<ExtendedNumber<W>>::into(W::default())),
source,
));
while let Some((_, u)) = hq.pop() {
if seen[u] {
continue;
}
seen[u] = true;
for &(v, w) in graph.adjacent(u) {
if !seen[v] {
let d = dist[u] + Into::<ExtendedNumber<W>>::into(w);
if d < dist[v] {
dist[v] = d;
hq.push((std::cmp::Reverse(d), v));
}
}
}
}
return dist;
}