SlideShare a Scribd company logo
Hash Table
  Code Review 1/20/11




                        S
Outline


S Hash Table Overview

S Hashing Overview

S Add Items

S Remove Items

S Search For Items

S Enumerate Items
Hash Table Overview


S Associative Array
  S Storage of Key / Value Pairs
  S Like an array, but the index can be any comparable type

S Each Key is Unique, though Keys can point to the same
  value

S The Key Type is mapped to an Index
Hashed???

S Hashing derives a fixed size result from an input
  S every hash returns same size and type

S Stable
  S The same input generates the same output ALWAYS

S Uniform
  S The hash value use should be uniformly distributed through available
      space (though impossible to have perfect uniformity)

S Efficient
  S The cost of generating a hash must be balanced with application needs

S Secure
  S The cost of finding data that produces a given hash is prohibitive
Hashing A String

    S Naïve implementation
      S Summing the ASCII value for each character

F        O          O                    102       111    111   324


    S Pros
      S Stable
      S Efficient

    S Cons
      S Not Uniform
         S   AdditiveHash(“foo”) = AdditiveHash(“oof ”)
       S Not Secure
Hashing A String

S Somewhat better
  S “Folds” bytes of every four characters into an integer (32bit)
   Lore          m ip          sum            dolo         r
   170199844     1885937773    54404403       1869377380   114
                 -707031079    -162986676     1706390704   1706390818

S Pros
  S Stable, Efficient and better uniformity

S Cons
  S Not secure (can be treated essentially as additive)
Hashing Functions


S There are lots of good hashing algorithms, you don’t have to write
   your own.
S Pick the right hash for the job at hand (all these are available in the
   .net framework)

       Name        Stable      Uniform     Efficient   Secure
       Additive    ✔                       ✔
       Folding     ✔           ✔           ✔
       CRC32       ✔           ✔           ✔
       MD5         ✔           ✔
       SHA-2       ✔           ✔                       ✔
Hash Table Overview


S Adding Jane
  S int index = GetIndex(Jane.Name);
  S _array[index] = Jane;

S What does GetIndex() do? It hashes the string
Simple Hash Examples
Handling Collisions


S Two distinct items have the same hash value
  S Items are assigned to the same index in the hash table

S Two common strategies
  S Open Addressing
       S   Moving to next index in the table
   S   Chaining
       S   Storing items into a linked list

S Frequency of Collisions
  S # of slots in the array
  S # of filled slots in the array
Finding Items

S Items are found by key
  S Person p = HashTable.Find(“Jane”)

S Open Addressing
  S Get the index of the key
  S If the value != null
     S   If keys match, return value
     S   If keys don’t match, check next index

S Chaining
  S Get index of the key
  S Find index in the list
Removing Items

S Items are removed by key
  S HashTable.Remove(“Jane”)

S Open Addressing
  S Get index of the key
  S If value != null
      S   If keys match, remove
      S   If keys don’t match, check next index

S Chaining
  S Get index of the key
  S Remove item from the linked list
Enumerating Keys & Values


S Open Addressing
  S Foreach(item in array)
      {if(item!=null) return item;}

S Chaining
  S Foreach(list in array)
      {if(list !=null)
       {foreach(item in list){return item;}}
      }

More Related Content

PPS
Ds 8
PPT
Hashing
PPTX
Hashing Technique In Data Structures
PPT
PDF
Application of hashing in better alg design tanmay
PPT
Data Structure and Algorithms Hashing
PPTX
Hashing data
Ds 8
Hashing
Hashing Technique In Data Structures
Application of hashing in better alg design tanmay
Data Structure and Algorithms Hashing
Hashing data

What's hot (20)

PPTX
Hashing
PDF
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
PPT
Hashing
PPT
Hash table
PPTX
Hashing in datastructure
PPT
4.4 hashing
PDF
Hashing Algorithm
PPTX
Hashing Techniques in Data Structures Part2
PPTX
linear probing
PPTX
Hashing algorithms and its uses
PPT
Hashing
PPTX
Hashing 1
PPT
Hashing PPT
PPT
Hash tables
PPTX
Hashing
PPTX
Hash table in data structure and algorithm
PPT
Hashing
PPT
18 hashing
PPT
Chapter 12 ds
Hashing
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing
Hash table
Hashing in datastructure
4.4 hashing
Hashing Algorithm
Hashing Techniques in Data Structures Part2
linear probing
Hashing algorithms and its uses
Hashing
Hashing 1
Hashing PPT
Hash tables
Hashing
Hash table in data structure and algorithm
Hashing
18 hashing
Chapter 12 ds
Ad

Viewers also liked (19)

PPT
Hashing
PDF
Hashing and Hash Tables
PPT
Hashing
PPT
Concept of hashing
PPT
Ch17 Hashing
PDF
PPTX
Intro to Hash tables
PPTX
Trees (data structure)
PPTX
Trees data structure
PPT
Threaded Programming
PPTX
Hash table and heaps
PPT
Smart antennas
PPTX
Dynamic Itemset Counting
PDF
Balanced Trees
PDF
Coalesced hashing / Hash Coalescido
PPT
17 Trees and graphs
PPT
Distributed Hash Table
PDF
08 Hash Tables
Hashing
Hashing and Hash Tables
Hashing
Concept of hashing
Ch17 Hashing
Intro to Hash tables
Trees (data structure)
Trees data structure
Threaded Programming
Hash table and heaps
Smart antennas
Dynamic Itemset Counting
Balanced Trees
Coalesced hashing / Hash Coalescido
17 Trees and graphs
Distributed Hash Table
08 Hash Tables
Ad

