Lecture14: Hashing Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Hash Tables
Advertisements

© 2004 Goodrich, Tamassia Hash Tables
Dictionaries 4/1/2017 2:36 PM Hash Tables  …
© 2004 Goodrich, Tamassia Hash Tables1  
© 2004 Goodrich, Tamassia Maps1. © 2004 Goodrich, Tamassia Maps2 A map models a searchable collection of key-value entries The main operations of a map.
Chapter 9: Maps, Dictionaries, Hashing Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided.
Maps. Hash Tables. Dictionaries. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia.
Data Structures Lecture 12 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT TTIT33 – Algorithms and optimization Lecture 5 Algorithms ADT Map,
Maps, Dictionaries, Hashtables
CSC311: Data Structures 1 Chapter 9: Maps and Dictionaries Objectives: Map ADT Hash tables –Hash functions and hash code –Compression functions and collisions.
CSC401 – Analysis of Algorithms Lecture Notes 5 Heaps and Hash Tables Objectives: Introduce Heaps, Heap-sorting, and Heap- construction Analyze the performance.
Dictionaries and Hash Tables1  
© 2004 Goodrich, Tamassia Maps1. © 2004 Goodrich, Tamassia Maps2 A map models a searchable collection of key-value entries The main operations of a map.
Hash Tables1 Part E Hash Tables  
Hash Tables1 Part E Hash Tables  
Hash Tables1 Part E Hash Tables  
Data Structures Hash Table (aka Dictionary) i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, Andreas Veneris, Glenn Brookshear,
Dictionaries 4/17/2017 3:23 PM Hash Tables  
Maps, Hash Tables and Dictionaries Chapter 10.1, 10.2, 10.3, 10.5.
Maps, Dictionaries, Hashing
1. 2 Problem RT&T is a large phone company, and they want to provide enhanced caller ID capability: –given a phone number, return the caller’s name –phone.
CS 221 Analysis of Algorithms Data Structures Dictionaries, Hash Tables, Ordered Dictionary and Binary Search Trees.
Hash Tables1   © 2010 Goodrich, Tamassia.
Dictionaries and Hash Tables1 Hash Tables  
Maps, Hash Tables, Skip Lists, Sets last Update: Nov 4, 2014 EECS2011: Maps, Hash Tables, Skip Lists, Sets1.
© 2004 Goodrich, Tamassia Hash Tables1  
CHAPTER 9 HASH TABLES, MAPS, AND SKIP LISTS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++,
Map ADT by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data Structures.
Maps & dictionaries Go&Ta A map models a searchable collection of key-value entries The main operations of a map are for searching, inserting,
Maps1 © 2010 Goodrich, Tamassia. Maps2  A map models a searchable collection of key-value entries  The main operations of a map are for searching, inserting,
© 2004 Goodrich, Tamassia Maps1. © 2004 Goodrich, Tamassia Maps2 A map models a searchable collection of key-value entries The main operations of a map.
1 COMP9024: Data Structures and Algorithms Week Eight: Maps and Dictionaries Hui Wu Session 2, 2014
1 COMP9024: Data Structures and Algorithms Week Nine: Maps and Dictionaries Hui Wu Session 1, 2016
1 COMP9024: Data Structures and Algorithms Week Eight: Maps and Dictionaries Hui Wu Session 1, 2014
Hash Maps Rem Collier Room A1.02 School of Computer Science and Informatics University College Dublin, Ireland.
Hash Tables 1/28/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Maps 1/28/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Hashing (part 2) CSE 2011 Winter March 2018.
Design & Analysis of Algorithm Map
CSCI 210 Data Structures and Algorithms
Hashing CSE 2011 Winter July 2018.
Dictionaries and Hash Tables
Dictionaries Dictionaries 07/27/16 16:46 07/27/16 16:46 Hash Tables 
© 2013 Goodrich, Tamassia, Goldwasser
Dictionaries 9/14/ :35 AM Hash Tables   4
Hash Tables 3/25/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Searching.
Data Structures Maps and Hash.
Hash Tables 11/22/2018 3:15 AM Hash Tables  1 2  3  4
Dictionaries 11/23/2018 5:34 PM Hash Tables   Hash Tables.
Dictionaries and Hash Tables
Copyright © Aiman Hanna All rights reserved
Maps.
Hash Tables   Maps Dictionaries 12/7/2018 5:58 AM Hash Tables  
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
Dictionaries and Hash Tables
Copyright © Aiman Hanna All rights reserved
"He's off the map!" - Eternal Sunshine of the Spotless Mind
Dictionaries 1/17/2019 7:55 AM Hash Tables   4
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
Hash Tables Computer Science and Engineering
Hash Tables Computer Science and Engineering
Hash Tables Computer Science and Engineering
Dictionaries 4/5/2019 1:49 AM Hash Tables  
Maps 4/25/2019 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
CS210- Lecture 17 July 12, 2005 Agenda Collision Handling
CS210- Lecture 16 July 11, 2005 Agenda Maps and Dictionaries Map ADT
Lecture 11 CS
Dictionaries and Hash Tables
Presentation transcript:

