CSC 212 – Data Structures Lecture 28: More Hash and Dictionaries.

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia 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.
CSE 1302 Lecture 23 Hashing and Hash Tables Richard Gesick.
The Dictionary ADT Definition A dictionary is an ordered or unordered list of key-element pairs, where keys are used to locate elements in the list. Example:
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.
Hashing as a Dictionary Implementation
© 2004 Goodrich, Tamassia Hash Tables1  
Using arrays – Example 2: names as keys How do we map strings to integers? One way is to convert each letter to a number, either by mapping them to 0-25.
Log Files. O(n) Data Structure Exercises 16.1.
Maps, Dictionaries, Hashtables
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.
© 2006 Pearson Addison-Wesley. All rights reserved13 A-1 Chapter 13 Hash Tables.
1 CSE 326: Data Structures Hash Tables Autumn 2007 Lecture 14.
CS 206 Introduction to Computer Science II 11 / 17 / 2008 Instructor: Michael Eckmann.
Hash Tables1 Part E Hash Tables  
Hash Tables1 Part E Hash Tables  
Introduction to Hashing CS 311 Winter, Dictionary Structure A dictionary structure has the form: (Key, Data) Dictionary structures are organized.
Hash Tables1 Part E Hash Tables  
Tirgul 7. Find an efficient implementation of a dynamic collection of elements with unique keys Supported Operations: Insert, Search and Delete. The keys.
Tirgul 8 Hash Tables (continued) Reminder Examples.
Hashing General idea: Get a large array
Dictionaries 4/17/2017 3:23 PM Hash Tables  
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Hash Tables. Container of elements where each element has an associated key Each key is mapped to a value that determines the table cell where element.
CS 206 Introduction to Computer Science II 04 / 06 / 2009 Instructor: Michael Eckmann.
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.
Hashing 1. Def. Hash Table an array in which items are inserted according to a key value (i.e. the key value is used to determine the index of the item).
CSC 213 – Large Scale Programming. Today’s Goal  Consider what will be important when searching  Why search in first place? What is its purpose?  What.
Dictionaries and Hash Tables. Dictionary A dictionary, in computer science, implies a container that stores key-element pairs called items, and allows.
1 HashTable. 2 Dictionary A collection of data that is accessed by “key” values –The keys may be ordered or unordered –Multiple key values may/may-not.
Hash Tables1   © 2010 Goodrich, Tamassia.
1 Symbol Tables The symbol table contains information about –variables –functions –class names –type names –temporary variables –etc.
Comp 335 File Structures Hashing.
LECTURE 36: DICTIONARY CSC 212 – Data Structures.
LECTURE 34: MAPS & HASH CSC 212 – Data Structures.
1 HASHING Course teacher: Moona Kanwal. 2 Hashing Mathematical concept –To define any number as set of numbers in given interval –To cut down part of.
Can’t provide fast insertion/removal and fast lookup at the same time Vectors, Linked Lists, Stack, Queues, Deques 4 Data Structures - CSCI 102 Copyright.
Hashing Hashing is another method for sorting and searching data.
© 2004 Goodrich, Tamassia Hash Tables1  
Hashing as a Dictionary Implementation Chapter 19.
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.
LECTURE 35: COLLISIONS CSC 212 – Data Structures.
Lecture 12COMPSCI.220.FS.T Symbol Table and Hashing A ( symbol) table is a set of table entries, ( K,V) Each entry contains: –a unique key, K,
Hashing Chapter 7 Section 3. What is hashing? Hashing is using a 1-D array to implement a dictionary o This implementation is called a "hash table" Items.
CSC 212 – Data Structures Lecture 26: Hash Tables.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Hash Tables © Rick Mercer.  Outline  Discuss what a hash method does  translates a string key into an integer  Discuss a few strategies for implementing.
Chapter 13 C Advanced Implementations of Tables – Hash Tables.
1 the hash table. hash table A hash table consists of two major components …
Hash Tables ADT Data Dictionary, with two operations – Insert an item, – Search for (and retrieve) an item How should we implement a data dictionary? –
1 Data Structures CSCI 132, Spring 2014 Lecture 33 Hash Tables.
Hash Tables Ellen Walker CPSC 201 Data Structures Hiram College.
CSC 213 – Large Scale Programming. Today’s Goal  Review when, where, & why we use Map s  Why Sequence -based approach causes problems  How hash can.
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.
Hashing (part 2) CSE 2011 Winter March 2018.
Hashing CSE 2011 Winter July 2018.
© 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.
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
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
Dictionaries 4/5/2019 1:49 AM Hash Tables  
CS210- Lecture 16 July 11, 2005 Agenda Maps and Dictionaries Map ADT
Dictionaries and Hash Tables
Presentation transcript:

