BFS vs Dijkstra vs 0–1 BFS

Use BFS to minimize the number of edges. If every edge costs 0 or 1, use 0–1 BFS to minimize total cost. For general nonnegative edge weights, use Dijkstra’s algorithm.

Which shortest-path algorithm should you use?

AlgorithmEdge weightsTime
BFSUnweighted or all equal and positiveO(V + E)
0–1 BFSOnly 0 and 1O(V + E)
DijkstraAny nonnegative weightsO((V + E) log V) with an indexed binary heap

These time bounds use adjacency lists, where V is the number of vertices and E the number of edges. All three algorithms start from one source and work with directed and undirected graphs. For directed graphs, follow outgoing edges.

One graph, three ways to price its edges

Find a path from vertex 0 to vertex 3 in an undirected graph. You can go directly from 0 to 3 or take the chain 0 → 1 → 2 → 3. The graph stays the same while the edge costs change.

EdgeBFS0–1 BFSDijkstra
0–3118
0–1102
1–2101
2–3102

BFS finds the fewest edges

Breadth-first search uses a first-in, first-out queue to visit vertices in layers. It visits vertices one edge away, then those two edges away, and so on. The first time it discovers a vertex, it has found a path using the fewest edges. Recording each vertex’s predecessor lets it reconstruct that path.

Here BFS returns 0 → 3, a path of one edge. If all edges have the same positive weight, multiplying the edge count by that weight gives the total cost, so the same path also minimizes cost.

Run the graph in the BFS shortest-path lesson

0–1 BFS finds the cheapest path with free and paid edges

When an edge improves a vertex’s tentative distance, put that vertex at the front of a double-ended queue (deque) if the edge costs 0, or at the back if it costs 1. This keeps the next distance to process at the front. A vertex can receive a cheaper distance after its first discovery, so update the distance whenever you find an improvement.

Here 0 → 1 → 2 → 3 costs 0, while the direct edge costs 1. The lesson returns distances [0, 0, 0, 0] in vertex order.

Run the graph in the 0–1 BFS lesson

Dijkstra’s algorithm handles any nonnegative weights

Pick the unsettled vertex with the smallest tentative distance, finalize its distance, then check whether going through it improves any neighboring distance. With nonnegative weights, continuing along a path cannot reduce its cost. A vertex with the smallest tentative distance is therefore safe to finalize, even when some edges cost zero.

Here Dijkstra first sees a direct route to 3 for 8. Processing vertices 1 and 2 reveals 0 → 1 → 2 → 3 for a total of 5. The lesson returns distances [0, 2, 3, 5] in vertex order.

The lesson scans vertices to find each minimum, which takes O(V² + E) time. An indexed binary heap with decrease-key takes O((V + E) log V) time. A heap that inserts a new entry for every improvement and skips stale entries takes O(V + E log(E + 1)) time. For simple graphs, this also fits the O((V + E) log V) bound.

Run the graph in the Dijkstra lesson

What if an edge has a negative weight?

Use an algorithm that supports negative weights, such as Bellman–Ford. A negative cycle reachable from the source makes the minimum cost undefined for any vertex reachable from that cycle, because repeated laps keep lowering the cost.