Lecture14: Hashing Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Maps A map models a searchable collection of key‐value entries Characteristics  The main operations: search, inserting, and deleting items  Multiple entries with the same key are not allowed. Applications  Address book  Student‐record database 2 Customer IDNameAddress 010‐5323‐3221Kim, XXXSeoul 011‐4221‐1949Lee, YYYPohang 010‐9492‐0988Park, ZZZDaegu 010‐8383‐3221Kim, XXXSeoul entry key value

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 The Map ADT Methods for map M  get(key) : if M has an entry with key, return its associated value; otherwise, return null  put(key,value) : insert entry (key,value) into the map M; if key is not already in M, then return null; else, return old value associated with the key  remove(key) : if the map M has an entry with key, remove it from M and return its associated value; else, return null  size()  isEmpty()  entrySet() : return an iterable collection of the entries in M  keySet() : return an iterable collection of the keys in M  values() : return an iterator of the values in M 3

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Example 4 OperationOutputMap isEmpty()trueØ put(5,A)null(5,A) put(7,B)null(5,A),(7,B) put(2,C)null(5,A),(7,B),(2,C) put(8,D)null(5,A),(7,B),(2,C),(8,D) put(2,E)C(5,A),(7,B),(2,E),(8,D) get(7)B(5,A),(7,B),(2,E),(8,D) get(4)null(5,A),(7,B),(2,E),(8,D) get(2)E(5,A),(7,B),(2,E),(8,D) size()4(5,A),(7,B),(2,E),(8,D) remove(5)A(7,B),(2,E),(8,D) remove(2)E(7,B),(8,D) get(2)null(7,B),(8,D) isEmpty()false(7,B),(8,D)

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 A Simple List-Based Map We can efficiently implement a map using an unsorted list We store the items of the map in a list S (based on a doubly-linked list), in arbitrary order 5 trailer header nodes/positions entries 9 c 6 c 5 c 8 c

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Methods for Map get(k) algorithm 6

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Methods for Map put(k,v) algorithm 7

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Methods for Map remove(v) algorithm 8

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Performance of a List-Based Map 9

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Problems and Solution 10

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Hashing 11

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Hash Functions and Hash Tables 12

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Hash Tables Use keys to store and access entries (in constant time) 13

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Example 14     …

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Hash Functions 15

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 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) 16

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Hash Codes (cont.) 17

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Compression Functions 18

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 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.  Simple, but requires additional memory outside the table 19   

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Map with Separate Chaining 20 Algorithm get(k) return A[h(k)].get(k) Algorithm put(k,v): t = A[h(k)].put(k,v) if t = null then // k is a new key n = n + 1 return t Algorithm remove(k) t = A[h(k)].remove(k) if t ≠ null then // k was found n = n - 1 return t

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Linear Probing

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Search with Linear Probing 22 Algorithm get(k) i  h(k) p  0 repeat c  A[i] if c =  return null else if c.getKey () = k return c.getValue() else i  (i + 1) mod N p  p + 1 until p = N return null

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Updates with Linear Probing If collided item is removed  The items with the same key value may not be accessible.  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 (key,value) is found, we replace it with the special item AVAILABLE and we return element value  Otherwise, we return null. 23

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Updates with Linear Probing 24

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Double Hashing 25

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Example of Double Hashing

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Performance of Hashing 27

28