Maps & Hashing Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.

Slides:



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

An Introduction to Hashing. By: Sara Kennedy Presented: November 1, 2002.
CSCE 3400 Data Structures & Algorithm Analysis
Hashing as a Dictionary Implementation
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.
Midterm 2 Overview Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Hashing. 2 Searching Consider the problem of searching an array for a given value –If the array is not sorted, the search requires O(n) time If the value.
Hashing. Searching Consider the problem of searching an array for a given value –If the array is not sorted, the search requires O(n) time If the value.
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.
Hashing Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Sets and Maps (and Hashing)
Data Structures & Java Collections Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Hashing. 2 Preview A hash function is a function that: When applied to an Object, returns a number When applied to equal Objects, returns the same number.
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Hashing Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Tree Traversals & Maps Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 206 Introduction to Computer Science II 04 / 06 / 2009 Instructor: Michael Eckmann.
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.
COSC 2007 Data Structures II
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
ICS220 – Data Structures and Algorithms Lecture 10 Dr. Ken Cosh.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
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.
Not overriding equals  what happens if you do not override equals for a value type class?  all of the Java collections will fail in confusing ways 1.
TECH Computer Science Dynamic Sets and Searching Analysis Technique  Amortized Analysis // average cost of each operation in the worst case Dynamic Sets.
ADSA: Hashing/ Advanced Data Structures and Algorithms Objectives – –introduce hashing, hash functions, hash tables, collisions, linear probing,
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 PROJECT 1. SEARCHING DATA STRUCTURES Consider a set of data with N data items stored in some data structure We must be able to insert, delete.
Hashing as a Dictionary Implementation Chapter 19.
1 Introduction to Hashing - Hash Functions Sections 5.1, 5.2, and 5.6.
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,
Hashing is a method to store data in an array so that sorting, searching, inserting and deleting data is fast. For this every record needs unique key.
Chapter 11 Hash Anshuman Razdan Div of Computing Studies
“Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought.
Hash Tables. 2 Exercise 2 /* Exercise 1 */ void mystery(int n) { int i, j, k; for (i = 1; i
SETS AND HASHING. SETS An un-ordered collection of values Operations (S and T are sets): S ∩ T // the intersection of S and T S U T // The Union of S.
A Introduction to Computing II Lecture 11: Hashtables Fall Session 2000.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
COSC 1030 Lecture 10 Hash Table. Topics Table Hash Concept Hash Function Resolve collision Complexity Analysis.
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 Hashing by Adlane Habed School of Computer Science University of Windsor May 6, 2005.
Hashing O(1) data access (almost) -access, insertion, deletion, updating in constant time (on average) but at a price… references: Weiss, Goodrich & Tamassia,
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.
Hashing. Searching Consider the problem of searching an array for a given value If the array is not sorted, the search requires O(n) time If the value.
Sections 10.5 – 10.6 Hashing.
Chapter 27 Hashing Jung Soo (Sue) Lim Cal State LA.
Hashing.
Hashing.
Slides by Steve Armstrong LeTourneau University Longview, TX
Efficiency add remove find unsorted array O(1) O(n) sorted array
Advanced Associative Structures
CMSC 341 Hashing Prof. Neary
Chapter 28 Hashing.
Hashing.
Chapter 21 Hashing: Implementing Dictionaries and Sets
CS202 - Fundamental Structures of Computer Science II
Hashing.
Hashing.
Hashing.
Hashing.
Hashing.
Skip List: Implementation
Presentation transcript:

Maps & Hashing Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park

Overview Maps Hashing Hash function Hash table

Set Data Structures Types Set Map Hash Table SetMapHash Table h(n)

Maps Map (associative array) Unordered collection of objects (values) One or more keys associated with each object Can use key to retrieve object Example A[“key1”] = … key1 key2 key3 key4

Maps Methods Void Put( Key, Object )// inserts element Object Get( Key )// returns element Void Remove( Key )// removes element

Maps Implementation approaches Two parallel arrays Unsorted Sorted Linked list Binary search tree Hash table

Maps Complexity (average case) PutGetRemove Unsorted array O(1) O(n) O(n) Sorted array O(n) O(log(n)) O(n) Linked list O(1)O(n)O(n) Binary search tree O(log(n)) O(log(n)) O(log(n)) Hash table O(1) O(1) O(1)

Hashing Approach Transform key into number (hash value) Use hash value to index object in hash table Use hash function to convert key to number

Hashing Hash Table Array indexed using hash values Hash Table A with size N Indices of A range from 0 to N-1 Store in A[ hashValue % N]

Hash Function Goal Scatter values uniformly across range Hash( ) = 0 Satisfies definition of hash function But not very useful Multiplicative congruency method Produces good hash values Hash value = (a  int(key)) % N Where N is table size a, N are large primes

Hash Function Example hashCode("apple") = 5 hashCode("watermelon") = 3 hashCode("grapes") = 8 hashCode("kiwi") = 0 hashCode("strawberry") = 9 hashCode("mango") = 6 hashCode("banana") = 2 Perfect hash function Unique values for each key kiwi banana watermelon apple mango grapes strawberry

Hash Function Suppose now hashCode("apple") = 5 hashCode("watermelon") = 3 hashCode("grapes") = 8 hashCode("kiwi") = 0 hashCode("strawberry") = 9 hashCode("mango") = 6 hashCode("banana") = 2 hashCode(“orange") = 3 Collision Same hash value for multiple keys kiwi banana watermelon apple mango grapes strawberry

Types of Hash Tables Open addressing Store objects in each table entry Chaining (bucket hashing) Store lists of objects in each table entry

Open Addressing Hashing Approach Hash table contains objects Probe  examine table entry Collision Move K entries past current location Wrap around table if necessary Find location for X 1. Examine entry at A[ key(X) ] 2. If entry = X, found 3. If entry = empty, X not in hash table 4. Else increment location by K, repeat

Open Addressing Hashing Approach Linear probing K = 1 May form clusters of contiguous entries Deletions Find location for X If X inside cluster, leave non-empty marker Insertion Find location for X Insert if X not in hash table Can insert X at first non-empty marker

Open Addressing Example Hash codes H(A) = 6H(C) = 6 H(B) = 7H(D) = 7 Hash table Size = 8 elements  = empty entry * = non-empty marker Linear probing Collision  move 1 entry past current location 

Open Addressing Example Operations Insert A, Insert B, Insert C, Insert D AA ABAB ABCABC DABCDABC

Open Addressing Example Operations Find A, Find B, Find C, Find D DABCDABC DABCDABC DABCDABC DABCDABC

Open Addressing Example Operations Delete A, Delete C, Find D, Insert C DCB*DCB* D*BCD*BC D*B*D*B* D*B*D*B*

Efficiency of Open Hashing Load factor = entries / table size Hashing is efficient for load factor < 90%

Chaining (Bucket Hashing) Approach Hash table contains lists of objects Find location for X Find hash code key for X Examine list at table entry A[ key ] Collision Multiple entries in list for entry

Chaining Example Hash codes H(A) = 6H(C) = 6 H(B) = 7H(D) = 7 Hash table Size = 8 elements  = empty entry 

Chaining Example Operations Insert A, Insert B, Insert C    A  A B  C B A

Chaining Example Operations Find B, Find A  C B A  C B A

Efficiency of Chaining Load factor = entries / table size Average case Evenly scattered entries Operations = O( load factor ) Worse case Entries mostly have same hash value Operations = O( entries )

Hashing in Java Collections hashMap & hashSet implement hashing Objects Built-in support for hashing boolean equals(object o) int hashCode() Can override with own definitions Must be careful to support Java contract

Java Contract hashCode() Must return same value for object in each execution, provided no information used in equals comparisons on the object is modified equals() if a.equals(b), then a.hashCode() must be the same as b.hashCode() if a.hashCode() != b.hashCode(), then !a.equals(b) a.hashCode() == b.hashCode() Does not imply a.equals(b) Though Java libraries will be more efficient if it is true