Similar to Hash tables (20)

PPTX
Data Structures and Agorithm: DS 24 Hash Tables.pptx
PPT
4.4 hashing02
PDF
Sienna 9 hashing
PPTX
hashing in data strutures advanced in languae java
PPTX
Lec12-Hash-Tables-27122022-125641pm.pptx
PPTX
18. Dictionaries, Hash-Tables and Set
PDF
Skiena algorithm 2007 lecture06 sorting
PPTX
Hashing techniques, Hashing function,Collision detection techniques
PPT
Hashing in Data Structure and analysis of Algorithms
PPT
Analysis Of Algorithms - Hashing
ZIP
Hashing
PPTX
hashing1.pptx Data Structures and Algorithms
PDF
hash_tables
PPTX
Hashing.pptx
PPT
11_hashtable-1.ppt. Data structure algorithm
PPT
Hash presentation
PPTX
Data Structures-Topic-Hashing, Collision
PDF
Hash pre
PDF
Algorithm chapter 7
PPT
lecture10.ppt
Data Structures and Agorithm: DS 24 Hash Tables.pptx
4.4 hashing02
Sienna 9 hashing
hashing in data strutures advanced in languae java
Lec12-Hash-Tables-27122022-125641pm.pptx
18. Dictionaries, Hash-Tables and Set
Skiena algorithm 2007 lecture06 sorting
Hashing techniques, Hashing function,Collision detection techniques
Hashing in Data Structure and analysis of Algorithms
Analysis Of Algorithms - Hashing
Hashing
hashing1.pptx Data Structures and Algorithms
hash_tables
Hashing.pptx
11_hashtable-1.ppt. Data structure algorithm
Hash presentation
Data Structures-Topic-Hashing, Collision
Hash pre
Algorithm chapter 7
lecture10.ppt

More from Chester Hartin (8)

PPTX
Xamarin 101
PPT
Proxy pattern
PPTX
Asynchronous programming
PPTX
Elapsed time
PPT
Dependency injection
PPTX
PPTX
Parallel extensions
PPT
Reflection
Xamarin 101
Proxy pattern
Asynchronous programming
Elapsed time
Dependency injection
Parallel extensions
Reflection

Hash tables

  • 1. Hash Table Code Review 1/20/11 S
  • 2. Outline S Hash Table Overview S Hashing Overview S Add Items S Remove Items S Search For Items S Enumerate Items
  • 3. Hash Table Overview S Associative Array S Storage of Key / Value Pairs S Like an array, but the index can be any comparable type S Each Key is Unique, though Keys can point to the same value S The Key Type is mapped to an Index
  • 4. Hashed??? S Hashing derives a fixed size result from an input S every hash returns same size and type S Stable S The same input generates the same output ALWAYS S Uniform S The hash value use should be uniformly distributed through available space (though impossible to have perfect uniformity) S Efficient S The cost of generating a hash must be balanced with application needs S Secure S The cost of finding data that produces a given hash is prohibitive
  • 5. Hashing A String S Naïve implementation S Summing the ASCII value for each character F O O 102 111 111 324 S Pros S Stable S Efficient S Cons S Not Uniform S AdditiveHash(“foo”) = AdditiveHash(“oof ”) S Not Secure
  • 6. Hashing A String S Somewhat better S “Folds” bytes of every four characters into an integer (32bit) Lore m ip sum dolo r 170199844 1885937773 54404403 1869377380 114 -707031079 -162986676 1706390704 1706390818 S Pros S Stable, Efficient and better uniformity S Cons S Not secure (can be treated essentially as additive)
  • 7. Hashing Functions S There are lots of good hashing algorithms, you don’t have to write your own. S Pick the right hash for the job at hand (all these are available in the .net framework) Name Stable Uniform Efficient Secure Additive ✔ ✔ Folding ✔ ✔ ✔ CRC32 ✔ ✔ ✔ MD5 ✔ ✔ SHA-2 ✔ ✔ ✔
  • 8. Hash Table Overview S Adding Jane S int index = GetIndex(Jane.Name); S _array[index] = Jane; S What does GetIndex() do? It hashes the string
  • 10. Handling Collisions S Two distinct items have the same hash value S Items are assigned to the same index in the hash table S Two common strategies S Open Addressing S Moving to next index in the table S Chaining S Storing items into a linked list S Frequency of Collisions S # of slots in the array S # of filled slots in the array
  • 11. Finding Items S Items are found by key S Person p = HashTable.Find(“Jane”) S Open Addressing S Get the index of the key S If the value != null S If keys match, return value S If keys don’t match, check next index S Chaining S Get index of the key S Find index in the list
  • 12. Removing Items S Items are removed by key S HashTable.Remove(“Jane”) S Open Addressing S Get index of the key S If value != null S If keys match, remove S If keys don’t match, check next index S Chaining S Get index of the key S Remove item from the linked list
  • 13. Enumerating Keys & Values S Open Addressing S Foreach(item in array) {if(item!=null) return item;} S Chaining S Foreach(list in array) {if(list !=null) {foreach(item in list){return item;}} }