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

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

AAL 217: DATA STRUCTURES

Hashing Techniques. Material based on slides by George Bebis

COMP171. 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

Hashing. Hashing Procedures

Lecture 4. Hashing Methods

Open Addressing: Linear Probing (cont.)

Cpt S 223. School of EECS, WSU

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

DATA STRUCTURES/UNIT 3

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

Understand how to deal with collisions

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.

UNIT III BALANCED SEARCH TREES AND INDEXING

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

Data and File Structures Chapter 11. Hashing

Hash Tables. Hashing Probing Separate Chaining Hash Function

Hash[ string key ] ==> integer value

Data Structures And Algorithms

Hash Table and Hashing

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

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

Worst-case running time for RANDOMIZED-SELECT

Module 3: Hashing Lecture 9: Static and Dynamic Hashing. The Lecture Contains: Static hashing. Hashing. Dynamic hashing. Extendible hashing.

Question Bank Subject: Advanced Data Structures Class: SE Computer

Data Structures - CSCI 102. CS102 Hash Tables. Prof. Tejada. Copyright Sheila Tejada

TABLES AND HASHING. Chapter 13

Algorithms in Systems Engineering ISE 172. Lecture 12. Dr. Ted Ralphs

Chapter 11: Indexing and Hashing

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

Chapter 27 Hashing. Objectives

Hashing for searching

Chapter 12: Indexing and Hashing. Basic Concepts

Dynamic Dictionaries. Operations: create insert find remove max/ min write out in sorted order. Only defined for object classes that are Comparable

More on Hashing: Collisions. See Chapter 20 of the text.

Chapter 12: Indexing and Hashing

Data and File Structures Laboratory

9/24/ Hash functions

Structures, Algorithm Analysis: CHAPTER 5: HASHING

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

HASH TABLES cs2420 Introduction to Algorithms and Data Structures Spring 2015

Chapter 12: Indexing and Hashing

III Data Structures. Dynamic sets

Successful vs. Unsuccessful

Algorithms and Data Structures

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

CS301 - Data Structures Glossary By

DATA STRUCTURES AND ALGORITHMS

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

CHAPTER 9 HASH TABLES, MAPS, AND SKIP LISTS

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

HASH TABLES. Hash Tables Page 1

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

CSE 214 Computer Science II Searching

CS 310 Hash Tables, Page 1. Hash Tables. CS 310 Hash Tables, Page 2

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

Chapter 11: Indexing and Hashing

Hash Tables. Gunnar Gotshalks. Maps 1

BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015

Algorithms with numbers (2) CISC4080, Computer Algorithms CIS, Fordham Univ.! Instructor: X. Zhang Spring 2017

Algorithms with numbers (2) CISC4080, Computer Algorithms CIS, Fordham Univ. Acknowledgement. Support for Dictionary

Hash Tables and Hash Functions

Module 5: Hashing. CS Data Structures and Data Management. Reza Dorrigiv, Daniel Roche. School of Computer Science, University of Waterloo

CSCD 326 Data Structures I Hashing

Hash table basics mod 83 ate. ate. hashcode()

Chapter 11: Indexing and Hashing

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

Cuckoo Hashing for Undergraduates

Kathleen Durant PhD Northeastern University CS Indexes

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

(i) It is efficient technique for small and medium sized data file. (ii) Searching is comparatively fast and efficient.

CSE373: Data Structures & Algorithms Lecture 17: Hash Collisions. Kevin Quinn Fall 2015

CSE 5311 Notes 5: Hashing

Database System Concepts, 6 th Ed. Silberschatz, Korth and Sudarshan See for conditions on re-use

Selection Queries. to answer a selection query (ssn=10) needs to traverse a full path.

Topic HashTable and Table ADT

SFU CMPT Lecture: Week 8

Outline for Today. Towards Perfect Hashing. Cuckoo Hashing. The Cuckoo Graph. Analysis of Cuckoo Hashing. Reducing worst-case bounds

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

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

Hashing file organization

4.1 COMPUTATIONAL THINKING AND PROBLEM-SOLVING

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

CS 3114 Data Structures and Algorithms READ THIS NOW!

