博客
关于我
深度优先搜索(DFS)
阅读量:682 次
发布时间:2019-03-17

本文共 1624 字,大约阅读时间需要 5 分钟。

深度优先搜索(缩写DFS)有点类似广度优先搜索,是对一个连通图进行遍历的算法。它的思想是从一个顶点V0开始,沿着一条路一直走到底,如果发现不能到达目标解,那就返回到上一个节点,然后从另一条路开始走到底,这种尽量往深处走的概念即是深度优先的概念。

给出如图3-1所示的图,求图中的V0出发,是否存在一条路径长度为4的搜索路径。

在这里插入图片描述
处理过程:
在这里插入图片描述在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
上面的例子:

/** * DFS核心伪代码 * 前置条件是visit数组全部设置成false * @param n 当前开始搜索的节点 * @param d 当前到达的深度,也即是路径长度 * @return 是否有解 */bool DFS(Node n, int d){   //输入当前结点以及当前遍历路径长度    if (d == 4){   //路径长度为返回true,表示此次搜索有解        return true;    }     for (Node nextNode in n){   //遍历跟节点n相邻的节点nextNode,        if (!visit[nextNode]){   //未访问过的节点才能继续搜索             //例如搜索到V1了,那么V1要设置成已访问            visit[nextNode] = true;             //接下来要从V1开始继续访问了,路径长度当然要加             if (DFS(nextNode, d+1)){   //如果搜索出有解                //例如到了V6,找到解了,你必须一层一层递归的告诉上层已经找到解                return true;            }             //重新设置成未访问,因为它有可能出现在下一次搜索的别的路径中            visit[nextNode] = false;         }        //到这里,发现本次搜索还没找到解,那就要从当前节点的下一个节点开始搜索。    }    return false;//本次搜索无解}

一般化,基于递归的深度优先搜索算法

/** * DFS核心伪代码 * 前置条件是visit数组全部设置成false * @param n 当前开始搜索的节点 * @param d 当前到达的深度 * @return 是否有解 */bool DFS(Node n, int d){       if (isEnd(n, d)){   //一旦搜索深度到达一个结束状态,就返回true        return true;    }     for (Node nextNode in n){   //遍历n相邻的节点nextNode        if (!visit[nextNode]){   //            visit[nextNode] = true;//在下一步搜索中,nextNode不能再次出现            if (DFS(nextNode, d+1)){   //如果搜索出有解                //做些其他事情,例如记录结果深度等                return true;            }             //重新设置成false,因为它有可能出现在下一次搜索的别的路径中            visit[nextNode] = false;        }    }    return false;//本次搜索无解}

参考:

https://www.cnblogs.com/DWVictor/p/10048554.html
https://blog.csdn.net/raphealguo/article/details/7523411

你可能感兴趣的文章
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named 'pandads'
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No static resource favicon.ico.
查看>>
no such file or directory AndroidManifest.xml
查看>>