HASH TABLES. Goal is to store elements k,v at index i = h k

Similar documents
CHAPTER 9 HASH TABLES, MAPS, AND SKIP LISTS

Hash Tables Hash Tables Goodrich, Tamassia

Chapter 9: Maps, Dictionaries, Hashing

Dictionaries-Hashing. Textbook: Dictionaries ( 8.1) Hash Tables ( 8.2)

This lecture. Iterators ( 5.4) Maps. Maps. The Map ADT ( 8.1) Comparison to java.util.map

Dictionaries and Hash Tables

CS2210 Data Structures and Algorithms

Priority Queue Sorting

Maps, Hash Tables and Dictionaries. Chapter 10.1, 10.2, 10.3, 10.5

Elementary Data Structures 2

Hash Tables. Johns Hopkins Department of Computer Science Course : Data Structures, Professor: Greg Hager

Data Structures Lecture 12

CSED233: Data Structures (2017F) Lecture10:Hash Tables, Maps, and Skip Lists

Hashing. Manolis Koubarakis. Data Structures and Programming Techniques

Open Addressing: Linear Probing (cont.)

Introduction to Hashing

Hash Tables. Gunnar Gotshalks. Maps 1

Introduction hashing: a technique used for storing and retrieving information as quickly as possible.

Introducing Hashing. Chapter 21. Copyright 2012 by Pearson Education, Inc. All rights reserved

Today: Finish up hashing Sorted Dictionary ADT: Binary search, divide-and-conquer Recursive function and recurrence relation

CMSC 341 Hashing (Continued) Based on slides from previous iterations of this course

DATA STRUCTURES AND ALGORITHMS

Hashing (Κατακερματισμός)

CMSC 341 Lecture 16/17 Hashing, Parts 1 & 2

Hash Tables. Hashing Probing Separate Chaining Hash Function

Dictionary. Dictionary. stores key-value pairs. Find(k) Insert(k, v) Delete(k) List O(n) O(1) O(n) Sorted Array O(log n) O(n) O(n)

Lecture 4. Hashing Methods

Hashing Techniques. Material based on slides by George Bebis

Introduction. hashing performs basic operations, such as insertion, better than other ADTs we ve seen so far

Fundamental Algorithms

CH 9 : MAPS AND DICTIONARIES

Algorithms and Data Structures

Hashing. Hashing Procedures

csci 210: Data Structures Maps and Hash Tables

Hash Tables Outline. Definition Hash functions Open hashing Closed hashing. Efficiency. collision resolution techniques. EECS 268 Programming II 1

CS 241 Analysis of Algorithms

AAL 217: DATA STRUCTURES

Understand how to deal with collisions

Chapter 27 Hashing. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved.

Hashing. October 19, CMPE 250 Hashing October 19, / 25

Topic HashTable and Table ADT

COSC160: Data Structures Hashing Structures. Jeremy Bolton, PhD Assistant Teaching Professor

HASH TABLES.

Hashing. Dr. Ronaldo Menezes Hugo Serrano. Ronaldo Menezes, Florida Tech

Hash Table and Hashing

General Idea. Key could be an integer, a string, etc e.g. a name or Id that is a part of a large employee structure

HashTable CISC5835, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang Fall 2018

CS1020 Data Structures and Algorithms I Lecture Note #15. Hashing. For efficient look-up in a table

CS 350 : Data Structures Hash Tables

Hash Tables. Hash functions Open addressing. March 07, 2018 Cinda Heeren / Geoffrey Tien 1

DS ,21. L11-12: Hashmap

Symbol Table. Symbol table is used widely in many applications. dictionary is a kind of symbol table data dictionary is database management

Hash Table. Ric Glassey

Hash[ string key ] ==> integer value

Hashing as a Dictionary Implementation

Practical Session 8- Hash Tables

Chapter 27 Hashing. Objectives

Cpt S 223. School of EECS, WSU

CITS2200 Data Structures and Algorithms. Topic 15. Hash Tables

! A Hash Table is used to implement a set, ! The table uses a function that maps an. ! The function is called a hash function.

