Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.

Slides:



Advertisements
Similar presentations
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Tables,
Advertisements

CSE 1302 Lecture 23 Hashing and Hash Tables Richard Gesick.
Hashing as a Dictionary Implementation
Appendix I Hashing. Chapter Scope Hashing, conceptually Using hashes to solve problems Hash implementations Java Foundations, 3rd Edition, Lewis/DePasquale/Chase21.
Hashing Chapters What is Hashing? A technique that determines an index or location for storage of an item in a data structure The hash function.
Hashing21 Hashing II: The leftovers. hashing22 Hash functions Choice of hash function can be important factor in reducing the likelihood of collisions.
Maps, Dictionaries, Hashtables
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 48 Hashing.
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.
1 CSE 326: Data Structures Hash Tables Autumn 2007 Lecture 14.
Sets and Maps (and Hashing)
Introduction to Hashing CS 311 Winter, Dictionary Structure A dictionary structure has the form: (Key, Data) Dictionary structures are organized.
Tirgul 7. Find an efficient implementation of a dynamic collection of elements with unique keys Supported Operations: Insert, Search and Delete. The keys.
Sets and Maps Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Tirgul 8 Hash Tables (continued) Reminder Examples.
Hashing General idea: Get a large array
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.
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
CSE 373 Data Structures and Algorithms Lecture 18: Hashing III.
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Hashing as a Dictionary Implementation Chapter 20 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
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.
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.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Hash Functions and the HashMap Class A Brief Overview On Green Marble John W. Benning.
CS212: DATA STRUCTURES Lecture 10:Hashing 1. Outline 2  Map Abstract Data type  Map Abstract Data type methods  What is hash  Hash tables  Bucket.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 10.
Big Java Chapter 16.
Hashing Table Professor Sin-Min Lee Department of Computer Science.
Hashing Chapter 20. Hash Table A hash table is a data structure that allows fast find, insert, and delete operations (most of the time). The simplest.
Copyright © 2002, Systems and Computer Engineering, Carleton University Hashtable.ppt * Object-Oriented Software Development Unit 8.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
TECH Computer Science Dynamic Sets and Searching Analysis Technique  Amortized Analysis // average cost of each operation in the worst case Dynamic Sets.
Sets, Maps and Hash Tables. RHS – SOC 2 Sets We have learned that different data struc- tures have different advantages – and drawbacks Choosing the proper.
Hashing Hashing is another method for sorting and searching data.
Hashing as a Dictionary Implementation Chapter 19.
1 Introduction to Hashing - Hash Functions Sections 5.1, 5.2, and 5.6.
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.
1 Hashing - Introduction Dictionary = a dynamic set that supports the operations INSERT, DELETE, SEARCH Dictionary = a dynamic set that supports the operations.
Chapter 10 Hashing. The search time of each algorithm depend on the number n of elements of the collection S of the data. A searching technique called.
Chapter 11 Hash Anshuman Razdan Div of Computing Studies
1 CSC 427: Data Structures and Algorithm Analysis Fall 2011 Space vs. time  space/time tradeoffs  hashing  hash table, hash function  linear probing.
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.
Copyright © Curt Hill Hashing A quick lookup strategy.
Chapter 13 C Advanced Implementations of Tables – Hash Tables.
Hash Tables From “Algorithms” (4 th Ed.) by R. Sedgewick and K. Wayne.
Hash Tables and Hash Maps. DCS – SWC 2 Hash Tables A Set and a Map are both abstract data types – we need a concrete implemen- tation in order to use.
Hashing O(1) data access (almost) -access, insertion, deletion, updating in constant time (on average) but at a price… references: Weiss, Goodrich & Tamassia,
1 Data Structures CSCI 132, Spring 2014 Lecture 33 Hash Tables.
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.
Collections Dwight Deugo Nesa Matic
CSE 373: Data Structures and Algorithms Lecture 16: Hashing III 1.
Hashing By Emily Nelson. The Official Definition Using a hash function to turn some kind of data in relatively small integers or Strings The “hash code”
CSC 212 – Data Structures Lecture 28: More Hash and Dictionaries.
Building Java Programs Generics, hashing reading: 18.1.
Appendix I Hashing.
Sets and Maps Chapter 9.
TCSS 342, Winter 2006 Lecture Notes
Slides by Steve Armstrong LeTourneau University Longview, TX
Efficiency add remove find unsorted array O(1) O(n) sorted array
CSE 373: Data Structures and Algorithms
Chapter 21 Hashing: Implementing Dictionaries and Sets
Sets and Maps Chapter 9.
slides created by Marty Stepp
Presentation transcript:

Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Lookup Tables and Hashing

