散列碰撞_散列中的碰撞和碰撞解决技术
散列碰撞
Prerequisite: Hashing data structure
先決條件: 哈希數據結構
碰撞 (Collisions)
Hash functions are there to map different keys to unique locations (index in the hash table), and any hash function which is able to do so is known as the perfect hash function. Since the size of the hash table is very less comparatively to the range of keys, the perfect hash function is practically impossible. What happens is, more than one keys map to the same location and this is known as a collision. A good hash function should have less number of collisions.
哈希函數可以將不同的鍵映射到唯一的位置(哈希表中的索引),任何能夠做到這一點的哈希函數都稱為完美哈希函數。 由于哈希表的大小相對于鍵范圍而言非常小,因此理想的哈希函數實際上是不可能的。 發生的事情是,有多個鍵映射到同一位置,這稱為碰撞 。 良好的哈希函數應具有較少的沖突數。
To understand what collision is let's check the below example,
要了解什么是碰撞,讓我們檢查以下示例,
Say, the set of keys are; {123, 124, 135, 1267, 2378, 9087} and hash table size is 10(0-9 indices)Now, If our hashing function is F(x)=digits in xThen 123->3 124->3 135->3 1267->4 2378->4 9087->4The hash table will look like,
哈希表看起來像
In the above example, we can see though there are 10 indices only 2 are being used and the collision rate is too high. 123, 124, 135 have collided while 1267, 2378, 9087 have collided.
在上面的示例中,我們可以看到盡管有10個索引,但僅使用了2個,并且沖突率太高。 123、124、135發生了碰撞,而1267、2378、9087發生了碰撞。
#include <bits/stdc++.h> using namespace std;//collision int main() {//set of input numbersvector<int> arr{ 123, 124, 135, 1267, 2378, 9087 };//using hashh function f(x)=no of digits in xcout << "using hashh function 1\n";for (int a : arr) {cout << a << "->" << to_string(a).length() << endl;}return 0; }Output:
輸出:
using hashh function 1 123->3 124->3 135->3 1267->4 2378->4 9087->4碰撞解決技術 (Collision Resolution Techniques)
Collision resolution is finding another location to avoid the collision. The most popular resolution techniques are,
碰撞解決方案正在尋找另一個位置來避免碰撞。 最受歡迎的解析技術是
Separate chaining
單獨鏈接
Open addressing
開放式尋址
Open addressing can be further divided into,
開放式尋址可以進一步分為:
Linear Probing
線性探測
Quadratic Probing
二次探測
Double hashing
雙重哈希
翻譯自: https://www.includehelp.com/data-structure-tutorial/collisions-in-hashing-and-collision-resolution-techniques.aspx
散列碰撞
總結
以上是生活随笔為你收集整理的散列碰撞_散列中的碰撞和碰撞解决技术的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 'unsigned char'-C编程中
- 下一篇: 《幽兰》第一句是什么