Why must a distance function satisfy the triangle inequality to be used with metric tree indexes?

Quick Overview

Asks why metric tree indexes such as ball trees and VP-trees need a distance function that satisfies the triangle inequality. Tests how the pruning bound is derived, what fails silently with non-metric distances such as cosine or squared Euclidean, and how to work around them.

Why must a distance function satisfy the triangle inequality to be used with metric tree indexes?

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: hard

Interview Round: Other

Ball trees, VP-trees and cover trees speed up nearest-neighbor search by skipping whole regions of the data. Why must the distance function satisfy the triangle inequality for these metric tree indexes to be used? ```hint Where the speed comes from Ask how a tree can skip a whole region without computing the distance from the query to any point inside it. ``` ```hint Derive the bound Write a lower bound on the distance from the query to any point inside a ball, using only the distance to the ball's center and its radius. ``` ```hint Try a counterexample Test that bound with squared Euclidean distance on a few points on a line. ``` ### What a Strong Answer Covers - How metric trees prune regions, and the lower bound that the triangle inequality gives for a ball and for a vantage point. - What goes wrong when the bound fails, and whether it is visible. - How to handle common non-metric distances. - Which other properties of a metric the index relies on, and how KD-trees differ. ### Follow-up Questions - Squared Euclidean distance is not a metric, yet k-nearest neighbors with it returns the same neighbors as Euclidean distance. Why, and how would you use that? - How would you find nearest neighbors under cosine similarity for millions of vectors? - Approximate indexes such as HNSW are often used with inner product, which is not a metric. Why is that acceptable there? - A custom distance violates the triangle inequality only rarely. How would you check whether a metric tree still returns the right neighbors?

Overview: Asks why metric tree indexes such as ball trees and VP-trees need a distance function that satisfies the triangle inequality. Tests how the pruning bound is derived, what fails silently with non-metric distances such as cosine or squared Euclidean, and how to work around them.

Nov 24, 2026
hardMachine Learning EngineerOtherMachine Learning
0
0

Ball trees, VP-trees and cover trees speed up nearest-neighbor search by skipping whole regions of the data. Why must the distance function satisfy the triangle inequality for these metric tree indexes to be used?

What a Strong Answer Covers Guidance

  • How metric trees prune regions, and the lower bound that the triangle inequality gives for a ball and for a vantage point.
  • What goes wrong when the bound fails, and whether it is visible.
  • How to handle common non-metric distances.
  • Which other properties of a metric the index relies on, and how KD-trees differ.

Follow-up Questions Guidance

  • Squared Euclidean distance is not a metric, yet k-nearest neighbors with it returns the same neighbors as Euclidean distance. Why, and how would you use that?
  • How would you find nearest neighbors under cosine similarity for millions of vectors?
  • Approximate indexes such as HNSW are often used with inner product, which is not a metric. Why is that acceptable there?
  • A custom distance violates the triangle inequality only rarely. How would you check whether a metric tree still returns the right neighbors?