1. 概述
在本教程中,我們將探討Java中的深度優先搜索
深度優先搜索(DFS)是一個應用於樹、圖等數據結構的遍歷算法。在移動到下一個分支之前,深度優先搜索會
深度爲優先原則去探索新的分支。
在接下來的部分中,我們將首先了解樹的實現,然後是圖。
要了解如何在Java中實現這些結構,請查看我們以前的關於 二叉樹 Binary Tree 和 圖 Graph 的教程。
2. 樹的深度優先搜索
使用 DFS 遍歷樹有三種不同的順序:
- 先序遍歷
- 中序遍歷
- 後序遍歷
2.1 先序遍歷
在先序遍歷中,先遍歷其根節點,依次是左子樹和右子樹。
使用遞歸簡單地實現先序遍歷:
- 訪問當前節點
- 遍歷左子樹
- 遍歷右子樹
public void traversePreOrder(Node node) {
if (node != null) {
visit(node.value);
traversePreOrder(node.left);
traversePreOrder(node.right);
}
}
使用非遞歸方式實現先序遍歷:
- 將根節點入棧
- 當棧不爲空
- 將當前節點(棧頂元素)彈棧
- 訪問當前節點
- 依次將當前節點右子節點、左子節點入棧
public void traversePreOrderWithoutRecursion() {
Stack<Node> stack = new Stack<Node>();
Node current = root;
stack.push(root);
while(!stack.isEmpty()) {
current = stack.pop();
visit(current.value);
if(current.right != null) {
stack.push(current.right);
}
if(current.left != null) {
stack.push(current.left);
}
}
}
2.2 中序遍歷
對於中序遍歷,先訪問其左子樹,然後根節點,最後訪問右子樹。
二叉搜索樹的中序遍歷意味着按值的遞增順序遍歷節點。
使用遞歸實現中序遍歷:
public void traverseInOrder(Node node) {
if (node != null) {
traverseInOrder(node.left);
visit(node.value);
traverseInOrder(node.right);
}
}
同時也可以使用非遞歸實現中序遍歷:
- 將根節點壓棧
- 當棧不爲空
- 繼續將左子節點壓棧,直到當前節點爲最左節點(即無左子節點)
- 訪問當前節點
- 將右子節點壓棧
public void traverseInOrderWithoutRecursion() {
Stack<Node> stack = new Stack<Node>();
Node current = root;
stack.push(root);
while(! stack.isEmpty()) {
while(current.left != null) {
current = current.left;
stack.push(current);
}
current = stack.pop();
visit(current.value);
if(current.right != null) {
current = current.right;
stack.push(current);
}
}
}
2.3 後序遍歷
最後,在後序遍歷中,在訪問根節點之前,依次先訪問左子節點、右子節點。
參考前邊,遞歸實現後序遍歷:
public void traversePostOrder(Node node) {
if (node != null) {
traversePostOrder(node.left);
traversePostOrder(node.right);
visit(node.value);
}
}
使用非遞歸實現後序遍歷:
- 將根節點入棧
- 當棧不爲空
- 檢查是否已經遍歷了左子樹和右子樹
- 如果沒有,則將右子節點和左子節點壓棧
public void traversePostOrderWithoutRecursion() {
Stack<Node> stack = new Stack<Node>();
Node prev = root;
Node current = root;
stack.push(root);
while (!stack.isEmpty()) {
current = stack.peek();
boolean hasChild = (current.left != null || current.right != null);
boolean isPrevLastChild = (prev == current.right ||
(prev == current.left && current.right == null));
if (!hasChild || isPrevLastChild) {
current = stack.pop();
visit(current.value);
prev = current;
} else {
if (current.right != null) {
stack.push(current.right);
}
if (current.left != null) {
stack.push(current.left);
}
}
}
}
3. 圖的深度優先搜索
圖和樹之間的主要區別在於圖可能包含循環。
因此,爲了避免循環搜索,我們將在訪問每個節點時對其進行標記。
接下來將會展示圖DFS的遞歸、非遞歸實現。
3.1 圖的DFS遞歸實現
首先,讓我們從遞歸開始:
- 從一個任意的節點開始
- 標記當前節點爲已訪問
- 訪問當前節點
- 遍歷未訪問的相鄰節點
public void dfs(int start) {
boolean[] isVisited = new boolean[adjVertices.size()];
dfsRecursive(start, isVisited);
}
private void dfsRecursive(int current, boolean[] isVisited) {
isVisited[current] = true;
visit(current);
for (int dest : adjVertices.get(current)) {
if (!isVisited[dest])
dfsRecursive(dest, isVisited);
}
}
3.2 圖的DFS非遞歸實現
也可以在不使用遞歸的情況下實現圖的DFS。我們也需要使用一個棧進行實現:
- 將從一個任意的節點開始
- 將節點壓棧
- 當棧不爲空
- 標記當前節點爲已訪問
- 訪問當前節點
- 將未訪問的相鄰頂點壓棧
public void dfsWithoutRecursion(int start) {
Stack<Integer> stack = new Stack<Integer>();
boolean[] isVisited = new boolean[adjVertices.size()];
stack.push(start);
while (!stack.isEmpty()) {
int current = stack.pop();
isVisited[current] = true;
visit(current);
for (int dest : adjVertices.get(current)) {
if (!isVisited[dest])
stack.push(dest);
}
}
}
3.4 拓撲排序
圖的深度優先搜索有很多應用。拓撲排序是深度優先搜索最著名的應用之一。
有向圖的拓撲排序是其頂點的線性排序,因此對於每個邊,源節點都位於目標之前。
要進行拓撲排序,需要對剛剛實現的DFS進行簡單的改造:
- 我們需要將訪問的頂點保持在堆棧中,因爲拓撲排序是以相反的順序訪問的頂點
- 只有在遍歷所有相鄰節點之後,纔會將訪問的節點推送到堆棧中。
public List<Integer> topologicalSort(int start) {
LinkedList<Integer> result = new LinkedList<Integer>();
boolean[] isVisited = new boolean[adjVertices.size()];
topologicalSortRecursive(start, isVisited, result);
return result;
}
private void topologicalSortRecursive(int current, boolean[] isVisited, LinkedList<Integer> result) {
isVisited[current] = true;
for (int dest : adjVertices.get(current)) {
if (!isVisited[dest])
topologicalSortRecursive(dest, isVisited, result);
}
result.addFirst(current);
}
4. 結論
在本文中,我們討論了樹和圖的深度優先搜索。
完整代碼見 GitHub 。
原文:https://www.baeldung.com/java-depth-first-search
作者:baeldung
譯者:陳苓洪