Tables. The Table ADT is used when information needs to be stored and acessed via a key usually, but not always, a string. For example: Dictionaries

Outline. hash tables hash functions open addressing chained hashing

COMP171. Hashing.

Today s Outline. CS 561, Lecture 8. Direct Addressing Problem. Hash Tables. Hash Tables Trees. Jared Saia University of New Mexico

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

Hash Open Indexing. Data Structures and Algorithms CSE 373 SP 18 - KASEY CHAMPION 1

HASH TABLES. Hash Tables Page 1

5. Hashing. 5.1 General Idea. 5.2 Hash Function. 5.3 Separate Chaining. 5.4 Open Addressing. 5.5 Rehashing. 5.6 Extendible Hashing. 5.

lecture23: Hash Tables

Advanced Algorithmics (6EAP) MTAT Hashing. Jaak Vilo 2016 Fall

Data Structure Lecture#22: Searching 3 (Chapter 9) U Kang Seoul National University

Worst-case running time for RANDOMIZED-SELECT

SFU CMPT Lecture: Week 8

Acknowledgement HashTable CISC4080, Computer Algorithms CIS, Fordham Univ.

TABLES AND HASHING. Chapter 13

Unit #5: Hash Functions and the Pigeonhole Principle

Hash Table. A hash function h maps keys of a given type into integers in a fixed interval [0,m-1]

CSE 332: Data Structures & Parallelism Lecture 10:Hashing. Ruth Anderson Autumn 2018

Data Structures And Algorithms

Hashing. Introduction to Data Structures Kyuseok Shim SoEECS, SNU.

HO #13 Fall 2015 Gary Chan. Hashing (N:12)

CSC263 Week 5. Larry Zhang.

CMSC 341 Hashing. Based on slides from previous iterations of this course

Direct File Organization Hakan Uraz - File Organization 1

Lecture 7: Efficient Collections via Hashing

The dictionary problem

Hashing. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Ch 3.4 The Integers and Division

CS 261 Data Structures

Hash Tables. Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015.

Fast Lookup: Hash tables

Chapter 5 Hashing. Introduction. Hashing. Hashing Functions. hashing performs basic operations, such as insertion,

Tirgul 7. Hash Tables. In a hash table, we allocate an array of size m, which is much smaller than U (the set of keys).

Hashing. 1. Introduction. 2. Direct-address tables. CmSc 250 Introduction to Algorithms

CSE100. Advanced Data Structures. Lecture 21. (Based on Paul Kube course materials)

Lecture 16 More on Hashing Collision Resolution

CSE 214 Computer Science II Searching

CS 10: Problem solving via Object Oriented Programming Winter 2017

CSCD 326 Data Structures I Hashing

Lecture 16. Reading: Weiss Ch. 5 CSE 100, UCSD: LEC 16. Page 1 of 40

Transcription:

CH 9.2 : HASH TABLES 1 ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES FROM JORY DENNY AND MUKULIKA GHOSH

HASH TABLES Sometimes a key can be interpreted or transformed into an address. In this case, we can use an implementation called a hash table for the Map ADT. Hash tables Essentially an array A of size N (either to an element itself or to a bucket ) A Hash function h k [0,N 1], h(k) is referred to as the hash value Example - h k = k mod N Goal is to store elements k,v at index i = h k 2

ISSUES WITH HASH TABLES Issues Collisions - some keys will map to the same index of H (otherwise we have a Direct Address Table). Chaining - put values that hash to same location in a linked list (or a bucket ) Open addressing - if a collision occurs, have a method to select another location in the table. Load factor Rehashing 3

EXAMPLE We design a hash table for a Map storing items (SSN, Name), where SSN (social security number) is a nine-digit positive integer 0 1 2 3 4 025-612-0001 981-101-0002 451-229-0004 Our hash table uses an array of size N = 10,000 and the hash function h k = last four digits of k 9997 9998 9999 200-751-9998 4

