Searching Smarter: From BFS and DFS to A*
Many problems in AI come down to finding a path: from a start state to a goal, through a graph of possible moves. How a search picks the next node to open decides how fast it gets there, and whether the path it finds is the best one.
Uninformed search
Breadth-first search (BFS) opens nodes level by level with a queue. It finds the path with the fewest steps, at the cost of keeping a whole level in memory. Depth-first search (DFS) dives down one branch with a stack: it needs little memory, but the first path it finds may be long, and on an infinite branch it may never come back.
Informed search
A heuristic h(n) estimates how far a node is from the goal. Greedy best-first search always opens the node with the smallest h(n): often fast, but easily fooled by a shortcut that leads nowhere.
A* adds the cost already paid, g(n), and opens the node with the smallest f(n) = g(n) + h(n). As long as the heuristic never overestimates, A* finds an optimal path.
Which one to use
Every step costs the same and the graph is small: BFS.
Memory is tight and any path will do: DFS.
A good heuristic exists, such as the straight-line distance on a map: A*.
Share

