Understand the difference between BFS and DFS

Breadth first search

    BFS stands for Breadth First Search, which is a vertex-based technology used to find the shortest path in the graph. It uses a first-in first-out queue data structure. In BFS, one vertex is selected and marked at a time, and then its neighboring vertices are accessed and stored in the queue. It is slower than DFS.

        A
       / \
      B   C
     /   / \
    D   E   F

    The output is:

A, B, C, D, E, F
Depth first search

    DFS stands for Depth First Search, which is an edge-based technology. It uses the stack data structure to perform two stages. First, the visited vertices are pushed onto the stack, and then if there are no unvisited vertices, the visited vertices are popped.

        A
       / \
      B   C
     /   / \
    D   E   F

    The output is:

A, B, D, C, E, F
BFS vs DFS
Serial number BFS DFS
1 BFS stands for breadth first search DFS stands for Depth First Search
2 BFS (Breadth First Search) uses the queue data structure to find the shortest path DFS (depth first search) uses stack data structure
3 BFS is more suitable for searching for vertices closer to a given source If there is a solution far away from the source, DFS is more suitable
4 BFS considers all neighbors first, so it is not suitable for decision trees in games or puzzles. DFS is more suitable for game or puzzle problems. We make a decision, and then explore all the paths to that decision. If this decision leads to a winning situation, we will stop.
5 When using an adjacency list, the time complexity of BFS is O(V+E); when using an adjacency matrix, the time complexity of BFS is O(V^2), where V represents a vertex and E represents an edge. When using the adjacency list, the time complexity of DFS is also O(V+E), when using the adjacency matrix, the time complexity of DFS is also O(V^2), where V represents a vertex and E represents an edge.
What are the BFS and DFS of a binary tree?

    A tree is usually traversed in two ways:

  • Breadth-first traversal
  • Depth-first traversal
    • In-order traversal (left-root-right)
    • Preorder traversal (root-left-right)
    • Post-order traversal (left-right-root)
      Insert picture description here
BFS and DFSs of above Tree

Breadth First Traversal : 1 2 3 4 5

Depth First Traversals:
      Preorder Traversal : 1 2 4 5 3 
      Inorder Traversal  :  4 2 5 1 3 
      Postorder Traversal : 4 5 2 3 1
Is there any difference in terms of extra space?

    The additional space required varies.

  1. The extra space required for horizontal sequence traversal is O(w), where w is the maximum width of the binary tree. In the horizontal sequence traversal, nodes of different levels are stored in a queue one after another.
  2. The extra space required for depth-first traversal is O(h), where h is the maximum depth of the binary tree. In depth-first traversal, the stack (or function call stack) stores all the ancestors of the node.

    The maximum width of the binary tree at depth (or height) h can be 2 h 2^h2h , where h starts from 0. So the maximum number of nodes can be at the last level. The worst case occurs when the binary tree is a perfect binary tree with 1, 3, 7, 15... nodes. In the worst case,2 h 2^h2The value of h is Ceil (n/2).
    The height of the balanced binary tree is O (Log n). The worst case occurs on a slanted tree, and the worst case is that the height becomes O(n).
    Therefore, in the worst case, the extra space required for both is O(n). But the worst case happens on different types of trees.
    From the above points, it is obvious that when the tree is more balanced, the additional space required for horizontal sequential traversal may be more, and when the tree is less balanced, the additional space required for depth-first traversal may be more.

how to choose?
  1. Additional space may be a factor to consider (explained above)
  2. Depth-first traversal is usually recursive, and recursive code requires function call system overhead.
  3. The most important point is that BFS accesses nodes from the root, while DFS accesses nodes from the leaves. So, if our problem is to search for something closer to the root, we prefer BFS. If the target node is close to the leaf, we will choose DFS.
Reference documents

[1]MKS075.Difference between BFS and DFS[EB/OL].https://www.geeksforgeeks.org/difference-between-bfs-and-dfs/,2020-07-03.
[2]Dheeraj Gupta.BFS vs DFS for Binary Tree[EB/OL].https://www.geeksforgeeks.org/bfs-vs-dfs-binary-tree/,2016-12-22.

Guess you like

Origin blog.csdn.net/zsx0728/article/details/114669472