Dijkstra's Algorithm
graph theory
weighted graphs
algorithm limitations
zero weight edges

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.

Practice algorithms

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:

text
1A -> B (0)
2A -> C (4)
3B -> C (1)
4B -> D (0)
5D -> C (1)

Starting from A, the shortest distances are:

  • 'A = 0'
  • 'B = 0'
  • 'D = 0'
  • 'C = 1'

A normal heap-based implementation computes that result correctly.

python
1import heapq
2
3
4def dijkstra(graph, start):
5    dist = {node: float("inf") for node in graph}
6    dist[start] = 0
7    pq = [(0, start)]
8
9    while pq:
10        current_dist, node = heapq.heappop(pq)
11        if current_dist > dist[node]:
12            continue
13
14        for neighbor, weight in graph[node]:
15            new_dist = current_dist + weight
16            if new_dist < dist[neighbor]:
17                dist[neighbor] = new_dist
18                heapq.heappush(pq, (new_dist, neighbor))
19
20    return dist
21
22
23graph = {
24    "A": [("B", 0), ("C", 4)],
25    "B": [("C", 1), ("D", 0)],
26    "C": [],
27    "D": [("C", 1)],
28}
29
30print(dijkstra(graph, "A"))

Output:

text
{'A': 0, 'B': 0, 'C': 1, 'D': 0}

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.

text
A -> B (2)
A -> C (5)
C -> B (-10)

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.

Course
Intermediate
27 lessons
15 hours
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 course
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.

Practice algorithms