SlideServe
Menu
Préférences
© SlideServe · v2026

Hash Tables

temima avatar
12 diapositives · 584 Views · Aug 10, 2014

Hash Tables. Hash tables. Definition: A data structure that uses a hash function to map keys into index of an array element. k1. k5. k2. k3. k4. Some properties of hash table. Size of hash table (Example will be shown.)

Hash Tables - slide 1
01 / 12Hash Tables
1 / 17

Télécharger la présentation

Hash Tables
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Hash Tables

  2. Hash tables • Definition: A data structure that uses a hash function to map keys into index of an array element. k1 k5 k2 k3 k4

  3. Some properties of hash table • Size of hash table (Example will be shown.) • Hash function: map keys into index of an array element. (To be continued…) • Multiplication Hash • Division Hash • Input to build a hash table: array of keys to store in the hash table • int [] input = {1,2,3,4,5,6,7,8} 1 2 / 3 4 / 5 6 / 7 8 /

  4. Example • Hash table size is 10 20 110 / 103 13 53 / 10 69 /

  5. Division Hash • h1(k) = k mod m • Returns the index of array • k is the key • m is the size of the hash table. • Good values of m: prime numbers smaller than and closest to the size of the input. See Table 1. • Java syntax of mod is %. Table 1.

  6. Multiplication hash • h2(k) = floor(m (kA mod 1) ) • m is size of hash table • Good values of m: prime numbers smaller than and closest to the size of the input. See table 1. • k is key • A = 0.61803 (Came from (sqrt(5) - 1)/2 ) • Hints: Use the decimal in your program is better, it may reduce your bugs.

  7. Collisions • When hashing a key, if collision happens the new key is stored in the linked list in that location • Number of collisions of a location = Number of elements in that location - 1 # of collisions = 2-1=1 20 110 / # of collisions = 3-1=2 103 13 53 /

  8. Collision Metrics • maxCollisions: Maximum number of collisions of all locations in a hash table • minCollisions: Minimum number of collisions of all locations in a hash table • totalCollisions: Total collisions of all locations in a hash table • Examples on the next slide

  9. maxCollisions = 2 • minCollisions = 1 • (** Note that the minCollisions will be at least 1 if there exists collisions in some locations, even if there are locations with 0 collisions. If there is no collisions at all, return 0. ) • totalCollisions = 4 # of collisions = 1 20 110 / # of collisions = 2 103 13 53 / 105 15 / # of collisions = 1

  10. Discussion • Why metrics? • It can tell us which hash is better according to the collision metrics • Why 3 metrics, why not just measure totalCollisions? • Let’s see an example.

  11. Which hash table is better? 20 110 / 103 13 / Hash table 1: totalCollisions = 4 103 13 / 103 13 / 20 110 103 13 103 / 13 / 103 / 13 / Hash table 2: totalCollisions = 4

  12. We not only want less collisions, but also want to distribute the collisions evenly into the hash table. That is why hash table 1 is better than hash table 2. • This lab is to implement two hash functions, division and multiplication and use metrics of collisions to demonstrate which hash is better.