哈希函数的特征

Prerequisite: Hashing data structure

先决条件: 哈希数据结构

The hash function is the component of hashing that maps the keys to some location in the hash table. As part of the hashing technique, we need a hash function to map the available keys to the set of indexes in the hash table. Say we have a set of keys ranging from [-1000, 1000] and have a hash table of size 10, index ranging from [0, 10].

哈希函数是哈希的组件,它将密钥映射到哈希表中的某个位置。 作为哈希技术的一部分,我们需要一个哈希函数将可用键映射到哈希表中的索引集。 假设我们有一组范围从[-1000,1000]的键,并且有一个大小为10的哈希表,索引范围是[0,10]。

That's why to map the keys we need hash functions. A hash function is termed as the perfect hash function is what is able to map the keys to unique locations. But unfortunately, there is no systematic way to generate hash function as the size of the key list is very large considered to hash table size.

这就是为什么要映射我们需要哈希函数的键的原因。 哈希函数被称为完美哈希函数,它是能够将密钥映射到唯一位置的函数。 但是遗憾的是,由于键列表的大小对于哈希表的大小而言非常大,因此没有系统的方法来生成哈希函数。

A popular hash function is a folding method where we fold the integers and sum the partitions and then take mod 10 finally.

流行的哈希函数是一种折叠方法 ,其中我们折叠整数并将分区求和,然后最终取mod 10。

For example,

例如,

The hash function for the folding method is = sum of the fold of size two %10
Say we have integer 60784567
So sum will be 60 + 78 + 45 + 67 = 250
So final location is 250 % 10 = 0

The below program computes the above folding method which is an example of the hash function.

下面的程序将计算上述折叠方法 ,该方法哈希函数的一个示例。

#include <bits/stdc++.h>
using namespace std;
//folding method
int main()
{
string s;
cout << "enter number\n";
cin >> s;
//folding and summing
int sum = 0;
for (int i = 0; i < s.length(); i += 2) {
if (i + 1 < s.length())
sum += stoi(s.substr(i, 2));
else
//when only one digit is left for folding
sum += stoi(s.substr(i, 1));
}
cout << s << "->" << sum % 10;
return 0;
}

Output:

输出:

enter number
60784567
60784567->0

Now if some other number also finds location 0, then it's called collision.

现在,如果其他一些数字也找到位置0,则称为碰撞

Characteristics of good hash function

哈希函数良好的特征

  1. Minimum collision

    最小碰撞

  2. High gain factor (distributes keys evenly). Like say we have 10 locations in the hash table, then almost 9 locations should have keys. It should not be the case that 4-5 locations have keys only where the keys collided.

    高增益因子(均匀分布密钥)。 像说在哈希表中有10个位置,那么几乎9个位置应该有键。 并非4-5个位置仅在按键碰撞的地方具有按键。

  3. Have a high load factor. Load factor means the number of keys stored/hash table size

    负载系数高。 负载因子是指存储的密钥数/哈希表大小

  4. Easy to compute

    易于计算

Exercises on hash function

哈希函数练习

Use the below hash function to compute the hashing and comment on the goodness of the hash function.

使用下面的哈希函数来计算哈希并评论哈希函数的优缺点。

1) F(key) = number of digits of key

1)F(key)=密钥位数

#include <bits/stdc++.h>
using namespace std;
//hash function 1
int main()
{
string s;
cout << "enter number\n";
cin >> s;
//f(s)=no of digit in s=length of s
cout << s << "->" << s.length();
return 0;
}

Output:

输出:

enter number
123452
123452->6

The above hash function is not good at all. Say we have set of keys all having the same digits, then all the keys will collide and the rest of the locations will remain empty.

上面的哈希函数根本不好。 假设我们有一组键都具有相同的数字,那么所有键将发生冲突,其余位置将保持为空。

2) F(key) = (rand()*key) % 10

2)F(键)=(rand()*键)%10

#include <bits/stdc++.h>
using namespace std;
//hash function 2
int main()
{
int n;
cout << "enter number\n";
cin >> n;
//f(n)=rand()*n
cout << n << "->" << (rand() * n) % 10;
return 0;
}

Output:

输出:

enter number
103456
103456->2

The above hash function is good as we are multiplying random integers and bringing randomness, the collision rate will be less.

上面的哈希函数很好,因为我们要乘以随机整数并带来随机性,则冲突率会更低。

Comparison of the above hash functions:

上面的哈希函数比较:

#include <bits/stdc++.h>
using namespace std;
//comparing goodness of hash function 1 &  2
int main()
{
//set of input numbers
vector<int> arr{ 12345, 234245, 1223123, 765845, 345234, 234534, 98675, 34523, 123, 3245 };
//using hash function 1
cout << "using hash function 1\n";
for (int a : arr) {
cout << a << "->" << to_string(a).length() % 10 << endl;
}
//using hash function 2
cout << "\n\nusing hashh function 2\n";
for (int a : arr) {
cout << a << "->" << (rand() * a) % 10 << endl;
}
return 0;
}

