thumb_up 3 thumb_down 0 flag 0

My first experience with BFS and DFS came when I tried to solve the problem to find the minimum number of moves it will take for a knight to go from position (r1,c1) to position (r2,c2) on a chess board. I tried a DFS algorithm and it would find an answer, but not always the best answer. For example, if we need to go from (1,1) to (3,1), and the DFS happens to pick its first move to go to the square (3,2) the knight will still reach the square (3,1) eventually, but not in the minimum number of moves which is 2 (going from (1,1) to (2,3) to (3,1). To solve this problem, the BFS is better.

The BFS can be implemented with a queue, but if the search space is big, the queue may exhaust memory. In this case, the DFS will take a long time, but not use much memory.