Hash Tables Hash Tables Goodrich, Tamassia

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

Data Structures Lecture 12

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

Dictionaries and Hash Tables

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

Priority Queue Sorting

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

Elementary Data Structures 2

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

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

CS2210 Data Structures and Algorithms

CHAPTER 9 HASH TABLES, MAPS, AND SKIP LISTS

Chapter 9: Maps, Dictionaries, Hashing

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

Hashing. Manolis Koubarakis. Data Structures and Programming Techniques

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)

Introduction to Hashing

Hash Tables. Gunnar Gotshalks. Maps 1

Open Addressing: Linear Probing (cont.)

CS 241 Analysis of Algorithms

CSE 214 Computer Science II Searching

CS 350 : Data Structures Hash Tables

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

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

csci 210: Data Structures Maps and Hash Tables

Understand how to deal with collisions

Practical Session 8- Hash Tables

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

Hash Tables. Hash functions Open addressing. November 24, 2017 Hassan Khosravi / Geoffrey Tien 1

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

CS/COE 1501

of characters from an alphabet, then, the hash function could be:

Hash Tables. Hashing Probing Separate Chaining Hash Function

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

Hashing Algorithms. Hash functions Separate Chaining Linear Probing Double Hashing

Lecture 4. Hashing Methods

Hashing. Hashing Procedures

Hash Table and Hashing

Dictionaries (Maps) Hash tables. ADT Dictionary or Map. n INSERT: inserts a new element, associated to unique value of a field (key)

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

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

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

The dictionary problem

Lecture 7: Efficient Collections via Hashing

HASH TABLES.

Topics on the Midterm

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

Final Exam. CSE 2011 Prof. J. Elder Last Updated: :41 PM

DATA STRUCTURES AND ALGORITHMS

Hashing. It s not just for breakfast anymore! hashing 1

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

CMSC 132: Object-Oriented Programming II. Hash Tables

Data Structures And Algorithms

DS ,21. L11-12: Hashmap

CS2 Algorithms and Data Structures Note 4

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

Final Exam. EECS 2011 Prof. J. Elder - 1 -

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

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

AAL 217: DATA STRUCTURES

Unit #5: Hash Functions and the Pigeonhole Principle

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

Hashing Techniques. Material based on slides by George Bebis

Lecture 17. Improving open-addressing hashing. Brent s method. Ordered hashing CSE 100, UCSD: LEC 17. Page 1 of 19

Algorithms and Data Structures

CS 10: Problem solving via Object Oriented Programming Winter 2017

Fundamental Algorithms

COMP171. Hashing.

Algorithms and Data Structures

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

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

Hash Tables Outline. Definition Hash functions Open hashing Closed hashing. Efficiency. collision resolution techniques. EECS 268 Programming II 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.

Hash Table. Ric Glassey

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

Outline. Computer Science 331. Desirable Properties of Hash Functions. What is a Hash Function? Hash Functions. Mike Jacobson.

HASH TABLES. Hash Tables Page 1

Chapter 27 Hashing. Objectives

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

Direct File Organization Hakan Uraz - File Organization 1

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

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

Cpt S 223. School of EECS, WSU

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

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

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

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. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

UNIT 5. Sorting and Hashing

Hashing. 5/1/2006 Algorithm analysis and Design CS 007 BE CS 5th Semester 2

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

CPSC 331 Term Test #2 March 26, 2007

Hash[ string key ] ==> integer value

Hashing as a Dictionary Implementation

9.2 Hash Tables. When implementing a map with a hash table, the goal is to store item (k, e) at index i = h(k)

7 Hashing: chaining. Summer Term 2010

Dictionaries and Hash Tables

Data Structures and Object-Oriented Design VIII. Spring 2014 Carola Wenk

CSC Design and Analysis of Algorithms. Lecture 9. Space-For-Time Tradeoffs. Space-for-time tradeoffs

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

Transcription:

Hash Tables 0 1 2 3 4 025-612-0001 981-101-0002 451-229-0004 Hash Tables 1

Hash Functions and Hash Tables A hash function h maps keys of a given type to integers in a fixed interval [0, N 1] Example: h(x) = x mod N is a hash function for integer keys The integer h(x) is called the hash value of key x A hash table for a given key type consists of Hash function h Array (called table) of size N When implementing a map with a hash table, the goal is to store item (k, o) at index i = h(k) Hash Tables 2

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

