-
Notifications
You must be signed in to change notification settings - Fork 32
/
GraphSearchSample.java
228 lines (204 loc) · 5.1 KB
/
GraphSearchSample.java
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class GraphSearchSample {
public static void main(String[] args) {
// 邻接矩阵存储下的DFS和BFS,对应的图见文章
AMGraph<String> amGraph = new AMGraph(9);
amGraph.insertVertex("A");
amGraph.insertVertex("B");
amGraph.insertVertex("C");
amGraph.insertVertex("D");
amGraph.insertVertex("E");
amGraph.insertVertex("F");
amGraph.insertVertex("G");
amGraph.insertVertex("H");
amGraph.insertVertex("I");
amGraph.insertEdge(0, 1);
amGraph.insertEdge(0, 5);
amGraph.insertEdge(1, 2);
amGraph.insertEdge(1, 8);
amGraph.insertEdge(1, 6);
amGraph.insertEdge(5, 6);
amGraph.insertEdge(5, 4);
amGraph.insertEdge(2, 3);
amGraph.insertEdge(8, 3);
amGraph.insertEdge(6, 3);
amGraph.insertEdge(6, 7);
amGraph.insertEdge(4, 3);
amGraph.insertEdge(4, 7);
amGraph.insertEdge(3, 7);
amGraph.DFS();
System.out.println();
System.out.println();
amGraph.BFS();
}
}
/**
* 邻接矩阵存储图
*
* 以无向图为例子
*
* AdjacencyMatrixGraph
*/
class AMGraph<T> {
/**
* 顶点集合
*/
private List<T> vertexList;
/**
* 边集合
*/
private int[][] edges;
/**
* 边的个数
*/
private int edgeNum;
/**
* 访问数组
*/
private boolean[] visited;
public AMGraph(int n) {
vertexList = new ArrayList<>(n);
visited = new boolean[n];
edges = new int[n][n];
edgeNum = 0;
}
/**
* 插入顶点
*
* @param vertex
*/
public void insertVertex(T vertex) {
vertexList.add(vertex);
}
/**
* 插入边
*
* @param v1
* @param v2
*/
public void insertEdge(int v1, int v2) {
insertEdge(v1, v2, 1);
}
/**
* 插入边,有weight值
*
* @param v1
* @param v2
* @param weight
*/
public void insertEdge(int v1, int v2, int weight) {
edges[v1][v2] = weight;
edges[v2][v1] = weight;
edgeNum++;
}
/**
* 获取顶点个数
*
* @return
*/
public int getVertexNum() {
return vertexList.size();
}
public T getVertexByIndex(int index) {
return vertexList.get(index);
}
/**
* 获取边的个数
*
* @return
*/
public int getEdgNum() {
return edgeNum;
}
/**
* 获取第一个邻接顶点
*
* @param index
* @return
*/
public int getFirstNeighbor(int index) {
for (int i = 0; i < vertexList.size(); i++) {
if (edges[index][i] > 0) {
return i;
}
}
return -1;
}
/**
* 根据前一个邻接结点的下标,获取下一个邻接结点
*
* @param v1Index 目标结点
* @param v2Index 前一个邻接结点的下标
* @return
*/
public int getNextNeighbor(int v1Index, int v2Index) {
for (int j = v2Index + 1; j < vertexList.size(); j++) {
if (edges[v1Index][j] > 0) {
return j;
}
}
return -1;
}
private void DFS(int i) {
// 标记当前元素已经访问
visited[i] = true;
System.out.println("当前访问顶点:" + getVertexByIndex(i));
int next = getFirstNeighbor(i);
while (next != -1) {
if (!visited[next]) {
DFS(next);
}
next = getNextNeighbor(i, next);
}
}
/**
* 深度优先搜索
*/
public void DFS() {
for (int i = 0; i < vertexList.size(); i++) {
visited[i] = false;
}
// 非连通图,不同的连通分量要单独进行DFS
for (int i = 0; i < vertexList.size(); i++) {
if (!visited[i]) {
DFS(i);
}
}
}
private void BFS(int i) {
// 标记当前元素已经访问
visited[i] = true;
System.out.println("当前访问顶点:" + getVertexByIndex(i));
int cur, next;
LinkedList<Integer> queue = new LinkedList<>();
queue.addLast(i);
while (!queue.isEmpty()) {
cur = queue.removeFirst();
next = getFirstNeighbor(cur);
while (next != -1) {
if (!visited[next]) {
// 标记当前元素已经访问
visited[next] = true;
System.out.println("当前访问顶点:" + getVertexByIndex(next));
queue.addLast(next);
}
next = getNextNeighbor(cur, next);
}
}
}
/**
* 广度优先搜索
*/
public void BFS() {
for (int i = 0; i < vertexList.size(); i++) {
visited[i] = false;
}
for (int i = 0; i < vertexList.size(); i++) {
if (!visited[i]) {
BFS(i);
}
}
}
}