Why use Dijkstra's algorithm instead of Best (Cheapest) First Search?

Alexander Suraphel picture Alexander Suraphel · Apr 29, 2012 · Viewed 13.8k times · Source

From what I have read so far. The Best First Search seems faster in terms of finding the shortest path to the goal because Dijkstra's algorithm has to relax all the nodes as it traverses the graph. What makes Dijkstra's algorithm better than Best First Search?

Answer

amit picture amit · Apr 29, 2012

EDIT: Your edit clarifies you are interested in Best-First Search, and not BFS.

Best-First Search is actually an informed algorithm, which expands the most promising node first. Very similar to the well known A* algorithm (actually A* is a specific best-first search algorithm).

Dijkstra is uninformed algorithm - it should be used when you have no knowledge on the graph, and cannot estimate the distance from each node to the target.

Note that A* (which is a s best-first search) decays into Dijkstra's algorithm when you use heuristic function h(v) = 0 for each v.

In addition, Best First Search is not optimal [not guaranteed to find the shortest path], and also A*, if you do not use an admissible heuristic function, while Dijkstra's algorithm is always optimal, since it does not relay on any heuristic.

Conclusion: Best-First Search should be prefered over dijkstra when you have some knowledge on the graph, and can estimate a distance from target. If you don't - an uninformed Best-First Search that uses h(v) = 0, and relays only on already explored vertices, decays into Dijkstra's algorithm.
Also, if optimality is important - Dijkstra's Algorithm always fit, while a best-first search algorithm (A* specifically) can be used only if an appropriate heuristic function is available.


Original answer - answering why to chose Dijkstra over BFS:

BFS fails when it comes to weighted graphs.

Example:

     A
   /   \
  1     5
 /       \
B----1----C

In here: w(A,B) = w(B,C) = 1, w(A,C) = 5.

BFS from A will return A->C as the shortest path, but for the weighted graph, it is a path of weight 5!!! while the shortest path is of weight 2: A->B->C.
Dijkstra's algorithm will not make this mistake, and return the shortest weighted path.

If your graph is unweighted - BFS is both optimal and complete - and should usually be prefered over dijkstra - both because it is simpler and faster (at least asymptotically speaking).