-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
breadth_first_search.rs
203 lines (174 loc) · 4.78 KB
/
breadth_first_search.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
192
193
194
195
196
197
198
199
200
201
202
203
use std::collections::HashSet;
use std::collections::VecDeque;
/// Perform a breadth-first search on Graph `graph`.
///
/// # Parameters
///
/// - `graph`: The graph to search.
/// - `root`: The starting node of the graph from which to begin searching.
/// - `target`: The target node for the search.
///
/// # Returns
///
/// If the target is found, an Optional vector is returned with the history
/// of nodes visited as its contents.
///
/// If the target is not found or there is no path from the root,
/// `None` is returned.
///
pub fn breadth_first_search(graph: &Graph, root: Node, target: Node) -> Option<Vec<u32>> {
let mut visited: HashSet<Node> = HashSet::new();
let mut history: Vec<u32> = Vec::new();
let mut queue = VecDeque::new();
visited.insert(root);
queue.push_back(root);
while let Some(currentnode) = queue.pop_front() {
history.push(currentnode.value());
// If we reach the goal, return our travel history.
if currentnode == target {
return Some(history);
}
// Check the neighboring nodes for any that we've not visited yet.
for neighbor in currentnode.neighbors(graph) {
if visited.insert(neighbor) {
queue.push_back(neighbor);
}
}
}
// All nodes were visited, yet the target was not found.
None
}
// Data Structures
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct Node(u32);
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct Edge(u32, u32);
#[derive(Clone)]
pub struct Graph {
#[allow(dead_code)]
nodes: Vec<Node>,
edges: Vec<Edge>,
}
impl Graph {
pub fn new(nodes: Vec<Node>, edges: Vec<Edge>) -> Self {
Graph { nodes, edges }
}
}
impl From<u32> for Node {
fn from(item: u32) -> Self {
Node(item)
}
}
impl Node {
pub fn value(&self) -> u32 {
self.0
}
pub fn neighbors(&self, graph: &Graph) -> Vec<Node> {
graph
.edges
.iter()
.filter(|e| e.0 == self.0)
.map(|e| e.1.into())
.collect()
}
}
impl From<(u32, u32)> for Edge {
fn from(item: (u32, u32)) -> Self {
Edge(item.0, item.1)
}
}
#[cfg(test)]
mod tests {
use super::*;
/* Example graph #1:
*
* (1) <--- Root
* / \
* (2) (3)
* / | | \
* (4) (5) (6) (7)
* |
* (8)
*/
fn graph1() -> Graph {
let nodes = vec![1, 2, 3, 4, 5, 6, 7];
let edges = vec![(1, 2), (1, 3), (2, 4), (2, 5), (3, 6), (3, 7), (5, 8)];
Graph::new(
nodes.into_iter().map(|v| v.into()).collect(),
edges.into_iter().map(|e| e.into()).collect(),
)
}
#[test]
fn breadth_first_search_graph1_when_node_not_found_returns_none() {
let graph = graph1();
let root = 1;
let target = 10;
assert_eq!(
breadth_first_search(&graph, root.into(), target.into()),
None
);
}
#[test]
fn breadth_first_search_graph1_when_target_8_should_evaluate_all_nodes_first() {
let graph = graph1();
let root = 1;
let target = 8;
let expected_path = vec![1, 2, 3, 4, 5, 6, 7, 8];
assert_eq!(
breadth_first_search(&graph, root.into(), target.into()),
Some(expected_path)
);
}
/* Example graph #2:
*
* (1) --- (2) (3) --- (4)
* / | / /
* / | / /
* / | / /
* (5) (6) --- (7) (8)
*/
fn graph2() -> Graph {
let nodes = vec![1, 2, 3, 4, 5, 6, 7, 8];
let undirected_edges = vec![
(1, 2),
(2, 1),
(2, 5),
(5, 2),
(2, 6),
(6, 2),
(3, 4),
(4, 3),
(3, 6),
(6, 3),
(4, 7),
(7, 4),
(6, 7),
(7, 6),
];
Graph::new(
nodes.into_iter().map(|v| v.into()).collect(),
undirected_edges.into_iter().map(|e| e.into()).collect(),
)
}
#[test]
fn breadth_first_search_graph2_when_no_path_to_node_returns_none() {
let graph = graph2();
let root = 8;
let target = 4;
assert_eq!(
breadth_first_search(&graph, root.into(), target.into()),
None
);
}
#[test]
fn breadth_first_search_graph2_should_find_path_from_4_to_1() {
let graph = graph2();
let root = 4;
let target = 1;
let expected_path = vec![4, 3, 7, 6, 2, 1];
assert_eq!(
breadth_first_search(&graph, root.into(), target.into()),
Some(expected_path)
);
}
}