什么时候使用深度优先搜索(DFS)与广度优先搜索(BFS)是否可行?

本文探讨了在什么情况下选择深度优先搜索(DFS)而非广度优先搜索(BFS)更为合适。DFS内存需求低,但可能深入不必要的路径;BFS适用于寻找最短路径。具体应用中,如在家庭树中找活着的人,DFS更快;找去世的成员,BFS更快。在Facebook好友推荐中,BFS用于找直接朋友,DFS用于找最短路径或检测循环。选择取决于数据特性和目标。
摘要由CSDN通过智能技术生成

本文翻译自:When is it practical to use Depth-First Search (DFS) vs Breadth-First Search (BFS)?

I understand the differences between DFS and BFS, but I'm interested to know when it's more practical to use one over the other? 我理解DFS和BFS之间的区别,但是我很想知道何时使用一个比另一个更实用?

Could anyone give any examples of how DFS would trump BFS and vice versa? 任何人都可以举例说明DFS如何胜过BFS,反之亦然?


#1楼

参考:https://stackoom.com/question/Dz3D/什么时候使用深度优先搜索-DFS-与广度优先搜索-BFS-是否可行


#2楼

Nice Explanation from http://www.programmerinterview.com/index.php/data-structures/dfs-vs-bfs/ 来自http://www.programmerinterview.com/index.php/data-structures/dfs-vs-bfs/的精彩解释

An example of BFS BFS的一个例子

Here's an example of what a BFS would look like. 这是BFS的样子。 This is something like Level Order Tree Traversal where we will use QUEUE with ITERATIVE approach (Mostly RECURSION will end up with DFS). 这类似于Level Order Tree Traversal,我们将使用QUEUE和ITERATIVE方法(大多数RECURSION将以DFS结束)。 The numbers represent the order in which the nodes are accessed in a BFS: 数字表示在BFS中访问节点的顺序:

在此输入图像描述

In a depth first search, you start at the root, and follow one of the branches of the tree as far as possible until either the node you are looking for is found or you hit a leaf node ( a node with no children). 在深度优先搜索中,从根开始,并尽可能地跟随树的一个分支,直到找到您要查找的节点或者您到达叶节点(没有子节点的节点)。 If you hit a leaf node, then you continue the search at the nearest ancestor with unexplored children. 如果您点击叶子节点,则继续搜索最近的祖先与未探测的子节点。

An example of DFS DFS的一个例子

Here's an example of what a DFS would look like. 这是DFS的外观示例。 I think post order traversal in binary tree will start work from the Leaf level first. 我认为二叉树中的发布顺序遍历将首先从Leaf级别开始工作。 The numbers represent the order in which the nodes are accessed in a DFS: 数字表示在DFS中访问节点的顺序:

在此输入图像描述

Differences between DFS and BFS DFS和BFS之间的差异

Comparing BFS and DFS, the big advantage of DFS is that it has much lower memory requirements than BFS, because it's not necessary to store all of the child pointers at each level. 比较BFS和DFS,DFS的一大优势是它比BFS具有更低的内存要求,因为没有必要在每个级别存储所有子指针。 Depending on the data and what you are looking for, either DFS or BFS could be advantageous. 根据数据和您要查找的内容,DFS或BFS可能是有利的。

For example, given a family tree if one were looking for someone on the tree who's still alive, then it would be safe to assume that person would be on the bottom of the tree. 例如,给定一个家谱,如果一个人在树上寻找仍然活着的人,那么可以安全地假设这个人将在树的底部。 This means that a BFS would take a very long time to reach that last level. 这意味着BFS需要很长时间才能达到最后一级。 A DFS, however, would find the goal faster. 但是,DFS会更快地找到目标。 But, if one were looking for a family member who died a very long time ago, then that person would be closer to the top of the tree. 但是,如果一个人正在寻找一个很久以前去世的家庭成员,那么这个人就会更接近树顶了。 Then, a BFS would usually be faster than a DFS. 然后,BFS通常比DFS快。 So, the advantages of either vary depending on the data and what you're looking for. 因此,两者的优势取决于数据和您正在寻找的内容。

