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.

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Tables,
Hash Tables and Sets Lecture 3. Sets A set is simply a collection of elements Unlike lists, elements are not ordered Very abstract, general concept with.
Hashing as a Dictionary Implementation
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Fall 2007CS 225 Sets and Maps Chapter 9. Fall 2007CS 225 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn.
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.
Fall 2007CS 225 Sets and Maps Chapter 7. Fall 2007CS 225 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn.
CSE 373 Data Structures and Algorithms Lecture 18: Hashing III.
Building Java Programs Inner classes, generics, abstract classes reading: 9.6, 15.4,
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
CS2110 Recitation Week 8. Hashing Hashing: An implementation of a set. It provides O(1) expected time for set operations Set operations Make the set empty.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
CSE 143 Lecture 20 Binary Search Trees continued; Tree Sets read slides created by Marty Stepp and Hélène Martin
111 © 2002, Cisco Systems, Inc. All rights reserved.
Chapter 18 Java Collections Framework
Hashing Hashing is another method for sorting and searching data.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
Hashing as a Dictionary Implementation Chapter 19.
The Map ADT and Hash Tables. 2 The Map ADT  Map: An abstract data type where a value is "mapped" to a unique key  Need a key and a value to insert new.
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.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
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.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
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.
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Implementing the Map ADT.  The Map ADT  Implementation with Java Generics  A Hash Function  translation of a string key into an integer  Consider.
Building Java Programs Generics, hashing reading: 18.1.
Sets and Maps Chapter 9.
CSE 143 Lecture 20 Binary Search Trees continued; Tree Sets
Building Java Programs
A tree set Our SearchTree class is essentially a set.
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)
Building Java Programs
Hashing CS2110.
CSE 373: Data Structures and Algorithms
Building Java Programs
A tree set Our SearchTree class is essentially a set.
slides created by Marty Stepp and Hélène Martin
EE 422C Sets.
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
CSE 373 Hash set implementation, continued
Binary Search Trees continued; Tree Sets
slides created by Marty Stepp
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
slides created by Marty Stepp
Building Java Programs
Hashing based on slides by Marty Stepp
slides created by Marty Stepp and Hélène Martin
Lecture 7: Linked List Basics reading: 16.2
CSE 373 Set implementation; intro to hashing
slides created by Marty Stepp and Hélène Martin
Presentation transcript:

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 that can be performed on it. –Describes what a collection does, not how it does it. Java's collection framework describes ADTs with interfaces: –Collection, Deque, List, Map, Queue, Set, SortedMap An ADT can be implemented in multiple ways by classes: –ArrayList and LinkedList implement List –HashSet and TreeSet implement Set –LinkedList, ArrayDeque, etc.implement Queue

3 SearchTree as a set We implemented a class SearchTree to store a BST of int s: Our BST is essentially a set of integers. Operations we support: –add –contains –remove... But there are other ways to implement a set overallRoot

4 How to implement a set? 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? index value index value

5 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 index value

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

7 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. index value

8 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? index value

9 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 value

10 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 value

11 Hash set code import java.util.*; // for List, LinkedList // All methods assume value != null; does not rehash public class HashIntSet { private static final int CAPACITY = 137; private List [] elements; // constructs new empty set public HashSet() { elements = (List []) (new List[CAPACITY]); } // adds the given value to this hash set public void add(int value) { int index = HF(value); if (elements[index] == null) { elements[index] = new LinkedList (); } elements[index].add(value); } // hashing function to convert objects to indexes private int HF(int value) { return Math.abs(value) % elements.length; }...

12 Final hash set code 2... // Returns true if this set contains the given value. public boolean contains(int value) { int index = HF(value); return elements[index] != null && elements[index].contains(value); } // Removes the given value from the set, if it exists. public void remove(int value) { int index = HF(value); if (elements[index] != null) { elements[index].remove(value); }

13 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

14 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. Example: a String's hashCode adds the ASCII values of its letters. –You can write your own hashCode methods in classes you write.

15 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, write an inner Entry node class with key and value fields; the map stores a List index value "Jeff"21 "Marty"14 "Kasey"20 "Stef " 35