-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponents.rs
55 lines (48 loc) · 1.19 KB
/
components.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
use crate::graph::Graph;
pub struct Component<'a, G: Graph> {
graph: &'a G,
visited: Vec<bool>,
id: Vec<Option<usize>>,
component_count: usize,
}
impl<'a, G> Component<'a, G>
where
G: Graph,
{
pub fn new(graph: &'a G) -> Self {
let visited = vec![false; graph.v()];
let id = vec![None; graph.v()];
let component_count = 0;
Self {
graph,
visited,
id,
component_count,
}
}
// 图的深度优先遍历
fn dfs(&mut self, v: usize) {
self.visited[v] = true;
self.id[v] = Some(self.component_count);
for i in self.graph.adj(v) {
if !self.visited[i] {
self.dfs(i);
}
}
}
// 返回图的联通分量个数
pub fn count(&mut self) -> usize {
for v in 0..self.graph.v() {
if !self.visited[v] {
self.dfs(v);
self.component_count += 1;
}
}
self.component_count
}
pub fn is_connected(&self, v: usize, w: usize) -> bool {
let max = self.graph.v();
assert!(v < max && w < max);
self.id[v] == self.id[w]
}
}