HASH FUNCTIONS A hash function is usually specified as the composition of two functions: Hash code: h 5 : keys integers Compression function: h 6 : integers [0, N 1] The hash code is applied first, and the compression function is applied next on the result, i.e., h k = h 6 h 5 k The goal of the hash function is to disperse the keys in an apparently random way 5

COLLISION RESOLUTION WITH CHAINING Collisions occur when different elements are mapped to the same cell Chaining: let each cell in the table point to a linked list of entries that map there Chaining is simple, but requires additional memory outside the table 0 1 025-612-0001 2 3 4 451-229-0004 981-101-0004 6

EXERCISE CHAINING Assume you have a hash table H with N = 9 slots (A[0 8]) and let the hash function be h k = k mod N Demonstrate (by picture) the insertion of the following keys into a hash table with collisions resolved by chaining 5, 28, 19, 15, 20, 33, 12, 17, 10 7

COLLISION RESOLUTION WITH OPEN ADDRESSING - LINEAR PROBING In Open addressing the colliding item is placed in a different cell of the table Linear probing handles collisions by placing the colliding item in the next (circularly) available table cell. So the ith cell checked is: h k, i = h k + i mod N Each table cell inspected is referred to as a probe Colliding items lump together, causing future collisions to cause a longer probe sequence Example: h k = k mod 13 Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in this order 0 1 2 3 4 5 6 7 8 9 10 11 12 41 18 44 59 32 22 31 73 0 1 2 3 4 5 6 7 8 9 10 11 12 8

SEARCH WITH LINEAR PROBING Consider a hash table A that uses linear probing find k We start at cell h k We probe consecutive locations until one of the following occurs An item with key k is found, or An empty cell is found, or N cells have been unsuccessfully probed Algorithm find k 1. i h k 2. p 0 3. repeat 4. c A i 5. if c 6. return null 7. else if c. key() = k 8. return c 9. else 10. i i + 1 mod N 11. p p + 1 12. until p = N 13. return null 9

UPDATES WITH LINEAR PROBING To handle insertions and deletions, we introduce a special object, called AVAILABLE, which replaces deleted elements erase k We search for an item with key k If such an item k, v is found, we replace it with the special item AVAILABLE put k, v We start at cell h(k) We probe consecutive cells until one of the following occurs A cell i is found that is either empty or stores AVAILABLE, or N cells have been unsuccessfully probed 10

EXERCISE OPEN ADDRESSING LINEAR PROBING Linear probing handles collisions by placing the colliding item in the next (circularly) available table cell. So the ith cell checked is: h k, i = h k + i mod N Assume you have a hash table H with N = 11 slots (A[0 10]) and let the hash function be h k = k mod N Demonstrate (by picture) the insertion of the following keys into a hash table with collisions resolved by linear probing. 10, 22, 31, 4, 15, 28, 17, 88, 59 11

COLLISION RESOLUTION WITH OPEN ADDRESSING QUADRATIC PROBING Linear probing has an issue with clustering Another strategy called quadratic probing uses a hash function h k, i = h k + i 6 mod N for i = 0, 1,, N 1 This can still cause secondary clustering 12

EXERCISE OPEN ADDRESSING QUADRATIC PROBING Quadratic probing uses a hash function: h k, i = h k + i 6 mod N for probes i = 0, 1,, N 1 Assume you have a hash table H with N = 11 slots (A[0 10]) and let the hash function be h k = k mod N Demonstrate (by picture) the insertion of the following keys into a hash table with collisions resolved by quadradic probing. 10, 22, 31, 4, 15, 28, 17, 88, 59 13

COLLISION RESOLUTION WITH OPEN ADDRESSING - DOUBLE HASHING Double hashing uses a secondary hash function h 6 (k) and handles collisions by placing an item in the first available cell of the series h k, i = h 5 k + ih 6 k mod N for i = 0, 1,, N 1 The secondary hash function h 6 k cannot have zero values (why not?) The table size N must be a prime to allow probing of all the cells Common choice of compression map for the secondary hash function: h 6 k = q k mod q where q < N q is a prime The possible values for h 6 k are 1, 2,, q 14

