本文介绍了什么时候使用深度优先搜索(DFS)和广度优先搜索(BFS)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解DFS和BFS之间的区别,但是我想知道什么时候使用DFS与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?

推荐答案

这在很大程度上取决于搜索树的结构以及解决方案(也就是搜索到的项目)的数量和位置。

That heavily depends on the structure of the search tree and the number and location of solutions (aka searched-for items).


  • 如果您知道解决方案离树的根并不远,则可能需要
    广度优先搜索(BFS)更好。

  • 如果树很深且解决方案很少,则深度优先搜索
    (DFS)可能会花费很长时间,但BFS可能会更快。

  • If you know a solution is not far from the root of the tree, abreadth first search (BFS) might be better.
  • 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.

如果树很宽,则BFS可能需要太多内存,因此
可能是完全不切实际的。

If the tree is very wide, a BFS might need too much memory, so itmight be completely impractical.

如果解决方案很常见但位于树的深处,则BFS可能是
不切实际的。

If solutions are frequent but located deep in the tree, BFS could beimpractical.

但是这些只是经验法则;您可能需要进行实验。

But these are just rules of thumb; you'll probably need to experiment.

这篇关于什么时候使用深度优先搜索(DFS)和广度优先搜索(BFS)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-04 06:53