Distance Metrics: Manhattan, Euclidean & Chebyshev
Geometric proximity depends on your distance metric. Euclidean ($L_2$) measures straight-line flight, Manhattan ($L_1$) measures strict grid navigation, and Chebyshev ($L_\infty$) measures the maximum coordinate difference 📏.
import math
def distances(p1, p2):
l2 = math.sqrt(sum((a - b) ** 2 for a, b in zip(p1, p2)))
l1 = sum(abs(a - b) for a, b in zip(p1, p2))
linf = max(abs(a - b) for a, b in zip(p1, p2))
return l2, l1, linf
a = (1, 2)
b = (4, 6)
l2, l1, linf = distances(a, b)
print(f"L2 (Euclidean): {l2:.1f} 📏")
print(f"L1 (Manhattan): {l1} 🏙️")
print(f"Linf (Chebyshev): {linf} ♟️")
# 59. Distance Metrics: Manhattan, Euclidean & Chebyshev Geometric proximity depends on your distance metric. Euclidean ($L_2$) measures straight-line flight, Manhattan ($L_1$) measures strict grid navigation, and Chebyshev ($L_\infty$) measures the maximum coordinate difference 📏. ```python import math def distances(p1, p2): l2 = math.sqrt(sum((a - b) ** 2 for a, b in zip(p1, p2))) l1 = sum(abs(a - b) for a, b in zip(p1, p2)) linf = max(abs(a - b) for a, b in zip(p1, p2)) return l2, l1, linf a = (1, 2) b = (4, 6) l2, l1, linf = distances(a, b) print(f"L2 (Euclidean): {l2:.1f} 📏") print(f"L1 (Manhattan): {l1} 🏙️") print(f"Linf (Chebyshev): {linf} ♟️") ``` In high-dimensional spaces (the curse of dimensionality), Manhattan distance ($L_1$) often outperforms Euclidean ($L_2$) because all pairwise $L_2$ distances converge to nearly identical numbers 🧠! --- ### Question Why is Chebyshev distance ($L_\infty$) known colloquially as 'Chessboard distance'? 🧠 - [ ] a. It mirrors the exact number of moves an unhindered King needs to travel between any two squares ♟️ - [ ] b. It can only compute distances between cells on an 8-by-8 integer grid - [ ] c. It strictly forbids any diagonal movements across coordinate axes - [ ] d. It was invented by Garry Kasparov during the 1997 Deep Blue match --- ### Challenge Given `p1 = (1, 2)` and `p2 = (4, 6)`, calculate Euclidean, Manhattan, and Chebyshev distances. Print them as a tuple `(round(l2, 1), l1, linf)`. 🎯 <details> <summary>Solution</summary> ```python import math p1 = (1, 2) p2 = (4, 6) l2 = math.sqrt(sum((a - b) ** 2 for a, b in zip(p1, p2))) l1 = sum(abs(a - b) for a, b in zip(p1, p2)) linf = max(abs(a - b) for a, b in zip(p1, p2)) print((round(l2, 1), l1, linf)) ``` </details>