liveBook · Manning

Single-source shortest path

As explained in Classic Computer Science Problems in Swift: Essential techniques for practicing programmers

The single-source shortest path problem asks for the minimum-weight paths from one starting vertex to every other vertex in a weighted graph.

The single-source shortest path problem finds the minimum-weight path from one starting vertex to every other vertex in a weighted graph. Dijkstra’s algorithm solves it greedily by repeatedly exploring the closest known vertex, updating distances and predecessor edges when shorter routes appear. section 4.4

Dijkstra’s algorithm proceeds as follows:

  • Add the starting vertex to an ascending priority queue. section 4.4
  • Remove the closest vertex, inspect every neighbor, and record a new distance and predecessor edge when the route is shorter. section 4.4
  • Repeat until the queue is empty, then return distances and predecessor information for reconstructing paths. section 4.4

A route from Los Angeles to Boston

The computed route passes through Riverside, Chicago, and Detroit: Los Angeles → Riverside → Chicago → Detroit → Boston. Its total weight is 2605, while the distance from Los Angeles to Los Angeles is 0.

How Classic Computer Science Problems in Swift: Essential techniques for practicing programmers covers it

How deep it goes
Explains the greedy procedure, priority queue, distance updates, predecessor tracking, and path reconstruction without formal proof.
Examples
Demonstrates city distances and reconstructs a Los Angeles–Boston route through Riverside, Chicago, and Detroit.
What it assumes
Assumes weighted graphs, vertices, edges, neighbor traversal, and priority queues.
What you can do after
Apply it via a weighted-graph implementation that returns minimum distances and predecessor edges for chosen routes.
How it connects
Connects weighted graphs, priority queues, greedy algorithms, breadth-first search, and path reconstruction.

Also explained in

Compare how 2 books explain single-source shortest path →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Classic Computer Science Problems in Swift: Essential techniques for practicing programmers. The section links point to the parts of the book it draws on.