您的位置:首页 > 其它

hash算法学习

2015-07-10 14:08 330 查看
转载自:http://blog.csdn.net/sangyongjia/article/details/37312851

              http://my.oschina.net/YeanXu/blog/15613
实现一个hash算法时需要思考一下三个问题:

      第一:hash函数的选择。

      第二:hash冲突的解决办法。

      第三:装填因子大小的选择。装填因子  a=n / m。其中m为hash表的bucket个数;n为关键字的个数。装填因子越大,产生hash冲突就严重。

Hash查找因为其O(1)的查找性能而著称,被对查找性能要求高的应用所广泛采用。它的基本思想是:

     (1) 创建一个定长的线性Hash表,一般可以初始化时指定length;

     (2) 设计Hash函数,将关键字key散射到Hash表中。其中hash函数设计是最为关键的,均匀分布、冲突概率小全在它;

     (3) 通常采用拉链方法来解决hash冲突问题,即散射到同一个hash表项的关键字,以链表形式来表示(也称为桶bucket);

     (4) 给定关键字key,就可以在O(1) + O(m)的时间复杂度内定位到目标。其中,m为拉链长度,即桶深。

Hash应用中,字符串是最为常见的关键字,应用非常普通,现在的程序设计语言中基本上都提供了字符串hash表的支持。字符串hash函数非常多,常见的主要有Simple_hash, RS_hash, JS_hash, PJW_hash, ELF_hash, BKDR_hash, SDBM_hash, DJB_hash, AP_hash, CRC_hash等。评估hash函数优劣的基准主要有以下两个指标:

     (1) 散列分布性

     即桶的使用率backet_usage = (已使用桶数) / (总的桶数),这个比例越高,说明分布性良好,是好的hash设计。

     (2) 平均桶长

     即avg_backet_len,所有已使用桶的平均长度。理想状态下这个值应该=1,越小说明冲突发生地越少,是好的hash设计。

hash函数计算一般都非常简洁,因此在耗费计算时间复杂性方面判别甚微

几个比较著名的哈希算法:
public final class HashFunctionLibrary {

public static long RSHash(String str) {
int b = 378551;
int a = 63689;
long hash = 0;

for (int i = 0; i < str.length(); i++) {
hash = hash * a + str.charAt(i);
a = a * b;
}

return hash;
}

public static long JSHash(String str) {
long hash = 1315423911;

for (int i = 0; i < str.length(); i++) {
hash ^= ((hash << 5) + str.charAt(i) + (hash >> 2));
}

return hash;
}

// 效果不好,见 http://blog.csdn.net/sangyongjia/article/details/37312851 public static long PJWHash(String str) {
long BitsInUnsignedInt = (long) (4 * 8);
long ThreeQuarters = (long) ((BitsInUnsignedInt * 3) / 4);
long OneEighth = (long) (BitsInUnsignedInt / 8);
long HighBits = (long) (0xFFFFFFFF) << (BitsInUnsignedInt - OneEighth);
long hash = 0;
long test = 0;

for (int i = 0; i < str.length(); i++) {
hash = (hash << OneEighth) + str.charAt(i);

if ((test = hash & HighBits) != 0) {
hash = ((hash ^ (test >> ThreeQuarters)) & (~HighBits));
}
}

return hash;
}

// 效果不好,见 http://blog.csdn.net/sangyongjia/article/details/37312851 public static long ELFHash(String str) {
long hash = 0;
long x = 0;

for (int i = 0; i < str.length(); i++) {
hash = (hash << 4) + str.charAt(i);

if ((x = hash & 0xF0000000L) != 0) {
hash ^= (x >> 24);
}
hash &= ~x;
}

return hash;
}

public static long BKDRHash(String str) {
long seed = 131; // 31 131 1313 13131 131313 etc..
long hash = 0;

for (int i = 0; i < str.length(); i++) {
hash = (hash * seed) + str.charAt(i);
}

return hash;
}

public static long SDBMHash(String str) {
long hash = 0;

for (int i = 0; i < str.length(); i++) {
hash = str.charAt(i) + (hash << 6) + (hash << 16) - hash;
}

return hash;
}

public static long DJBHash(String str) {
long hash = 5381;

for (int i = 0; i < str.length(); i++) {
hash = ((hash << 5) + hash) + str.charAt(i);
}

return hash;
}

public static long DEKHash(String str) {
long hash = str.length();

for (int i = 0; i < str.length(); i++) {
hash = ((hash << 5) ^ (hash >> 27)) ^ str.charAt(i);
}

return hash;
}

public static long BPHash(String str) {
long hash = 0;

for (int i = 0; i < str.length(); i++) {
hash = hash << 7 ^ str.charAt(i);
}

return hash;
}

public static long FNVHash(String str) {
long fnv_prime = 0x811C9DC5;
long hash = 0;

for (int i = 0; i < str.length(); i++) {
hash *= fnv_prime;
hash ^= str.charAt(i);
}

return hash;
}

public static long APHash(String str) {
long hash = 0xAAAAAAAA;

for (int i = 0; i < str.length(); i++) {
if ((i & 1) == 0) {
hash ^= ((hash << 7) ^ str.charAt(i) * (hash >> 3));
} else {
hash ^= (~((hash << 11) + str.charAt(i) ^ (hash >> 5)));
}
}

return hash;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: