-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDepthFirstSearchRecursive.java
42 lines (35 loc) · 1.27 KB
/
DepthFirstSearchRecursive.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
package com.camnter.basicexercises.graph;
import com.camnter.basicexercises.core.AdjacencyMatrixGraph;
/**
* 图 深度优先搜索(BFS)
* <p/>
* 就是从一个顶点开始找,找到底为止
*
* @author CaMnter
*/
public class DepthFirstSearchRecursive<T> {
public void dfsRecursive(AdjacencyMatrixGraph<T> graph) {
boolean[] visited = new boolean[graph.getNumOfVertex()];
for (int i = 0; i < graph.vertexes.size(); i++) {
if (visited[i]) continue;
recursive(graph, visited, i);
}
}
public void recursive(AdjacencyMatrixGraph<T> graph, boolean[] visited, int i) {
// 标记
visited[i] = true;
System.out.print(graph.vertexes.get(i).toString() + " ");
for (int j = 0; j < graph.vertexes.size(); j++) {
/**
* 有边 && 没访问过边的另外一个节点
*/
if (graph.adjacencyMatrix[i][j] == 1 && !visited[j]) {
recursive(graph, visited, j);
}
}
}
public static void main(String[] args) {
DepthFirstSearchRecursive<String> depthFirstSearchRecursive = new DepthFirstSearchRecursive<String>();
depthFirstSearchRecursive.dfsRecursive(AdjacencyMatrixGraph.getBiggerGraph());
}
}