24-2 Objectives: Learn about lookup tables Learn about hashing Review java.util.HashSet and java.util.HashMap

24-3 Lookup Tables A lookup table is a one-dimensional array that helps to find data very quickly. The array stores references to data records (or some values). A data record is identified by some key. The value of a key is directly translated into an array index using a simple formula.

24-4 Lookup Tables (cont’d) Only one key can be mapped onto a particular index (no collisions). The index that corresponds to a key must fall into the valid range (from 0 to array.length - 1). Access to data is “instantaneous” (O(1)).

24-5 Lookup Tables: Example 1 Zip codes Corresponding locales Some table entries remain unused

24-6 Lookup Tables: Example 2 private static final int [ ] n_thPowerOf3 = { 1, 3, 9, 27, 81, 243, 729, 2187, 6561, };... // precondition: 0 <= n < 10 public int powOf3 (int n) { return n_thPowerOf3 [ n ]; }

24-7 Lookup Tables: Example colors used in a particular image; each of the palette entries corresponds to a triplet of RGB values

24-8 Applications of Lookup Tables Data retrieval Data compression and encryption Tabulating functions Color mapping

24-9 Hash Tables A hash table is similar to a lookup table. The value of a key is translated into an array index using a hash function. The index computed for a key must fall into the valid range. The hash function can map different keys onto the same array index — this situation is called a collision.

24-10 Hash Tables (cont’d) The hash function should map the keys onto the array indices randomly and uniformly. A well-designed hash table and hash function minimize the number of collisions. There are two common techniques for resolving collisions: chaining and probing.

24-11 Chaining Buckets Each element in the array is itself a collection, called a bucket (a list or a BST), which is searched for the desired key

24-12 Probing The probing function recalculates the index If the place where we want to store the key is occupied by a different key, we store the former in another location in the same array, computed using a certain probing formula

24-13 java.util.HashSet and java.util.HashMap Classes These classes implement the Set and Map interfaces, respectively, using hash tables (with chaining). This implementation may be more efficient than TreeSet and TreeMap.

24-14 HashSet and HashMap (cont’d) Collisions are resolved through chaining. The sizes of buckets must remain relatively small. Load factor: Load factor = Total number of items Number of buckets

24-15 HashSet and HashMap (cont’d) Fine tuning:  Load factor too large  lots of collisions  Load factor too small  wasted space and slow iterations over the whole set If the load factor exceeds the specified limit, the table is automatically rehashed into a larger table; if possible this should be avoided.

24-16 HashSet and HashMap (cont’d) Objects in a HashSet or keys in a HashMap must have a reasonable int hashCode method that overrides Object’s hashCode and helps calculate the hashing function. The hashCode method returns an int from the entire int range; it is later mapped on the range of indices in a particular hash table. String, Integer, Double each have a reasonable hashCode defined.

24-17 hashCode Examples For String:  (where s i is Unicode for the i-th character in the string) For Person: public int hashCode ( ) { return getFirstName ().hashCode () + getLastName ().hashCode (); }

24-18 Consistency HashSet / HashMap first use hashCode, then equals. TreeSet / TreeMap use only compareTo (or a comparator) For consistent performance, these methods should agree with each other:  x.equals (y)  x.compareTo (y) == 0  x.equals (y)  x.hashCode() == y.hashCode()

24-19 HashSet Constructors Never mind...

24-20 HashMap Constructors

24-21 Review: What is the main difference between a lookup table and a hash table? What is a collision? Name two common techniques for resolving collisions. What is a bucket? What is a load factor?

24-22 Review (cont’d): How is hash table performance affected when the load factor is too high? Too low? What happens to a HashSet or a HashMap when the load factor exceeds the specified limit? HashSet’s no-args constructor sets the initial capacity to 16 and the load factor limit to How many values can be stored in this table before it is rehashed?

24-23 Review (cont’d): What is the sequence of values returned by an iterator for a HashSet? What is the range of values for the hashCode method? Which method(s) of an object are used to find it in a TreeSet? A HashSet?