## Walking dogs and comparing sticks

Then he dropped two in at once, and leant over the bridge to see which of them would come out first; and one of them did; but as they were both the same size, he didn’t know if it was the one which he wanted to win, or the other one. — A. A. Milne

It’s useful to have a way of measuring how different two sticks (or fir cones) are in size, shape, and their position in a river. Yes, we have the Hausdorff distance ${d_H}$ between sets, but it does not take into account the orientation of sticks. And it performs poorly when the sticks are broken: the Hausdorff distance between these blue and red curves does not capture the disparity of their shapes:

Indeed, ${d_H}$ is relatively small here, because from any point of red curve one can easily jump to some point of the blue curve, and the other way around. However, this kind of measurement completely ignores the fact that curves are meant to be traveled along in a continuous, monotone way.

There is a concept of distance that is better suited for comparing curves: the Fréchet distance ${d_F}$. Wikipedia gives this (folklore) description:

Imagine a dog walking along one curve and the dog’s owner walking along the other curve, connected by a leash. Both walk continuously along their respective curve from the prescribed start point to the prescribed end point of the curve. Both may vary their speed, and even stop, at arbitrary positions and for arbitrarily long. However, neither can backtrack. The Fréchet distance between the two curves is the length of the shortest leash that is sufficient for traversing both curves in this manner.

To get started, let’s compute this distance for two oriented line segments ${AB}$ and ${CD}$. The length of the leash must be at least ${|AC|}$ in order to begin the walk, and at least ${|BD|}$ to finish. So,

$\displaystyle d_F(AB,CD) \ge \max(|AC|, |BD|)$

In fact, equality holds here. In order to bound ${d_F}$ from above, we just need one parametrization of the segments. Take the parametrization proportional to length:

$\displaystyle P=(1-t)A+tB,\quad Q=(1-t)C+tD$

Then ${|PQ|^2}$ is the quadratic polynomial of ${t}$. Without doing any computations, we can say the coefficient of ${t^2}$ is nonnegative, because ${|PQ|^2}$ cannot be negative for any ${t\in\mathbb R}$. Hence, this polynomial is a convex function of ${t}$, which implies that its maximum on the interval ${[0,1]}$ is attained at an endpoints. And the endpoints we already considered. (By the way, this proof works in every CAT(0) metric space.)

In general, the Fréchet distance is not realized by constant-speed parametrization. Consider these two curves, each with a long detour:

It would be impractical for the dog and the owner to go on the detour at the same time. One should go first while the other waits for his/her/its turn. In particular, we see symmetry breaking here: even for two perfectly symmetric curves, the Fréchet-optimal parametrizations would not be symmetric to each other.

It is not obvious from the definition of ${d_F}$ whether it is a metric; as usual, it’s the triangle inequality that is suspect. However, ${d_F}$ indeed satisfies the triangle inequality. To prove this, we should probably formalize the definition of ${d_F}$. Given two continuous maps ${f,g}$ from ${[0,1]}$ into ${\mathbb R^2}$ (or any metric space), define

$\displaystyle d_F(f,g) = \inf_{\phi,\psi}\max_{[0,1]} |f\circ \phi-g\circ \psi|$

where ${\phi}$ and ${\psi}$ range over all nondecreasing functions from ${[0,1]}$ onto itself. Actually, we can require ${\phi}$ and ${\psi}$ to be strictly increasing (it only takes a small perturbation), which in dog/owner terms means they are not allowed to stop, but can mosey along as slowly as they want. Then we don’t need both ${\phi}$ and ${\psi}$, since

$\displaystyle \max_{[0,1]} |f\circ \phi-g\circ \psi| = \max_{[0,1]} |f -g\circ (\psi\circ \phi^{-1})|$

So, given ${f,g,h}$ we can pick ${\phi}$ such that ${\max_{[0,1]} |f-g\circ \phi|}$ is within ${\epsilon}$ of ${d_F(f,g)}$; then pick ${\psi}$ such that ${\max_{[0,1]} | g\circ \phi - h\circ \psi | }$ is within ${\epsilon}$ of ${d_F(g,h)}$. Then

$\displaystyle d_F(f,h)\le \max_{[0,1]} |f- g\circ \phi|+\max_{[0,1]} |g\circ \phi - h\circ \psi| \le d_F(f,g)+d_F(g,h)+2\epsilon$

and the triangle inequality follows.