AI Glossary / Mathematics and optimization / Intermediate
Triangle inequality
Definition
The triangle inequality says that the distance directly between two points is no greater than the sum of their distances through a third point. For a metric d, it is written d(x,z) ≤ d(x,y)+d(y,z). A related norm form is ||u+v|| ≤ ||u||+||v||. These rules formalize a basic consistency property of distance and length.
What it means
In ordinary geometry, one side of a triangle cannot be longer than the other two sides combined. The same idea extends to objects represented by vectors, strings, or other structures when the chosen distance is a metric.
The inequality is not a claim that every number called a distance has this property. A custom dissimilarity score may violate it. Even a familiar transformation of a valid distance can break it: squared Euclidean distance is an important example.
This distinction matters because some algorithms use distance relationships to rule out candidates without calculating every comparison. If their assumptions fail, they can discard an answer that should have been retained.
How it works
For a metric, apply the inequality to any three objects. Rearranging two versions gives the reverse triangle bound |d(x,p)−d(y,p)| ≤ d(x,y). Combining both directions yields a useful interval: the distance between x and y lies between the absolute difference and the sum of their distances to a reference point p.
For vector norms, the rule bounds the length of a sum by the sum of component lengths. If two perturbations have sizes at most a and b in the same norm, their combined size is at most a+b.
These are bounds, not exact predictions. Cancellation, alignment, and the geometry of the selected distance determine how close the actual value comes to a bound.
Three practical examples
- A nearest-neighbor system can use valid metric bounds to skip a candidate that cannot fall within the current search radius.
- An engineering calculation can bound combined numerical or measurement perturbations when each is measured using the same norm.
- An analyst designing a custom dissimilarity can test simple triples for violations before choosing an indexing method whose correctness depends on metric assumptions.
Think of it this way
Think of stretching a cord from one peg to another. Routing the cord via a third peg cannot require less length than the straight connection in ordinary Euclidean space. The analogy explains geometric distance, but a business score such as dissatisfaction or a learned similarity is not automatically a cord length. Its mathematical properties need separate justification.
Worked example
Take points A = (0,0), B = (3,0), and C = (3,4). Their Euclidean distances are d(A,B)=3, d(B,C)=4, and d(A,C)=5. The inequality reads 5 ≤ 3+4 = 7.
Now consider a search query q, candidate x, and reference point p in any metric space. Suppose d(q,p)=5 and d(x,p)=12. The reverse triangle bound gives d(q,x) ≥ |12−5| = 7. If the search only needs candidates within distance 6 of q, x can be excluded using this bound. The upper bound is 17, so these two known distances locate the unknown distance within [7,17], not at one exact value.
Finally, examine squared Euclidean distance on the real line at 0, 1, and 2. The squared distance from 0 to 2 is 4, while the two squared distances through 1 sum to 1+1=2. The proposed inequality 4 ≤ 2 fails.
Squaring distances preserves their ranking from a fixed query, but it does not preserve the triangle inequality. Ranking equivalence therefore does not justify reusing every metric-based pruning formula unchanged.
Illustrative case study
In a fictional catalog-search project, a team replaces Euclidean distance with its square to avoid repeated square-root calculations. A direct exhaustive ranking still agrees, but a custom index begins missing candidates.
The index had applied triangle-based bounds directly to the squared quantities. The team reconstructs a small counterexample and revises the implementation so that its bounds use the correct distance relationships.
The lesson is that a computational shortcut can preserve ordering while changing the algebra required for search correctness. This scenario does not claim that all indexes require metrics or that squared distances are inherently unsuitable for retrieval.
Common misconceptions and mistakes
Do not reverse the inequality: the two-leg sum is an upper bound on the direct metric distance. Equality is allowed, such as points arranged along a straight line in the appropriate order.
Do not assume a triangle inequality alone proves that a function is a metric. Nonnegativity, symmetry, and the requirement that distinct points have positive distance must also be considered.
Do not apply a norm bound across incompatible units or different norms without a justified conversion. A one-unit bound under one scaling need not be one unit under another.
Finally, a few numerical tests can expose a violation, but passing a few tests does not establish the property for all possible inputs.
Business use
The business relevance is dependable use of distance-based methods. When a supplier proposes an accelerated similarity search, ask which distance is used and which properties its pruning or guarantees require.
For a custom score, validate the mathematical assumptions before translating a speed improvement into a correctness claim. Also separate exact search from approximate search: an approximate method may intentionally trade recall for speed, which is a different issue from an invalid bound silently removing valid results.
People also ask
Does the inequality require an ordinary geometric triangle?
No. It can apply in any metric space.
Can equality occur?
Yes. For real points 0, 2, and 5, the distance 5 equals 2+3.
Does taking the square of a metric always preserve it?
No. The worked example shows a triangle-inequality failure for squared Euclidean distance.
Check your understanding
Distances from a reference point to a query and candidate are 4 and 10. What metric lower bound can you give on the query-candidate distance, and can the candidate be excluded from a radius-5 search?
Answer: The lower bound is |10−4| = 6. Since 6 exceeds 5, it can be excluded under the stated metric assumptions.
Related terms
- Metric
- Norm
- Euclidean distance
- Manhattan distance
- Nearest neighbor
- Similarity Search
- reverse triangle inequality
- dissimilarity
Learn next
Draw several triples of points and calculate both upper and lower distance bounds. Then repeat with squared distances to find a failure. Study metric-space definitions before using triangle-based pruning in an implementation or interpreting a search algorithm's guarantee.
More in Mathematics and optimization
- Absolute error loss - Absolute error loss measures a numerical prediction's error as the absolute difference between its predicted and reference values: L(y,y-hat)=|y-y-hat|.
- Active-set method - An active-set method is an optimization approach that works with a changing subset of constraints believed to be tight at the solution.
- Adjoint state method - The adjoint state method computes how a chosen objective changes when the parameters of a mathematical model change.
- Σ-algebra
- Algebraic connectivity - Algebraic connectivity is the second-smallest eigenvalue of a graph's Laplacian matrix.
- Algorithm IMED - Algorithm IMED, short for Indexed Minimum Empirical Divergence, is a method for choosing actions in a stochastic multi-armed bandit problem.
Sources
MIT OpenCourseWare, Introduction to Functional Analysis, Spring 2021, Casey Rodriguez lectures with notes by Andrew Lin: norm and metric definitions and the induced-metric argument. https://ocw.mit.edu/courses/18-102-introduction-to-functional-analysis-spring-2021/8fb8d5c170f1613151aca71de21027bc_MIT18_102s21_full_lec.pdf
Previous: Trend-stationary process Next: Triangular network coding