




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年补偿贸易借款合同协议书样本
- 语出建筑(山东联盟)知到课后答案智慧树章节测试答案2025年春潍坊科技学院
- 毕业答辩开题报告-1
- 2024年浙大宁波理工学院招聘事业编制工作人员真题
- 第六单元 美丽的校园-认识方向(教案)-二年级上册数学青岛版
- 2024年山东省精神卫生中心招聘真题
- 2024年宁德市闽东医院聘用烧伤科副主任医师招聘笔试真题
- 水表出售合同范本
- 2024年临沧市市属事业单位考试真题
- 2024年拉萨市市属事业单位考试真题
- 多发性硬化课件
- 2019全国中学生生物学联赛试题详解
- 2025年职业指导师专业能力测试卷:职业心理健康与心理测评试题
- 安徽省蚌埠市2024-2025学年高三(下)第二次质检物理试卷(含解析)
- 2025届山东省菏泽市高三下学期一模政治试题及答案
- 乒乓球爱好者如何制定乒乓球训练计划
- 2025年湖南省长沙市长郡教育集团九年级下学期第一次学情分析(中考一模)语文试题(含解析)
- 江西南昌市2025届高三语文一模作文:对“差不多”“尽力了”的思考
- 【语文】《青蒿素:人类征服疾病的一小步》《一名物理学家的教育历程》课件2024-2025学年统编版高一语文必修下册
- 初级社工师《社会工作实务》考试(重点)题库300题(含答案解析)
- 2025哈尔滨亚洲冬季运动会主题宣讲课件
评论
0/150
提交评论