FrechetDist.jl

The Fréchet distance between polygonal curves in Euclidean space, in Julia.

Source code: github.com/sarielhp/FrechetDist.jl

Gallery of examples: sarielhp.github.io/FrechetDist.jl/gallery: pairs of curves with their distances, free-space diagrams and animated morphings.

AI statement. The rewrite of the old version was done by Claude (Anthropic's Claude Opus 5.5, and Claude Sonnet 5.5 for the earlier part, in October 2026), who also carried out the optimization and research that made this version so much faster. This includes rewriting most of the code and the documentation (including this page).

FrechetDist computes the Fréchet distance between curves in low dimensions (the plane, 3D) and in high dimensions (thousands of coordinates). Approximating the distance is very fast, and frechet_c_compute_bisect gets it to any chosen precision. The exact distance, frechet_c_compute_exact, takes quadratic time and memory in the number of vertices; on high-dimensional curves it is 81 to 158 times faster than Fred on all threads (see High-dimensional curves: comparison with Fred).

Use these two instead of the older frechet_c_compute (a relative tolerance, 10⁻⁵ by default, with a matching that realizes the distance): on some inputs its performance is catastrophically bad. On example 35 of the gallery, two zig-zags of 15 and 16 vertices, it takes about 16 minutes and 10 GB of memory, against under a millisecond for frechet_c_compute_exact.

Function Documentation

FrechetDist.frechet_c_compute — Function
frechet_c_compute(P, Q, f_accept_approx = true, tolerance = 1e-5) -> Morphing

The older algorithm for the continuous Fréchet distance: the distance to a relative tolerance (1e-5 by default), with a monotone matching that realizes it.

Use the exact version instead

For the distance, use frechet_c_compute_exact (exact) or frechet_c_compute_bisect (to a given precision) instead. On some inputs the performance of this function is catastrophically bad: on example 35 of the gallery, two zig-zags of 15 and 16 vertices, it takes about 16 minutes and 10 GB of memory, against under a millisecond for frechet_c_compute_exact (its refinement keeps growing the curves); on example 36, of 6 and 12 vertices, it takes 15 seconds. Use it only when you need the matching.

The result also carries a certified lower bound, m.lower_bound, and m.ratio = m.leash / m.lower_bound (1 when the distance is exact).

Details

This works by first computing a very rough approximation, followed by distance-sensitive simplification of the curves. It then computes the monotone frver distance between the simplified curves, and combines it to get a distance between the two original curves. It makes sure the answers are the same, otherwise, it repeats with a finer simplification/approximation until they are equal.

Finally, the algorithm uses the frverwithoffsets distance between the two simplified curves to compute a lower bound, and makes sure this is equal to the Frechet distance computed. If they are equal, then the upper/lower bounds on the Frechet distance of the two curves are the same, which implies that the computed distance is indeed the desired Frechet distance.

More details

To ensure convergence, the monotone distance between the simplified curves is computed by refinement (frechet_mono_via_refinement), so that it approaches their VE-retractable distance.

Arguments

  • facceptapprox: return once the leash is within a factor 1 + tolerance of the certified lower bound. The test for agreement up to tolerance runs first and accepts slightly more, so this flag matters only when rounding puts the lower bound above the leash. It is kept for compatibility.

  • Tolerance: If |a-b| <= tolerance*( |a| + |b| ) then we consider numbers to be equal. In a perfect world tolerance would be zero. But floating point issues require us to pick some value.

source
FrechetDist.frechet_c_approx — Function
frechet_c_approx

Approximates the continuous Frechet distance between the two input curves. Returns a monotone morphing realizing it.

Arguments

  • approx : The output morphing has Frechet distance <= approx*optimal.

Importantly, approx can be larger than 2, if you want a rough approximation.

The quality of approximation is available at ret.ratio: ret.lower_bound (= ret.leash/ret.ratio) is a certified lower bound on the Frechet distance, while ret.leash is an upper bound.

source
FrechetDist.frechet_c_decide_exact — Function
frechet_c_decide_exact(P, Q, r; tree = nothing) -> Bool

Exact decision procedure for the continuous Fréchet distance: is $d_F(P, Q) \le r$? It is the free-space reachability test of Alt and Godau (1995), in O(n m d) time for curves of n and m vertices in ℝ^d, and O(n + m) memory. Unlike frechet_c_compute, no tolerance is involved: the answer is that of the exact algorithm, carried out in floating point.

Floating point can change the answer only when r is within rounding of the distance (a relative 1e-15 or so, more for nearly degenerate configurations). The free intervals are computed from the closest point on each segment, and whether the free space continues through a vertex is decided by comparing that vertex's squared distance with r^2 directly.

Most queries are settled sooner, by tests that are exact as well:

  • the endpoint distances, and an upper bound (the endpoint distance plus the widths of the curves);
  • greedy monotone walks by adaptive steps over pieces of the curves that lie in balls within r of each other, on one curve at a time and on both at proportional rates, and, in dimension 6 and up, over pieces close (in the Fréchet distance) to segments whose ends are within r (a "yes");
  • a vertex of one curve with no point of the other within r, tried from where the first walk stopped (a "no");
  • in the free-space search, a recursive decomposition of the diagram into boxes, each split in two: a box that nothing enters is skipped, a box whose two curve pieces are all within r of each other is settled through its boundary, and an exit side whose free part is one run, all of it reachable, is settled from the side alone.

The pieces of the curves are described by tree: LazyBoxTree (their bounding boxes, computed as the search needs them), BoxTree (the same, all computed first), or SausageTree (the segment between the ends of a piece, thickened to contain it: a description that needs only distances between points, so it does not grow with the dimension). The default, nothing, takes LazyBoxTree below dimension SAUSAGE_TREE_DIM (64) and SausageTree from it on (see default_tree).

The answer is that of the plain sweep, up to rounding at the distance itself. A box search that does more work than a constant times the cells (or recurses too deep) gives up to the plain sweep, so the worst case is O(n m d).

frechet_c_compute_exact uses it to find the exact distance among the critical values.

Takes any curves the other algorithms take (static or Vector points; matrix curves are converted). Curves of one vertex are allowed. r must be finite and non-negative. Also takes curves in a Hilbert space known only through squared distances (for example the feature space of a kernel): curves of virtual points of a HilbertSpace of BasicCompGeometry (vcurve(S, indices)), both of the same space; then the computations use only the cached squared distances of the space, the pieces are described by SausageTree (a bounding box needs coordinates), and the results agree with the same computation on coordinates up to rounding. See the manual page "Curves in Hilbert spaces".

Example

m = frechet_c_compute(P, Q)
frechet_c_decide_exact(P, Q, m.leash)                 # true
frechet_c_decide_exact(P, Q, m.leash * (1 - 1e-6))    # false, unless m.leash is not optimal
source
FrechetDist.frechet_c_compute_exact — Function
frechet_c_compute_exact(P, Q; rng = Random.default_rng(), method = :auto) -> Float64

The exact continuous Fréchet distance between the curves P and Q, by the randomized algorithm of Har-Peled and Raichel ("The Fréchet distance revisited and extended", Section 6): the distance is the smallest critical value of Alt and Godau (vertex-vertex, vertex-segment and monotonicity events) that the exact decision procedure (frechet_c_decide_exact) accepts. A random sample of 4 N² critical values (N the larger number of vertices), sorted and binary searched with the decider, leaves an interval that holds the distance and, with high probability, O(N log N) critical values. These are listed (the monotonicity values by a kinetic sweep along each segment, which keeps the points on the segment in a balanced search tree: O(log N) per event), sorted, and searched again.

When the sample would cost more than about 35 decisions on the curves themselves (N² ≥ 16 (n + m) d: long curves in low dimension), the interval comes instead from a bisection with the fast decision procedure (frechet_c_decide_exact on DecideCurves) to a relative width of 1e-10; the critical values in it are computed from the coordinates, segment by segment, and searched. With method = :auto (the default) this is done without tables: the fast decision procedure also certifies the ends of the interval, searches the critical values, and checks that nothing well below the result is accepted; when a check fails, the tables are computed and the search is done on them. With method = :tables, every decision of the search is made on the tables by the plain cell by cell sweep, independent of the filters and the box search of the fast decision procedure: the reference to check the default against. The two give the same result, bit for bit, on all inputs tested.

Slow: without tables, O((n + m) d) memory and, besides the decisions, O(n m d) time to compute the critical values near the distance (O(n m log N) when many points of a segment are close together). With the tables: O(n m d) to tabulate, once, the distances between vertices and segments (on all of Julia's threads; the result does not depend on their number); then O(N² log N) to sample and sort, O(log N) decisions of O(n m) each, and O(n m log N) to list (with high probability). The tables take 40 n m bytes and the sample (drawn only when the bisection does not settle it) 32 N² bytes. frechet_c_compute is usually much faster; this function is the reference it can be checked against, with no tolerance.

The result is a critical value, computed in floating point, so exact up to its rounding. Randomness affects only the running time: every rng gives the same result, bit for bit. A candidate counts as accepted when the decider accepts it plus 64 ulps (a relative 1.4e-14), which absorbs the different rounding of the decider and of the critical value; two critical values closer than that cannot be told apart.

Takes any curves the other algorithms take (static or Vector points; matrix curves are converted); curves of one vertex are allowed.

Also takes curves in a Hilbert space known only through squared distances (for example the feature space of a kernel): curves of virtual points of a HilbertSpace of BasicCompGeometry (vcurve(S, indices)), both of the same space. On them the result is the smallest Float64 the decision procedure accepts (the plain sweep with method = :tables), found by bisection: squared distances place points on a segment only roughly when it is much shorter than the distances to it, so its critical values are not listed. It agrees with coordinates up to rounding (measured: 5.5e-14 relative or less), or to about the length of the shortest segment. See the manual page "Curves in Hilbert spaces".

source
FrechetDist.frechet_c_compute_bisect — Function
frechet_c_compute_bisect(P, Q; rtol = 1e-10, atol = 0.0) -> Float64

The continuous Fréchet distance between the curves P and Q, to within max(atol, rtol * d) / 2 (d the distance): a bisection with the exact decision procedure (frechet_c_decide_exact) on prepared curves (DecideCurve), until the interval known to hold the distance is at most max(atol, rtol * lo) long; the result is its middle.

The interval starts at the distance of the end points, a lower bound, and at the largest distance between the bounding boxes of the curves, an upper bound. The first decision is at the lower bound plus the tolerance: the distance is often that of the end points (the curves then end at their farthest pair), and then one decision settles it. The curves are prepared without their widths (DecideCurve(...; width = false)): computing them costs a pass over the curves that the decisions of a bisection rarely need.

Each decision costs O(n + m) for its filters and, when they do not settle it, a search of the free space by boxes that is usually much faster than the O(n m) of the cell by cell sweep; about log2((hi - lo) / tolerance) decisions are made. With rtol down to about 1e-14 the result is the distance up to the rounding of the decision procedure; frechet_c_compute_exact gives the critical value itself.

atol = 1e-10, rtol = 0 is the stopping rule of FrechetLight::calcDistance (the C++ code of Bringmann et al.).

Takes the curves frechet_c_decide_exact takes, including curves of virtual points of a HilbertSpace (BasicCompGeometry); there the upper end of the first interval is the largest distance between the balls around the first vertices that hold the curves.

source
FrechetDist.frechet_dist_upper_bound — Function
frechet_dist_upper_bound

Returns a rough upper bound on the Frechet distance between the two curves. This upper bound is on the continuous distance. There is no guarantee on how bad the approximation is. This is used as a starting point for real approximation of the Frechet distance, and should not be used otherwise.

source
FrechetDist.frechet_width_approx — Function
frechet_width_approx

2-approximation to the Frechet distance between seg = P[first(rng)]-P[last(rng)] and the polygon P[rng] Here, rng is a range i:j

This function implements a greedy matching along the segment: you move to the closest point on seg to the current point, making sure never to move back.

The greedy matching is monotone, so the result is at least the distance δ. It is at most 2δ: an optimal matching puts each vertex within δ of seg, and the closest points of earlier vertices lie at most δ further along, so the greedy point is within 2δ of the vertex. On random curves the ratio reaches 1.99.

source

VE-Frechet distance and refinement

The VE-Frechet distance considers only vertex-edge events. Its retractable version is a lower bound on the Frechet distance, and monotonizing its morphing gives an upper bound; refining the curves along the morphing closes the gap.

FrechetDist.frechet_ve_r_compute — Function
frechet_ve_r_compute( P, Q )

The VE-retractable Frechet distance of P and Q, a lower bound on their Frechet distance. Returns a Morphing whose leash (m.leash) is that distance; the morphing may move backwards along the curves. Morphing_monotonize turns it into a monotone morphing, whose leash is an upper bound on the Frechet distance.

source
FrechetDist.frechet_ve_r_compute_ext — Function
frechet_ve_r_compute_ext( P, Q, p_offs, q_offs, f_use_offsets = false )

frechet_ve_r_compute with per-vertex offsets. With f_use_offsets, p_offs and q_offs hold one finite non-negative value per vertex of P and Q, subtracted from the event distances: the event of P[i] and Q[j] costs d - p_offs[i] - q_offs[j], and a vertex-edge event subtracts the larger offset of the edge's two ends. frechet_c_compute uses this to certify a lower bound. Without f_use_offsets the two vectors are ignored.

source
FrechetDist.frechet_ve_r_compute_range — Function
frechet_ve_r_compute_range

Return a range of distances. The lower end is the VE-Frechet
distance, the upper end is a constructive upper bound on the
Frechet distance.
source
FrechetDist.FEVER_compute — Function
FEVER_compute

Computes the VE-Frechet distance between P and Q. Unlike the other implementation, this one does not use hashing, instead allocating quadratic space to perform lookups. This seems wasteful, but works reasonably well for small curves (say of size at most 200).

source
FrechetDist.FEVER_compute_range — Function
FEVER_compute_range( P, Q, upper_bound )

frechet_ve_r_compute_range with the quadratic-space search of FEVER_compute. Returns (lo, hi): lo is the VE-Frechet distance of P and Q, and hi the leash of its monotonized morphing, an upper bound on the Frechet distance. upper_bound prunes the search; if the pruned search does not reach the end, the range is computed without it.

source
FrechetDist.frechet_mono_via_refinement — Function
frechet_mono_via_refinement( P, Q, approx )

Computes the monotone Frechet distance between P and Q using the
ve_r algorithm, refining the curves (adding vertices) until the
monotonized morphing's leash is within a factor `approx` of the
VE-retractable distance, a lower bound stored in `m.lower_bound`.
In the worst case it only approximates the Frechet distance.

Note that this function might be too slow if the two curves
are huge, since it does not simplify them before computing the
distance.

Returns

Returns a 4-tuple ( m, f, P, Q ): m: Morphing realizing result. f: true if the monotone leash equals the VE-retractable distance, so that the leash is the Frechet distance. P: Refined first input polygon Q: Refined second input polygon

source
FrechetDist.frechet_mono_via_refinement_ext! — Function
frechet_mono_via_refinement_ext!( out, P, Q, f_snapshots, approx = 1.00001 )

frechet_mono_via_refinement(P, Q, approx), which also records its rounds: with f_snapshots, the VE-retractable morphing of every refinement round is appended to out. Returns the same 4-tuple (m, f, P, Q).

source
FrechetDist.frechet_mono_via_refinement_delta — Function
frechet_mono_via_refinement_delta( Pa, Qa, delta, f_fix_prm = false )

A monotone morphing mm between Pa and Qa, refined until its leash is within delta of the VE-retractable distance (a lower bound on the Fréchet distance), which is stored in mm.lower_bound. Returns (mm, P, Q) with the refined curves P and Q. Refinement stops after 65 rounds even when the gap is larger (the curves grow with every round, so delta = 0 may never be met); check mm.leash - mm.lower_bound <= delta when it matters. With f_fix_prm, mm is mapped back to the original curves.

source
FrechetDist.frechet_refinement — Function
frechet_refinement( P, Q, m )

Refines P and Q along the morphing m: each curve gets the points where m's events lie on its edges, and points between them (extract_refined_polygon). Returns the two refined curves.

source