Stay up all night and get angry, graphic algorithm! Intuitive explanation of BFS and DFS

I. Introduction

When we first came into contact with BFS and DFS, it should be "traversal of graphs" in data structure class. There is also a problem, traversing the binary tree, we will often use BFS and DFS. Their implementations are very simple, so I won't hesitate to post the code here.

If you want to see the code, you can read the topic " Sword Finger Offer (38): The Depth of Binary Tree ", which can be solved using BFS and DFS. So, what's the difference between the two "traversed" sequences?

This article will simply talk about their differences and their respective applications, and will not involve any code. We take "traversal of graphs" as an example to illustrate.

Second, the difference

Breadth-First-Search (abbreviated as BFS) is a search algorithm implemented using queues . To put it simply, the search process is similar to "throwing a stone into the lake to create ripples."

The depth-first search algorithm (Depth-First-Search, abbreviated as DFS) is a search algorithm implemented by recursion . Simply put, its search process is similar to "don't hit the south wall, don't look back".

The focus of BFS is on queues, while the focus of DFS is on recursion. This is their essential difference.

For a typical example, as shown in the figure below, gray represents the wall, green represents the starting point, and red represents the ending point. It is stipulated that you can only take one step at a time, and you can only go down or right. Find the shortest path from green to red.

For the above problem, both BFS and DFS can find the result, the difference between them is the difference in complexity. I can tell you first that BFS is the better algorithm for this question.

Schematic diagram of BFS:

As shown in the figure above, starting from the starting point, for each point out of the queue, it is necessary to traverse the points around it. Therefore, the search process of BFS is very similar to "throwing a stone into the lake and causing layers of ripples", which is the origin of the "breadth" in the "breadth-first search algorithm".

DFS schematic:

3. Summary

Now, you might as well look at the graph and look at the traversal sequence you printed out. Is it clear at a glance?

Finally, let’s talk about their application direction.

BFS is often used to find a single shortest route. Its characteristic is that "the search is the optimal solution", while DFS is used to find all solutions. It has high space efficiency, and the found is not necessarily the optimal solution, it must be recorded. And complete the entire search, so in general, deep search requires very efficient pruning (please Baidu for the concept of pruning).

PS: BFS and DFS are very important algorithms. If readers want to learn more about them, it is recommended to go to OJ or Leetcode to find some related problems for training, which will definitely give you a different world.

As shown in the figure above, starting from the starting point, you must traverse all the points in one direction before changing the direction... Therefore, the search process of DFS is very similar to "don't hit the south wall and don't look back", which is " The origin of "depth" in "Depth-First Search Algorithms".

Finally, I will give you a copy to help me get the data structure of BAT and other first-tier manufacturers. It was written by a Google master, and it is very useful for students who have weak algorithms or need to improve:

Google and Ali's Leetcode brushing notes

As well as the BAT algorithm engineer learning route, books + videos, complete learning route and instructions that I have compiled, it will definitely help those who want to become algorithm engineers (extraction code: jack):

How I became an algorithm engineer, super detailed learning path

Don't just collect it, come to like it, refill~

Guess you like

Origin blog.csdn.net/c406495762/article/details/117307841