Theory I Algorithm Design and Analysis (6 Hashing: Chaining) Prof. Th. Ottmann.

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Hash Tables
Advertisements

Lecture 6 Hashing. Motivating Example Want to store a list whose elements are integers between 1 and 5 Will define an array of size 5, and if the list.
Theory I Algorithm Design and Analysis (5 Hashing) Prof. Th. Ottmann.
Hashing as a Dictionary Implementation
What we learn with pleasure we never forget. Alfred Mercier Smitha N Pai.
© 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.
September 26, Algorithms and Data Structures Lecture VI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Maps. Hash Tables. Dictionaries. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia.
Theory I Algorithm Design and Analysis (7 Hashing: Open Addressing)
TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT TTIT33 – Algorithms and optimization Lecture 5 Algorithms ADT Map,
Hashing Techniques.
Hash Table indexing and Secondary Storage Hashing.
Dictionaries and Hash Tables1  
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.
Hash Tables1 Part E Hash Tables  
Hash Tables1 Part E Hash Tables  
Design and Analysis of Algorithms - Chapter 71 Hashing b A very efficient method for implementing a dictionary, i.e., a set with the operations: – insert.
Tirgul 9 Hash Tables (continued) Reminder Examples.
Hash Tables1 Part E Hash Tables  
COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables.
Tirgul 8 Hash Tables (continued) Reminder Examples.
Lecture 10: Search Structures and Hashing
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Dictionaries 4/17/2017 3:23 PM Hash Tables  
Lecture 6 Hashing. Motivating Example Want to store a list whose elements are integers between 1 and 5 Will define an array of size 5, and if the list.
Hashing The Magic Container. Interface Main methods: –Void Put(Object) –Object Get(Object) … returns null if not i –… Remove(Object) Goal: methods are.
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.
Spring 2015 Lecture 6: Hash Tables
Hashing Table Professor Sin-Min Lee Department of Computer Science.
1 HashTable. 2 Dictionary A collection of data that is accessed by “key” values –The keys may be ordered or unordered –Multiple key values may/may-not.
Hash Tables1   © 2010 Goodrich, Tamassia.
Dictionaries and Hash Tables1 Hash Tables  
Comp 335 File Structures Hashing.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
Hashing Hashing is another method for sorting and searching data.
© 2004 Goodrich, Tamassia Hash Tables1  
Hashing as a Dictionary Implementation Chapter 19.
WEEK 1 Hashing CE222 Dr. Senem Kumova Metin
“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.
Hashing Chapter 7 Section 3. What is hashing? Hashing is using a 1-D array to implement a dictionary o This implementation is called a "hash table" Items.
Hash Tables. 2 Exercise 2 /* Exercise 1 */ void mystery(int n) { int i, j, k; for (i = 1; i
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.
1 Lecture 21: Hash Tables Wednesday, November 17, 2004.
CHAPTER 9 HASH TABLES, MAPS, AND SKIP LISTS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++,
CS6045: Advanced Algorithms Data Structures. Hashing Tables Motivation: symbol tables –A compiler uses a symbol table to relate symbols to associated.
1 i206: Lecture 12: Hash Tables (Dictionaries); Intro to Recursion Marti Hearst Spring 2012.
1 Data Structures CSCI 132, Spring 2014 Lecture 33 Hash Tables.
CMSC 341 Hashing Readings: Chapter 5. Announcements Midterm II on Nov 7 Review out Oct 29 HW 5 due Thursday CMSC 341 Hashing 2.
CSC 413/513: Intro to Algorithms Hash Tables. ● Hash table: ■ Given a table T and a record x, with key (= symbol) and satellite data, we need to support:
TOPIC 5 ASSIGNMENT SORTING, HASH TABLES & LINKED LISTS Yerusha Nuh & Ivan Yu.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
Lecture14: Hashing Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Hash Tables 1/28/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Hashing Alexandra Stefan.
Week 8 - Wednesday CS221.
EEE2108: Programming for Engineers Chapter 8. Hashing
Hashing Alexandra Stefan.
Dictionaries Dictionaries 07/27/16 16:46 07/27/16 16:46 Hash Tables 
© 2013 Goodrich, Tamassia, Goldwasser
Hash Tables 3/25/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
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
Hashing Alexandra Stefan.
Dictionaries 1/17/2019 7:55 AM Hash Tables   4
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
A Hash Table with Chaining
CS210- Lecture 16 July 11, 2005 Agenda Maps and Dictionaries Map ADT
Presentation transcript:

Theory I Algorithm Design and Analysis (6 Hashing: Chaining) Prof. Th. Ottmann

Possible ways of treating collisions Treatment of collisions: Collisions are treated differently in different methods. A data set with key s is called a colliding element if bucket B h(s) is already taken by another data set. What can we do with colliding elements? 1. Chaining: Implement the buckets as linked lists. Colliding elements are stored in these lists. 2. Open Addressing: Colliding elements are stored in other vacant buckets. During storage and lookup, these are found through so-called probing.

Chaining (1) The hash table is an array (length m) of lists. Each bucket is realized by a list. class hashTable { List[] ht; // an array of lists hashTable (int m){ // Construktor ht = new List[m]; for (int i = 0; i < m; i++) ht[i] = new List(); // Construct a list }... } Two different ways of using lists: 1. Direct chaining: Hash table only contains list headers; the data sets are stored in the lists. 2. Separate chaining: Hash table contains at most one data set in each bucket as well as a list header. Colliding elements are stored in the list.

Hashing by chaining Keys are stored in overflow lists This type of chaining is also known as direct chaining. h(k) = k mod hash table T pointer colliding elements

Chaining Lookup key k - Compute h(k) and overflow list T[h(k)] - Look for k in the overflow list Insert a key k - Lookup k (fails) - Insert k in the overflow list Entfernen eines Schlüssels k - Lookup k (successfully) - Remove k from the overflow list  only list operations

Implementation in Java class TableEntry { private Object key,value; } abstract class HashTable { private TableEntry[] tableEntry; private int capacity; // Constructor HashTable (int capacity) { this.capacity = capacity; tableEntry = new TableEntry [capacity]; for (int i = 0; i <= capacity-1; i++) tableEntry[i] = null; } // the hash function protected abstract int h (Object key); // insert element with given key and value (if not there already) public abstract void insert (Object key Object value); // delete element with given key (if there) public abstract void delete (Object key); // lookup element with given key public abstract Object search (Object key); } // class hashTable

Implementation in Java class ChainedTableEntry extends TableEntry { // Constructor ChainedTableEntry(Object key, Object value) { super(key, value); this.next = null; } private ChainedTableEntry next; } class ChainedHashTable extends HashTable { // the hash function public int h(Object key) { return key.hashCode() % capacity ; } // lookup key in the hash table public Object search (Object key) { ChainedTableEntry p; p = (ChainedTableEntry) tableEntry[h(key)]; // Go through the liste until end reached or key found while (p != null && !p.key.equals(key)) { p = p.next; } // Return result if (p != null) return p.value; else return null; }

Implementation in Java /* Insert an element with given key and value (if not there) */ public void insert (Object key, Object value) { ChainedTableEntry entry = new ChainedTableEntry(key, value); // Get table entry for key int k = h (key); ChainedTableEntry p; p = (ChainedTableEntry) tableEntry [k]; if (p == null){ tableEntry[k] = entry; return ; } // Lookup key while (!p.key.equals(key) && p.next != null) { p = p.next; } // Insert the element (if not there) if (!p.key.equals(key)) p.next = entry; }

Implementation in Java // Delete element with given key (if there) public void delete (Object key) { int k = h (key); ChainedTableEntry p; p = (ChainedTableEntry) TableEntry [k]; TableEntry[k] = recDelete(p, key); } // Delete element with key recursively (if there) public ChainedTableEntry recDelete (ChainedTableEntry p, Object key) { /* recDelete returns a pointer to the start of the list that p points to, in which key was deleted */ if (p == null) return null; if (p.key.equals(key)) return p.getNext(); // otherwise: p.next = recDelete(p.next, key); return p; } public void printTable () {...} } // class ChainedHashTable

Test program public class ChainedHashingTest { public static void main(String args[]){ Integer[] t= new Integer[args.length]; for (int i = 0; i < args.length; i++) t[i] = Integer.valueOf(args[i]); ChainedHashTable h = new ChainedHashTable(7); for (int i = 0; i <= t.length - 1; i++) h.insert(t[i], null); h.printTable (); h.delete(t[0]); h.delete(t[1]); h.delete(t[6]); h.printTable(); } } Call: java ChainedHashingTest Output : 0: -| 0: -| 1: 15 -> 43 -| 1: 15 -| 2: 2 -| 2: 2 -| 3: -| 3: -| 4: 53 -| 4: -| 5: 12 -> 5 -> 19 -| 5: 5 -> 19 -| 6: -| 6: -|

Analysis of direct chaining Uniform hashing assumption: All hash addresses are chosen with the same probability, i.e.: Pr(h(k i ) = j) = 1/m independent from operation to operation Average chain length for n entries: n/m = Definition: C´ n = Expected number of entries inspected during a failed search C n = Expected number of entries inspected during a successful search Analysis:

Chaining Advantages: + C n and C´ n are small + > 1 possible + real distances + suitable for secondary memory Efficiency of lookup C n (erfolgreich) C´ n (erfolglos) Disadvantages: - Additional space for pointers - Colliding elements are outside the hash table

Summary Analysis of hashing with chaining: worst case: h(s) always yields the same value, all data sets are in a list. Behavior as in linear lists. average case: – Successful lookup & delete: complexity (in inspections) ≈ × load factor – Failed lookup & insert: complexity ≈ load factor This holds for direct chaining, with separate chaining the complexity is a bit higher. best case: lookup is an immediate success: complexity  O(1).