Why do we need hashing?

Data Structures. Topic #6

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

We assume uniform hashing (UH):

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

Database Applications (15-415)

Chapter 12: Query Processing. Chapter 12: Query Processing

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

CS 241 Analysis of Algorithms

Lecture 10 March 4. Goals: hashing. hash functions. closed hashing. application of hashing

Chapter 20 Hash Tables

CSIT5300: Advanced Database Systems

Hashed-Based Indexing

Data Structures (CS 1520) Lecture 23 Name:

Table ADT and Sorting. Algorithm topics continuing (or reviewing?) CS 24 curriculum

Transcription:

Introduction Chapter 5 Hashing hashing performs basic operations, such as insertion, deletion, and finds in average time 2 Hashing a hash table is merely an of some fixed size hashing converts into locations in a hash table searching on the key becomes something like array lookup hashing is typically a many-to-one map: multiple keys are mapped to the same array index mapping multiple keys to the same position results in a that must be resolved two parts to hashing: a hash function, which transforms keys into array indices a collision resolution procedure Hashing Functions let be the set of search keys hash functions map into the set of in the hash table ideally, distributes over the slots of the hash table, to minimize collisions if we are hashing items, we want the number of items hashed to each location to be close to example: Library of Congress Classification System hash function if we look at the first part of the call numbers (e.g., E470, PN1995) collision resolution involves going to the stacks and looking through the books almost all of CS is hashed to QA75 and QA76 (BAD) 3 4

Hashing Functions suppose we are storing a set of nonnegative integers given, we can obtain hash values between 0 and 1 with the hash function when is divided by fast operation, but we need to be careful when choosing example: if, is just the lowest-order bits of are all the hash values equally likely? choosing to be a not too close to a power of 2 works well in practice Hashing Functions we can also use the hash function below for floating point numbers if we interpret the bits as an two ways to do this in C, assuming long int and double types have the same length first method uses C to accomplish this task 5 6 Hashing Functions we can also use the hash function below for floating point numbers if we interpret the bits as an integer (cont.) second uses a, which is a variable that can hold objects of different types and sizes Hashing Functions we can hash strings by combining a hash of each is an additional parameter we get to choose if is larger than any character value, then this approach is what you would obtain if you treated the string as a base- 7 8

Hashing Functions Hashing Functions K&R suggest a slightly simpler hash function, corresponding to we can use the idea for strings if our search key has parts, say, street, city, state: hash = ((street * R + city) % M) * R + state) % M; same ideas apply to hashing vectors Weiss suggests 9 10 Hash Functions Hash Functions the choice of parameters can have a effect on the results of hashing compare the text's string hashing algorithm for different pairs of and plot of the number of words hashed to each hash table location; we use the American dictionary from the aspell program as data (305,089 words) example: good: words are 11 12

Hash Functions Hash Functions example: very bad example: better 13 14 Hash Functions Collision Resolution example: bad hash table collision occurs when elements hash to the in the table various for dealing with collision separate chaining open addressing linear probing other methods 15 16

Separate Chaining separate chaining keep a list of all elements that to the same location each location in the hash table is a example: first 10 squares Separate Chaining insert, search, delete in lists all proportional to of linked list insert new elements can be inserted at duplicates can increment of list other structures could be used instead of lists binary search tree another hash table linked lists good if table is and hash function is good 17 18 Separate Chaining how long are the linked lists in a hash table? value: where is the number of keys and is the size of the table is it reasonable to assume the hash table would exhibit this behavior? load factor average length of a list time to search: function + time to unsuccessful search: successful search: time to evaluate the hash the list Separate Chaining observations more important than table size general rule: make the table as large as the number of elements to be stored, keep table size prime to ensure good 19 20

Separate Chaining declaration of hash structure Separate Chaining hash member function 21 22 Separate Chaining routines for separate chaining Separate Chaining routines for separate chaining 23 24