One more example is Facebook; 另一个例子是Facebook; Suggestion on Friends of Friends. 对朋友之友的建议。 We need immediate friends for suggestion where we can use BFS. 我们需要直接的朋友来建议我们可以使用BFS。 May be finding the shortest path or detecting the cycle (using recursion) we can use DFS. 可能是找到最短路径或检测周期(使用递归)我们可以使用DFS。


#3楼

According to the properties of DFS and BFS. 根据DFS和BFS的属性。 For example,when we want to find the shortest path. 例如,当我们想要找到最短路径时。 we usually use bfs,it can guarantee the 'shortest'. 我们通常使用bfs,它可以保证'最短'。 but dfs only can guarantee that we can come from this point can achieve that point ,can not guarantee the 'shortest'. 但是dfs只能保证我们能够从这一点来实现那一点,不能保证'最短'。


#4楼

DFS is more space-efficient than BFS, but may go to unnecessary depths. DFS比BFS更节省空间,但可能会达到不必要的深度。

Their names are revealing: if there's a big breadth (ie big branching factor), but very limited depth (eg limited number of "moves"), then DFS can be more preferrable to BFS. 他们的名字很明显:如果有一个很大的广度(即大分支因子),但深度非常有限(例如“移动”数量有限),那么DFS可能更适合BFS。


On IDDFS 在IDDFS上

It should be mentioned that there's a less-known variant that combines the space efficiency of DFS, but (cummulatively) the level-order visitation of BFS, is the iterative deepening depth-first search . 应该提到的是,有一个鲜为人知的变体结合了DFS的空间效率,但(累积地)BFS的水平顺序访问,是迭代加深深度优先搜索 This algorithm revisits some nodes, but it only contributes a constant factor of asymptotic difference. 该算法重新访问了一些节点,但它只是渐近差的常数因子。


#5楼

That heavily depends on the structure of the search tree and the number and location of solutions (aka searched-for items). 这在很大程度上取决于搜索树的结构以及解决方案的数量和位置(也就是搜索项目)。

  • If you know a solution is not far from the root of the tree, a breadth first search (BFS) might be better. 如果您知道解决方案离树的根不远,那么广度优先搜索(BFS)可能会更好。
  • If the tree is very deep and solutions are rare, depth first search (DFS) might take an extremely long time, but BFS could be faster. 如果树很深并且解决方案很少,深度优先搜索(DFS)可能需要很长时间,但BFS可能会更快。

  • If the tree is very wide, a BFS might need too much memory, so it might be completely impractical. 如果树很宽,BFS可能需要太多内存,所以它可能完全不切实际。

  • If solutions are frequent but located deep in the tree, BFS could be impractical. 如果解决方案频繁但位于树的深处,那么BFS可能是不切实际的。

  • If the search tree is very deep you will need to restrict the search depth for depth first search (DFS), anyway (for example with iterative deepening). 如果搜索树非常深,则无论如何都需要限制深度优先搜索(DFS)的搜索深度(例如,使用迭代加深)。

But these are just rules of thumb; 但这些只是经验法则; you'll probably need to experiment. 你可能需要进行实验。


#6楼

Breadth First Search is generally the best approach when the depth of the tree can vary, and you only need to search part of the tree for a solution. 当树的深度可以变化时,广度优先搜索通常是最好的方法,并且您只需要搜索树的一部分以获得解决方案。 For example, finding the shortest path from a starting value to a final value is a good place to use BFS. 例如,找到从起始值到最终值的最短路径是使用BFS的好地方。

Depth First Search is commonly used when you need to search the entire tree. 当您需要搜索整个树时,通常会使用深度优先搜索。 It's easier to implement (using recursion) than BFS, and requires less state: While BFS requires you store the entire 'frontier', DFS only requires you store the list of parent nodes of the current element. 它比BFS更容易实现(使用递归),并且需要更少的状态:虽然BFS要求您存储整个“前沿”,但DFS只需要存储当前元素的父节点列表。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值