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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
use std::collections::HashSet;
use crate::graph::{Edge, Graph, Node};
#[derive(Clone)]
pub(crate) struct Waypoint {
pub leg: Option<Edge>,
pub previous: Option<Box<Waypoint>>,
pub node: Node,
}
impl Waypoint {
pub fn from(edge: Option<Edge>, node: Node, previous: Option<Box<Waypoint>>) -> Waypoint {
return Waypoint {
leg: edge,
previous,
node,
};
}
}
pub trait PathFinding {
fn execute(&self, source: Node, target: Node, graph: &Graph) -> Graph;
}
pub fn find(source: usize, target: usize, graph: &Graph, path_finding: Box<dyn PathFinding>) -> Graph {
let source_node = graph.nodes_lookup.get(&source);
let target_node = graph.nodes_lookup.get(&target);
if source_node.is_none() || target_node.is_none() {
return Graph::from(Vec::new());
};
return path_finding.execute(source_node.unwrap().clone(), target_node.unwrap().clone(), graph);
}
pub(crate) fn walk_back(waypoint: Waypoint) -> HashSet<Edge> {
let mut edges = HashSet::new();
let mut path = Some(Box::new(waypoint));
while path.is_some() {
let current = path.unwrap();
let leg = current.leg;
let previous = current.previous;
path = previous.clone();
if leg.is_some() {
edges.insert(leg.unwrap());
}
}
return edges;
}
#[test]
fn walk_back_with_only_one_waypoint_should_succeed() {
let waypoint = Waypoint::from(Some(Edge::from(0, 0, 1, 1.0)), Node {
id: 1,
edges: Vec::new(),
}, None);
let mut sum_weight = 0.0;
for edge in walk_back(waypoint) {
sum_weight += edge.weight;
}
assert_eq!(1.0, sum_weight)
}
#[test]
fn walk_back_without_leg_should_succeed() {
let waypoint = Waypoint::from(None, Node {
id: 1,
edges: Vec::new(),
}, None);
let edges = walk_back(waypoint);
assert_eq!(0, edges.len());
}
#[test]
fn walk_back_with_path_should_succeed() {
let edges = walk_back(stubbed_path());
let mut sum_weight = 0.0;
for edge in &edges {
sum_weight += edge.weight;
}
assert_eq!(10.0, sum_weight);
assert_eq!(10, edges.len());
}
#[test]
fn should_find_path_with_depth_first_search_in_undirected_graph() {
let graph = undirected_graph();
let dfs = find(0, 2, &graph,
Box::from(crate::depth_first::DepthFirstSearch {}) as Box<dyn PathFinding>);
let mut total_cost: f32 = 0.0;
for edge in dfs.edges {
total_cost += edge.weight;
}
assert_eq!(1.4285715, total_cost);
}
#[test]
fn should_find_path_with_depth_first_search_in_directed_graph() {
let dfs = find(4, 1, &directed_graph(),
Box::from(crate::depth_first::DepthFirstSearch {}) as Box<dyn PathFinding>);
let mut total_cost: f32 = 0.0;
for edge in dfs.edges {
total_cost += edge.weight;
}
assert_eq!(39.0, total_cost);
}
#[test]
fn should_find_path_with_breadth_first_search_in_undirected_graph() {
let graph = undirected_graph();
let bfs = find(0, 2, &graph,
Box::from(crate::breadth_first::BreadthFirstSearch {}) as Box<dyn PathFinding>);
let mut total_cost: f32 = 0.0;
for edge in bfs.edges {
total_cost += edge.weight;
}
assert_eq!(0.2857143, total_cost);
}
#[test]
fn should_find_path_with_breadth_first_search_in_directed_graph() {
let bfs = find(4, 1, &directed_graph(),
Box::from(crate::breadth_first::BreadthFirstSearch {}) as Box<dyn PathFinding>);
let mut total_cost: f32 = 0.0;
for edge in bfs.edges {
total_cost += edge.weight;
}
assert_eq!(39.0, total_cost);
}
#[test]
fn should_find_path_with_bi_breadth_first_search_in_undirected_graph() {
let graph = undirected_graph();
let bfs = find(0, 2, &graph,
Box::from(crate::breadth_first::BiBreadthFirstSearch {}) as Box<dyn PathFinding>);
let mut total_cost: f32 = 0.0;
for edge in bfs.edges {
total_cost += edge.weight;
}
assert_eq!(0.2857143, total_cost);
}
#[test]
fn should_find_path_with_bi_breadth_first_search_in_directed_graph() {
let bfs = find(4, 1, &directed_graph(),
Box::from(crate::breadth_first::BiBreadthFirstSearch {}) as Box<dyn PathFinding>);
let mut total_cost: f32 = 0.0;
for edge in bfs.edges {
total_cost += edge.weight;
}
assert_eq!(39.0, total_cost);
}
#[test]
fn should_find_path_with_one_edge() {
let mut edges = Vec::new();
edges.push(Edge::from(0, 0, 1, 1.0));
let bfs = find(0, 1, &Graph::from(edges),
Box::from(crate::breadth_first::BiBreadthFirstSearch {}) as Box<dyn PathFinding>);
let mut total_cost: f32 = 0.0;
for edge in &bfs.edges {
total_cost += edge.weight;
}
assert_eq!(1.0, total_cost);
assert_eq!(1, bfs.edges.len());
}
#[test]
fn should_not_find_path_with_same_target_and_source() {
let mut edges = Vec::new();
edges.push(Edge::from(0, 0, 1, 1.0));
let bfs = find(1, 1, &Graph::from(edges),
Box::from(crate::breadth_first::BiBreadthFirstSearch {}) as Box<dyn PathFinding>);
let mut total_cost: f32 = 0.0;
for edge in &bfs.edges {
total_cost += edge.weight;
}
assert_eq!(0.0, total_cost);
assert_eq!(0, bfs.edges.len());
}
#[test]
fn should_not_find_path_with_unknown_target() {
let mut edges = Vec::new();
edges.push(Edge::from(0, 0, 1, 1.0));
let bfs = find(0, 2, &Graph::from(edges),
Box::from(crate::breadth_first::BiBreadthFirstSearch {}) as Box<dyn PathFinding>);
let mut total_cost: f32 = 0.0;
for edge in &bfs.edges {
total_cost += edge.weight;
}
assert_eq!(0.0, total_cost);
assert_eq!(0, bfs.edges.len());
}
#[test]
fn should_not_find_path_with_unknown_source() {
let mut edges = Vec::new();
edges.push(Edge::from(0, 0, 1, 1.0));
let bfs = find(2, 0, &Graph::from(edges),
Box::from(crate::breadth_first::BiBreadthFirstSearch {}) as Box<dyn PathFinding>);
let mut total_cost: f32 = 0.0;
for edge in &bfs.edges {
total_cost += edge.weight;
}
assert_eq!(0.0, total_cost);
assert_eq!(0, bfs.edges.len());
}
#[test]
fn should_find_path_with_source_and_target_reversed() {
let mut edges = Vec::new();
edges.push(Edge::from(0, 0, 1, 1.0));
let bfs = find(1, 0, &Graph::from(edges),
Box::from(crate::breadth_first::BiBreadthFirstSearch {}) as Box<dyn PathFinding>);
let mut total_cost: f32 = 0.0;
for edge in &bfs.edges {
total_cost += edge.weight;
}
assert_eq!(1.0, total_cost);
assert_eq!(1, bfs.edges.len());
}
#[test]
fn should_find_path_with_bi_breadth_first_search_in_graphs_with_one_connection() {
let bfs = find(0, 13, &graphs_with_one_connection(),
Box::from(crate::breadth_first::BiBreadthFirstSearch {}) as Box<dyn PathFinding>);
let mut total_cost: f32 = 0.0;
for edge in bfs.edges {
total_cost += edge.weight;
}
assert_eq!(50.0, total_cost);
}
#[test]
fn should_find_path_with_dijkstra_in_graphs_with_one_connection() {
let bfs = find(0, 13, &graphs_with_one_connection(),
Box::from(crate::dijkstra::Dijkstra {}) as Box<dyn PathFinding>);
let mut total_cost: f32 = 0.0;
for edge in bfs.edges {
total_cost += edge.weight;
}
assert_eq!(50.0, total_cost);
}
#[cfg(test)]
fn undirected_graph() -> Graph {
let edge1 = Edge::from(0, 1, 2, 0.0);
let edge2 = Edge::from(1, 2, 1, 0.0);
let edge3 = Edge::from(2, 2, 3, 0.1428571429);
let edge4 = Edge::from(3, 3, 2, 0.1428571429);
let edge5 = Edge::from(4, 1, 0, 0.2857142857);
let edge6 = Edge::from(5, 0, 1, 0.2857142857);
let edge7 = Edge::from(6, 3, 4, 0.2857142857);
let edge8 = Edge::from(7, 4, 3, 0.2857142857);
let edge9 = Edge::from(8, 1, 3, 0.4285714286);
let edge10 = Edge::from(9, 3, 1, 0.4285714286);
let edge11 = Edge::from(10, 0, 3, 0.8571428571);
let edge12 = Edge::from(11, 3, 0, 0.8571428571);
let edge13 = Edge::from(12, 0, 4, 1.0);
let edge14 = Edge::from(13, 4, 0, 1.0);
return Graph::from(Vec::from([edge1, edge2, edge3, edge4, edge5, edge6, edge7,
edge8, edge9, edge10, edge11, edge12, edge13, edge14]));
}
#[cfg(test)]
fn directed_graph() -> Graph {
let edge1 = Edge::from(0, 4, 0, 7.0);
let edge2 = Edge::from(1, 0, 2, 12.0);
let edge3 = Edge::from(2, 0, 3, 60.0);
let edge4 = Edge::from(3, 2, 1, 20.0);
let edge5 = Edge::from(4, 2, 3, 32.0);
let edge6 = Edge::from(5, 1, 0, 10.0);
return Graph::from(Vec::from([edge1, edge2, edge3, edge4, edge5, edge6]));
}
#[cfg(test)]
fn graphs_with_one_connection() -> Graph {
let edge1 = Edge::from(0, 0, 4, 1.0);
let edge2 = Edge::from(1, 1, 4, 2.0);
let edge3 = Edge::from(2, 4, 6, 3.0);
let edge4 = Edge::from(3, 3, 5, 4.0);
let edge5 = Edge::from(4, 2, 5, 5.0);
let edge6 = Edge::from(5, 5, 6, 6.0);
let edge7 = Edge::from(6, 6, 7, 7.0);
let edge8 = Edge::from(7, 11, 9, 8.0);
let edge9 = Edge::from(8, 12, 9, 9.0);
let edge10 = Edge::from(9, 9, 8, 10.0);
let edge11 = Edge::from(10, 14, 10, 11.0);
let edge12 = Edge::from(11, 13, 10, 12.0);
let edge13 = Edge::from(12, 10, 8, 13.0);
let edge14 = Edge::from(13, 8, 7, 14.0);
let edge15 = Edge::from(14, 4, 0, 1.0);
let edge16 = Edge::from(15, 4, 1, 2.0);
let edge17 = Edge::from(16, 6, 4, 3.0);
let edge18 = Edge::from(17, 5, 3, 4.0);
let edge19 = Edge::from(18, 5, 2, 5.0);
let edge20 = Edge::from(19, 6, 5, 6.0);
let edge21 = Edge::from(20, 7, 6, 7.0);
let edge22 = Edge::from(21, 9, 11, 8.0);
let edge23 = Edge::from(22, 9, 12, 9.0);
let edge24 = Edge::from(23, 8, 9, 10.0);
let edge25 = Edge::from(24, 10, 14, 11.0);
let edge26 = Edge::from(25, 10, 13, 12.0);
let edge27 = Edge::from(26, 8, 10, 13.0);
let edge28 = Edge::from(27, 7, 8, 14.0);
return Graph::from(Vec::from([edge1, edge2, edge3, edge4, edge5, edge6, edge7, edge8,
edge9, edge10, edge11, edge12, edge13, edge14, edge15, edge16, edge17, edge18, edge19, edge20,
edge21, edge22, edge23, edge24, edge25, edge26, edge27, edge28]));
}
#[cfg(test)]
fn stubbed_path() -> Waypoint {
let start_point = Waypoint::from(Some(Edge::from(0, 0, 1, 1.0)), Node {
id: 0,
edges: Vec::new(),
}, None);
let mut current = start_point;
for i in 0..10 {
let edge_stub = Some(Edge::from(i, 0, 1, 1.0));
current = Waypoint::from(edge_stub.clone(), Node {
id: 0,
edges: Vec::new(),
}, Some(Box::from(current.clone())))
}
return current.clone();
}