Breadth-first Search (BFS) is one of the well-known brute-force search algorithms which has been widely used. The basic idea is just like its name: do the searching by expansion. Staring with the sources, the expansion diffuses layer by layer. In contrast, another common search algorithm is Depth-first Search (DFS) which focuses on one route first until reaching the end, then starts the next possible route.
So one way to understand BFS is that: BFS searches all the possible routes at the same time. And for each route, BFS usually makes the same amount of progress. In the middle of the search, once a route becomes invalid, it can be dropped right away.
So for BFS,
1. the minimum distance can be obtained since the shortest routes can be always found before other routes;
2. to avoid redundant search, need to make sure each position (state) is only searched once. [DFS also needs this].
3. a queue is usually used for the search, to easier realize the layer-by-layer expansion.
Hard Level
Interview Questions
Comments
Post a Comment