Download presentation
Presentation is loading. Please wait.
Published byOscar Garrett Modified over 9 years ago
1
CpSc 3220 File and Database Processing Hashing
2
Exercise – Build a B + - Tree Construct an order-4 B + -tree for the following set of key values: (2, 3, 5, 7, 11, 17, 9, 6, 29, and 4) Assume the tree is initially empty and values are added in ascending order. Now delete keys 2, 5, and 17
3
Objectives Survey Hashing Concepts Investigate Hashing Algorithms Study Collision Reduction Analyze Performance Investigate File Deterioration Look at Patterns of Access
4
Schematic View of Hash File 0 0. 1 0 1 0. Record for Key Record for Keyx hash Key
5
Basic Hashing Concepts A hash file contains a fixed number of record spaces Each record space is of a fixed size A hash function determines the address of a record space for a given key A hash function may give same address for two different records A single address for different keys is called a collision. Different keys that give identical addresses are called synonyms. A hash function that gives no collisions is called a perfect hash function.
6
Objectives for a Hash File Package Keep collisions ‘low’ – Spread out (distribute) records over address space – Use extra memory (increase address space) – Put more than one record per address Handle collisions efficiently
7
Outline for a Simple Hashing Algorithm 1.Put Key in numerical form 2.Fold and Add to reduce numerical form to ‘integer’ size 3.Divide by the size of the address space and use remainder as RRN address (offset) of Key
8
Simple Hash Function (when Key is an alphanumeric string) int Hash (string key) { int sum = 0; int len = strlen(key); if (len % 2 == 1) key = concat(key, ‘ ‘)// make len even for (int j = 0; j < len; j += 2) sum = (sum + 256 * (ord)key[j] + (ord)key[j+1]) % FILE_SIZE; return sum; }
9
Hash Function Distribution Uniform (Perfect) Random Worse than random We will look at random distributions
10
Predicting Record Distribution If r records are distributed randomly into N spaces, the probability that a given address will have exactly x records assigned to it is p(x) = (r!/( (r-x)! x! ) )/(1-(1/N)) r-x (1/N) x p(0) – probability that an address is not used p(1) – probability that no collision occurs p(2) – probability that 1 collision occurs etc. Difficult to compute for large values of r and N.
11
Poisson’s Function For large values of r and N, p(x) can be approximately by this function p(x) = ( (r/N) x e -(r/N) ) / x! The value r/N is the ratio of the number of records to the number of address spaces. If only one record is placed in each space it is a measure of the percent of storage space that will be used (the packing density).
12
From Page 484 of File Structures by Folk, Zoellick, and Riccardi
13
Collision Resolution Using Progressive Overflow ( Linear Probing) 0 0. 1 1 1 0. Record for Key0 Record for Key1 Record for Key2 hash Key3 H i = (hash(key) + i) mod TableSize
14
ASL = (total # probes)/(# of Recs)
15
Address Spaces Can Hold More Than One Record 2 1 2 0 2 1 0 Key a Key r Key k Key x Key w Key d Key b Key t Packing Density = r/(bN)Address Density = r/N
16
Implementation Issues Loading a Hash File Deletions – Tombstones – Performance Effects
17
Other Collision Resolution Techniques Quadratic Hashing – H(i) = (hash(key) + i 2 ) mod TS Double Hashing – H(i) = (hash(key) + f(i)) mod TS where f(i) = i*hash2(key) – Note that hash2(key) must never be zero Separate Overflow Area Chained Overflow with Separate Overflow Area Scatter Tables
18
Patterns of Record Access 20 percent of records account for 80 percent of activity Most active records must be in home address or performance deteriorates
19
Summary Hashing provides O(1) direct access performance. If hash function gives collisions ASL may increase. Collisions can be reduced by: – Spreading out records (choosing a better hash fct) – Using extra memory – Using buckets Poisson Distribution allows us to analyze hash file performance Better overflow handling can reduce ASL Record Deletion requires special handling Consider record access patterns Hashing does not provide efficient sequential access Hashing requires that we fix file size in advance
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.