SlideShare ist ein Scribd-Unternehmen logo
1 von 13
Downloaden Sie, um offline zu lesen
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;}}
      }

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Hashing
HashingHashing
Hashing
 
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
 
Hashing
HashingHashing
Hashing
 
Hash table
Hash tableHash table
Hash table
 
Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructure
 
4.4 hashing
4.4 hashing4.4 hashing
4.4 hashing
 
Hashing Algorithm
Hashing AlgorithmHashing Algorithm
Hashing Algorithm
 
Hashing
HashingHashing
Hashing
 
Hashing Techniques in Data Structures Part2
Hashing Techniques in Data Structures Part2Hashing Techniques in Data Structures Part2
Hashing Techniques in Data Structures Part2
 
linear probing
linear probinglinear probing
linear probing
 
Hashing algorithms and its uses
Hashing algorithms and its usesHashing algorithms and its uses
Hashing algorithms and its uses
 
Hashing
HashingHashing
Hashing
 
Hashing 1
Hashing 1Hashing 1
Hashing 1
 
Hashing PPT
Hashing PPTHashing PPT
Hashing PPT
 
Hash tables
Hash tablesHash tables
Hash tables
 
Hashing
HashingHashing
Hashing
 
Hash table in data structure and algorithm
Hash table in data structure and algorithmHash table in data structure and algorithm
Hash table in data structure and algorithm
 
Hashing
HashingHashing
Hashing
 
18 hashing
18 hashing18 hashing
18 hashing
 
Chapter 12 ds
Chapter 12 dsChapter 12 ds
Chapter 12 ds
 

Andere mochten auch

Andere mochten auch (19)

Hashing
HashingHashing
Hashing
 
Hashing and Hash Tables
Hashing and Hash TablesHashing and Hash Tables
Hashing and Hash Tables
 
Hashing
HashingHashing
Hashing
 
Concept of hashing
Concept of hashingConcept of hashing
Concept of hashing
 
Ch17 Hashing
Ch17 HashingCh17 Hashing
Ch17 Hashing
 
Hashing
HashingHashing
Hashing
 
Intro to Hash tables
Intro to Hash tablesIntro to Hash tables
Intro to Hash tables
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 
Trees data structure
Trees data structureTrees data structure
Trees data structure
 
Threaded Programming
Threaded ProgrammingThreaded Programming
Threaded Programming
 
Ie
IeIe
Ie
 
Hash table and heaps
Hash table and heapsHash table and heaps
Hash table and heaps
 
Smart antennas
Smart antennasSmart antennas
Smart antennas
 
Dynamic Itemset Counting
Dynamic Itemset CountingDynamic Itemset Counting
Dynamic Itemset Counting
 
Balanced Trees
Balanced TreesBalanced Trees
Balanced Trees
 
Coalesced hashing / Hash Coalescido
Coalesced hashing / Hash CoalescidoCoalesced hashing / Hash Coalescido
Coalesced hashing / Hash Coalescido
 
17 Trees and graphs
17 Trees and graphs17 Trees and graphs
17 Trees and graphs
 
Distributed Hash Table
Distributed Hash TableDistributed Hash Table
Distributed Hash Table
 
08 Hash Tables
08 Hash Tables08 Hash Tables
08 Hash Tables
 

Ähnlich wie Hash tables

Data Structure and Algorithms: What is Hash Table ppt
Data Structure and Algorithms: What is Hash Table pptData Structure and Algorithms: What is Hash Table ppt
Data Structure and Algorithms: What is Hash Table pptJUSTFUN40
 
Regular Expression Cheat Sheet
Regular Expression Cheat SheetRegular Expression Cheat Sheet
Regular Expression Cheat SheetSydneyJohnson57
 
Sienna 9 hashing
Sienna 9 hashingSienna 9 hashing
Sienna 9 hashingchidabdu
 
Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptxAgonySingh
 
Hashing_and_collision.pptx
Hashing_and_collision.pptxHashing_and_collision.pptx
Hashing_and_collision.pptxpunit444kaushik
 

Ähnlich wie Hash tables (6)

Data Structure and Algorithms: What is Hash Table ppt
Data Structure and Algorithms: What is Hash Table pptData Structure and Algorithms: What is Hash Table ppt
Data Structure and Algorithms: What is Hash Table ppt
 
Regular Expression Cheat Sheet
Regular Expression Cheat SheetRegular Expression Cheat Sheet
Regular Expression Cheat Sheet
 
Sienna 9 hashing
Sienna 9 hashingSienna 9 hashing
Sienna 9 hashing
 
Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptx
 
Hashing_and_collision.pptx
Hashing_and_collision.pptxHashing_and_collision.pptx
Hashing_and_collision.pptx
 
4.4 hashing02
4.4 hashing024.4 hashing02
4.4 hashing02
 

Mehr von Chester Hartin

Mehr von Chester Hartin (8)

Xamarin 101
Xamarin 101Xamarin 101
Xamarin 101
 
Proxy pattern
Proxy patternProxy pattern
Proxy pattern
 
Asynchronous programming
Asynchronous programmingAsynchronous programming
Asynchronous programming
 
Elapsed time
Elapsed timeElapsed time
Elapsed time
 
Dependency injection
Dependency injectionDependency injection
Dependency injection
 
Map kit
Map kitMap kit
Map kit
 
Parallel extensions
Parallel extensionsParallel extensions
Parallel extensions
 
Reflection
ReflectionReflection
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;}} }