When to use DFS, when to use BFS?

Source: Internet
Author: User

When to use DFS, when to use BFS?

The title of the two-dimensional array, n less than 20, applies to DFS. and generally n<= 200,n<=1000 this, certainly cannot use DFS to do. And it's not just the whole problem. DFS is not available, and DFS is not used at every step.

Basic steps of BFS

1. Add an initial point (one or more) to the end of a collection

2. Remove the point from the head of the assembly, determine the perimeter of the initial point, and join the qualifying points to the queue

3. Repeat the 2 operation until the collection is empty. (typically each point is queued only once)

In general, the problem solved with DFS can be solved with BFS.

DFS (Deep Search while considering backtracking)

bfs= queue, into queue, out queue, dfs= stack, stack, out stack

BFS is based on a layer of access, so suitable for a target to find the shortest steps, you think layers of search each layer represents a step. BFS first access is the sibling node, only this layer of all access to the next layer, that is, the first layer of BFS represents the current can go to the location (node). DFS is implemented by recursion, it takes precedence over the search depth, then backtracking, and first accesses the child nodes that have not been visited.
DFS is used for connectivity issues because its operational thinking is very similar to that of the human brain, so it is more natural to solve connectivity problems. BFS is used to solve the shortest-circuit problem, it needs to store each layer of information in the process, so its operation needs to store a large amount of information, if the human brain can also store a large number of messages, the brain can theoretically run BFS.
In general, the memory required to run BFS is larger than the amount of memory that DFS requires (Dfs accesses one route at a time, BFS accesses multiple routes at once), and DFS is prone to exploding (stacks are not easy to "control"), and BFS can solve the "burst queue" risk well by controlling the queue.
Their respective advantages need to be analyzed by practical problems and applied to different problems according to their respective characteristics in order to get the best performance.

When to use DFS, when to use BFS?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.