-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathDFS.java
More file actions
413 lines (344 loc) · 13.5 KB
/
DFS.java
File metadata and controls
413 lines (344 loc) · 13.5 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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/**
* 深度优先搜索(DFS)实现 (Java)
*
* 特点:
* - 优先往纵深方向探索
* - 使用栈或递归
* - 回溯机制
* - 时间复杂度:O(V + E)
* - 空间复杂度:O(V)
*/
import java.util.*;
public class DFS {
// 图结构(邻接表)
static class Graph {
private List<List<Integer>> adjLists;
private int numVertices;
public Graph(int vertices) {
this.numVertices = vertices;
this.adjLists = new ArrayList<>(vertices);
for (int i = 0; i < vertices; i++) {
this.adjLists.add(new ArrayList<>());
}
}
// 添加边(无向图)
public void addEdge(int src, int dest) {
adjLists.get(src).add(dest);
adjLists.get(dest).add(src); // 无向图
}
// 获取邻接顶点
public List<Integer> getAdjVertices(int vertex) {
return adjLists.get(vertex);
}
public int getNumVertices() {
return numVertices;
}
}
// DFS递归实现
private static void dfsRecursive(Graph graph, int vertex, boolean[] visited, List<Integer> result) {
// 标记当前顶点为已访问
visited[vertex] = true;
result.add(vertex);
// 递归访问所有未访问的邻接顶点
for (int adjVertex : graph.getAdjVertices(vertex)) {
if (!visited[adjVertex]) {
dfsRecursive(graph, adjVertex, visited, result);
}
}
}
// DFS递归遍历(包装函数)
public static List<Integer> dfsRecursiveTraversal(Graph graph, int startVertex) {
boolean[] visited = new boolean[graph.getNumVertices()];
List<Integer> result = new ArrayList<>();
dfsRecursive(graph, startVertex, visited, result);
return result;
}
// DFS迭代实现(使用栈)
public static List<Integer> dfsIterative(Graph graph, int startVertex) {
boolean[] visited = new boolean[graph.getNumVertices()];
Stack<Integer> stack = new Stack<>();
List<Integer> result = new ArrayList<>();
// 将起始顶点入栈
stack.push(startVertex);
while (!stack.isEmpty()) {
// 出栈一个顶点
int currentVertex = stack.pop();
// 如果该顶点未被访问
if (!visited[currentVertex]) {
visited[currentVertex] = true;
result.add(currentVertex);
// 将所有未访问的邻接顶点入栈
// 注意:为了保持与递归相似的顺序,需要反向入栈
List<Integer> neighbors = graph.getAdjVertices(currentVertex);
for (int i = neighbors.size() - 1; i >= 0; i--) {
int adjVertex = neighbors.get(i);
if (!visited[adjVertex]) {
stack.push(adjVertex);
}
}
}
}
return result;
}
// DFS查找路径
private static boolean dfsFindPath(Graph graph, int current, int target,
boolean[] visited, List<Integer> path) {
// 标记当前顶点为已访问
visited[current] = true;
path.add(current);
// 如果找到目标
if (current == target) {
return true;
}
// 递归访问所有邻接顶点
for (int adjVertex : graph.getAdjVertices(current)) {
if (!visited[adjVertex]) {
if (dfsFindPath(graph, adjVertex, target, visited, path)) {
return true;
}
}
}
// 回溯:从路径中移除当前顶点
path.remove(path.size() - 1);
return false;
}
// 查找两点之间的路径
public static List<Integer> findPath(Graph graph, int start, int end) {
boolean[] visited = new boolean[graph.getNumVertices()];
List<Integer> path = new ArrayList<>();
if (dfsFindPath(graph, start, end, visited, path)) {
return path;
}
return null;
}
// DFS检测环
private static boolean dfsDetectCycle(Graph graph, int vertex, int parent, boolean[] visited) {
visited[vertex] = true;
for (int adjVertex : graph.getAdjVertices(vertex)) {
// 如果邻接顶点未被访问,递归检查
if (!visited[adjVertex]) {
if (dfsDetectCycle(graph, adjVertex, vertex, visited)) {
return true;
}
}
// 如果邻接顶点已被访问且不是父节点,则存在环
else if (adjVertex != parent) {
return true;
}
}
return false;
}
// 检测图中是否存在环
public static boolean hasCycle(Graph graph) {
boolean[] visited = new boolean[graph.getNumVertices()];
for (int i = 0; i < graph.getNumVertices(); i++) {
if (!visited[i]) {
if (dfsDetectCycle(graph, i, -1, visited)) {
return true;
}
}
}
return false;
}
// DFS计算连通分量
public static List<List<Integer>> dfsConnectedComponents(Graph graph) {
boolean[] visited = new boolean[graph.getNumVertices()];
List<List<Integer>> components = new ArrayList<>();
for (int i = 0; i < graph.getNumVertices(); i++) {
if (!visited[i]) {
List<Integer> component = new ArrayList<>();
dfsRecursive(graph, i, visited, component);
components.add(component);
}
}
return components;
}
// DFS拓扑排序(用于有向无环图)
public static List<Integer> dfsTopologicalSort(Graph graph) {
boolean[] visited = new boolean[graph.getNumVertices()];
Stack<Integer> stack = new Stack<>();
// 对每个未访问的顶点进行DFS
for (int i = 0; i < graph.getNumVertices(); i++) {
if (!visited[i]) {
topologicalSortUtil(graph, i, visited, stack);
}
}
// 弹出栈中的元素得到拓扑排序
List<Integer> result = new ArrayList<>();
while (!stack.isEmpty()) {
result.add(stack.pop());
}
return result;
}
// 拓扑排序的辅助函数
private static void topologicalSortUtil(Graph graph, int vertex,
boolean[] visited, Stack<Integer> stack) {
visited[vertex] = true;
for (int adjVertex : graph.getAdjVertices(vertex)) {
if (!visited[adjVertex]) {
topologicalSortUtil(graph, adjVertex, visited, stack);
}
}
// 将顶点入栈
stack.push(vertex);
}
// DFS检测强连通分量(用于有向图)
public static List<List<Integer>> dfsStronglyConnectedComponents(Graph graph) {
// Kosaraju算法
boolean[] visited = new boolean[graph.getNumVertices()];
Stack<Integer> stack = new Stack<>();
// 第一步:对原图进行DFS,将顶点按完成时间入栈
for (int i = 0; i < graph.getNumVertices(); i++) {
if (!visited[i]) {
fillOrder(graph, i, visited, stack);
}
}
// 第二步:创建转置图
Graph transposed = getTranspose(graph);
// 第三步:按栈中顺序对转置图进行DFS
Arrays.fill(visited, false);
List<List<Integer>> components = new ArrayList<>();
while (!stack.isEmpty()) {
int vertex = stack.pop();
if (!visited[vertex]) {
List<Integer> component = new ArrayList<>();
dfsRecursive(transposed, vertex, visited, component);
components.add(component);
}
}
return components;
}
// 填充顺序的辅助函数
private static void fillOrder(Graph graph, int vertex, boolean[] visited, Stack<Integer> stack) {
visited[vertex] = true;
for (int adjVertex : graph.getAdjVertices(vertex)) {
if (!visited[adjVertex]) {
fillOrder(graph, adjVertex, visited, stack);
}
}
stack.push(vertex);
}
// 获取转置图
private static Graph getTranspose(Graph graph) {
Graph transposed = new Graph(graph.getNumVertices());
for (int v = 0; v < graph.getNumVertices(); v++) {
for (int adjVertex : graph.getAdjVertices(v)) {
transposed.adjLists.get(adjVertex).add(v);
}
}
return transposed;
}
// 检查是否为二分图
public static boolean isBipartite(Graph graph) {
int[] color = new int[graph.getNumVertices()]; // 0: 未染色, 1: 染色A, -1: 染色B
for (int i = 0; i < graph.getNumVertices(); i++) {
if (color[i] == 0) {
Stack<Integer> stack = new Stack<>();
color[i] = 1;
stack.push(i);
while (!stack.isEmpty()) {
int currentVertex = stack.pop();
for (int adjVertex : graph.getAdjVertices(currentVertex)) {
if (color[adjVertex] == 0) {
color[adjVertex] = -color[currentVertex];
stack.push(adjVertex);
} else if (color[adjVertex] == color[currentVertex]) {
return false;
}
}
}
}
}
return true;
}
// 打印列表
private static void printList(List<Integer> list) {
System.out.print("[");
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i));
if (i < list.size() - 1) {
System.out.print(", ");
}
}
System.out.println("]");
}
// 打印连通分量
private static void printComponents(List<List<Integer>> components) {
for (int i = 0; i < components.size(); i++) {
System.out.print("分量 " + (i + 1) + ": ");
printList(components.get(i));
}
}
public static void main(String[] args) {
System.out.println("=== 深度优先搜索(DFS)演示 ===\n");
// 创建图
Graph graph = new Graph(8);
// 添加边
graph.addEdge(0, 1);
graph.addEdge(0, 2);
graph.addEdge(1, 3);
graph.addEdge(1, 4);
graph.addEdge(2, 5);
graph.addEdge(2, 6);
graph.addEdge(3, 7);
graph.addEdge(4, 7);
graph.addEdge(5, 6);
System.out.println("图结构:");
System.out.println("0 -- 1 -- 3 -- 7");
System.out.println("| | |");
System.out.println("| | |");
System.out.println("2 -- 4 --------|");
System.out.println("| |");
System.out.println("| |");
System.out.println("5 -- 6\n");
// 1. DFS递归遍历
System.out.println("1. DFS递归遍历(从顶点0开始)");
List<Integer> recursiveResult = dfsRecursiveTraversal(graph, 0);
System.out.print("结果: ");
printList(recursiveResult);
System.out.println();
// 2. DFS迭代遍历
System.out.println("2. DFS迭代遍历(从顶点0开始)");
List<Integer> iterativeResult = dfsIterative(graph, 0);
System.out.print("结果: ");
printList(iterativeResult);
System.out.println();
// 3. 查找路径
System.out.println("3. 查找路径");
List<Integer> path = findPath(graph, 0, 7);
if (path != null) {
System.out.print("从 0 到 7 的路径: ");
printList(path);
} else {
System.out.println("未找到从 0 到 7 的路径");
}
System.out.println();
// 4. 检测环
System.out.println("4. 检测环");
if (hasCycle(graph)) {
System.out.println("图中存在环");
} else {
System.out.println("图中不存在环");
}
System.out.println();
// 5. 计算连通分量
System.out.println("5. 计算连通分量");
List<List<Integer>> components = dfsConnectedComponents(graph);
System.out.println("连通分量数: " + components.size());
printComponents(components);
System.out.println();
// 6. 从不同起点开始DFS
System.out.println("6. 从不同起点开始DFS");
System.out.print("从顶点3开始(递归): ");
printList(dfsRecursiveTraversal(graph, 3));
System.out.print("从顶点3开始(迭代): ");
printList(dfsIterative(graph, 3));
// 7. 检查是否为二分图
System.out.println("\n7. 检查是否为二分图");
if (isBipartite(graph)) {
System.out.println("该图是二分图");
} else {
System.out.println("该图不是二分图");
}
}
}