Double Hashing(双重hash)

 

双重哈希是开放寻址哈希表中的冲突解决技术。双重哈希的思想是在发生冲突时对键做第二个哈希函数。

双重哈希可以处理 :
(hash1(key) + i * hash2(key)) % TABLE_SIZE
这里 hash1() 、 hash2() 是hash 函数, TABLE_SIZE 是hash表大小
(如果发生冲突,i递增然后重复运算)

通俗的二次Hash函数:hash2(key) = PRIME – (key % PRIME)

PRIME一般选择小于 TABLE_SIZE 的质数

 

优质的二次Hash函数应该具备这些条件:

  • 二次Hash函数结果不能为0
  • 第二个哈希函数要可以覆盖表的每一个单元

 

实例

// CPP program to implement double hashing 
#include <bits/stdc++.h> 
using namespace std; 
  
// Hash table size 
#define TABLE_SIZE 13 
  
// Used in second hash function. 
#define PRIME 7 
  
class DoubleHash 
{ 
    // Pointer to an array containing buckets 
    int *hashTable; 
    int curr_size; 
  
public: 
  
    // function to check if hash table is full 
    bool isFull() 
    { 
  
        // if hash size reaches maximum size 
        return (curr_size == TABLE_SIZE); 
    } 
  
    // function to calculate first hash 
    int hash1(int key) 
    { 
        return (key % TABLE_SIZE); 
    } 
  
    // function to calculate second hash 
    int hash2(int key) 
    { 
        return (PRIME - (key % PRIME)); 
    } 
  
    DoubleHash() 
    { 
        hashTable = new int[TABLE_SIZE]; 
        curr_size = 0; 
        for (int i=0; i<TABLE_SIZE; i++) 
            hashTable[i] = -1; 
    } 
  
    // function to insert key into hash table 
    void insertHash(int key) 
    { 
        // if hash table is full 
        if (isFull()) 
            return; 
  
        // get index from first hash 
        int index = hash1(key); 
  
        // if collision occurs 
        if (hashTable[index] != -1) 
        { 
            // get index2 from second hash 
            int index2 = hash2(key); 
            int i = 1; 
            while (1) 
            { 
                // get newIndex 
                int newIndex = (index + i * index2) % 
                                        TABLE_SIZE; 
  
                // if no collision occurs, store 
                // the key 
                if (hashTable[newIndex] == -1) 
                { 
                    hashTable[newIndex] = key; 
                    break; 
                } 
                i++; 
            } 
        } 
  
        // if no collision occurs 
        else
            hashTable[index] = key; 
        curr_size++; 
    } 
  
    // function to display the hash table 
    void displayHash() 
    { 
        for (int i = 0; i < TABLE_SIZE; i++) 
        { 
            if (hashTable[i] != -1) 
                cout << i << " --> "
                     << hashTable[i] << endl; 
            else
                cout << i << endl; 
        } 
    } 
}; 
  
// Driver's code 
int main() 
{ 
    int a[] = {19, 27, 36, 10, 64}; 
    int n = sizeof(a)/sizeof(a[0]); 
  
    // inserting keys into hash table 
    DoubleHash h; 
    for (int i = 0; i < n; i++) 
        h.insertHash(a[i]); 
  
    // display the hash Table 
    h.displayHash(); 
    return 0; 
}

结果

0
1 --> 27
2
3
4
5 --> 10
6 --> 19
7
8
9
10 --> 36
11
12 --> 64

 

 

 

  • 21
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
哈希(spectral hashing)是一种用于图像的索引和相似性搜索的哈希方法。它的目标是将高维图像数据映射到低维二进制编码(哈希码)空间中,以便能够在高效的时间内对图像进行相似性比较。 谱哈希的核心思想是利用图像的谱信息进行编码。它首先将每个图像表示为一个图像邻接矩阵,该矩阵描述了图像中像素之间的相似性关系。然后,通过对邻接矩阵进行谱分解,得到特征向量和特征值。接着,从特征向量中选择最重要的几个进行投影,并将其转化为二进制码。 谱哈希的优点在于它能够保持图像之间的相似性关系。通过谱分解,它能够提取出数据的主要结构,将图像从高维度空间映射到低维度空间,同时保持图像之间的欧几里德距离。这就使得在哈希码空间中进行相似度度量成为可能,也使得对图像进行快速搜索和检索变得更加高效。 除此之外,谱哈希还具有一些其他的优点。它能够在高维空间和低维哈希码空间之间建立一种映射关系,从而实现了跨空间的相似性比较。同时,由于采用了二进制编码,它在存储和计算上更加高效。此外,谱哈希还具有一定的容错能力,即使在图像数据存在噪声或变形的情况下,仍然能够保持相似性的度量。 总的来说,谱哈希是一种用于图像索引和相似性搜索的有效方法。它通过利用图像的谱信息进行特征提取和编码,能够在高效的时间内实现图像的相似性比较和检索,具有较好的容错能力和存储计算效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值