slides created by Marty Stepp

Slides:



Advertisements
Similar presentations
CSE 1302 Lecture 23 Hashing and Hash Tables Richard Gesick.
Advertisements

Hashing Techniques.
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.
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.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
Hashing as a Dictionary Implementation Chapter 19.
CSC 427: Data Structures and Algorithm Analysis
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.
Building Java Programs Bonus Slides Hashing. 2 Recall: ADTs (11.1) abstract data type (ADT): A specification of a collection of data and 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.
1 CSE 331 Hash codes; annotations slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Chapter 11 Sets © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
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.
CSE 373: Data Structures and Algorithms Lecture 16: Hashing III 1.
Building Java Programs Generics, hashing reading: 18.1.
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.
Appendix I Hashing.
Sets and Maps Chapter 9.
Sections 10.5 – 10.6 Hashing.
Searching, Maps,Tries (hashing)
Hashing.
TCSS 342, Winter 2006 Lecture Notes
Recitation 10 Prelim Review.
Hashing CSE 2011 Winter July 2018.
School of Computer Science and Engineering
CSC 427: Data Structures and Algorithm Analysis
Efficiency add remove find unsorted array O(1) O(n) sorted array
Hash Tables.
Hash tables Hash table: a list of some fixed size, that positions elements according to an algorithm called a hash function … hash function h(element)
Hashing CS2110 Spring 2018.
Design and Analysis of Algorithms
Advanced Associative Structures
Hash Table.
CSE373: Data Structures & Algorithms Lecture 14: Hash Collisions
Building Java Programs
Hashing CS2110.
Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Chapter 21 Hashing: Implementing Dictionaries and Sets
Resolving collisions: Open addressing
CSE373: Data Structures & Algorithms Lecture 14: Hash Collisions
Data Structures and Algorithms
slides created by Marty Stepp and Hélène Martin
EE 422C Sets.
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
slides adapted from Marty Stepp and Hélène Martin
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
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 Hash set implementation, continued
CSE 373, Copyright S. Tanimoto, 2002 Hashing -
CS202 - Fundamental Structures of Computer Science II
Advanced Implementation of Tables
CSE 143 Lecture 25 Set ADT implementation; hashing read 11.2
CSE 143 Lecture 25 Advanced collection classes
Sets and Maps Chapter 9.
CSE 373 Separate chaining; hash codes; hash maps
Recitation 10 Prelim Review.
Building Java Programs
Collision Handling Collisions occur when different elements are mapped to the same cell.
Hashing based on slides by Marty Stepp
slides created by Marty Stepp and Hélène Martin
CSE 373 Set implementation; intro to hashing
Chapter 13 Hashing © 2011 Pearson Addison-Wesley. All rights reserved.
CSE 373: Data Structures and Algorithms
slides created by Marty Stepp and Hélène Martin
Presentation transcript:

slides created by Marty Stepp CSE 143 Lecture 25 Hashing read 11.2 slides created by Marty Stepp http://www.cs.washington.edu/143/

SearchTree as a set We implemented a class SearchTree to store a BST of ints: Our BST is essentially a set of integers. Operations we support: add contains remove ... But there are other ways to implement a set... 91 60 87 29 55 42 -3 overallRoot Lets try to think about how we might make a set from a binary search tree - we add by traversing the tree and finding the proper insertion point - we remove in a similar way, find the node, replace with next largest - contains is simply traversing the tree

Implementing a HashSet Elements of a TreeSet (IntTree) are in BST sorted order. We need this in order to add or search in O(log N ) time. But it doesn't really matter what order the elements appear in a set, so long as they can be added and searched quickly. Consider the task of storing a set in an array. What would make a good ordering for the elements? we get sortedness from a binary search tree, but we don't need it if we were storing ints we could simply store them in an array We get locality from the first strategy, a contains call can be performed in linear time Is this second way linear? Do we have to traverse the whole array now? index 1 2 3 4 5 6 7 8 9 value 11 24 49 index 1 2 3 4 5 6 7 8 9 value 11 24 49

