LECTURE 35: COLLISIONS CSC 212 – Data Structures.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Hash Tables CS 310 – Professor Roch Weiss Chapter 20 All figures marked with a chapter and section number are copyrighted © 2006 by Pearson Addison-Wesley.
Hash Tables.
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
Hashing Part Two Better Collision Resolution Small parts of this material stolen from "File Organization and Access" by Austing and Cassel.
© 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.
Implementation of Linear Probing (continued) Helping method for locating index: private int findIndex(long key) // return -1 if the item with key 'key'
Hashing Techniques.
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.
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  
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.
COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables.
Hashing General idea: Get a large array
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
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.
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.
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.
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.
Data Structures and Algorithm Analysis Hashing Lecturer: Jing Liu Homepage:
1 Hash table. 2 Objective To learn: Hash function Linear probing Quadratic probing Chained hash table.
1 Hash table. 2 A basic problem We have to store some records and perform the following:  add new record  delete record  search a record by key Find.
Comp 335 File Structures Hashing.
LECTURE 36: DICTIONARY CSC 212 – Data Structures.
LECTURE 34: MAPS & HASH CSC 212 – Data Structures.
CSE 326: Data Structures Lecture #13 Bart Niswonger Summer Quarter 2001.
Storage and Retrieval Structures by Ron Peterson.
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.
March 23 & 28, Csci 2111: Data and File Structures Week 10, Lectures 1 & 2 Hashing.
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,
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,
1 Hashing - Introduction Dictionary = a dynamic set that supports the operations INSERT, DELETE, SEARCH Dictionary = a dynamic set that supports the operations.
Hashing Basis Ideas A data structure that allows insertion, deletion and search in O(1) in average. A data structure that allows insertion, deletion and.
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.
Hash Table March COP 3502, UCF 1. Outline Hash Table: – Motivation – Direct Access Table – Hash Table Solutions for Collision Problem: – Open.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
CSE 373 Data Structures and Algorithms Lecture 17: Hashing II.
Tirgul 11 Notes Hash tables –reminder –examples –some new material.
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.
Hashing by Rafael Jaffarove CS157b. Motivation  Fast data access  Search  Insertion  Deletion  Ideal seek time is O(1)
CS 206 Introduction to Computer Science II 04 / 08 / 2009 Instructor: Michael Eckmann.
CSC 413/513: Intro to Algorithms Hash Tables. ● Hash table: ■ Given a table T and a record x, with key (= symbol) and satellite data, we need to support:
CSC 212 – Data Structures Lecture 28: More Hash and Dictionaries.
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 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.
Hash table CSC317 We have elements with key and satellite data
Slides by Steve Armstrong LeTourneau University Longview, TX
Hashing Alexandra Stefan.
Hash Table.
Resolving collisions: Open addressing
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
Hashing Alexandra Stefan.
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
CSE 373: Data Structures and Algorithms
Presentation transcript:

LECTURE 35: COLLISIONS CSC 212 – Data Structures

Map Performance  Want fast implementation  911 Operators immediately need addresses  Performance of Google measured in TB/s  O(log n) time too slow to achieve these times  Would love to use arrays  Converts key to int with hash functions  Once hashed, put, remove & get in O(1) time  But is this even possible?

Hash Table  Array locations either:  null  Reference to Entry  Marker value *  May not be contiguous  Better when spread out  Hash key to index  Begin methods with hash  Entry at that index Hash Table Entry s “Jay Doe” “Bob Doe” “Jill Roe” … … “Rhi Smith” 9999

Ideal World  key hashed to unique index  Hash and done, Entry is there And then… You wake up

Bad Hash  Perfect hash does not exist  Cannot know all keys beforehand  Cluster around a few indices  Or find all keys hashed to same index  Handling bad hash is a necessary  Given Entry must first check key  Store multiple Entry s with same hash  (Shot of adrenaline restarts heart) ‏

Collisions

Linear Probing  Algorithm used in musical chairs  At index where key hashed examine Entry  Circle through array until empty index found  Algorithm is very simple  But creates clusters of Entry s

Linear Probe Example h(x) = x mod 13 Now add: 44 h(44) = 5 20 h(20) = 7 22 h(22) = 9 31 h(31) =

Probing Reaction Oh, **** Adding to hash table still O(n)‏

Quadratic Probe  Avoids primary clustering problems  But does create secondary clustering  No one cares about secondary clustering  Linear and quadratic probe equally complex  Where key is hashed, k, examine Entry  Check (k + j 2 ) % length: k +1, k +4, k +9, k +16, …  Continue probing until unused array slot found  Guaranteed to work when:  Table size is prime number  Under 50% full

Quadratic Probe Example h(x) = x mod 13 Now add: 44 h(44) = 5 20 h(20) = 7 22 h(22) = 9 31 h(31) = 5

Quadratic Probing Reaction Darn it to heck. Adding to hash table still O(n)‏

 Solve bad hash with even more hash  Use 2 nd hash function very different from first  2 nd hash function not allowed to return zero  Re-hash key using 2 nd function after the collision  Check index equal to sum of two hash function  Re-add 2 nd hash for more probes  Guaranteed to work when  Table size is prime number Double Hashing

Double Hash Example h(x) = x mod 13 h 2 (x) = 5 – (x mod 5) Now add: 44 h(44) = 5 20 h(20) = 7 22 h(22) = 9 31 h(31) = 5

Double Probing Reaction Sweet! Double hashing keeps put O(n)‏

Probing and Searching  Search index where key hashed  If cannot place Entry at index  The array must keep being probed  Stop only at usable index  May need to probe every index!  Searching takes O(n) even with hash  May need to reallocate & rehash table  Worst case O(n) put even with perfect hash

Linear Probe Example  What happens when we remove an Entry ?  Set index to null in most structures  Consider if we call remove(44)‏  get(31) called, what would happen?  First check index it is hashed  Checks first probe indexed… & stops at null Even Better! I buggy code!

* Marker Value Explained  Mark cleared indices in hash table  Since collision could have happened, continue search  Index can be used to store new Entry  Ways to show that array index is clear  Entry with null key could be used  Make key which is never used  Use static final field of type Entry

 Hash tables can require O(n) complexity  Provide O(1) time if you are really good  Ultimately depends on hash function used  Choose wisely and be rich Why Use Hash Table & Probes?

 Finish week #12 assignment  Due at usual time, whatever that may be  Work on programming project #4 (due 12/1) ‏  If you want to take the 2 nd chance midterm  Study before test on Thursday from 9:30 – 10:20  Read sections – of the book  Start discussion of what hash is  How we implement hash tables  What makes hash & hashing good Before Next Lecture…