付费下载
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、 Data Structures and Algorithm Analysis in CCS222HashingMotivating ExampleSuppose we want to store a list whose elements are integers between 1 and 5:Define an array of size 5, and if the list has element j, then j is stored in Aj-1, otherwise Aj-1 contains 0.Complexity of find operation is O(1), bu
2、t the example is too simplistic to be relevant to most situations.Hash tableThe objective of a hash table is to find an element optimizing for average case search time, not worst case.Supposing we know the elements belong to 1,2U.If we are allowed an overall space of U, then this can be done as desc
3、ribed before.But U can be very large (think how many train tickets are sold each day, but we want to be able to access each tickets information in a reasonable amount of time).Space for storage is called a “hash table” (H).Section 5.1, WeissAssume that the hash table has size MThere must be a hash f
4、unction which maps an element to a value p in 0,.M-1, and the element is placed in position p in the hash table. The function is called hj, (the hash value for j is hj)If hj = k, then the element is added to Hk.Suppose we want a list of integers, then an example hash function is hj = j modulo M.Pseu
5、do-Random Functions Consider a simplistic hash function such as M = 5List contains 1 , 3, 9, 81398 Uh oh! We have a collision!Entry 01234Hash tableWhat happens if the desired elements are not numbers, (e.g., names)?Then we use a function to convert each element to an integer and hash the integer.If
6、we want to store string, such as “abc”Represent each symbol by the ASCII code, choose a number r to modify it, then calculate the integer value ASCII(a)r2 + ASCII(b)r + ASCII ( c )What about non-numerics?ImplementationHash tables are arrays.The size of a hash table is normally a prime number.Two dif
7、ferent elements may hash to the same value (collision).Hashing needs collision resolution.Hash functions are chosen so that the hash values are spread over 0,.M-1, and there are only few collisions.Separate ChainingStore all the elements mapped to the same position in a linked list.Hk is the list of
8、 all elements mapped to k.To find an element j, compute h(j). Let h(j) = k. Then search in bucket list HkTo insert an element j, compute h(j). Let h(j) = k. Then insert in bucket list HkTo delete an element, delete from the bucket list.1398 Entry 01234Insertion is O(1) (for bucket depth of 1).Worst
9、case searching complexity depends on the maximum length of a list Hp. O(q) if q is the maximum length.We are interested in average search time, not worst case.A) Search for 7, look in position 2 in the array, find it empty, conclude 7 is not thereB) Search for 13, look in position 3 in the array, se
10、arch the bucket list, 13 not found, conclude that 13 is not thereC) Insert 4, add it in the bucket list starting with 9Some ExamplesLoad factor is the average size of a list. = number of elements in the hash table/ number of positions in the hash table(M)Average find complexity is 1 + We want to be
11、approximately 1To reduce worst case complexity we choose hash functions which distribute the elements evenly in the list.Ideal Search ComplexityOpen AddressingSeparate chaining requires manipulation of pointers and dynamic memory allocation which are expensive.Open addressing is an alternate scheme.
12、Want to insert key (element) j Compute h(j) = kIf Hk is empty store in Hk, otherwise try Hk+1, Hk+2, etc. (increment in modulo size)Linear ProbingEvery position in hash table contains one element each.Can always insert a key as long as the table is not fullFinding may be difficult if the table is cl
13、ose to full. To maintain performance could require memory allocation many times in excess actual data to be stored.M = 5List contains 1 , 3, 9, 81398 Entry 01234Linear ProbingThe idea is to declare a hash table large enough so that it is never full.Initially, all slots are empty.Elements are inserte
14、d as described.When an element is deleted, the space is marked deleted (empty and deleted are different).During the find operation, one looks for element k starting from where it should be (Hh(k), till the element is found, or an empty slot is found.In the latter case, we conclude that the element i
15、s not in the list.Memory is CheapLooking for 13, Start from the position which has 3, then look at that with 9, then that with 8, next with 1, reach an empty slot, conclude not there Looking for 8, start from the position which has 3, then look at that with 9, then that with 8, conclude foundM = 5Li
16、st contains 1 , 3, 9, 81398 Entry 01234When It WorksIs there any problem if empty and deleted are not distinguished? Yes, may conclude that element not present even if it isDelete 9 for some reasonSearch for 8, start from the position with 3, go to next slot, finds nothing, conclude empty and thus 8
17、 is not there!M = 5List contains 1 , 3, 9, 81398 Entry 01234When It DoesntXThus a separate mechanism for differentiating between empty and deleted is required (lazy deletion, of a sorts).1) When we insert an element k, then start from Hh(k) and move till an empty or deleted slot can be found. 2) An
18、element can be inserted as long as the hash-table is not full.3) If hash values are clustered, then even if hash table is relatively empty, finding may be difficult.Linear Probing, SummarizedQuadratic ProbingAn alternative to linear probing.To insert key k, try slot h(k). If the slot is full try slot h(k) + 1, then h(k) + 4, then h(k) + 9 and so on.Advantage?Are we guaranteed to be able to insert as long as the hash table is not full? Clustering should be reduced.M = 3, first two positions full, let h(k) = 0, h(k) + n2 m
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026内蒙古锡林郭勒盟东乌珠穆沁旗事业单位引进急需紧缺人才3人考试模拟试题及答案解析
- 2026年阿克苏市交通运输系统事业单位人员招聘考试备考试题及答案详解
- 2026福建厦门半导体投资集团有限公司招聘考试参考题库及答案解析
- 2026年崇左市财政系统事业单位人员招聘考试备考试题及答案详解
- 2026湖南大学附属中学校医招聘1人考试模拟试题及答案解析
- 2026 增肌期粉条课件
- 2026年达州市辅警招聘考试备考试题及答案详解
- 2026春季中国南水北调集团文旅发展有限公司 (新闻宣传中心)招聘1人考试备考试题及答案解析
- 2026 儿童餐食设计课件
- 职业规划标准模板
- 2018年四川省绵阳市中考地理试卷(解析版)
- 住院患者身体约束护理团标精神科保护性约束实施及解除专家共识
- 如何成为一个合格的面试官课件
- 小学五年级家长会语文老师的课件
- AI在药物研发中的应用
- 新人教版七至九年级英语单词表
- 关键施工技术、工艺与工程项目实施的重点、难点和解决方案
- 2023年环境卫生(正高)考试历年难点与易错点考核试题3答案解析
- 50套普通话测试题与答案
- GB/T 4325.23-2013钼化学分析方法第23部分:氧量和氮量的测定惰气熔融红外吸收法-热导法
- GB/T 2970-2016厚钢板超声检测方法
评论
0/150
提交评论