COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables.

Slides:



Advertisements
Similar presentations
Chapter 11. Hash Tables.
Advertisements

Hash Tables.
Hash Tables CIS 606 Spring 2010.
Hashing.
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.
CSCE 3400 Data Structures & Algorithm Analysis
© 2004 Goodrich, Tamassia Hash Tables1  
CS 253: Algorithms Chapter 11 Hashing Credit: Dr. George Bebis.
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 18: Hash Tables.
Hashing Techniques.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Hashing CS 3358 Data Structures.
1.1 Data Structure and Algorithm Lecture 9 Hashing Topics Reference: Introduction to Algorithm by Cormen Chapter 12: Hash Tables.
Hash Tables How well do hash tables support dynamic set operations? Implementations –Direct address –Hash functions Collision resolution methods –Universal.
11.Hash Tables Hsu, Lih-Hsing. Computer Theory Lab. Chapter 11P Directed-address tables Direct addressing is a simple technique that works well.
© 2006 Pearson Addison-Wesley. All rights reserved13 A-1 Chapter 13 Hash Tables.
Hash Tables1 Part E Hash Tables  
Hash Tables1 Part E Hash Tables  
Hashing COMP171 Fall Hashing 2 Hash table * Support the following operations n Find n Insert n Delete. (deletions may be unnecessary in some applications)
Hash Tables1 Part E Hash Tables  
Tirgul 7. Find an efficient implementation of a dynamic collection of elements with unique keys Supported Operations: Insert, Search and Delete. The keys.
Lecture 10: Search Structures and Hashing
Hashing General idea: Get a large array
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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.
Hashtables David Kauchak cs302 Spring Administrative Talk today at lunch Midterm must take it by Friday at 6pm No assignment over the break.
Spring 2015 Lecture 6: Hash Tables
Symbol Tables Symbol tables are used by compilers to keep track of information about variables functions class names type names temporary variables etc.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 4 Search Algorithms.
Implementing Dictionaries Many applications require a dynamic set that supports dictionary-type operations such as Insert, Delete, and Search. E.g., a.
1 Hash table. 2 A basic problem We have to store some records and perform the following:  add new record  delete record  search a record by key Find.
Hash Tables1   © 2010 Goodrich, Tamassia.
David Luebke 1 10/25/2015 CS 332: Algorithms Skip Lists Hash Tables.
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
Hashing Sections 10.2 – 10.3 CS 302 Dr. George Bebis.
Hashing Hashing is another method for sorting and searching data.
© 2004 Goodrich, Tamassia Hash Tables1  
Hashing Amihood Amir Bar Ilan University Direct Addressing In old days: LD 1,1 LD 2,2 AD 1,2 ST 1,3 Today: C
Searching Given distinct keys k 1, k 2, …, k n and a collection of n records of the form »(k 1,I 1 ), (k 2,I 2 ), …, (k n, I n ) Search Problem - For key.
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,
LECTURE 35: COLLISIONS CSC 212 – Data Structures.
1 Hashing - Introduction Dictionary = a dynamic set that supports the operations INSERT, DELETE, SEARCH Dictionary = a dynamic set that supports the operations.
Hashing Basis Ideas A data structure that allows insertion, deletion and search in O(1) in average. A data structure that allows insertion, deletion and.
Hash Tables. 2 Exercise 2 /* Exercise 1 */ void mystery(int n) { int i, j, k; for (i = 1; i
Hash Table March COP 3502, UCF 1. Outline Hash Table: – Motivation – Direct Access Table – Hash Table Solutions for Collision Problem: – Open.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
Tirgul 11 Notes Hash tables –reminder –examples –some new material.
Introduction to Algorithms 6.046J/18.401J LECTURE7 Hashing I Direct-access tables Resolving collisions by chaining Choosing hash functions Open addressing.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
Hashtables David Kauchak cs302 Spring Administrative Midterm must take it by Friday at 6pm No assignment over the break.
CS6045: Advanced Algorithms Data Structures. Hashing Tables Motivation: symbol tables –A compiler uses a symbol table to relate symbols to associated.
1 Hash Tables Chapter Motivation Many applications require only: –Insert –Search –Delete Examples –Symbol tables –Memory management mechanisms.
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:
Hashing Jeff Chastine.
Hash table CSC317 We have elements with key and satellite data
Hashing CSE 2011 Winter July 2018.
Hashing Alexandra Stefan.
Hashing Alexandra Stefan.
CSE 2331/5331 Topic 8: Hash Tables CSE 2331/5331.
Dictionaries and Their Implementations
Resolving collisions: Open addressing
CSCE 3110 Data Structures & Algorithm Analysis
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
Hashing Sections 10.2 – 10.3 Lecture 26 CS302 Data Structures
EE 312 Software Design and Implementation I
Ch Hash Tables Array or linked list Binary search trees
EE 312 Software Design and Implementation I
Presentation transcript:

COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables

Data Dictionary A data structure that supports: –Insert –Search –Delete –Examples: Binary Search Tree Red Black Tree B Tree Link List

Hash Table An effective data dictionary –Worst Case: Θ(n) time –Under assumptions: O(1) time Generalization of an array Size proportional to the number of keys actually stored Array index is computed from the key

Direct Address Tables Universe U: –a set that contains all the possible keys Table has |U| slots. Each key in U is mapped into one unique entry in the Table Insert, delete and search takes O(1) time Works well when U is small If U is large, impractical

Hash Function Assume Hash Table has m slots Hash function h is used to compute slot from the key k h maps U into the slots of a hash table h: U → {0, 1, …, m-1} |U| > m, at least 2 keys will have the same hash value, collision “Good” hash function can minimize the number of collision

If the keys are not natural number: –Interpret them as natural number using suitable radix notation –Example: character string into radix-128 integer Division method –h(k) = k mod m –m is usually a prime –Avoid m too close to an exact power of 2 –Ex Choose m = 2 p -1 and k is a character string interpreted in radix 2 p. Show that if x can be derived from y by permuting its characters, then h(x) = h(y).

Multiplication method –h(k) =  m ( k A mod 1) , 0 < A < 1 –Value m is not critical –Usually choose m to be power of 2 –It works better with some values of A Eg. (√5 – 1 ) / 2

Separate Chaining Put all the elements that hash to the same slot in a link list Element is inserted into the head of the link list Worst case insertion: O(1) Worst case search: O(n) Worst case deletion: O(1)

Given a hash table has m slots that stores n elements, we define load factor α –α= n/m Simple uniform hashing –Element is equally likely to hash into any of the m slots, independently of where any other element has hashed to Under Simple uniform hashing –Average time for search = Θ(1+α)

Open Addressing Each table slot contains either an element or NIL When collision happens, we successively examine, or probe, the hash table until we find an empty slot to put the key Deletion is done by marking the slot as “Deleted” but not “NIL” Hash function h now takes two values: –The key value and the probe number –h(k, i)

Linear Probing –h(k, i) = ( h’(k) + i ) mod m –Initial probe determine the entire probe sequence, there are only m distinct probe sequence –Primary clustering Quadratic Probing –h(k, i) = ( h’(k) + c 1 i + c 2 i ) mod m –there are only m distinct probe sequence –Secondary clustering

Double hashing –Make use of 2 different hash function –h(k, i) = ( h 1 (k) + ih 2 (k) ) mod m –ih 2 (k) should be co-prime with m –Usually take m as a prime number –Probe sequence depends on both has function, so there are m 2 probe sequences –Double hashing is better then linear or quadratic probing

Trie Assumption: –Digital data / radix –Tree structure is used Insertion is done by creating a path of nodes from the root to the data Deletion is done by removing the pointer that points to that element Time Complexity: O(L) Max # of keys for given L = 128 L+1 - 1

Memory Usage: –Node size * Number of node –((N+1)*pointer size) * (L * n) –N: radix –L: maximum length of the keys –n: number of keys Improvement 1 –Put all nodes into an array of nodes –Replace pointer by array index –|Array index| =  lg (L * n) 

Improvement 2 –Eliminate nodes with a single child –Do Skipping –Label each internal node with its position Improvement 3 –De La Briandais Tree –Eliminate null pointer in the internal node –Save memory when array are sparsely populated