Module 3: Similarity and Distance
3. Manhattan Distance and Other Metrics
Overview
Besides Euclidean, there are other distance metrics. The best known is Manhattan (a path along a grid).
Manhattan distance
Formula:
manhattan_distance(A, B) = |b₁ - a₁| + |b₂ - a₂| + ... + |bₙ - aₙ|
= ∑|bᵢ - aᵢ|
2D example:
A = [2, 3]
B = [5, 7]
Manhattan = |5-2| + |7-3| = 3 + 4 = 7
Interpretation: The distance you walk along a grid (city streets).
Visual comparison (2D)
↑ B(5,7)
| ╱|
7 | ╱ |
|╱ | ← Manhattan (the grid path)
3 A───→
2 5
Euclidean (diagonal): 5
Manhattan (grid): 7
Other metrics
Minkowski distance (the generalization):
distance = (∑|bᵢ - aᵢ|ᵖ)^(1/p)
p=1: Manhattan
p=2: Euclidean
p=∞: Chebyshev (the largest difference on any single axis)
When to use Manhattan
Use cases:
- Movement on a grid (robots in a warehouse)
- Categorical data (differences across attributes)
- When outliers are a problem (Manhattan is more robust)
In AI: Rare for embeddings (Euclidean or cosine are more common).
Next capsule: 04-cosine-similarity.md — The key metric for AI.