liveBook · Manning

Distance

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

A measure of separation between locations or points in a feature space.

In a weighted graph, distance is the accumulated edge weight from a starting vertex. Dijkstra’s algorithm tracks the shortest distance found so far to each vertex, updating it when a cheaper route appears. A nil value means the distance is not yet known. section 4.4

Dijkstra’s algorithm uses distances to guide its search:

  • Start the root at the supplied start distance, typically zero, and place it in a priority queue. section 4.4
  • Explore the closest queued vertex; for each neighbor, record the new distance and connecting edge if no route is known or the new route is shorter. section 4.4
  • Continue until the queue is empty, then return distances and the edges used to reach vertices. section 4.4

Routes from Los Angeles

In a weighted city graph, the distance to Boston is 2605 via Riverside, Chicago, and Detroit. The distance records total route weight, while the stored edges reconstruct the path.

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

How deep it goes
Explains initialization, priority-queue exploration, and shorter-route updates through a Swift implementation.
Examples
Uses a city graph to calculate distances from Los Angeles and trace a route to Boston.
What it assumes
Assumes familiarity with graph edges and priority queues while tracking unknown distances explicitly.
What you can do after
Apply Dijkstra’s algorithm to compute shortest distances and recover paths in a weighted graph.
How it connects
Connects shortest paths with Dijkstra’s algorithm, weighted graphs, and priority queues.

Also explained in

Compare how 10 books explain distance →

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.