For faster navigation, this Iframe is preloading the Wikiwand page for 深度优先搜索.

深度优先搜索

深度优先搜索
節點搜索的順序
節點搜索的順序
節點進行深度优先搜索的順序
概况
類別搜索演算法
資料結構
复杂度
平均時間複雜度
空間複雜度
最佳解
完全性
相关变量的定义
分支係數
圖的最大深度

深度优先搜索算法(英語:Depth-First-Search,缩写为DFS)是一种用于遍历或搜索算法。这个算法会尽可能深地搜索树的分支。当节点v的所在边都己被探寻过,搜索将回溯到发现节点v的那条边的起始节点。这一过程一直进行到已发现从源节点可达的所有节点为止。如果还存在未被发现的节点,则选择其中一个作为源节点并重复以上过程,整个进程反复进行直到所有节点都被访问为止。[1](p. 603)这种算法不会根据图的结构等信息调整执行策略[來源請求]

深度优先搜索是图论中的经典算法,利用深度优先搜索算法可以产生目标图的拓扑排序[1](p. 612),利用拓扑排序表可以方便的解决很多相关的图论问题,如无权最长路径问题等等。

因发明“深度优先搜索算法”,約翰·霍普克洛夫特罗伯特·塔扬1986年共同获得计算机领域的最高奖:图灵奖[2]

演算方法

  1. 首先将根节点放入stack中。
  2. stack中取出第一个节点,并检验它是否为目标。
    如果找到目标,则结束搜寻并回传结果。
    否则将它某一个尚未检验过的直接子节点加入stack中。
  3. 重复步骤2。
  4. 如果不存在未检测过的直接子节点。
    将上一级节点加入stack中。
    重复步骤2。
  5. 重复步骤4。
  6. stack为空,表示整张图都检查过了——亦即图中没有欲搜寻的目标。结束搜寻并回传“找不到目标”。

C++的實作

定义一个结构体来表达一個二叉树的节点的结构:

struct Node {
    int self;     // 数据
    Node *left;   // 左孩子
    Node *right;  // 右孩子
};

那么我们在搜索一个树的时候,从一个节点开始,能首先获取的是它的两个子节点。例如:

A是第一个访问的,然后顺序是B和D、然后是E。然后再是C、F、G。那么我们怎么来保证这个顺序呢?

这里就应该用堆栈的结构,因为堆栈是一个后进先出(LIFO)的顺序。通过使用C++STL,下面的程序能帮助理解:

const int TREE_SIZE = 9;
std::stack<Node *> unvisited;
Node nodes[TREE_SIZE];
Node *current;

//初始化树
for (int i = 0; i < TREE_SIZE; i++) {
    nodes[i].self = i;
    int child = i * 2 + 1;
    if (child < TREE_SIZE) // Left child
        nodes[i].left = &nodes[child];
    else
        nodes[i].left = NULL;
    child++;
    if (child < TREE_SIZE) // Right child
        nodes[i].right = &nodes[child];
    else
        nodes[i].right = NULL;
}

unvisited.push(&nodes[0]); //先把0放入UNVISITED stack

// 树的深度优先搜索在二叉树的特例下,就是二叉树的先序遍历操作(这里是使用循环实现)
// 只有UNVISITED不空
while (!unvisited.empty()) {
    current = (unvisited.top()); //当前访问的
    unvisited.pop();
    if (current->right != NULL)
        unvisited.push(current->right );
    if (current->left != NULL)
        unvisited.push(current->left);
    cout << current->self << endl;
}

参考文献

  1. ^ 1.0 1.1 Introduction to Algorithms [算法导论]. ISBN 978-7-111-40701-0. 
  2. ^ Robert E Tarjan - A.M. Turing Award Winner. [2017-10-29]. (原始内容存档于2017-10-30) (英语). 

參見

{{bottomLinkPreText}} {{bottomLinkText}}
深度优先搜索
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?