Hash Functions A hash function is usually specified as the composition of two functions: Hash code: h 1 :keys integers Compression function: h 2 : integers [0, N 1] The hash code is applied first, and the compression function is applied next on the result, i.e., h(x) = h 2 (h 1 (x)) The goal of the hash function is to disperse the keys in an apparently random way Hash Tables 4

Hash Codes Memory address: We reinterpret the memory address of the key object as an integer (default hash code of all Java objects) 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 Java) 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 Java) Hash Tables 5

Hash Codes (cont.) 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 0 a 1 a n 1 We evaluate the polynomial p(z) = a 0 + a 1 z + a 2 z 2 + + a n 1 z n 1 at a fixed value z, ignoring overflows Especially suitable for strings (e.g., the choice z = 33 gives few collisions on an application using English words) Polynomial p(z) can be evaluated in O(n) time using Horner s rule: The following polynomials are successively computed, each from the previous one in O(1) time p 0 (z) = a n 1 p i (z) = a n i 1 + zp i 1 (z) (i = 1, 2,, n 1) We have p(z) = p n 1 (z) Hash Tables 6

Compression Functions Division: h 2 (y) = y mod N The size N of the hash table is usually chosen to be a prime The reason has to do with number theory and is beyond the scope of this course Multiply, Add and Divide (MAD): h 2 (y) = (ay + b)modn a and b are nonnegative integers such that a mod N 0 Otherwise, every integer would map to the same value b Hash Tables 7

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

Dictionary Methods with Separate Chaining used for Collisions Delegate operations to a list-based dictionary at each cell: Algorithm find(k): Output: The value associated with the key k in the dictionary, or null if there is no entry with key equal to k in the dictionary return A[h(k)].get(k) {delegate the get to the list-based map at A[h(k)]} Algorithm insert(k,v): Output: If there is an existing entry in our dictionary with key equal to k, then we return its value (replacing it with v); otherwise, we return null t = A[h(k)].put(k,v) {delegate the put to the list-based map at A[h(k)]} if t = null then {k is a new key} n = n + 1 return t Algorithm remove(k): Output: The (removed) value associated with key k in the dictionary, or null if there is no entry with key equal to k in the dictionary t = A[h(k)].remove(k) {delegate the remove to the list-based map at A[h(k)]} if t null then {k was found} n = n - 1 return t Hash Tables 9

Linear Probing 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 Each table cell inspected is referred to as a probe Colliding items lump together, causing future collisions to cause a longer sequence of probes Example: h(x) = x 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 Hash Tables 10

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) i h(k) p 0 repeat c A[i] if c = return null else if c.key () = k return c.element() else i (i + 1) mod N p p + 1 until p = N return null Hash Tables 11

Updates with Linear Probing To handle insertions and deletions, we introduce a special object, called AVAILABLE, which replaces deleted elements remove(k) We search for an entry with key k If such an entry (k, o) is found, we replace it with the special item AVAILABLE and we return element o Else, we return null insert(k, o) We throw an exception if the table is full 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 We store entry (k, o) in cell i Hash Tables 12

Double Hashing Double hashing uses a secondary hash function d(k) and handles collisions by placing an item in the first available cell of the series (i + jd(k)) mod N for j = 0, 1,, N 1 The secondary hash function d(k) cannot have zero values The table size N must be a prime to allow probing of all the cells Common choice of compression function for the secondary hash function: d 2 (k) = q k mod q where q < N q is a prime The possible values for d 2 (k) are 1, 2,, q Hash Tables 13

Example of Double Hashing Consider a hash table storing integer keys that handles collision with double hashing N = 13 h(k) = k mod 13 d(k) = 7 k mod 7 Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in this order k h(k ) d (k ) Probes 18 5 3 5 41 2 1 2 22 9 6 9 44 5 5 5 10 59 7 4 7 32 6 3 6 31 5 4 5 9 0 73 8 4 8 0 1 2 3 4 5 6 7 8 9 10 11 12 31 41 18 32 59 73 22 44 0 1 2 3 4 5 6 7 8 9 10 11 12 Hash Tables 14

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 α = n/n 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 α) The expected running time of all the dictionary 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 Hash Tables 15