Function Repository Resource:

DiscreteHausdorffDistance

Source Notebook

Find the Hausdorff distance between two sets of elements

Contributed by: Sjoerd Smit

ResourceFunction["DiscreteHausdorffDistance"][set1, set2]

returns the Hausdorff distance between two sets, which is the maximum distance you need to travel from a point in one set to reach a point in the other.

Details and Options

Given a distance function d that measures the minimum distance from an element to a set, the Hausdorff distance is given by:
ResourceFunction["DiscreteHausdorffDistance"] takes the same options as Nearest.

Examples

Basic Examples (2) 

Measure the distance between two sets of numbers:

In[1]:=
ResourceFunction["DiscreteHausdorffDistance"][{0, 1/2, 2}, {1, 3, -2}]
Out[1]=

This is the maximum of the following distances:

In[2]:=
dists1 = Nearest[{0, 1/2, 2} -> "Distance", {1, 3, -2}]
Out[2]=
In[3]:=
dists2 = Nearest[{1, 3, -2} -> "Distance", {0, 1/2, 2}]
Out[3]=
In[4]:=
Max[dists1, dists2]
Out[4]=

Scope (1) 

Use a custom DistanceFunction for sets of strings:

In[5]:=
ResourceFunction[
 "DiscreteHausdorffDistance"][{"aa", "bb", "cc"}, {"ab", "de"}, DistanceFunction -> HammingDistance]
Out[5]=

Applications (2) 

Compute the distance between sets of points in 2D space:

In[6]:=
SeedRandom[1234];
set1 = RandomPoint[Circle[], 5];
set2 = RandomPoint[Circle[{0, 0}, 2], 10];
ResourceFunction["DiscreteHausdorffDistance"][set1, set2]
Out[9]=

Show the maximum distances from one set to the other as arrows. DiscreteHausdorffDistance is equal to the longest of these two arrows:

In[10]:=
arrows = MapApply[
   Replace[
     First@TakeLargestBy[
       Nearest[#1 -> All, #2, 1][[All, 1]] -> All,
       Key["Distance"],
       1
       ],
     assoc_Association :> Arrow[{
        #2[[assoc["Index"]]],
        assoc["Element", "Element"]
        }]
     ] &,
   {
    {set1, set2},
    {set2, set1}
    }
   ];
Show[ListPlot[{set1, set2}, AspectRatio -> 1], Graphics[arrows]]
Out[11]=

Publisher

Sjoerd Smit

Version History

  • 1.0.0 – 26 April 2023

Related Resources

License Information