-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathdfs.rs
76 lines (71 loc) · 1.95 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#![allow(clippy::many_single_char_names,clippy::explicit_counter_loop, clippy::redundant_closure)]
use super::adjacencylists::AdjacencyLists;
use chapter01::interface::{Graph, Stack};
use chapter03::sllist::SLList;
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
enum Color {
White,
Grey,
Black,
}
fn do_dfs(g: &AdjacencyLists, i: usize, c: &mut [Color]) {
if let Some(e) = c.get_mut(i) {
*e = Color::Grey
}
let edges = g.out_edges(i);
for j in edges.into_iter() {
if let Some(Color::White) = c.get(j) {
if let Some(e) = c.get_mut(j) {
*e = Color::Grey
}
do_dfs(g, j, c);
}
}
if let Some(e) = c.get_mut(i) {
*e = Color::Black
}
}
pub fn dfs(g: &AdjacencyLists, r: usize) {
let mut c = vec![Color::White; g.nvertices()];
do_dfs(g, r, &mut c);
}
pub fn dfs2(g: &AdjacencyLists, r: usize) {
let mut c = vec![Color::White; g.nvertices()];
let mut s = SLList::new();
s.push(r);
while let Some(i) = s.pop() {
if let Some(Color::White) = c.get(i) {
if let Some(e) = c.get_mut(i) {
*e = Color::Grey
}
let edges = g.out_edges(i);
for j in edges.into_iter() {
s.push(j);
}
}
}
}
#[cfg(test)]
mod test {
use super::*;
use rand::{thread_rng, Rng};
use std::collections::HashSet;
#[test]
fn test_dfs() {
let n = 50;
let mut adjm = AdjacencyLists::new(n);
let mut set: HashSet<(usize, usize)> = HashSet::new();
let mut rng = thread_rng();
for _ in 0..(5 * n) {
let (i, j) = (rng.gen_range(0, n), rng.gen_range(0, n));
if !set.contains(&(i, j)) {
set.insert((i, j));
adjm.add_edge(i, j);
}
}
dfs(&adjm, 0);
println!("done");
dfs2(&adjm, 0);
println!("done2");
}
}