-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathComponent.java
More file actions
74 lines (60 loc) · 2.01 KB
/
Component.java
File metadata and controls
74 lines (60 loc) · 2.01 KB
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
// 求无权图的联通分量
public class Component {
Graph G; // 图的引用
private boolean[] visited; // 记录dfs的过程中节点是否被访问
private int ccount; // 记录联通分量个数
private int[] id; // 每个节点所对应的联通分量标记
// 图的深度优先遍历
void dfs( int v ){
visited[v] = true;
id[v] = ccount;
for( int i: G.adj(v) ){
if( !visited[i] )
dfs(i);
}
}
// 构造函数, 求出无权图的联通分量
public Component(Graph graph){
// 算法初始化
G = graph;
visited = new boolean[G.V()];
id = new int[G.V()];
ccount = 0;
for( int i = 0 ; i < G.V() ; i ++ ){
visited[i] = false;
id[i] = -1;
}
// 求图的联通分量
for( int i = 0 ; i < G.V() ; i ++ )
if( !visited[i] ){
dfs(i);
ccount ++;
}
}
// 返回图的联通分量个数
int count(){
return ccount;
}
// 查询点v和点w是否联通
boolean isConnected( int v , int w ){
assert v >= 0 && v < G.V();
assert w >= 0 && w < G.V();
return id[v] == id[w];
}
// 测试图的联通分量算法
public static void main(String[] args) {
// TestG1.txt
String filename1 = "testG1.txt";
SparseGraph g1 = new SparseGraph(13, false);
ReadGraph readGraph1 = new ReadGraph(g1, filename1);
Component component1 = new Component(g1);
System.out.println("TestG1.txt, Component Count: " + component1.count());
System.out.println();
// TestG2.txt
String filename2 = "testG2.txt";
SparseGraph g2 = new SparseGraph(6, false);
ReadGraph readGraph2 = new ReadGraph(g2, filename2);
Component component2 = new Component(g2);
System.out.println("TestG2.txt, Component Count: " + component2.count());
}
}