DFS & BFS - cocoder39/coco39_LC GitHub Wiki

dfs vs bfs

It depends on the **structure of the search tree** and **the number and location of solutions** (aka searched-for items). If you know a solution is not far from the root of the tree, a breadth first search (BFS) might be better. If the tree is very deep and solutions are rare, depth first search (DFS) might take an extremely long time, but BFS could be faster. If the tree is very wide, a BFS might need too much memory, so it might be completely impractical. If solutions are frequent but located deep in the tree, BFS could be impractical. If the search tree is very deep you will need to restrict the search depth for depth first search (DFS), anyway (for example with iterative deepening).

bfs

shortest path (BFS Shortest Path Template)

dfs

Distribute elements to K buckets (dfs + pruning)

tree (divide-and-conquer)

matrix

combination

permutation

set

bitmask

recursion

backtrack