哈希算法(精品).ppt
《哈希算法(精品).ppt》由会员分享,可在线阅读,更多相关《哈希算法(精品).ppt(24页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Hash Tables 1Comp 122,Spring 2004hashtables-2Lin/DeviDictionary wDictionary:Dynamic-set data structure for storing items indexed using keys.Supports operations Insert,Search,and Delete.Applications:Symbol table of a compiler.Memory-management tables in operating systems.Large-scale distributed syste
2、ms.wHash Tables:Effective way of implementing dictionaries.Generalization of ordinary arrays.Comp 122,Fall 2003hashtables-3Lin/DeviDirect-address Tables wDirect-address Tables are ordinary arrays.wFacilitate direct addressing.Element whose key is k is obtained by indexing into the kth position of th
3、e array.wApplicable when we can afford to allocate an array with one position for every possible key.i.e.when the universe of keys U is small.wDictionary operations can be implemented to take O(1)time.Details in Sec.11.1.Comp 122,Fall 2003hashtables-4Lin/DeviHash TableswNotation:U Universe of all po
4、ssible keys.K Set of keys actually stored in the dictionary.|K|=n.wWhen U is very large,Arrays are not practical.|K|U|.wUse a table of size proportional to|K|The hash tables.However,we lose the direct-addressing ability.Define functions that map keys to slots of the hash table.Comp 122,Fall 2003hash
5、tables-5Lin/DeviHashingwHash function h:Mapping from U to the slots of a hash table T0.m1.h:U 0,1,m1wWith arrays,key k maps to slot Ak.wWith hash tables,key k maps or“hashes”to slot Thk.whk is the hash value of key k.Comp 122,Fall 2003hashtables-6Lin/DeviHashing0m1h(k1)h(k4)h(k2)=h(k5)h(k3)U(univers
6、e of keys)K(actualkeys)k1k2k3k5k4collisionComp 122,Fall 2003hashtables-7Lin/DeviIssues with HashingwMultiple keys can hash to the same slot collisions are possible.Design hash functions such that collisions are minimized.But avoiding collisions is impossible.Design collision-resolution techniques.wS
7、earch will cost(n)time in the worst case.However,all operations can be made to have an expected complexity of(1).Comp 122,Fall 2003hashtables-8Lin/DeviMethods of ResolutionwChaining:(拉链法)Store all elements that hash to the same slot in a linked list.Store a pointer to the head of the linked list in
8、the hash table slot.wOpen Addressing(线性探测法)All elements stored in hash table itself.When collisions occur,use a systematic(consistent)procedure to store elements in free slots of the table.k20m1k1k4k5k6k7k3k8Comp 122,Fall 2003hashtables-9Lin/DeviCollision Resolution by Chaining0m1h(k1)=h(k4)h(k2)=h(
9、k5)=h(k6)h(k3)=h(k7)U(universe of keys)K(actualkeys)k1k2k3k5k4k6k7k8h(k8)XXXComp 122,Fall 2003hashtables-10Lin/Devik2Collision Resolution by Chaining0m1U(universe of keys)K(actualkeys)k1k2k3k5k4k6k7k8k1k4k5k6k7k3k8Comp 122,Fall 2003hashtables-11Lin/DeviHashing with ChainingDictionary Operations:wCha
10、ined-Hash-Insert(T,x)Insert x at the head of list Th(keyx).Worst-case complexity O(1).wChained-Hash-Delete(T,x)Delete x from the list Th(keyx).Worst-case complexity proportional to length of list with singly-linked lists.O(1)with doubly-linked lists.wChained-Hash-Search(T,k)Search an element with ke
11、y k in list Th(k).Worst-case complexity proportional to length of list.Comp 122,Fall 2003hashtables-12Lin/DeviAnalysis on Chained-Hash-SearchwLoad factor=n/m=average keys per slot.m number of slots.n number of elements stored in the hash table.wWorst-case complexity:(n)+time to compute h(k).wAverage
12、 depends on how h distributes keys among m slots.wAssume Simple uniform hashing.Any key is equally likely to hash into any of the m slots,independent of where any other key hashes to.O(1)time to compute h(k).wTime to search for an element with key k is Q(|Th(k)|).wExpected length of a linked list=lo
13、ad factor=n/m.Comp 122,Fall 2003hashtables-13Lin/DeviExpected Cost of an Unsuccessful SearchProof:wAny key not already in the table is equally likely to hash to any of the m slots.wTo search unsuccessfully for any key k,need to search to the end of the list Th(k),whose expected length is.wAdding the
14、 time to compute the hash function,the total time required is(1+).Theorem:An unsuccessful search takes expected time(1+).Comp 122,Fall 2003hashtables-14Lin/DeviExpected Cost of a Successful SearchProof:wThe probability that a list is searched is proportional to the number of elements it contains.wAs
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 算法 精品
限制150内