-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfs.rs
56 lines (41 loc) · 1.1 KB
/
dfs.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
use std::collections::{HashMap, HashSet};
type Id = i32;
pub struct Graph {
pub nodes: HashMap<Id, Vec<Id>>
}
impl Graph {
pub fn new() -> Self {
Graph { nodes: HashMap::new() }
}
pub fn dfs(&self, start: Id, path: &mut Vec<Id>, visited: &mut HashSet<Id>) {
visited.insert(start);
path.push(start);
if let Some(neighbors) = self.nodes.get(&start) {
for &n in neighbors {
if !visited.contains(&n) {
visited.insert(n);
self.dfs(n, path, visited)
}
}
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test() {
// 1 -- 2 -- 3
// | |
// 4 ------- |
let mut g = Graph::new();
g.nodes.insert(1, vec![2, 4]);
g.nodes.insert(2, vec![1, 3]);
g.nodes.insert(3, vec![2, 4]);
g.nodes.insert(4, vec![1, 3]);
let mut path = Vec::new();
let mut visited = HashSet::new();
g.dfs(1, &mut path, &mut visited);
assert_eq!(path, vec![1,2,3,4]);
}
}