Hashing hash: To map a value to an integer index. hash table: An array that stores elements via hashing. hash function: An algorithm that maps values to indexes. one possible hash function for integers: HF(I)  I % length set.add(11); // 11 % 10 == 1 set.add(49); // 49 % 10 == 9 set.add(24); // 24 % 10 == 4 set.add(7); // 7 % 10 == 7 This idea is exactly what a hash set uses to perform operations in constant time To hash is to transform a value into an integer When we use this technique to stores elements in an array we call the structure a hash table The function we use to map between the values and integers is called a hash function index 1 2 3 4 5 6 7 8 9 value 11 24 49

Hashing objects It is easy to hash an integer I (use index I % length ). How can we hash other types of values (such as objects)? All Java objects contain the following method: public int hashCode() Returns an integer hash code for this object. We can call hashCode on any object to find its preferred index. How is hashCode implemented? Depends on the type of object and its state. You can write your own hashCode methods in classes you write. A hash table wouldn’t be much good if all we could store are ints We need a way to map any object to an integer In Java we can do this by using the built in HashCode function that comes with all objects This is a method that is built into all objects, since all objects implictly extend Object, we get this method for free You can sorta think of a hashCode like the int version of toString. What is different? How do you think String’s compute their hashCode?

String's hashCode The hashCode function for String objects looks like this: public int hashCode() { int hash = 0; for (int i = 0; i < this.length(); i++) { hash = 31 * hash + this.charAt(i); } return hash; Early versions of the Java examined only the first 16 characters. For some common data this led to poor hash table performance. As with any general hashing function, collisions are possible. Example: "Ea" and "FB" have the same hash value.

Efficiency of hashing Add: simply set elementData[hashCode(i)] = i; public static int hashCode(int i) return Math.abs(i) % elementData.length; } Add: simply set elementData[hashCode(i)] = i; Search: check if elementData[hashCode(i)] == i; Remove: set elementData[hashCode(i)] = 0; What is the runtime of add, contains, and remove? O(1)! OMGWTFBBQFAST Are there any problems with this approach?

Collisions collision: When a hash function maps two or more elements to the same index. set.add(11); set.add(49); set.add(24); set.add(7); set.add(54); // collides with 24! collision resolution: An algorithm for fixing collisions. - Collisions are one of the major problems with hash tables. We need some way to resolve the conflicts. index 1 2 3 4 5 6 7 8 9 value 11 54 49

Probing probing: Resolving a collision by moving to another index. linear probing: Moves to the next index. set.add(11); set.add(49); set.add(24); set.add(7); set.add(54); // collides with 24 Is this a good approach? There are other approaches such as quadratic probing. What does this mean if we don’t find the desired value? When can we stop? index 1 2 3 4 5 6 7 8 9 value 11 24 54 49

Clustering clustering: Clumps of elements at neighboring indexes. slows down the hash table lookup; you must loop through them. set.add(11); set.add(49); set.add(24); set.add(7); set.add(54); // collides with 24 set.add(14); // collides with 24, then 54 set.add(86); // collides with 14, then 7 Now a lookup for 94 must look at 7 out of 10 total indexes. index 1 2 3 4 5 6 7 8 9 value 11 24 54 14 86 49

Chaining chaining: Resolving collisions by storing a list at each index. add/search/remove must traverse lists, but the lists are short impossible to "run out" of indexes, unlike with probing index 1 2 3 4 5 6 7 8 9 value 11 24 7 49 - Is this still constant time? 54 14

Rehashing rehash: Growing to a larger array when the table is too full. Cannot simply copy the old array to a new one. (Why not?) load factor: ratio of (# of elements ) / (hash table length ) many collections rehash when load factor ≅ .75 can use big prime numbers as hash table sizes to reduce collisions 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 24 7 49 11 54 14

Implementing hash maps A hash map is just a set where the lists store key/value pairs: // key value map.put("Marty", 14); map.put("Jeff", 21); map.put("Kasey", 20); map.put("Stef", 35); Instead of a List<Integer>, write an inner Entry node class with key and value fields; the map stores a List<Entry> index 1 2 3 4 5 6 7 8 9 value "Stef" 35 "Marty" 14 "Jeff" 21 "Kasey" 20