Why use Dijkstra's Algorithm if Breadth First Search (BFS) can do the same thing faster?
AlgorithmGraphDijkstraBreadth First-SearchAlgorithm Problem Overview
Both can be used to find the shortest path from single source. BFS runs in O(E+V)
, while Dijkstra's runs in O((V+E)*log(V))
.
Also, I've seen Dijkstra used a lot like in routing protocols.
Thus, why use Dijkstra's algorithm if BFS can do the same thing faster?
Algorithm Solutions
Solution 1 - Algorithm
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”).
Solution 2 - Algorithm
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.
Solution 3 - Algorithm
Dijkstra's algorithm
- Like BFS for weighted graphs.
- If all costs are equal, Dijkstra = BFS
Solution 4 - Algorithm
From implementation perspective, the Dijkstra's algorithm could be implemented exactly like a BFS by swapping the queue
with a priority queue
.
Solution 5 - Algorithm
There is a confusion about this, it is possible to use modified BFS algorithm to find a shortest path in a weighted directed graph:
- Use priority queue instead of a normal queue
- Don't track visited nodes, and instead track distance from the starting node
Because of 2, some nodes will be visited more then once, which makes it less efficient comparing to Dijkstra.
shortest = sys.maxsize
queue = [(0, src, 0)]
while queue:
(cost, node, hops) = heapq.heappop(queue)
if node == dst:
shortest = min(distance, cheapest)
for (node_to, node_distance) in edges[node]:
heapq.heappush(queue, (cost + node_distance, node_to, hops + 1))
Solution 6 - Algorithm
- BFS only works when you’re counting shortest path as number of steps edges, or whatever application assigns identical (but positive) weights to all edges.
- The difference between BFS and Dijkstra is just replacing the FIFO queue with a priority queue!
Note that this doesn’t fix the positive weights constraint on edges, a famous shortcoming Dijkstra (and BFS) has that is fixed by Bellman-Ford by paying a speed penalty
Source: Chapters 8.4, 8.6 in Erickson, Jeff (2019). Algorithms