Can Dijkstra's Algorithm work on a graph with weights of 0?
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
Yes. Dijkstra's algorithm works correctly on graphs with zero-weight edges as long as no edge has a negative weight. Zero is still non-negative, so it does not violate the greedy argument that makes the algorithm correct.
Why Zero Weights Are Safe
Dijkstra repeatedly selects the unprocessed node with the smallest known distance from the source and relaxes its outgoing edges. That strategy remains valid when some edges cost 0 because a zero-weight edge does not create a path cheaper than the already finalized minimum in a way that breaks the proof.
The actual boundary is:
- positive weights are safe
- zero weights are safe
- negative weights are unsafe
Many explanations oversimplify this and say “positive weights only,” but that is stricter than necessary.
A Small Example
Consider this graph:
Starting from A, the shortest distances are:
- '
A = 0' - '
B = 0' - '
D = 0' - '
C = 1'
A normal heap-based implementation computes that result correctly.
Output:
What Zero Weights Change in Practice
Zero-weight edges often create more ties in the priority queue because several nodes may end up with the same shortest distance. That does not affect correctness. It only changes the order in which equally good candidates are processed.
Zero-weight cycles are also fine in the shortest-path setting as long as no edge is negative. Cycling around them does not reduce path cost, so they do not create the “keep improving forever” problem that negative cycles cause.
What Actually Breaks Dijkstra
Negative edges are the real issue. A node can be finalized too early and later discovered to have a cheaper path that goes through a negative edge.
If the algorithm settles B first at 2, it misses the better path through C, whose total cost is -5.
That is why graphs with negative edges need Bellman-Ford, Johnson's algorithm, or another method designed for that setting.
Complexity Does Not Change
Zero-weight edges do not change the standard time complexity. With a binary heap, Dijkstra still runs in roughly O((V + E) log V).
What changes is only the shape of the queue traffic. You may get more equal-distance entries, but not a different asymptotic cost.
Common Pitfalls
The most common mistake is believing Dijkstra requires strictly positive weights. It does not. It requires non-negative weights.
Another mistake is blaming zero-weight edges for bugs caused by an incorrect implementation. A missing stale-entry check in the priority queue can produce wrong or slow behavior even on valid graphs.
Developers also sometimes assume zero-weight cycles behave like negative cycles. They do not. They create equal-cost alternatives, not endlessly improving paths.
Summary
- Dijkstra works on graphs with zero-weight edges.
- The real requirement is non-negative weights, not strictly positive weights.
- Zero-weight cycles are not a correctness problem.
- Negative edges are the reason to switch algorithms.
- If zero weights seem to break your code, inspect the implementation before doubting the theory.
Related reading
- Can Dijkstra's Single Source Shortest Path Algorithm detect an infinite cycle in a graph?
- Can hash tables really be O1?
- Can I abuse a predicate to perform operations on the elements before remove_if removes them?
- Can I dispatch messages with a custom algorithm instead of round robin using RabbitMQ?
- Can I do this with Boost interval_map?
- Can I encode a string to an integer in a way that preserves lexicographic string closeness?
- Can I dispatch messages with a custom algorithm instead of round robin using RabbitMQ?
- Can I guess the appropriate epsilon for RDP Ramer-Douglas-Peucker?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.