Escolar Documentos
Profissional Documentos
Cultura Documentos
COMPSCI 220
Hashing:
a technique of storing values in the tables and searching for them in linear, O(n), worst-case and extremely fast, O(1), average-case time
Lecture 11 COMPSCI 220 - AP G. Gimel'farb 2
In the only 6.3% full table (since 23/365 = 0.063) there is better than 50% chance of a collision!
Therefore: 50% chance of collision if = 0.063
Lecture 11 COMPSCI 220 - AP G. Gimel'farb 5
N #1 N(N #1) " ; N N2 N # 2 N(N #1)(N # 2) Q N (3) = Q N (2) " ; ... N N3 N # n + 1 N(N #1)...(N # n + 1) Q N (n) = Q N (n #1) " N Nn Q N (1) = 1 " Q N (2) = Q N (1)
Lecture 11 COMPSCI 220 - AP G. Gimel'farb 6
N ; N
COMPSCI 220
N! N n (N " n)!
Lemma 3.33: The average number of probes for successful, Tss(), and unsuccessful, Tus(), search in a hash table with load factor = m/n is, respectively, $ $ 1 '2 ' $ 1 ' Tss ( " ) = 0.5&1+ ) and Tus ( ") = 0.5&1+ & & % 1# " ) ) % 1# " ( ( ) % (
Drawbacks: clustering of keys in the table
Lecture 11 COMPSCI 220 - AP G. Gimel'farb 8
Lemma 3.35: Assuming that OADH provides nearly uniform hashing, the average number of probes for successful, Tss(), and unsuccessful, Tus(), search is, respectively, 1 1 $ 1 ' Tss ( " ) = ln& ) and Tus ( ") = 1# " " %1# " (
COMPSCI 220 - AP G. Gimel'farb 9 Lecture 11 COMPSCI 220 - AP G. Gimel'farb 10
Lecture 11
Lecture 11
COMPSCI 220
Division:
choose a prime number as the table size n convert keys, k, into integers use the remainder h(k) = k mod n as a hash value of k get the double hashing decrement using the quotient k = max{1, (k/n) mod n}
COMPSCI 220 - AP G. Gimel'farb 14
Lecture 11
Middle-squaring:
choose a middle section of the integer key, k square the chosen section use a middle section of the result as h(k)
Ex.: k = 013402122 mid: 402 4022=161404 mid: h(k) = 6140
Lecture 11 COMPSCI 220 - AP G. Gimel'farb 15
Average numbers of probe addresses examined for a successful (Tss()) and unsuccessful (Tus()) search:
Tss()
SC 1+/2
17
Tus() 0.5(1+(1/(1))2)
Lecture 11
Representation AVL tree Hash table O(1) O(n) O(1) O(1) O(log n) O(1) O(log n) O(1) O(log n) O(1) O(n) O(n log n)**)
18
*) also: Retrieve, Update **)To enumerate a hash table, entries must first be sorted in ascending order of keys that takes O(n log n) time
COMPSCI 220 - AP G. Gimel'farb