Deciding, and the distance by bisection

frechet_c_decide_exact(P, Q, r) answers whether the continuous Fréchet distance of two curves is at most r, exactly (up to the rounding of the free intervals), and frechet_c_compute_bisect(P, Q) finds the distance to a given precision by bisection with it. On the benchmarks below, the decision procedure is 1.2 to 2.5 times as fast as the C++ code of Bringmann, Künnemann and Nusser, built with the same flags, on 8 of 13 sets of curves, about as fast on four, and slower on one (Berlin, whose tiny curves take tens of nanoseconds per query); the bisection is about as fast as their distance computation (faster on three sets, slower on one of tiny curves), and significantly faster than CGAL's at the same precision.

using FrechetDist

frechet_c_decide_exact(P, Q, 0.8)              # is d_F(P, Q) <= 0.8?
frechet_c_compute_bisect(P, Q)                 # d_F(P, Q) to within 1e-10 relative
frechet_c_compute_bisect(P, Q; atol = 1e-10, rtol = 0)  # the stopping rule of BKN

# Many queries on the same curves: prepare them once.
A, B = FrechetDist.DecideCurve(P), FrechetDist.DecideCurve(Q)
frechet_c_decide_exact(A, B, 0.8)

The decision procedure

The answer is that of the free-space reachability test of Alt and Godau (1995): the free space of the two curves at radius r is the set of pairs of points, one on each curve, within distance r; the distance is at most r exactly when a monotone path in it joins the starting pair to the ending pair. The plain test sweeps all n m cells of the diagram, one free interval per cell edge: O(n m d) time. frechet_c_decide_exact gives the same answers, but settles most queries much sooner, by tests that are exact as well, in this order:

  1. Endpoints and bounds (O(1) on prepared curves). The endpoints must be within r. If every point of one curve is within r of every point of the other (by their bounding boxes), the answer is yes; so it is if the endpoint distance plus the widths of the two curves (the Fréchet distance of each to the segment joining its ends) is at most r.
  2. Greedy walks (prove "yes"). Walks through pairs of pieces of the two curves that lie in balls within r of each other (a ball around a piece is found from its arc length): a monotone walk that stays within r all the way shows the answer is yes. The first walk advances one curve at a time, the second both at proportional rates, with step lengths that grow by half after a success and halve after a failure (the walks adaptiveGreedy and adaptiveSimultaneousGreedy of Bringmann, Künnemann and Nusser). From dimension 6 on, a third walk steps over pieces close (in the Fréchet distance) to segments whose ends are within r: two long, nearly straight runs are matched in one step.
  3. A far vertex (proves "no"). Between the two walks, a vertex of one curve with no point of the other within r: the vertices are tried from where the first walk stopped, at offsets growing by a factor 1.5, and the other curve is scanned by pieces, skipping those whose ball is too far (as in BKN's filters).
  4. The free space, by boxes. The diagram is split recursively into boxes, each in two: where the scan of a side found its free run broken, or else across the longer side (or parallel to the side whose exits alone are needed). A box that nothing enters is skipped. A box whose two pieces of curve lie within r of each other is free as a whole, and is settled through its boundary. Otherwise each exit of the box that is needed (its top edges, its right edges, or, at the end of the diagram, either) is decided from its side alone when it can be: when the free part of the side is one run, all of it reachable from where the box is entered, which is checked by the segment across the box from the start of the run (the pruning rules of Bringmann, Künnemann and Nusser). The runs and the segments across are found by skipping along the curves by arc length (a run of segments that is certainly within, or certainly beyond, r of a point is skipped in one step), and what a box learns of its sides is passed to its parts. Boxes of at most 64 cells are swept cell by cell.

All of it takes O(n + m) memory. The box search alone can be slower than the plain sweep: on a zigzag of n vertices against a curve of 12, each split peels off two columns and the search takes time quadratic in n, with recursion n / 2 deep. So a search gives up once its work (the sides of the boxes it visits) exceeds 16 times the cells, or its recursion 1,000 splits, and the plain sweep decides: the worst case is O(n m d). On the benchmarks of this page no search gives up. The constant is not small: on the zigzag the decision takes about 7.5 times as long as the plain sweep in the plane, and 20 to 80 times as long in ℝ^64 (n from 4001 to 32001; the ratio grows with n although the work counted stays within the budget, likely from memory access, which was not isolated).

Pieces of curves: boxes and sausages

The box search describes pieces of the curves (runs of segments, in a binary tree of 8-segment blocks) by a container:

TreeA piece is described byDefault
LazyBoxTreeits bounding box, computed when first needed (the top of the tree by one scan of the curve, a subtree by one scan of its piece)below dimension 64
BoxTreeits bounding box, all computed when the curve is prepared (the curve can then be shared by threads)never
SausageTreea sausage: the segment joining the ends of the piece, thickened by the largest distance from a vertex of the piece to itfrom dimension 64 on

A bounding box grows with the dimension: in ℝ^1200, the box of a few video frames contains points far from all of them, and the tests that use it rarely succeed. A sausage does not: its tests need only distances between points. Two pieces lie within r of each other when the farthest ends of their segments are within r minus the two thicknesses (the distance between points of two segments is convex, so it is largest at their ends); a point is away from a piece by its distance to the segment minus the thickness. A sausage node also stores an upper bound on the Fréchet distance between its piece and its segment, which the third greedy walk uses. Nodes are filled when first read, a bottom node by a scan of its piece, a node above from its children in O(d).

The tree can be chosen: frechet_c_decide_exact(P, Q, r; tree = SausageTree), or DecideCurve(P, SausageTree). Since a sausage needs only distances, it is also the tree of curves in a Hilbert space known only through distances (see Curves in Hilbert spaces).

Prepared curves

DecideCurve(P) checks a curve once and stores its bounding box, its width and its prefix lengths (for the balls of the walks and scans), so that the O(1) tests of the first step do not touch its vertices; the tree is built when a decision first needs it, and kept. (Below dimension 64 the decider now reads none of the boxes of the tree: the scans of the box search step by the balls of the pieces, from the prefix lengths.) DecideCurve(P, Tree; width = false) skips the width (an O(n) pass); the bisection does that.

The distance by bisection

frechet_c_compute_bisect(P, Q; rtol = 1e-10, atol = 0) keeps an interval known to hold the distance and halves it with the decision procedure on prepared curves, until it is at most max(atol, rtol * lo) long; it returns the middle. The interval starts at the endpoint distance (a lower bound) and the largest distance between the bounding boxes (an upper bound). The first decision is at the lower bound plus the tolerance: the distance is often that of the endpoints (the curves end at their farthest pair), and then that one decision settles it. While the ratio of the ends is large, the interval is split at their geometric mean, then at the middle. Most decisions are settled by the filters; only the last few, close to the distance, search the free space.

With rtol down to about 1e-14 the result is the distance up to the rounding of the decision procedure; the exact distance is the critical value itself.

Results

One thread. Every program reads the curves first and runs once untimed.

The decider against the C++ code of Bringmann, Künnemann and Nusser

The decider of Bringmann, Künnemann and Nusser (BKN; Walking the Dog Fast in Practice, SoCG 2019; FrechetLight::lessThanWithFilters, commit 93f8d44) is the fastest published one: Conradi, van der Hoog and Rotenberg (SoCG 2026) find it faster than every other code they test. It is run against frechet_c_decide_exact twice: the Julia package, and the same decider ported to C++ line by line (the same tests in the same order, the same floating-point operations), built into one program with BKN's code and with the same flags (-O3 -march=native -fno-math-errno -flto). The port separates the algorithm from the language.

The queries, each a pair of curves and a distance r:

  • BKN's benchmark. Their files <set>_query_decider_<k>_<l>_<plus|minus>.txt of 1000 queries: a curve and one of its 2^k-th to (2^(k+1) − 1)-th nearest by Fréchet distance, at random (k = 1 the nearest, the hardest), and the distance d of the pair times 1 + 2^l (plus) or 1 − 2^l (minus), l from −10 to 2. On sigspatial (GIS Cup 2017), characters (handwriting) and geolife (GPS); the sample is every tenth of 40 random files per dataset (cpp_compare/sample40.txt), 4,000 queries each.
  • Porto and Mopsi. Porto taxi trips (16,384 of the ECML/PKDD 2015 challenge) and Mopsi routes (6,730, 2014). The pairs are drawn by BKN's own generator, unchanged; the query distance is d(1 + δ) or d(1 − δ) by a fair coin, δ = 2^-10, 2^-14, 2^-18 and 2^-22, so the answer of every query is known: 56,000 and 47,992 queries.
  • Conradi et al. The pairs of the eight sets of their benchmark (200 per set, 110 of OV), with queries made the same way: 6,040.

Per query file, each query has its own copy of its curves, so that no decision reuses the work of another; one untimed pass, then 30 timed passes, the minimum, summed over the files; 10 rounds that alternate which code runs first, and the median (the Julia runs: 2 or 3 rounds, Conradi et al. 3). The work per curve done when it is read is not timed, for both: the bounding box and the prefix lengths (BKN), and also the tree of the boxes of its pieces (FrechetDist). Three machines: AMD Ryzen 7 5700G (g++ 16.2), Qualcomm Snapdragon X Elite (ARM64, Linux under WSL2, g++ 16.2) and Intel Core Ultra 7 265 (g++ 13.3, on a performance core); Julia 1.13.4. Every answer is right: those of the port are the same as BKN's on all 897,000 queries of their benchmark, those of the Julia package on the 120,000 of 40 files per dataset (reproduce.rb decider), and both give the known answer on the others.

The speedup over BKN per set and machine

The speedup is BKN's time over the other's (the figure leaves out Berlin and UniD, whose 800 queries take under 0.2 ms in all):

DataSetRyzen: portJuliaSnapdragon: portJuliaCore Ultra: portJulia
BKNsigspatial1.961.712.051.831.871.73
BKNcharacters1.181.011.321.091.201.05
BKNgeolife1.741.581.881.731.651.49
taxiPorto1.771.561.891.611.771.52
routesMopsi1.411.271.471.371.421.19
Conradi et al.drifter2.672.382.612.502.382.10
Conradi et al.pigeons1.911.701.851.811.691.47
Conradi et al.chicago1.671.401.751.451.561.40
Conradi et al.UniD1.271.051.400.921.201.23
Conradi et al.Berlin1.360.851.420.771.260.96
Conradi et al.geolife1.201.061.141.101.090.94
Conradi et al.athens1.131.021.231.091.100.99
Conradi et al.OV1.961.612.511.822.171.70
  • The port is faster than BKN on every set and machine, by 1.1 to 2.7 times. The Julia package is 1.2 to 2.5 times as fast on eight sets, and about as fast on four (characters, and geolife, athens and UniD of Conradi et al.; UniD 0.92 to 1.23). It is slower on Berlin, whose curves of 6 to 12 vertices are decided in tens of nanoseconds, where the cost of a Julia call (about 30 ns) is most of it: it takes 1.04 to 1.3 times BKN's time. The timings of UniD and Berlin (800 queries in under 0.2 ms) are the noisiest.
  • The sample matters: on all 40 files of BKN's sample (Ryzen, one round) the port is 1.63, 1.37 and 1.75 times as fast, and the Julia package 1.43, 1.20 and 1.57.
  • In BKN's own protocol (deciderComparisonExp: one FrechetLight for all the queries, the mean of five runs per file) the port is 1.75, 1.82 and 1.73 times as fast on Porto (Ryzen, Snapdragon, Core Ultra) and 1.38, 1.41 and 1.38 on Mopsi.
  • The speedup changes little with how close the query distance is to the distance (within about 7% from δ = 2^-10 to 2^-22):

The speedup of the port by δ

  • At -O2 (the optimization level of their CMake build) the port is faster on every set and machine but athens on x86-64 (0.97 and 0.99, about even), by 1.0 to 2.6 times; the speedups are lower than at -O3 on x86-64 (OV most: 1.43 and 1.51 against 1.96 and 2.17), whose baseline, which -O2 targets, has no FMA instruction, so the fused products of the port become library calls. BKN's default build (-O2 -g, with sqrt setting errno) is about 5 times slower than with these flags.
  • The C++ port, the drivers, the scripts that make the Porto and Mopsi data and the queries, and the raw results of the three machines are in examples/benchmarks/cpp_compare/fd_cpp/ of the repository (its README tells how to rerun them); the port is not part of the package. ruby examples/benchmarks/reproduce.rb decider runs the older harness of examples/benchmarks/cpp_compare on 40 files per dataset, which times the building of the trees inside the decisions: there the Julia package takes 0.97, 0.90 and 0.71 times BKN's time.

The distance: against BKN and CGAL

The benchmark of Conradi, van der Hoog and Rotenberg, On computing the (exact) Fréchet distance with a frog (SoCG 2026): eight sets of real planar curves; the first 100 pairs of each, 800 in all (ruby examples/benchmarks/reproduce.rb distance reruns it). The C++ codes are built with -O3 -march=native -fno-math-errno (CGAL also with -ffp-contract=off). Total seconds, one thread, AMD Ryzen 7 5700G, the minimum of three timed runs:

Setbisection, atol 1e-10BKN, atol 1e-10CGAL, 1e-10bisection, rtol 1e-14BKN, rtol 1e-14
geolife0.030.031.090.040.04
pigeons0.030.071.860.050.12
athens0.060.062.320.080.08
ov0.911.2727.361.081.61
chicago0.000.010.160.010.01
drifter0.010.021.040.020.04
unid0.000.000.120.000.00
berlin0.000.000.010.000.00
total1.061.4533.951.281.90
  • BKN is FrechetLight::calcDistance (stopping rule max - min < 1e-10; the rtol column is a copy with max - min < 1e-14 min); CGAL is its dD Fréchet distance package with error 1e-10, which certifies its bounds.
  • The bisection is about as fast as BKN's distance computation: faster on OV (on every pair), pigeons and drifter, slower on UniD (by the mean per pair, 0.013 ms against 0.006 ms), and about as fast on the others. Per pair it is faster on 2 to 48 of the 100 pairs of the other sets, most of them cheap (BKN's median on the pairs lost: 0.001 to 0.44 ms). It is faster than CGAL on every pair.
  • Accuracy against exact values (the sets whose coordinates the exact code of Conradi et al. represents exactly): FrechetDist within 5.1e-11 (half the tolerance, plus rounding), BKN within 8.4e-11. At rtol 1e-14, FrechetDist is within 7.5e-14 relative of the exact values.

High dimensions

The sausages keep the search effective in high dimensions. On the 780 pairs of 40 video clips in ℝ^1200 (KTH actions at 40 × 30 pixels), the bisection takes 14.6 s in all; on the 66 pairs of 12 clips in ℝ^19200 (160 × 120 pixels), 62.5 s. The same curves as curves of a Hilbert space, each distance between frames computed once and cached, take 4.7 and 14.0 times less than on the coordinates (see Curves in Hilbert spaces).