CSE 373, Copyright S. Tanimoto, 2002 Hashing -

Slides:



Advertisements
Similar presentations
Chapter 11. Hash Tables.
Advertisements

1 Designing Hash Tables Sections 5.3, 5.4, Designing a hash table 1.Hash function: establishing a key with an indexed location in a hash table.
Hash Tables.
Hashing.
Lecture 6 Hashing. Motivating Example Want to store a list whose elements are integers between 1 and 5 Will define an array of size 5, and if the list.
Skip List & Hashing CSE, POSTECH.
© 2004 Goodrich, Tamassia Hash Tables1  
Hashing Techniques.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 48 Hashing.
Sets and Maps Chapter 9. Chapter 9: Sets and Maps2 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn about.
Tirgul 7. Find an efficient implementation of a dynamic collection of elements with unique keys Supported Operations: Insert, Search and Delete. The keys.
COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables.
CSE 326 Hashing Richard Anderson (instead of Martin Tompa)
Hashing General idea: Get a large array
Lecture 6 Hashing. Motivating Example Want to store a list whose elements are integers between 1 and 5 Will define an array of size 5, and if the list.
CS 206 Introduction to Computer Science II 04 / 06 / 2009 Instructor: Michael Eckmann.
Symbol Tables Symbol tables are used by compilers to keep track of information about variables functions class names type names temporary variables etc.
Hashing Table Professor Sin-Min Lee Department of Computer Science.
Hashing Chapter 20. Hash Table A hash table is a data structure that allows fast find, insert, and delete operations (most of the time). The simplest.
Appendix E-A Hashing Modified. Chapter Scope Concept of hashing Hashing functions Collision handling – Open addressing – Buckets – Chaining Deletions.
Searching Given distinct keys k 1, k 2, …, k n and a collection of n records of the form »(k 1,I 1 ), (k 2,I 2 ), …, (k n, I n ) Search Problem - For key.
CS201: Data Structures and Discrete Mathematics I Hash Table.
Chapter 12 Hash Table. ● So far, the best worst-case time for searching is O(log n). ● Hash tables  average search time of O(1).  worst case search.
David Luebke 1 11/26/2015 Hash Tables. David Luebke 2 11/26/2015 Hash Tables ● Motivation: Dictionaries ■ Set of key/value pairs ■ We care about search,
1 Hashing - Introduction Dictionary = a dynamic set that supports the operations INSERT, DELETE, SEARCH Dictionary = a dynamic set that supports the operations.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Hashing 1 Hashing. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
Hash Tables ADT Data Dictionary, with two operations – Insert an item, – Search for (and retrieve) an item How should we implement a data dictionary? –
Sets and Maps Chapter 9. Chapter Objectives  To understand the Java Map and Set interfaces and how to use them  To learn about hash coding and its use.
1 What is it? A side order for your eggs? A form of narcotic intake? A combination of the two?
DS.H.1 Hashing Chapter 5 Overview The General Idea Hash Functions Separate Chaining Open Addressing Rehashing Extendible Hashing Application Example: Geometric.
Sets and Maps Chapter 9.
CE 221 Data Structures and Algorithms
Hashing (part 2) CSE 2011 Winter March 2018.
Chapter 27 Hashing Jung Soo (Sue) Lim Cal State LA.
Hashing.
Design & Analysis of Algorithm Hashing (Contd.)
Hashing CSE 2011 Winter July 2018.
Hash Tables (Chapter 13) Part 2.
Efficiency add remove find unsorted array O(1) O(n) sorted array
Quadratic probing Double hashing Removal and open addressing Chaining
Advanced Associative Structures
Hash Table.
Hash Table.
Chapter 28 Hashing.
Richard Anderson (instead of Martin Tompa)
CSE373: Data Structures & Algorithms Lecture 14: Hash Collisions
Hash Tables.
Hashing CS2110.
Hashing as a Dictionary Implementation
Chapter 21 Hashing: Implementing Dictionaries and Sets
CSCE 3110 Data Structures & Algorithm Analysis
Sparse matrices, hash tables, cell arrays
Hashing Alexandra Stefan.
CS202 - Fundamental Structures of Computer Science II
Sets and Maps Chapter 9.
Overflow Handling An overflow occurs when the home bucket for a new pair (key, element) is full. We may handle overflows by: Search the hash table in some.
EE 312 Software Design and Implementation I
Dictionaries 4/5/2019 1:49 AM Hash Tables  
Overflow Handling An overflow occurs when the home bucket for a new pair (key, element) is full. We may handle overflows by: Search the hash table in some.
Overflow Handling An overflow occurs when the home bucket for a new pair (key, element) is full. We may handle overflows by: Search the hash table in some.
slides created by Marty Stepp
CS210- Lecture 16 July 11, 2005 Agenda Maps and Dictionaries Map ADT
Hashing.
Data Structures and Algorithm Analysis Hashing
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
EE 312 Software Design and Implementation I
Chapter 13 Hashing © 2011 Pearson Addison-Wesley. All rights reserved.
Lecture-Hashing.
CSE 373: Data Structures and Algorithms
Presentation transcript:

