The intuition BFS and DFS: Algorithms basis

The intuition BFS and DFS: Algorithms basis

https://cuijiahua.com/blog/2018/01/alogrithm_10.html

The intuition BFS and DFS: Algorithms basis

I. Introduction

Our first contact  BFS  time and DFS, should be "graph traversal" in terms of data structures class. There is a brush problem when we often use the binary tree traversal BFS and DFS. Their implementation is very simple, I will not shiver went paste code.

Want to see can see the code " to prove safety Offer (thirty-eight): binary tree of depth " This topic can be solved using the BFS and DFS. So, what is the difference between these two "walk" sequence in the end?

This article will simply say something about the difference between them and the respective application, does not involve any code. We "traverse the map" for example, is described.

Second, the difference

Breadth-first search algorithm (Breadth-First-Search, abbreviated BFS) , it is a use of queue search algorithm works. In simple terms, its search process and "a stone thrown into the lake, the ripples" similar.

Depth-first search algorithm (Depth-First-Search, abbreviated as the DFS) , is a use of a recursive search algorithm works. In simple terms, its search process and "do not hit the brick wall does not look back" is similar.

BFS is focused on the queue, but DFS focus is recursive. This is the essential difference between them.

For a typical example, as shown below, represents the gray walls, green represents the starting point, the red represents the end, the provisions can only go one step further, and can only go down or right. Seeking a green to red shortest path.

The intuition BFS and DFS: Algorithms basis

For the above problem, BFS and DFS results are obtained, the difference between them is that there are differences in complexity. I can tell you that the problem is better BFS algorithm.

BFS Illustration:

The intuition BFS and DFS: Algorithms basis

As shown above, from the starting point, for each point in the queue, the point which must be traversed around. So BFS search process and "a stone thrown into the lake, the ripples," is very similar to the origin of the "breadth" namely the "breadth-first search algorithm" in.

DFS Illustration:

The intuition BFS and DFS: Algorithms basis

As shown, from the starting point as shown above, first of all point in one direction will change direction traversed ...... So, DFS search process and "do not hit the brick wall does not look back," very similar, namely " the origin "in the" depth "of depth-first search algorithm.

Third, the summary

Now, you may wish to shining map, go see if you print out the traversal sequence, it is not clear?

Lastly is under the direction of their application.

BFS used to find a single shortest route, which is characterized by "the optimal solution is found to the" DFS while looking for solutions to all problems, its high space efficiency, and to find the optimal solution is not necessarily to be recorded and complete the search, so under normal circumstances, deep search requires a very efficient pruning (pruning concepts please Baidu).

PS: BFS and DFS algorithms are very important, if you want readers to better understand them, it is recommended to find some OJ or training related to the title match on Leetcode, we will give you a different kind of world.

Guess you like

Origin www.cnblogs.com/kungfupanda/p/11248014.html