The short answer
Dijkstra’s algorithm is simple and fast on graphs with non-negative weights, but it has real limits. Its biggest one is that it cannot handle negative edge weights. It also does not detect negative cycles, it finds paths from one source at a time, and its running time depends on how its priority queue is built.
Negative edge weights
This is the most important limit. Consider three vertices, A, B and C. The edge from A to B has weight 2, the edge from A to C has weight 5, and the edge from C to B has weight −4.
The cheapest route from A to B goes through C, with a total of 5 + (−4) = 1. Dijkstra’s algorithm finalizes B at 2 as soon as it is the smallest estimate in the queue, so it never updates B when the cheaper route appears. It reports 2, which is wrong.
The method depends on the rule that extending a path never makes it cheaper. Negative edges break that rule. The Bellman-Ford algorithm handles this case, and dijkstra algorithm vs bellman ford compares the two in more detail.
No check for negative cycles
A negative cycle is a loop with a total weight below zero. Going around it again lowers the cost, so no shortest path exists. Dijkstra’s algorithm has no step that looks for this situation, so its output may not mean anything when a negative cycle is present. Bellman-Ford can detect a negative cycle with one extra pass.
One source at a time
Dijkstra’s algorithm finds shortest paths from a single starting vertex. To get paths between every pair of vertices, you have to run it from each vertex, or use a different method. Floyd-Warshall is one such method. It handles negative edges when there are no negative cycles, and it runs in O(V³) time.
Dependence on the priority queue
The running time of Dijkstra’s algorithm depends on the priority queue. With a binary heap it takes O((V + E) log V) time. With a plain array it takes O(V²) time, which can be slower on sparse graphs, where the number of edges is small compared with V². If big O notation is unfamiliar, big o data structures explains how to read these expressions.
Searching more than needed
Dijkstra’s algorithm expands vertices in order of distance from the source. If you only need a path to one target, it still settles every vertex that is closer than that target, which can be wasted work. A* search adds an estimate of the remaining distance to guide the search toward the target, but it needs a suitable estimate to work well.
Changing edge weights
If edge weights change often, such as travel times that shift with traffic, the shortest paths may need to be computed again. The basic algorithm does not update old results when a single weight changes. Specialized variants exist for this case, but they are more complicated.
When another method fits better
- Negative weights: use Bellman-Ford.
- Unweighted graphs, where every edge counts the same: breadth-first search finds shortest paths measured in number of edges.
- Connecting all vertices cheaply: this is a minimum spanning tree problem, which Prim’s or Kruskal’s algorithm solves.
Bottom line
Dijkstra’s algorithm is a strong default for shortest paths when every edge weight is zero or positive. Before using it, check that assumption, and check whether you need paths from one source or from every vertex.