liveBook · Manning

Distance

As explained in Advanced Algorithms and Data Structures

A distance function is a function that assigns a path-cost contribution or distance value to an edge or other argument.

Choosing the closest warehouse for an order is easy when there are five candidates: compute each distance and take the smallest. With thousands of stores and many orders, repeatedly checking every candidate becomes costly. A distance function supplies the value used to compare candidates; the search method determines how many such comparisons are needed. section 8.2

First identify what the function measures. For points, it compares a target with stored points. A nearest-neighbor query returns the closest point or N closest points; a distance threshold can instead define a spherical search region. Coordinates need not describe physical space: price and rating can form a two-dimensional point. The nearest-neighbor structures described here require Euclidean distance for these dimensions. section 8.3

A distance function is also a structural choice in an SS-tree. To decide whether a point may lie in a node’s bounding sphere, compare its distance from the sphere’s centroid with the radius. This distance can be Euclidean or another valid metric: it must be non-negative, zero only for a point compared with itself, symmetric, and satisfy the triangle inequality. section 10.3.1

Listing 10.4 Method SsNode::intersectsPoint
function SsNode:: intersectsPoint(point)                 #1
  return distance(this.centroid, point) <= this.radius   #2

The sphere-intersection test reduces to one distance calculation and a comparison with the node’s radius. section 10.3.1

Distance thresholds in canopy clustering

Select a point p as a canopy center and compute distance(p, q) for each point q. If the value is below T1, add q to the canopy; if it is also below the smaller threshold T2, remove q as a possible future center. A faster approximate distance metric can reduce work, at the cost of a less precise preliminary grouping that a later clustering algorithm can refine. section 13.1.3

For graph paths, the argument changes from a pair of points to an edge. A* accepts distance(e) as the contribution to the cost of reaching the next vertex; its heuristic separately estimates the remaining cost to the goal. Choosing distance(e) = e.weight and a zero heuristic gives Dijkstra’s behavior. Choosing distance(e) = 1 and a zero heuristic counts edges, as breadth-first search does. Here, “distance” means a path-cost contribution, not necessarily a geometric metric. section 14.5

How Advanced Algorithms and Data Structures covers it

How deep it goes
Works through threshold tests and A* parameter choices, while distinguishing Euclidean requirements from an SS-tree’s more general metric requirements.
Examples
Uses warehouse selection, price-and-rating points, canopy thresholds, SS-tree bounding spheres, and weighted versus unit-cost graph edges.
What it assumes
Assumes familiarity with points and graph edges, while defining Euclidean distance and stating the conditions for a valid metric.
What you can do after
Apply it via nearest-neighbor queries, clustering thresholds, SS-tree sphere tests, or an A* edge-cost argument.
How it connects
Links nearest-neighbor search, SS-trees, canopy clustering, A*, Dijkstra’s algorithm, breadth-first search, and heuristics.

Also explained in

Compare how 10 books explain distance →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Advanced Algorithms and Data Structures. The section links point to the parts of the book it draws on.