forked from TheAlgorithms/Rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
floyd_warshall.rs
191 lines (176 loc) · 6.17 KB
/
floyd_warshall.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use num_traits::Zero;
use std::collections::BTreeMap;
use std::ops::Add;
type Graph<V, E> = BTreeMap<V, BTreeMap<V, E>>;
/// Performs the Floyd-Warshall algorithm on the input graph
/// The graph is a weighted, directed graph with no negative cycles
///
/// Returns a map storing the distance from each node to all the others
/// I.e. For each vertex u, map[u][v] == Some(distance) means
/// distance is the sum of the weights of the edges on the shortest path
/// from u to v
///
/// For a key v, if map[v].len() == 0, then v cannot reach any other vertex, but is in the graph
/// (island node, or sink in the case of a directed graph)
pub fn floyd_warshall<V: Ord + Copy, E: Ord + Copy + Add<Output = E> + num_traits::Zero>(
graph: &Graph<V, E>,
) -> BTreeMap<V, BTreeMap<V, E>> {
let mut map: BTreeMap<V, BTreeMap<V, E>> = BTreeMap::new();
for (u, edges) in graph.iter() {
if !map.contains_key(u) {
map.insert(*u, BTreeMap::new());
}
map.entry(*u).or_default().insert(*u, Zero::zero());
for (v, weight) in edges.iter() {
if !map.contains_key(v) {
map.insert(*v, BTreeMap::new());
}
map.entry(*v).or_default().insert(*v, Zero::zero());
map.entry(*u).and_modify(|mp| {
mp.insert(*v, *weight);
});
}
}
let keys = map.keys().copied().collect::<Vec<_>>();
for &k in &keys {
for &i in &keys {
if map[&i].get(&k).is_none() {
continue;
}
for &j in &keys {
if i == j {
continue;
}
if !map[&k].contains_key(&j) {
continue;
}
let entry_i_j = map[&i].get(&j);
let entry_i_k = map[&i][&k];
let entry_k_j = map[&k][&j];
match entry_i_j {
Some(&e) => {
if e > entry_i_k + entry_k_j {
map.entry(i).or_default().insert(j, entry_i_k + entry_k_j);
}
}
None => {
map.entry(i).or_default().insert(j, entry_i_k + entry_k_j);
}
};
}
}
}
map
}
#[cfg(test)]
mod tests {
use super::{floyd_warshall, Graph};
use std::collections::BTreeMap;
fn add_edge<V: Ord + Copy, E: Ord + Copy>(graph: &mut Graph<V, E>, v1: V, v2: V, c: E) {
graph.entry(v1).or_insert_with(BTreeMap::new).insert(v2, c);
}
fn bi_add_edge<V: Ord + Copy, E: Ord + Copy>(graph: &mut Graph<V, E>, v1: V, v2: V, c: E) {
add_edge(graph, v1, v2, c);
add_edge(graph, v2, v1, c);
}
#[test]
fn single_vertex() {
let mut graph: Graph<usize, usize> = BTreeMap::new();
graph.insert(0, BTreeMap::new());
let mut dists = BTreeMap::new();
dists.insert(0, BTreeMap::new());
dists.get_mut(&0).unwrap().insert(0, 0);
assert_eq!(floyd_warshall(&graph), dists);
}
#[test]
fn single_edge() {
let mut graph = BTreeMap::new();
bi_add_edge(&mut graph, 0, 1, 2);
bi_add_edge(&mut graph, 1, 2, 3);
let mut dists_0 = BTreeMap::new();
dists_0.insert(0, BTreeMap::new());
dists_0.insert(1, BTreeMap::new());
dists_0.insert(2, BTreeMap::new());
dists_0.get_mut(&0).unwrap().insert(0, 0);
dists_0.get_mut(&1).unwrap().insert(1, 0);
dists_0.get_mut(&2).unwrap().insert(2, 0);
dists_0.get_mut(&1).unwrap().insert(0, 2);
dists_0.get_mut(&0).unwrap().insert(1, 2);
dists_0.get_mut(&1).unwrap().insert(2, 3);
dists_0.get_mut(&2).unwrap().insert(1, 3);
dists_0.get_mut(&2).unwrap().insert(0, 5);
dists_0.get_mut(&0).unwrap().insert(2, 5);
assert_eq!(floyd_warshall(&graph), dists_0);
}
#[test]
fn graph_1() {
let mut graph = BTreeMap::new();
add_edge(&mut graph, 'a', 'c', 12);
add_edge(&mut graph, 'a', 'd', 60);
add_edge(&mut graph, 'b', 'a', 10);
add_edge(&mut graph, 'c', 'b', 20);
add_edge(&mut graph, 'c', 'd', 32);
add_edge(&mut graph, 'e', 'a', 7);
let mut dists_a = BTreeMap::new();
dists_a.insert('d', BTreeMap::new());
dists_a.entry('a').or_insert(BTreeMap::new()).insert('a', 0);
dists_a.entry('b').or_insert(BTreeMap::new()).insert('b', 0);
dists_a.entry('c').or_insert(BTreeMap::new()).insert('c', 0);
dists_a.entry('d').or_insert(BTreeMap::new()).insert('d', 0);
dists_a.entry('e').or_insert(BTreeMap::new()).insert('e', 0);
dists_a
.entry('a')
.or_insert(BTreeMap::new())
.insert('c', 12);
dists_a
.entry('c')
.or_insert(BTreeMap::new())
.insert('a', 30);
dists_a
.entry('c')
.or_insert(BTreeMap::new())
.insert('b', 20);
dists_a
.entry('c')
.or_insert(BTreeMap::new())
.insert('d', 32);
dists_a.entry('e').or_insert(BTreeMap::new()).insert('a', 7);
dists_a
.entry('b')
.or_insert(BTreeMap::new())
.insert('a', 10);
dists_a
.entry('a')
.or_insert(BTreeMap::new())
.insert('d', 44);
dists_a
.entry('a')
.or_insert(BTreeMap::new())
.insert('b', 32);
dists_a
.entry('a')
.or_insert(BTreeMap::new())
.insert('b', 32);
dists_a
.entry('b')
.or_insert(BTreeMap::new())
.insert('c', 22);
dists_a
.entry('b')
.or_insert(BTreeMap::new())
.insert('d', 54);
dists_a
.entry('e')
.or_insert(BTreeMap::new())
.insert('c', 19);
dists_a
.entry('e')
.or_insert(BTreeMap::new())
.insert('d', 51);
dists_a
.entry('e')
.or_insert(BTreeMap::new())
.insert('b', 39);
assert_eq!(floyd_warshall(&graph), dists_a);
}
}