Hamming distance

From Wikipedia, the free encyclopedia

3-bit binary cube for finding hamming distance
3-bit binary cube for finding hamming distance
Two example distances: 100->011 has distance 3 (red path); 010->111 has distance 2 (blue path)
Two example distances: 100->011 has distance 3 (red path); 010->111 has distance 2 (blue path)
4-bit binary hypercube for finding hamming distance
4-bit binary hypercube for finding hamming distance
Two example distances: 0100->1001 has distance 3 (red path); 0110->1110 has distance 1 (blue path)
Two example distances: 0100->1001 has distance 3 (red path); 0110->1110 has distance 1 (blue path)

In information theory, the Hamming distance between two strings of equal length is the number of positions for which the corresponding symbols are different. Put another way, it measures the number of substitutions required to change one into the other, or the number of errors that transformed one string into the other.

For example:

  • The Hamming distance between 1011101 and 1001001 is 2.
  • The Hamming distance between 2143896 and 2233796 is 3.
  • The Hamming distance between "toned" and "roses" is 3.

Contents

[edit] Special properties

For a fixed length n, the Hamming distance is a metric on the vector space of the words of that length, as it obviously fulfills the conditions of non-negativity, identity of indiscernibles and symmetry, and it can be shown easily by complete induction that it satisfies the triangle inequality as well. The Hamming distance between two words a and b can also be seen as the Hamming weight of ab for an appropriate choice of the − operator.

For binary strings a and b the Hamming distance is equivalent to the number of ones in a xor b. The metric space of length-n binary strings, with the Hamming distance, is known as the Hamming cube; it is equivalent as a metric space to the set of distances between vertices in a hypercube graph. One can also view a binary string of length n as a vector in \mathbb{R}^n by treating each symbol in the string as a real coordinate; with this embedding, the strings form the vertices of an n-dimensional hypercube, and the Hamming distance of the strings is equivalent to the Manhattan distance between the vertices.

[edit] History and applications

The Hamming distance is named after Richard Hamming, who introduced it in his fundamental paper about error-detecting and error-correcting codes (1950). It is used in telecommunication to count the number of flipped bits in a fixed-length binary word as an estimate of error, and therefore is sometimes called the signal distance. Hamming weight analysis of bits is used in several disciplines including information theory, coding theory, and cryptography. However, for comparing strings of different lengths, or strings where not just substitutions but also insertions or deletions have to be expected, a more sophisticated metric like the Levenshtein distance is more appropriate.

[edit] Algorithm example

The Javascript function hamdist() computes the Hamming distance between two strings (or "sequential objects" mapped into strings) of equal length.

Note: if the strings have different length, they can be padded with gap symbols to achieve the same length. For more information, consult the technical reference and distance and similarity concepts.

function hamdist(s1, s2)
{ 
   // We alert user if the two strings differ in length.
   if (s1.length != s2.length) {
      alert("Both sequence must be of the same length!");
      return -1;
   }
   
   var distance = 0;      // zero = no difference between two strings.
   
   // Match the two strings, character by character.  If they are NOT
   // identical, increment distance by 1.
   for(var i = 0; i < s1.length; i++)
      if (s1.charAt(i) != s2.charAt(i))
         distance++;
   return distance;
}

The same algorithm in Python:

def hamdist(s1, s2):
        assert len(s1) == len(s2)
        return sum(ch1 != ch2 for ch1, ch2 in zip(s1, s2))

[edit] References

Adapted in part from Federal Standard 1037C.

Richard W. Hamming. Error Detecting and Error Correcting Codes, Bell System Technical Journal 26(2):147-160, 1950.

[edit] See also

[edit] External links