打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
(1)Hamming distance

Hamming distance

(2012-12-23 22:55:06)
标签:

hammingdistance

it

分类: 沉淀
   In information theory,the Hammingdistance betweentwo strings ofequal length is the number of positions at which the correspondingsymbols are different. Put another way, it measures the minimumnumber of substitutions requiredto change one string into the other, or the numberoferrors thattransformed one string into the other.
   在信息论中,两个等长字符串之间的海明距离是两个字符串对应位置的不同字符的个数。换句话说,它就是将一个字符串变换成另外一个字符串所需要替换的字符个数

Examples

The Hamming distance between:

  • "toned" and "roses" is3.  “toned”和“roses”的海明距离是3。
  • 1011101 and 1001001 is2.  1011101”和“1001001”的海明距离是2.
  • 2173896 and 2233796 is3.  2173896”和“2233796”的海明距离是3.

For a fixed length n,the Hamming distance is a metric onthe vector space of the words of length n, as it obviously fulfillsthe conditions of non-negativity, identity of indiscernibles andsymmetry, and it can be shown easilyby complete induction thatit satisfies the triangle inequality aswell. The Hamming distance between twowords a and b canalso be seen as the Hamming weight of a?b foran appropriate choice of the ? operator.

对于一个固定的长度为n的海明距离是长度为n的词语向量空间中一种度量,因为它显然满足的非负,身份的不可分辨和对称性三个条件,并且它可以被完全归纳法容易地推导出它满足三角不等式。两个词之间的海明距离a和b中也可以看出,为A-B的一个合适的选择- 运算符的海明权重。

三bit位海明距离立方体

100->011 海明距离为3,010->111海明距离为2

4bit位的超正方体模拟的海明距离
0110-》1110 的海明距离为1,0100-》1001的海明距离为3.

For binary strings a and b the Hamming distance is equal tothe number of ones (population count) in a XOR b. The metric spaceof length-n binary strings, with the Hamming distance, is known asthe Hamming cube; it is equivalent as a metric space to the set ofdistances between vertices in a hypercube graph. One can also viewa binary string of length n as a vector in  bytreating each symbol in the string as a real coordinate; with thisembedding, the strings form the vertices of an n-dimensionalhypercube, and the Hamming distance of the strings is equivalent tothe Manhattan distance between the vertices.
对于二进制字符串的a和b,海明距离为等于在a XORb运算结果中1的个数(普遍算法)。度量空间长度-n的二进制字符串,与海明距离,被称为的海明立方体,它相当于一个超立方体图中的顶点之间的距离的集合作为一个度量空间。人们也可以将一个长度为n的二进制串作为一个向量,由处理中的每个符号的字符串作为一个真正的坐标;与此嵌入,字符串形成一个n维超立方体的顶点,和字符串的海明距离是相当于顶点之间的曼哈顿距离。

The Hamming distance is named after Richard Hamming, who introduced it in hisfundamental paper onHamming codes Errordetecting and error correcting codes in1950.[1] Itis used intelecommunication tocount the number of flipped bits in a fixed-length binary word asan estimate of error, and therefore is sometimes calledthesignal distance. Hamming weight analysis of bits is usedin several disciplines including informationtheorycoding theory, and cryptography. However, for comparing strings ofdifferent lengths, or strings where not just substitutions but alsoinsertions or deletions have to be expected, a more sophisticatedmetric like the Levenshtein distance ismore appropriate. For q-ary strings overan alphabet ofsize q ≥ 2the Hamming distance is applied in case oforthogonal modulation, while the Lee distance is used for phasemodulation.If q 2or q 3both distances coincide.

The Hamming distance is also used in systematics as a measure ofgenetic distance.[2]

On a grid (such as a chessboard), the points ata Lee distance of 1 constitutethe von Neumannneighborhood of that point.

海明距离是以理查德·卫斯里·海明(1950年,在汉明码错误检测和纠错码在的基本文件中介绍了海明距离)的名字命名的。它是用在电信,计算一个固定长度的二进制字的翻转位作为估计值的误差,因此有时也被称为信号距离。汉明权重分析位应用在信息理论,编码理论,密码学等多个学科。然而,对于比较不同的长度,或可以预期的不只是替换,插入或删除的字符串的字符串,如Levenshtein距离更复杂的度量值是比较合适的。 对于q元字母表Q≥2大小以上的字符串海明距离适用,而Leedistance是用于相位调制的正交调制的情况下。当q =2或q =3两种距离是一致的。海明距离也用于系统性遗传距离的度量。在一个网格(如一个棋盘)上,Lee距离为1的点构成的冯·诺伊曼的那点附近。


 


python代码如下:

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

print (hamming_distance("gdad","glas"))

结果是2


 

C语言代码如下:

unsigned hamdist(unsigned x, unsigned y)
{
unsigned dist = 0, val = x ^ y;

// Count the number of set bits
while(val)
{
++dist;
val &= val - 1;
}

return dist;
}

int main()
{
unsigned x="abcdcc";
unsigned y="abccdd";
unsigned z=hamdist(x,y);
printf("%d",z);
}
上面的代码结果是3。
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
汉明距离 Hamming distance
Python 字符串相似性的几种度量方法
​LeetCode刷题实战461:汉明距离
Levenshtein Distance(LD)-计算两字符串相似度算法 - chenlb...
字符串相似度算法(编辑距离算法 Levenshtein Distance)原理及C#代码实...
LeetCode之Hamming Distance
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服