80 likes | 285 Views
Lab 6. Hash Function. Signature function f ( x ) Uniqueness Brevity Efficiency Order-preservation String x Digest f ( x ) Shorter. Hash Applications. Authentication Digital Signatures Efficient Storage Hash Table. Hash Table. Database structure Searchable by equality.
E N D
Hash Function • Signature function f(x) • Uniqueness • Brevity • Efficiency • Order-preservation • String x Digestf(x) • Shorter
Hash Applications • Authentication • Digital Signatures • Efficient Storage • Hash Table
Hash Table • Database structure • Searchable by equality. • Suppose you have a hash function h that produces |H|-bit hashes. • Make a size 2|H| array. • Add(x): data[h(x)] = x • Find(x): if data[h(x)] == x return x • Else return null.
Hash Table • But… • |X| > |H| • h(a) = h(b) • Rare! • 1000 random strings • Only 1 or 2 are likely to be someone’s name. • Maybe you actually store these in a linked list • Add(x): data[h(x)].add(x) • Find(x): if data[h(x)].contains(x) return x • Else return null.
Key/Value Pair • Don’t actually store x… • void Add(key,value): data[h(key)].add(value) • value Find(key): if data[h(key)].contains(key) • return data[h(key)].find(key) • else return null
Hash Function • “Division method” • h(x) = x mod m • “Multiplication method” • h(x) = m(xA mod l)
Trie • Method of storing data in a tree based on its hash value. • Path from the root to leaf x is some prefix of h(x) • (Left = 0, Right = 1)