Lecture 17 - Hashing - Symbol Table ADT Hashing Symbol...

Info iconThis preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Symbol Table ADT Hashing Symbol Table Implementations Binary Search Insert O(1) O(1) O(N) O(log(N)) Search O(1) O(N) O(log(N)) O(log(N)) Key-Indexed Array Sequential Search Balanced BST Recall the Key-Indexed Array ● It uses the search key as an array index – No comparison operation. ● Constraints of Key values – Distinct – Non-negative integers – Range of should be small Hash Tables ● Similar to Key-index array – Maps search key to array index (also called table address ) ● Main Components – Hash function that transforms the search key to a table address – Collision-resolution for keys that map to the same address ● Balance Between speed and memory – More key mappings , Less collision (faster) – More key mappings , More memory Hash Function Characteristics ● Hash function h(k) – k = Key data – Map k to M table addresses – Transform k into integers in the range [ , M - 1 ] ● Ideal hash functions – Easy to compute – Every outcome is equally likely (random function) Hash Function 1 (float data) ● Keys: – Data: floating point – Range: (0,1) greater than 0, less than 1 ● Hash function: – floor(k * M) – Round down to integer (remove decimal) ● Example: – M = 50 k: 0.00 to 1.00 Key 1: 0.98 Table Address: 49 Key 1: 0.456 Table Address: 22 Key 1: 0.01 Table Address: Key 1: 0.32 Table Address: 16 Hash Function 2 (float data) ● Keys: – Data: floating point – Range: (s,t) greater than s, less than t ● Hash function: – M * (k – s)/(t - s) – (k – s)/(t – s) is used to generate a number ( , 1 ) ● Example: – M = 50 k: 0.50 to 1.00 Key 1: 0.98 Table Address: 48 Key 1: 0.651 Table Address: 15 Key 1: 0.71 Table Address: 21 Key 1: 0.532 Table Address: 3 Hash Function 3 (w-bit integer) ● Keys: – Data: w-bit integers – Range: [0, 2 w ) ● Hash function: – M * k/2 w watch out for data overflow (data range) – M * (k>>w) ● Example: – M = 50 k: 8-bit integers [0, 256) Key 1: 5 Table Address: Key 1: 125 Table Address: 24 Key 1: 65 Table Address: 12 Key 1: 98 Table Address: 19 Hash Function 4 (w-bit integer) ● Keys: – Data: w-bit integers – Range: [0, 2 w ) ● Hash function: – k mod M (k % M) – a.k.a Modular Hash Function ● Example: – M = 50 k: 8-bit integers [0, 256) Key 1: 5 Table Address: 5 Key 1: 125 Table Address: 25 Key 1: 65 Table Address: 15 Key 1: 98 Table Address: 48 Hash Function 5 (float data) ● Keys: – Data: floating point – Range: (s,t) greater than s, less than t ● Hash function: – floor(2 w * (k – s)/(t - s)) mod M – floor(2 w * (k – s)/(t – s)) => range [0, 2 w ) ● Example: – M = 50 k: 0.5 to 1.00 (use w=8) Key 1: 0.98 Table Address: 45 Key 1: 0.651 Table Address: 27 Key 1: 0.71 Table Address: 7 Key 1: 0.532 Table Address: 15 Modular hash function ● Reduces the probability of collision ● Table size, M, is set to a prime number ● Alternative Function – – Use an arbitrary value for ● Popular choice: golder ratio h  k = ⌊  k ⌋ modM  = 0.618033... Handling long keys...
View Full Document

{[ snackBarMessage ]}

Page1 / 118

Lecture 17 - Hashing - Symbol Table ADT Hashing Symbol...

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online