CSE 373, Copyright S. Tanimoto, 2002 Hashing -

CSE 373, Copyright S. Tanimoto, 2002 Hashing - Motivation Many applications need to store "associations." Rapid retrieval is sometimes more important than storage efficiency. Hashing is flexible family of techniques for implementing associations between keys and values. CSE 373, Copyright S. Tanimoto, 2002 Hashing -

Mathematical Description Suppose we have a function mapping keys to values. Its domain is a set KEYS For example, KEYS = {0, 1, ..., m-1}, or KEYS = the set of all possible ASCII strings. Its range is a set of possible values. For example, the values may themselves be ASCII strings. How can we represent such a function? CSE 373, Copyright S. Tanimoto, 2002 Hashing -

A Dictionary Abstract Data Type A function expressed as a finite set of (key,value) pairs. A: KEYS  VALUES { (key1, value1), ..., (keyn, valuen)} Methods: PUT: DICTIONARIES  KEYS  VALUES  DICTIONARIES GET: DICTIONARIES  KEYS  VALUES GETALLKEYS: DICTIONARIES  2KEYS For any set S, the set of all possible subsets of S is written 2S, and is called the power set of S. CSE 373, Copyright S. Tanimoto, 2002 Hashing -

One Implementation of a Dictionary: An Association List ( (key1, value1), ..., (keyn, valuen) ) (key1,value1) (key2,value2) (keyn,valuen) Worst case time for GET is (n) cell examinations. Expected case time for a successful GET is n/2 cell examinations, also (n). CSE 373, Copyright S. Tanimoto, 2002 Hashing -

Hashing: Practical Implementations of the Dictionary ADT. A hash table is a 1-dimensional array in which each cell stores zero or more associations of a dictionary. The array index for (keyi, valuei) is determined by applying a "hash function" to the key: h(keyi), and then possibly taking additional steps, depending on the particular hashing method and whether there are any "collisions". CSE 373, Copyright S. Tanimoto, 2002 Hashing -

CSE 373, Copyright S. Tanimoto, 2002 Hashing - Hashing with Chains h(keyi) (keyi1,valuei1) (keyi2,valuei2) (keyin,valuein) keyi (keyj1,valuej1) (keyj2,valuej2) Each table entry is the head of a linked list of elements all of which share the same hash value h(keyi). This is sometimes called "open" hashing. CSE 373, Copyright S. Tanimoto, 2002 Hashing -

CSE 373, Copyright S. Tanimoto, 2002 Hashing - Closed Hashing h(keyi) keyi valuei keyi The associations are all stored within the hash table; not on linked lists. CSE 373, Copyright S. Tanimoto, 2002 Hashing -

CSE 373, Copyright S. Tanimoto, 2002 Hashing - Hashing Example Keys: 4-digit numbers. Values: names. Let h(d1d2d3d4) = (d1 + d2 + d3 + d4) mod 10. E.g., h(1978) = 5. Data: (1978, VAX-11/780), (1982, IBM-PC), (1984, Macintosh) 0: 1982 IBM-PC 1: 2: 1984 Macintosh 3: 4: 5: 1978 VAX-11/780 6: 7: 8: 9: CSE 373, Copyright S. Tanimoto, 2002 Hashing -

Hashing Example (continued) Let h(d1d2d3d4) = (d1 + d2 + d3 + d4) mod 10. E.g., h(1978) = 5. Put (1993, Java). h(1993) = 2. Collision! 0: 1982 IBM-PC 1: 2: 1984 Macintosh 3: 4: 5: 1978 VAX-11/780 6: 7: 8: 9: CSE 373, Copyright S. Tanimoto, 2002 Hashing -

Hashing Example (continued) Linear Probing: Try h(1993) + 1 mod 10. If we keep getting collisions, the formula is h(d1d2d3d4) + k mod 10, in the kth attempt. If all 10 positions are full, a new hash table must be created and all the old elements placed in the new table. 0: 1982 IBM-PC 1: 2: 1984 Macintosh 3: 1993 Java 4: 5: 1978 VAX-11/780 6: 7: 8: 9: CSE 373, Copyright S. Tanimoto, 2002 Hashing -

Collision Resolution Methods Linear Probing: (h(key) + ck) mod n Quadratic Probing: (h(key) + ck2) mod n Double hashing: (i  h2(key)) mod n Rehashing: Create a larger hash table and try again. (Only perform when the load factor is at least 0.5). Note: n should normally be a prime number to help avoid collisions. The constant c is normally small, and typically is 1. CSE 373, Copyright S. Tanimoto, 2002 Hashing -

Deletion in Closed Hash Tables Simply removing an association can break "chains" formed after collisions and make it difficult to perform GET operations on associations that collided with now-deleted associations. Therefore, it is common to use a "delete bit" to mark an association as deleted. When the table gets too full of associations and deleted associations, rehashing is necessary. The rehashing may use a table of the same size as or smaller than before (if many of the entries are deleted entries), or it may use a larger table. CSE 373, Copyright S. Tanimoto, 2002 Hashing -