-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.rs
65 lines (53 loc) · 1.41 KB
/
main.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
mod graph;
mod graph_dfs {
use crate::graph::Graph;
pub fn dfs(g: &Graph) -> Vec<usize> {
let mut visited = vec![false; g.v()];
let mut res = Vec::new();
for v in 0..g.v() {
if visited[v] == false {
__dfs(&g, v, &mut visited, &mut res);
}
}
return res;
}
pub fn post_dfs(g: &Graph) -> Vec<usize> {
let mut visited = vec![false; g.v()];
let mut res = Vec::new();
for v in 0..g.v() {
if visited[v] == false {
__post_dfs(&g, v, &mut visited, &mut res);
}
}
return res;
}
fn __dfs(g: &Graph, v: usize, visited: &mut Vec<bool>, res: &mut Vec<usize>) {
visited[v] = true;
res.push(v);
for &w in g.adj_edge(v) {
if visited[w] == false {
__dfs(g, w, visited, res);
}
}
}
fn __post_dfs(g: &Graph, v: usize, visited: &mut Vec<bool>, res: &mut Vec<usize>) {
visited[v] = true;
for &w in g.adj_edge(v) {
if visited[w] == false {
__post_dfs(g, w, visited, res);
}
}
res.push(v);
}
}
use crate::{
graph::Graph,
graph_dfs::{dfs, post_dfs},
};
pub fn main() {
let g = Graph::from("g.txt");
let res = dfs(&g);
println!("{:?}", res);
let res2 = post_dfs(&g);
println!("{:?}", res2);
}