Maps. Hash Tables. Dictionaries. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia.

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Hash Tables
Advertisements

© 2004 Goodrich, Tamassia Hash Tables
© 2004 Goodrich, Tamassia Hash Tables1  
© 2004 Goodrich, Tamassia Dictionaries   
© 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.
9.3 The Dictionary ADT    … 01/25/98, Taipei, … 03/04/98, Yunlin, … 02/15/98, Douliou, … 01/20/98,
Dictionaries1 © 2010 Goodrich, Tamassia m l h m l h m l.
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
© 2004 Goodrich, Tamassia Dictionaries   
CSC311: Data Structures 1 Chapter 9: Maps and Dictionaries Objectives: Map ADT Hash tables –Hash functions and hash code –Compression functions and collisions.
Dictionaries and Hash Tables1  
1 Chapter 9 Maps and Dictionaries. 2 A basic problem We have to store some records and perform the following: add new record add new record delete record.
© 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
CS 221 Analysis of Algorithms Data Structures Dictionaries, Hash Tables, Ordered Dictionary and Binary Search Trees.
CS212: DATA STRUCTURES Lecture 10:Hashing 1. Outline 2  Map Abstract Data type  Map Abstract Data type methods  What is hash  Hash tables  Bucket.
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.
1 i206: Lecture 12: Hash Tables (Dictionaries); Intro to Recursion Marti Hearst Spring 2012.
CH 9 : MAPS AND DICTIONARIES 1 ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
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.
Lecture14: Hashing Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
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 CSE 2011 Winter July 2018.
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.
Data Structures and Database Applications Hashing and Hashtables in C#
Dictionaries < > = Dictionaries Dictionaries
Data Structures Maps and Hash.
Data Structures and Database Applications Hashing and Hashtables in C#
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.
Ordered Maps & Dictionaries
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
CH 9 : Maps And Dictionary
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 < > = Dictionaries Dictionaries
Dictionaries and Hash Tables
Presentation transcript:

Maps. Hash Tables. Dictionaries. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

A map models a searchable collection of key-value entries. A map stores key-value pairs (k, v) which we call entries. The main operations of a map are for searching, inserting, and deleting items. Multiple entries with the same key are not allowed (map ADT requires each key to be unique). Applications: address book. student-record database. CPSC 3200 University of Tennessee at Chattanooga – Summer © 2010 Goodrich, Tamassia

get(k): if the map M has an entry with key k, return its associated value; else, return null. put(k, v): insert entry (k, v) into the map M; if key k is not already in M, then return null; else, return old value associated with k. remove(k): if the map M has an entry with key k, 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 CPSC 3200 University of Tennessee at Chattanooga – Summer © 2010 Goodrich, Tamassia

CPSC 3200 University of Tennessee at Chattanooga – Summer © 2010 Goodrich, Tamassia

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. The unsorted list implementation is effective only for maps of small size (e.g., historical record of logins to a workstation) trailer header nodes/positions entries 9 c 6 c 5 c 8 c CPSC 3200 University of Tennessee at Chattanooga – Summer © 2010 Goodrich, Tamassia

Algorithm get(k): Input: A key k Output: The value for key k in M, or null if there is no key k in M for each position p in S.positions( ) do if p.element( ).getKey( ) = k then return p.element( ).getValue( ) return null {there is no entry with key equal to k} Time complexity ? CPSC 3200 University of Tennessee at Chattanooga – Summer © 2010 Goodrich, Tamassia

Algorithm put(k,v): Input: A key-value pair (k,v) Output: The old value associated with key k in M, or null if k is new for each position p in S.positions( ) do if p.element( ).getKey( ) = k then t ← p.element( ).getValue( ) B.set(p,(k,v)) return t {return the old value} S.addLast((k,v)) n ← n+1 {increment variable storing number of entries} return null {there was no previous entry with key equal to k} Time complexity ? CPSC 3200 University of Tennessee at Chattanooga – Summer © 2010 Goodrich, Tamassia

Algorithm remove(k): Input: A key k Output: The (removed) value for key k in M, or null if k is not in M for each position p in S.positions( ) do if p.element().getKey() = k then t ← p.element().getValue() S.remove(p) n ← n−1 {decrement variable storing number of entries} return t {return the removed value} return null {there is no entry with key equal to k} Time complexity ? CPSC 3200 University of Tennessee at Chattanooga – Summer © 2010 Goodrich, Tamassia

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, v) at index i = h(k) A hash function h maps keys of a given type to integers in a fixed interval [0, N - 1] The integer h(k) is called the hash value of key k CPSC 3200 University of Tennessee at Chattanooga – Summer © 2010 Goodrich, Tamassia

We design a hash table for a map 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     … CPSC 3200 University of Tennessee at Chattanooga – Summer © 2010 Goodrich, Tamassia

A hash function is usually specified as the composition of two functions: Hash code: mapping the key k to integer h 1 : keys  integers Compression function: mapping the hash code to an integer in range of indices [0, N-1] 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. CPSC 3200 University of Tennessee at Chattanooga – Summer © 2010 Goodrich, Tamassia

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 Separate chaining is simple, but requires additional memory outside the table    CPSC 3200 University of Tennessee at Chattanooga – Summer © 2010 Goodrich, Tamassia

Delegate operations to a list-based map at each cell: 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 CPSC 3200 University of Tennessee at Chattanooga – Summer © 2010 Goodrich, Tamassia

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 © 2010 Goodrich, Tamassia CPSC 3200 University of Tennessee at Chattanooga – Summer

CPSC 3200 University of Tennessee at Chattanooga – Summer