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?