-
Notifications
You must be signed in to change notification settings - Fork 10
/
DepthFirstSearch.java
65 lines (54 loc) · 1.84 KB
/
DepthFirstSearch.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
package com.camnter.basicexercises.graph;
import com.camnter.basicexercises.core.AdjacencyMatrixGraph;
import java.util.Stack;
/**
* 图 深度优先搜索(BFS)
* <p/>
*
* @author CaMnter
*/
public class DepthFirstSearch<T> {
public void dfs(AdjacencyMatrixGraph<T> graph) {
boolean[] visited = new boolean[graph.getNumOfVertex()];
Stack<Integer> stack = new Stack<Integer>();
for (int i = 0; i < graph.vertexes.size(); i++) {
if (visited[i]) continue;
/**
* 顶点 序号入栈
*/
stack.add(i);
do {
/**
* 顶点 序号出栈
*/
int cur = stack.pop();
if (!visited[cur]) {
/**
* 标记 && 打印
*/
System.out.print(graph.vertexes.get(cur) + " ");
visited[cur] = true;
/**
* 逆序遍历,是因为栈是后进先出的
* V2 V3 进去的话
* 先出栈的是 V3
*/
for (int j = graph.vertexes.size() - 1; j >= 0; j--) {
/**
* 横向遍历 && 排除访问
* 都入栈
*/
if (visited[j]) continue;
if (graph.adjacencyMatrix[cur][j] == 1) {
stack.push(j);
}
}
}
} while (!stack.isEmpty());
}
}
public static void main(String[] args) {
DepthFirstSearch<String> depthFirstSearch = new DepthFirstSearch<String>();
depthFirstSearch.dfs(AdjacencyMatrixGraph.getBiggerGraph());
}
}