Open Addressing linked lists incur extra costs time to for new cells effort and complexity of defining second data structure a different collision strategy involves placing colliding keys in nearby slots if a collision occurs, try cells until an empty one is found bigger table size needed with load factor should be below three common strategies linear probing quadratic probing double hashing Linear Probing linear probing insert operation when is hashed, if slot is open, place there if there is a collision, then start looking for an empty slot starting with location in the hash table, and proceed through,, 0, 1, 2, wrapping around the hash table, looking for an empty slot search operation is similar checking whether a table entry is vacant (or is one we seek) is called a 25 26 Linear Probing Linear Probing example: add 89, 18, 49, 58, 69 with and as long as the table is, a vacant cell can be found but time to locate an empty cell can become large blocks of occupied cells results in primary deleting entries leaves some entries may no longer be found may require moving many other entries expected number of probes for search hits: for insertion and search misses: for, these values are and, respectively 27 28

Linear Probing Quadratic Probing performance of linear probing (dashed) vs. more random collision resolution adequate up to Successful, Unsuccessful, Insertion quadratic probing eliminates collision function is quadratic example: add 89, 18, 49, 58, 69 with and 29 30 Quadratic Probing in linear probing, letting table get nearly greatly hurts performance quadratic probing no of finding an empty cell once the table gets larger than half full at most, of the table can be used to resolve collisions if table is half empty and the table size is prime, then we are always guaranteed to accommodate a new element could end up with situation where all keys map to the same table location Quadratic Probing quadratic probing collisions will probe the same alternative cells clustering causes less than half an extra probe per search 31 32

Double Hashing double hashing apply a second hash function to and probe across function must never evaluate to make sure all cells can be probed Double Hashing double hashing example with is a prime smaller than table size insert 89, 18, 49, 58, 69 33 34 Double Hashing double hashing example (cont.) note here that the size of the table (10) is not prime if 23 inserted in the table, it would collide with 58 since and the table size is 10, only one alternative location, which is taken Rehashing table may get run time of operations may take too long insertions may for quadratic resolution too many removals may be intermixed with insertions solution: build a new table (with a new hash function) go through original hash table to compute a hash value for each (non-deleted) element insert it into the new table 35 36

Rehashing Rehashing example: insert 13, 15, 24, 6 into a hash table of size 7 with example (cont.) insert 23 table will be over 70% full; therefore, a new table is created 37 38 Rehashing Rehashing example (cont.) new table is size 17 new hash function all old elements are inserted into new table rehashing run time since elements and to rehash the entire table of size roughly must have been insertions since last rehash rehashing may run OK if in if interactive session, rehashing operation could produce a slowdown rehashing can be implemented with could rehash as soon as the table is half full could rehash only when an insertion fails could rehash only when a certain is reached may be best, as performance degrades as load factor increases 39 40

hash tables so far average case for insertions, searches, and deletions separate chaining: worst case some queries will take nearly logarithmic time worst-case time would be better important for applications such as lookup tables for routers and memory caches if is known in advance, and elements can be, worst-case time is achievable perfect hashing assume all items known separate chaining if the number of lists continually increases, the lists will become shorter and shorter with enough lists, high probability of two problems number of lists might be unreasonably the hashing might still be unfortunate can be made large enough to have probability of no collisions if collision detected, clear table and try again with a different hash function (at most done 2 times) 41 42 perfect hashing (cont.) how large must be? theoretically, should be, which is solution: use lists resolve collisions by using hash tables instead of linked lists each of these lists can have elements each secondary hash table will use a different hash function until it is can also perform similar operation for primary hash table total size of secondary hash tables is at most perfect hashing (cont.) example: slots 1, 3, 5, 7 empty; slots 0, 4, 8 have 1 element each; slots 2, 6 have 2 elements each; slot 9 has 3 elements 43 44

cuckoo hashing bound known for a long time researchers surprised in 1990s to learn that if one of two tables were chosen as items were inserted, the size of the largest list would be, which is significantly smaller main idea: use 2 tables neither more than full use a separate hash function for each item will be stored in one of these two locations collisions resolved by elements cuckoo hashing (cont.) example: 6 items; 2 tables of size 5; each table has randomly chosen hash function A can be placed at position 0 in Table 1 or position 2 in Table 2 a search therefore requires at most 2 table accesses in this example item deletion is trivial 45 46 cuckoo hashing (cont.) insertion ensure item is not already in one of the tables use first hash function and if first table location is, insert there if location in first table is occupied element there and place current item in correct position in first table displaced element goes to its alternate hash position in the second table cuckoo hashing (cont.) example: insert A insert B (displace A) 47 48

