“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.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Tables,
CSE 1302 Lecture 23 Hashing and Hash Tables Richard Gesick.
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:
CSCE 3400 Data Structures & Algorithm Analysis
Hashing as a Dictionary Implementation
© 2004 Goodrich, Tamassia Hash Tables1  
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.
Theory I Algorithm Design and Analysis (7 Hashing: Open Addressing)
Implementation of Linear Probing (continued) Helping method for locating index: private int findIndex(long key) // return -1 if the item with key 'key'
Maps, Dictionaries, Hashtables
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.
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.
Maps & Hashing Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
1 CSE 326: Data Structures Hash Tables Autumn 2007 Lecture 14.
HASHING CSC 172 SPRING 2002 LECTURE 22. Hashing A cool way to get from an element x to the place where x can be found An array [0..B-1] of buckets Bucket.
Hash Tables1 Part E Hash Tables  
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.
CS 206 Introduction to Computer Science II 04 / 06 / 2009 Instructor: Michael Eckmann.
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.
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).
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 17 Disk Storage, Basic File Structures, and Hashing.
HASHING Section 12.7 (P ). HASHING - have already seen binary and linear search and discussed when they might be useful (based on complexity)
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture8.
CS212: DATA STRUCTURES Lecture 10:Hashing 1. Outline 2  Map Abstract Data type  Map Abstract Data type methods  What is hash  Hash tables  Bucket.
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.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 4 Search Algorithms.
Hash Tables1   © 2010 Goodrich, Tamassia.
Appendix E-A Hashing Modified. Chapter Scope Concept of hashing Hashing functions Collision handling – Open addressing – Buckets – Chaining Deletions.
“Enthusiasm releases the drive to carry you over obstacles and adds significance to all you do.” – Norman Vincent Peale Thought for the Day.
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 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.
CSC 427: Data Structures and Algorithm Analysis
Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.
Chapter 11 Hash Tables © John Urrutia 2014, All Rights Reserved1.
Chapter 11 Hash Anshuman Razdan Div of Computing Studies
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.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
Tirgul 11 Notes Hash tables –reminder –examples –some new material.
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.
CPSC 252 Hashing Page 1 Hashing We have already seen that we can search for a key item in an array using either linear or binary search. It would be better.
Hash Tables © Rick Mercer.  Outline  Discuss what a hash method does  translates a string key into an integer  Discuss a few strategies for implementing.
1 the hash table. hash table A hash table consists of two major components …
Hashing O(1) data access (almost) -access, insertion, deletion, updating in constant time (on average) but at a price… references: Weiss, Goodrich & Tamassia,
Week 9 - Friday.  What did we talk about last time?  Collisions  Open addressing ▪ Linear probing ▪ Quadratic probing ▪ Double hashing  Chaining.
Searching Tables Table: sequence of (key,information) pairs (key,information) pair is a record key uniquely identifies information, so no duplicate records.
CMSC 341 Hashing Readings: Chapter 5. Announcements Midterm II on Nov 7 Review out Oct 29 HW 5 due Thursday CMSC 341 Hashing 2.
Hash Tables Ellen Walker CPSC 201 Data Structures Hiram College.
“Sense shines with a double luster when it is set in humility. An able and yet humble man is a jewel worth a kingdom.” – William Penn Thought for the Day.
Chapter 5 Record Storage and Primary File Organizations
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.
Hashing CSE 2011 Winter July 2018.
Slides by Steve Armstrong LeTourneau University Longview, TX
Week 8 - Wednesday CS221.
Advanced Associative Structures
Searching Tables Table: sequence of (key,information) pairs
Collision Handling Collisions occur when different elements are mapped to the same cell.
Presentation transcript:

“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 for the Day

Hash Functions Essential ingredient for a successful hash table Numeric keys –simple Textual (alphanumeric) keys –e.g. 609A1234 –Not so simple! key Hash Function index

Collisions Prevented by using perfect hashing functions But this is not always possible Other solutions? Two types of hash tables: –Internal hashing –External hashing

Internal Hashing with Open Addressing Hash the key to find location If it is already occupied: start “probing” –simple approach: try immediately following positions programming programming words 59Collision! words Probe

Probing Example shows linear probing Other possibilities –add constant amount to hash value –add variable amount to hash value –apply second hash function to get probe “distance” Must be consistent

Important Side Effect Must be very careful when removing items programmingwords Must mark deleted entries One of three states: –empty –occupied –deleted X

Clustering and Efficiency As the hash table fills up collisions become more frequent Clustering –primary clustering –secondary clustering Efficiency decreases

Clustering: Solutions? Make the table bigger than required Use external hashing Use more sophisticated probing techniques –helps decrease secondary clustering

Deletions Decrease efficiency of searching If deletions are frequent, need to consider rebuilding the table periodically

External Hashing Using the hash function identifies a bucket into which the key should be placed programming 59 programming words 59 Collision! words

External Hashing Buckets hold several values As the buckets fill up, efficiency decreases –no worse than probing –usually better (no “secondary clustering”) Big advantage: –space is not limited by table size

Implementing the Buckets Many possible approaches –linked lists –binary search trees –secondary hash tables! We will use a simple unordered linked list

The ExternalHashTable Class public class ExternalHashTable implements Dictionary { private static final int DEF_SIZE = 101; private class EntryNode extends DictionaryPair { EntryNode next; } // class EntryNode private EntryNode [] table; private int hash (K aKey) // Scale hash value for table size {... } // hash... } // class ExternalHashTable The buckets

Hashing Function How do we handle the hashing function? Java to the rescue! –Object class has hashCode method public int hashCode ()

Using the hashCode Method private int hash (K aKey) // Scale hash value for table size { return ((aKey.hashCode() & 0x7FFFFFFF) % table.length); } // hash Need to ensure it is positive and in correct range:

The Buckets table... key value key value key value

The insert Method public void insert (K aKey, V aValue) // Insert new element or update existing one { int index = hash(aKey); // Look for aKey in linked list EntryNode c; for (c = table[index]; c != null && ! c.getKey().equals(aKey); c = c.next) ;... } // insert

The insert Method (cont.)... if (c == null) // Insert new node { EntryNode n = new EntryNode (aKey, aValue); n.next = table[index]; table[index] = n; } else // Update existing entry c.setValue(aValue);

Other Methods Quite simple Remember structure: –array of linked lists Example: public boolean isEmpty () // Tell whether hash table is empty { for (int k = 0; k < table.length; k++) if (table[k] != null) return false; // Found at least one entry return true; // Found no entries } // isEmpty

External Hash Table: Iterators Slightly more complicated: –need to work through array –and work through linked lists table... key value key value key value