DFS和BFS是否可以互换?

发布于 2025-02-08 15:57:34 字数 91 浏览 1 评论 0 原文

我知道DFS适合某些问题,而BFS对其他问题有好处,但是如果使用DFS可以解决某些问题,可以用BFS(或Vise Versa)解决(也许不那么最佳)吗?有证据吗?谢谢!

I know DFS is good for some problems and BFS is good for some other problems but if something can be solved using DFS, can it be solved (maybe less optimally) with BFS (or vise versa)? And is there a proof for it? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隐诗 2025-02-15 15:57:34

否。

最短路径是可以用BF轻松解决的典型示例。但是,对于DFS,您要么必须接受无尽循环的可能性,要么避免重新审视节点,并且在许多情况下找不到最短的路径。

示例以其他方式很难找到。但是例如,用于平面测试的算法取决于以下事实:在DFS中,您知道当前节点的整个路径的属性。而且您在BFS中没有这种情况。

No.

Shortest path is the canonical example of something that can be solved easily with BFS. But with DFS you either have to accept the possibility of endless loops, or else avoid revisiting nodes and fail to find shortest paths in many cases.

Examples going the other way are harder to find. But for example the Hopcroft-Tarjan algorithm for planarity testing relies on the fact that in a DFS you know properties of the entire path to the present node. And that context you do not have in a BFS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文