Output:

输出:

using hash function 1
12345->5
234245->6
1223123->7
765845->6
345234->6
234534->6
98675->5
34523->5
123->3
3245->4
using hashh function 2
12345->9
234245->4
1223123->3
765845->-1
345234->-4
234534->4
98675->6
34523->0
123->5
3245->-9

翻译自: https://www.includehelp.com/data-structure-tutorial/hash-functions-and-its-characteristics.aspx

哈希函数的特征

哈希函数的特征_哈希函数及其特征相关推荐

  1. filter函数的用法_动态数组函数系列5| 筛选函数FILTER,单条件多条件动态筛选

    FILTER函数是筛选函数,就是在源数据中按照我们的条件筛选出我们想要的数据.除了常规的数据筛选,还可以进行多条件的"且"或者"或"的筛选. 下面我们来看看这个 ...

  2. 廖雪峰讲python高阶函数求导_高阶函数 - 廖雪峰的Python2.7教程 - 广州尚鹏

    高阶函数英文叫Higher-order function.什么是高阶函数?我们以实际代码为例子,一步一步深入概念. 变量可以指向函数 以Python内置的求绝对值的函数abs()为例,调用该函数用以下 ...

  3. 廖雪峰讲python高阶函数求导_高阶函数 · 廖雪峰的Python3.x教程 · 看云

    [TOC] ### 引入 高阶函数英文叫`Higher-order function`.什么是高阶函数?我们以实际代码为例子,一步一步深入概念. ### 变量可以指向函数 以Python内置的求绝对值 ...

  4. python中哈希是什么意思_在python中向量化特征哈希

    您可以使用带权重的bincount来执行您要求的操作: >>> np.bincount(h,weights=x) array([ 10., 5., 10., 10., 6.]) 对于 ...

  5. 哈希在线计算工具_哈希:开发人员的绝佳工具

    哈希在线计算工具 Hashing is an important topic for programmers and computer science students to be familiar ...

  6. python函数图像绘制、函数不固定_无法在函数中绘制tkinter图像

    我正在制作一个垄断游戏,我试图在画布上绘制图像,但它只有在功能不起作用的情况下才会起作用:def make_image(root, location, canvas): photo = PhotoIm ...

  7. python测验5 函数和代码_测验5: 函数和代码复用 (第5周)-单选题

    1.以下选项不是函数作用的是:‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬ A. 提 ...

  8. python中id函数的用法_用id函数做几个测试

    Python内置的id函数其实非常简单,就是将参数对象的内存地址返回,即id函数返回的是一个很大的整数(地址).基于Python语言的特性,本文做了几个测试,还比较有趣. 相同整数的id相同 > ...

  9. 不是区块链的特征_区块链的四大特征

    在对比特币和以太坊这两个主要系统,讨论了区块链的价值表示和价值转移这两个基础功能,探讨了数字资产.通证与通证经济系统之后,我们再来看看区块链的特征与用途,尝试回答"区块链有什么用" ...

最新文章

  1. Elasticsearch官档翻译——2 2 在Linux上启动服务
  2. Android跳转intent简单教程
  3. python中rename函数_python os.rename(…)不起作用!
  4. mysql出现多线程操作同一个表的情况,应该怎么办?
  5. 华为电话面试题java_华为java面试题(含电话面试)
  6. YUI 的模块信息配置优先级关系梳理
  7. 手写实现java中的trim_JS中字符串trim()使用示例
  8. java new对象 =null_在Java中将对象分配为null会影响垃圾回收吗?
  9. CCF201612-5 卡牌游戏(募集解题代码)
  10. 操作系统—进程同步与互斥问题之生产者消费者问题,附赠PV操作题解题思路(思维导图版)
  11. cas 怎么过滤带pathvariable_过滤材料怎么摆?记住这个“公式”就好~
  12. yolov3 使用darknet的python接口使用
  13. 无需服务端的多平台同步利器(btsync)
  14. python凹多边形分割_Unity 凹多边形三角剖分
  15. 电子书阅读器背景颜色修改方法
  16. 09-线程池与进程池
  17. 山东网通和电信的DNS
  18. 古代物流是如何进行的?
  19. 隐藏身份证中间几位工具类
  20. openGauss亮相VLDB2020,展示内存优化研究成果

热门文章

  1. 爬取链家网二手房数据并保存到mongodb中
  2. FileProvider 的使用(Failed to find configured root that contains/storage/emulated/0/DCIM/ )
  3. 树莓派安装kodi(随缘补充)
  4. 好客租房移动web项目(3)
  5. SpringAOP实战和理论
  6. Mac OS X系统上使用MacDown
  7. java程序中的进制(进制的转换十六进制和八进制)
  8. Question Answering论文(问答系统阅读理解)
  9. 2013 成都赛区网赛 D Minimum palindrome
  10. 22、js - 处理异常