cuckoo hashing (cont.) insert C cuckoo hashing (cont.) insert F (displace E) (E displaces A) insert D (displace C) and E (A displaces B) (B relocated) 49 50 cuckoo hashing (cont.) insert G cuckoo hashing (cont.) cycles displacements are G D B A E F C G insertions should require < displacements if a certain number of displacements is reached on an insertion, tables can be with new hash functions also results in a displacement cycle 51 52

cuckoo hashing (cont.) variations higher number of tables (3 or 4) place item in second hash slot immediately instead of other items allow each cell to store keys space utilization increased cuckoo hashing (cont.) benefits worst-case lookup and deletion times avoidance of potential for potential issues extremely sensitive to choice of hash functions time for insertion increases rapidly as load factor approaches 0.5 53 54 hopscotch hashing improves on linear probing algorithm linear probing tries cells in sequential order, starting from hash location, which can be long due to primary and secondary clustering instead, hopscotch hashing places a bound on of the probe sequence results in worst-case constant-time lookup can be parallelized hopscotch hashing (cont.) if insertion would place an element too far from its hash location, go backward and other elements evicted elements cannot be placed farther than the maximal length each position in the table contains information about the current element inhabiting it, plus others that to it 55 56

hopscotch hashing (cont.) example: MAX_DIST = 4 hopscotch hashing (cont.) example: insert H in 9 each bit string provides 1 bit of information about the current position and the next 3 that follow 1: item hashes to current location; 0: no try in position 13, but too far, so try candidates for eviction (10, 11, 12) evict G in 11 57 58 hopscotch hashing (cont.) example: insert I in 6 hopscotch hashing (cont.) example: insert I in 6 position 14 too far, so try positions 11, 12, 13 G can move down one position 13 still too far; F can move down one position 12 still too far, so try positions 9, 10, 11 B can move down three now slot is open for I, fourth from 6 59 60

universal hashing in principle, we can end up with a situation where all of our keys are hashed to the in the hash table (bad) more realistically, we could choose a hash function that does not distribute the keys to avoid this, we can choose the hash function so that it is independent of the keys being stored yields provably good performance on average universal hashing (cont.) let be a finite collection of functions mapping our set of keys to the range } is a collection if for each pair of distinct keys, the number of hash functions for which is at most that is, with a randomly selected hash function, the chance of a between distinct and is not more than the probability of a collision if and were chosen randomly and independently from } 61 62 universal hashing (cont.) example: choose a prime sufficiently large that every key is in the range 0 to (inclusive) let and then the family is a universal class of hash functions extendible hashing amount of data too large to fit in main consideration is then the number of disk accesses assume we need to store records and records fit in one disk block current problems if probing or separate chaining is used, collisions could cause to be examined during a search rehashing would be expensive in this case 63 64

extendible hashing (cont.) allows search to be performed in disk accesses insertions require a bit more use B-tree as increases, height of B-tree could make height = 1, but multi-way branching would be extremely high extendible hashing (cont.) example: 6-bit integers root contains 4 pointers determined by first 2 bits each leaf has up to 4 elements 65 66 extendible hashing (cont.) example: insert 100100 place in third leaf, but full split leaf into 2 leaves, determined by 3 bits extendible hashing (cont.) example: insert 000000 first leaf split 67 68

extendible hashing (cont.) considerations several directory may be required if the elements in a leaf agree in more than D+1 leading bits number of bits to distinguish bit strings does not work well with ( duplicates: does not work at all) final points choose hash function carefully watch separate chaining: close to 1 probing hashing: 0.5 hash tables have some not possible to find min/max not possible to for a string unless the exact string is known binary search trees can do this, and is only slightly worse than 69 70 final points (cont.) hash tables good for symbol table gaming remembering locations to avoid recomputing through transposition table spell checkers 71