Shortest paths and spanning trees answer different questions

← Back to Blog & Articles

Chapter 13 dealt with pure connectivity questions — is there a path, is there a cycle, can I order these nodes.

the companion book dealt with pure connectivity questions — is there a path, is there a cycle, can I order these nodes. this article adds a second dimension: cost. Now every edge has a weight (or, in the unweighted case, every edge counts as one “step”), and the question shifts from “can I get there” to “what’s the cheapest way to get there,” or “what’s the cheapest way to connect everyone.” That shift changes which traversal tool is correct, and using the wrong one is one of the most common graph mistakes candidates make under interview pressure.

The signal to watch for is explicit costs on edges — prices, distances, latencies, travel times — combined with a question about minimizing or maximizing some total. If every edge effectively costs the same (an unweighted grid, a social network where every connection counts equally), plain breadth-first search already gives you shortest paths for free, because BFS naturally explores nodes in order of increasing distance. The moment edges have different weights, plain BFS stops being correct — it will find a path with the fewest hops, not the cheapest one — and you need something that accounts for cost while choosing what to explore next. That’s Dijkstra’s algorithm, which is really BFS with a priority queue substituted for a plain queue so that the traversal always expands the currently-cheapest known frontier node next. A related but distinct question is: instead of the shortest path between two specific nodes, what’s the cheapest way to connect all nodes into a single network, using a subset of the available edges? That’s a minimum spanning tree question, solved by Prim’s or Kruskal’s algorithm.

These techniques don’t apply when edges can have negative weights — Dijkstra’s greedy “always expand the cheapest frontier” assumption breaks down if a later edge could make an already-finalized path cheaper in retrospect. Negative weights call for Bellman-Ford, which relaxes every edge repeatedly instead of greedily; you’ll see a bounded, practical version of that idea in this article’s second problem, where the “at most K stops” constraint replaces the need for full negative-weight handling. They also don’t apply if there’s no cost structure at all and you just want connectivity — that’s back to the companion book.

this article builds up from unweighted BFS to constrained relaxation, to full Dijkstra, to spanning trees, and closes with a Dijkstra variant that’s really about interpreting the output differently rather than changing the algorithm.

Continue with the book

Explore the full treatment, worked examples, and exercises in Algorithmic Edge.


Companion article for Algorithmic Edge, published by Choral Software. Article text and original examples © 2026 Choral Software. All rights reserved.

Scroll to Top