DFS(Depth-first search) : Don’t look back until you hit the south wall
BFS(Depth-first search) : Divergent search (split search)
Take the classic example: the maze problem
Draw a maze with 1 for walls and 0 for roads.
DFS thought
Start at the beginning and follow one path to the end. If you cannot reach the desired solution, go back to the previous node and follow another path to the end (i.e. go as deep as you can).
BFS thought
Start at the beginning and look for layers (divergent search) (go around)
Advantages of DFS: Less memory consumption(Easy time limit)
BFS advantage: Less time consumption(Prone to memory overflow)
DFS fits the problem type: given the initial state and the target state, it is required to judge from the initial state to the target stateIf there is a solution.
BFS is suitable for the problem type: given the initial state and the target state, it is required to solve from the initial state to the target stateThe optimal solution.
For example: the maze problem above. To ask if there is a path of length 9 from the top left corner of the maze to the bottom right corner of the maze, use DFS. To ask: What is the shortest path from the top left corner of the maze to the bottom right corner of the maze, use BFS.