CSC 212 – Data Structures Lecture 28: More Hash and Dictionaries

Question of the Day What do you get when you cross a mountain climber and a grape? Nothing, you cannot cross a scalar.

Hash Table Used to implement Map  Must accept a key and return a value  Tries for O (1) time by using arrays…  … but may end up needing O (n) time Do not know Keys type ahead of time  Flexible design must handle anything  Must consider what we want from hash

Hashing Want use array to hold our table  Indices provide O (1) access times But array index must be an int  With int Keys, could just use array from start  Not flexible at all So, hash function serves three purposes:  Convert Key to int in repeatable manner  Limit range of int to size of the array  Spread entries out evenly across the range

Hash Function First thing hash must do: turn Key into int  Easy for numeric data types For String, add value of each character  Makes “spot”, “pots”, “stop”, & “opts” equal  Improve upon this using polynomial like x 0  x 1 a  x 2 a 2  …  x n  1 a n  1 would compute ‘ s ’  ‘ p ’* z  ‘ o ’* z 2  ‘ t ’* z 3 Basically, has to be specific to type of Key

Compression Need to limit table size  Unused entries waste space and do not help When z =33, “spot” = 4,293,383  “triskaidekaphobia” = too big for my calculator To be useful, hash must be compressible Easiest compression uses modulus (%)  Using prime number exploits splits regularity MAD method first does multiply and add  Can help spread hash more evenly

Collisions Two Keys could always share hash code  Array can only hold one value per entry  Need to some way of working around this Three commonly used handling schemes  Separate chaining: array contains Lists of entries hashed to that index  Linear probing: loop through array looking for first open array location  Double hashing: use more hash to find an empty array location

Separate Chaining Table is an array of List “Chain” whenever there is a collision  Each entry at index in which it is hashed  Search List to see if a there is a matching Key    | “Jane” | “Joe” | “Bob”

Linear Probing Table is array of Entrys Normally, Entry located where key is hashed  If index used, circle through array for first empty one  Collisions cause pockets of filled slots, slowing access Collisions means key could be in any location  But either fill removed slots or mark specially  Only search until first empty specially marked slot

Double Hashing Solution to bad hash is more hash!  Table is again array of Entrys When there is a collision, re-hash key using second hash function  Re-use second hash value to probe on repeated collisions  Re-multiplier and table size should be relatively prime, so entire table will be scanned

Store integers using double hashing  N  13  h(k)  k mod 13  d(k)  7  k mod 7 Insert keys: 18, 41, 22, 44, 59, 32, 31 Example of Double Hashing

Dictionary ADT dic·tion·ar·y 1. Reference book containing alphabetical list of words, with information given for each word 2. Book listing words with translations into other language Dictionary ADT maps a key to 1 or more values  Used by Google, computer security logs, databases Like a Map, Dictionary works with entries  But Dictionary removes an Entry and adds findAll() to return Iterator over entries with given key

Implementing a Dictionary Dictionary similar to a Map  Can use List or hash table Implementation is virtually identical except:  Allow multiple entries with same key  When searching in remove, must match key and value  Add findAll() to return instance of class implementing Iterator Class could just have array to which you add entries with key matching search key

Your Turn Get back into groups and do activity

Before Next Lecture… Keep up with your reading! Complete Week #10 Assignment Review Programming Assignment #3