当前位置:   article > 正文

dfs和bfs差别_BFS和DFS之间的区别

dfs和bfs差别_BFS和DFS之间的区别

dfs和bfs差别

Here you will learn about difference between BFS and DFS algorithm or BFS vs. DFS.

在这里,您将了解BFS和DFS算法或BFS与DFS之间的区别。

Breadth First Search (BFS) and Depth First Search (DFS) are two popular algorithms to search an element in Graph or to find whether a node can be reachable from root node in Graph or not. And these are popular traversing methods also.

广度优先搜索(BFS)和深度优先搜索(DFS)是在Graph中搜索元素或查找是否可以从Graph中的根节点访问节点的两种流行算法。 这些也是流行的遍历方法。

When we apply these algorithms on a Graph, we can see following types of nodes.

当我们在图上应用这些算法时,我们可以看到以下类型的节点。

  1. Non-Visited nodes: These nodes not yet visited.

    非访问节点:这些节点尚未访问。

  2. Visited nodes: These nodes are visited.

    被访问的节点:这些节点被访问。

  3. Explored nodes: A node is said to be explored when it was visited and all nodes which are connected (adjacent) to that node also visited.

    探索的节点:据说一个节点在被访问时会被探索,并且所有(相邻)连接到该节点的节点也都被访问过。

BFS和DFS之间的区别 (Difference between BFS and DFS)

S. No.Breadth First Search (BFS)Depth First Search (DFS)
1.BFS visit nodes level by level in Graph.DFS visit nodes of graph depth wise. It visits nodes until reach a leaf or a node which doesn’t have non-visited nodes.
2.A node is fully explored before any other can begin.Exploration of a node is suspended as soon as another unexplored is found.
3.Uses Queue data structure to store Un-explored nodes.Uses Stack data structure to store Un-explored nodes.
4.BFS is slower and require more memory.DFS is faster and require less memory.
5.Some Applications:
  • Finding all connected components in a graph.
  • Finding the shortest path between two nodes.
  • Finding all nodes within one connected component.
  • Testing a graph for bipartiteness.
Some Applications:
  • Topological Sorting.
  • Finding connected components.
  • Solving puzzles such as maze.
  • Finding strongly connected components.
  • Finding articulation points (cut vertices) of the graph.
序号 广度优先搜索(BFS) 深度优先搜索(DFS)
1。 BFS在Graph中逐级访问节点。 DFS访问图深度明智的节点。 它会访问节点,直到到达叶或没有未访问节点的节点为止。
2。 在开始任何其他节点之前,必须先充分探究一个节点。 一旦发现另一个未探索节点,就会暂停对该节点的探索。
3。 使用队列数据结构存储未探索的节点。 使用堆栈数据结构存储未探索的节点。
4。 BFS较慢,需要更多的内存。 DFS更快并且需要更少的内存。
5, 一些应用:
  • 在图中查找所有连接的组件。
  • 查找两个节点之间的最短路径。
  • 查找一个连接的组件内的所有节点。
  • 测试图的二等性。
一些应用:
  • 拓扑排序。
  • 查找连接的组件。
  • 解决迷宫等难题。
  • 查找牢固连接的组件。
  • 查找图形的关节点(切顶点)。

Example

Considering A as starting vertex.

将A作为起始顶点。

Difference between BFS and DFS

Image Source

图片来源

Note: There are many sequences possible for BFS and DFS. Using permutations we can find how many are there.

注意: BFS和DFS有许多可能的序列。 使用排列,我们可以找到其中的数目。

Comment below if you found any information incorrect or missing in above tutorial for difference between dfs and bfs.

如果您发现上面的教程中的dfs和bfs之间存在差异,则您在以下教程中发现任何不正确或缺失的信息时,请在下面发表评论。

翻译自: https://www.thecrazyprogrammer.com/2017/06/difference-between-bfs-and-dfs.html

dfs和bfs差别

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/445540?site
推荐阅读
相关标签
  

闽ICP备14008679号