Why use Dijkstra's Algorithm if Breadth First Search (BFS) can do the same thing faster?

Dijkstra allows assigning distances other than 1 for each step. For example, in routing the distances (or weights) could be assigned by speed, cost, preference, etc. The algorithm then gives you the shortest path from your source to every node in the traversed graph.

Meanwhile BFS basically just expands the search by one “step” (link, edge, whatever you want to call it in your application) on every iteration, which happens to have the effect of finding the smallest number of steps it takes to get to any given node from your source (“root”).


If you consider travel websites, these use Dijkstra's algorithm because of weights (distances) on nodes.

If you will consider the same distance between all nodes, then BFS is the better choice.

For example, consider A -> (B, C) -> (F) with edge weights given by A->B = 10, A->C = 20, B->F = C->F = 5.

Here, if we apply BFS, the answer will be ABF or ACF, as both are shortest paths (with respect to the number of edges), but if we apply Dijstra's, the answer will be ABF only because it considers the weights on the connected path.


Dijkstra's algorithm

  • Like BFS for weighted graphs.
  • If all costs are equal, Dijkstra = BFS

Source : https://cs.stanford.edu/people/abisee/gs.pdf