EXERCISE OPEN ADDRESSING DOUBLE HASHING Double hashing uses a secondary hash function h 6 (k) and handles collisions by placing an item in the first available cell of the series h k, i = h 5 k + ih 6 k mod N for i = 0, 1,, N 1 Assume you have a hash table H with N = 11 slots (A[0 10]) and let the primary and secondary hash functions be h 1 k = k mod N h 6 k = 1 + (k mod N 1 ) Demonstrate (by picture) the insertion of the following keys into a hash table with collisions resolved by quadradic probing. 10, 22, 31, 4, 15, 28, 17, 88, 59 15

PERFORMANCE OF HASHING In the worst case, searches, insertions and removals on a hash table take O n time The worst case occurs when all the keys inserted into the map collide The load factor λ = Z affects the performance of a [ hash table Assuming that the hash values are like random numbers, it can be shown that the expected number of probes for an insertion with open addressing is 1 1 λ = 1 1 n \ = 1 N n N \ = N N n N The expected running time of all the Map ADT operations in a hash table is O 1 In practice, hashing is very fast provided the load factor is not close to 100% Applications of hash tables Small databases Compilers Browser caches 16

UNIFORM HASHING ASSUMPTION The probe sequence of a key k is the sequence of slots probed when looking for k In open addressing, the probe sequence is h k, 0, h k, 1,, h k, N 1 Uniform Hashing Assumption Each key is equally likely to have any one of the N! permutations of {0, 1,,N 1} as is probe sequence Note: Linear probing and double hashing are far from achieving Uniform Hashing Linear probing: N distinct probe sequences Double Hashing: N 6 distinct probe sequences 17

PERFORMANCE OF UNIFORM HASHING Theorem: Assuming uniform hashing and an open-address hash table with load factor λ = Z < 1, the expected [ number of probes in an unsuccessful search is at most 5. 5`a Exercise: compute the expected number of probes in an unsuccessful search in an open address hash table with λ = 5, λ = b dd, and λ =. 6 c 5ee 18

HASH CODES Memory address: We reinterpret the memory address of the key object as an integer Good in general, except for numeric and string keys Integer cast: We reinterpret the bits of the key as an integer Suitable for keys of length less than or equal to the number of bits of the integer type (e.g., byte, short, int and float in C++) Component sum: We partition the bits of the key into components of fixed length (e.g., 16 or 32 bits) and we sum the components (ignoring overflows) Suitable for numeric keys of fixed length greater than or equal to the number of bits of the integer type (e.g., long and double in C++) 19

HASH CODES Polynomial accumulation: We partition the bits of the key into a sequence of components of fixed length (e.g., 8, 16 or 32 bits) a e a 5 a Z`5 We evaluate the polynomial p z = a e + a 5 z + a 6 z 6 + + a Z`5 z Z`5 at a fixed value z, ignoring overflows Especially suitable for strings (e.g., the choice z = 33 gives at most 6 collisions on a set of 50,000 English words) Cyclic Shift: Like polynomial accumulation except use bit shifts instead of multiplications and bitwise or instead of addition Can be used on floating point numbers as well by converting the number to an array of characters 20

COMPRESSION FUNCTIONS Division: h 6 k = k mod N The size N of the hash table is usually chosen to be a prime (based on number theory principles and modular arithmetic) Multiply, Add and Divide (MAD): h 6 k = ak + b mod N a and b are nonnegative integers such that a mod N 0 Otherwise, every integer would map to the same value b 21

ON REHASHING Keeping the load factor low is vital for performance When resizing the table: Reallocate space for the array Design a new hash function (new parameters) for the new array size For each item you reinsert it into the table 22

SUMMARY MAPS/DICTIONARIES (SO FAR) put(k, v) find(k) Space Log File O(1) O(n) O(n) Direct Address Table (map only) O(1) O(1) O(N) Lookup Table (ordered map/dictionary) O(n) O(log n) O(n) Hashing (chaining) O(1) O(n/N) O(n + N) Hashing (open addressing) O 1 1 n N O 1 1 n N O(N) 23