Class ExactMatchDistance

java.lang.Object
org.cicirello.sequences.distance.ExactMatchDistance
All Implemented Interfaces:
SequenceDistanceMeasurer, SequenceDistanceMeasurerDouble

public final class ExactMatchDistance extends Object implements SequenceDistanceMeasurer
ExactMatch distance (or Hamming Distance) of a pair of non-binary strings (or more generally sequences) is the number of sequence (or string) positions where the two sequences differ. This class supports comparison of Java String objects, as well as comparisons of arrays of any of Java's eight primitive types, and arrays of any object type. It is the count of the number of positions for which the two sequences contain different elements.

Most other implementations of Hamming distance require the two strings to be of the same length. Ours does not. If the two sequences (i.e., arrays or Strings) are of different lengths, then the difference in length affects distance. E.g., although "abbd" is a distance of 1 from "abcd" since they differ in one position, "abbd" is a distance of 4 from "abcdefg" (the one difference, plus the three extra characters).

Runtime: O(n), where n is the length of the shorter of the two sequences.

If your sequences are guaranteed not to have duplicates, and to contain the same set of elements, then consider instead using the ExactMatchDistance class, which assumes permutations of the integers from 0 to N-1.

Exact match distance was introduced specifically for permutations in:
S. Ronald, "More distance functions for order-based encodings," in Proc. IEEE CEC. IEEE Press, 1998, pp. 558–563.

  • Constructor Details

    • ExactMatchDistance

      public ExactMatchDistance()
      Constructs the distance measurer as specified in the class documentation.
  • Method Details

    • distance

      public int distance(int[] s1, int[] s2)
      Description copied from interface: SequenceDistanceMeasurer
      Measures the distance between two arrays.
      Specified by:
      distance in interface SequenceDistanceMeasurer
      Parameters:
      s1 - First array.
      s2 - Second array.
      Returns:
      distance between s1 and s2
    • distance

      public int distance(long[] s1, long[] s2)
      Description copied from interface: SequenceDistanceMeasurer
      Measures the distance between two arrays.
      Specified by:
      distance in interface SequenceDistanceMeasurer
      Parameters:
      s1 - First array.
      s2 - Second array.
      Returns:
      distance between s1 and s2
    • distance

      public int distance(short[] s1, short[] s2)
      Description copied from interface: SequenceDistanceMeasurer
      Measures the distance between two arrays.
      Specified by:
      distance in interface SequenceDistanceMeasurer
      Parameters:
      s1 - First array.
      s2 - Second array.
      Returns:
      distance between s1 and s2
    • distance

      public int distance(byte[] s1, byte[] s2)
      Description copied from interface: SequenceDistanceMeasurer
      Measures the distance between two arrays.
      Specified by:
      distance in interface SequenceDistanceMeasurer
      Parameters:
      s1 - First array.
      s2 - Second array.
      Returns:
      distance between s1 and s2
    • distance

      public int distance(char[] s1, char[] s2)
      Description copied from interface: SequenceDistanceMeasurer
      Measures the distance between two arrays.
      Specified by:
      distance in interface SequenceDistanceMeasurer
      Parameters:
      s1 - First array.
      s2 - Second array.
      Returns:
      distance between s1 and s2
    • distance

      public int distance(boolean[] s1, boolean[] s2)
      Description copied from interface: SequenceDistanceMeasurer
      Measures the distance between two arrays.
      Specified by:
      distance in interface SequenceDistanceMeasurer
      Parameters:
      s1 - First array.
      s2 - Second array.
      Returns:
      distance between s1 and s2
    • distance

      public int distance(double[] s1, double[] s2)
      Description copied from interface: SequenceDistanceMeasurer
      Measures the distance between two arrays.
      Specified by:
      distance in interface SequenceDistanceMeasurer
      Parameters:
      s1 - First array.
      s2 - Second array.
      Returns:
      distance between s1 and s2
    • distance

      public int distance(float[] s1, float[] s2)
      Description copied from interface: SequenceDistanceMeasurer
      Measures the distance between two arrays.
      Specified by:
      distance in interface SequenceDistanceMeasurer
      Parameters:
      s1 - First array.
      s2 - Second array.
      Returns:
      distance between s1 and s2
    • distance

      public int distance(String s1, String s2)
      Description copied from interface: SequenceDistanceMeasurer
      Measures the distance between two Strings.
      Specified by:
      distance in interface SequenceDistanceMeasurer
      Parameters:
      s1 - First String.
      s2 - Second String.
      Returns:
      distance between s1 and s2
    • distance

      public int distance(Object[] s1, Object[] s2)
      Description copied from interface: SequenceDistanceMeasurer
      Measures the distance between two arrays of objects. The objects in the arrays must be of a class that has overridden the Object.equals method.
      Specified by:
      distance in interface SequenceDistanceMeasurer
      Parameters:
      s1 - First array.
      s2 - Second array.
      Returns:
      distance between s1 and s2
    • distance

      public <T> int distance(List<T> s1, List<T> s2)
      Description copied from interface: SequenceDistanceMeasurer
      Measures the distance between two lists of objects. The objects in the lists must be of a class that has overridden the Object.equals method.
      Specified by:
      distance in interface SequenceDistanceMeasurer
      Type Parameters:
      T - Type of List elements.
      Parameters:
      s1 - First list.
      s2 - Second list.
      Returns:
      distance between s1 and s2