Algorithms and Data Structures Lecture VI

Slides:



Advertisements
Similar presentations
Chapter 11. Hash Tables.
Advertisements

Hash Tables CIS 606 Spring 2010.
What we learn with pleasure we never forget. Alfred Mercier Smitha N Pai.
September 26, Algorithms and Data Structures Lecture VI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 18: Hash Tables.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Hashing CS 3358 Data Structures.
REPRESENTING SETS CSC 172 SPRING 2002 LECTURE 21.
Hashing COMP171 Fall Hashing 2 Hash table * Support the following operations n Find n Insert n Delete. (deletions may be unnecessary in some applications)
Tirgul 7. Find an efficient implementation of a dynamic collection of elements with unique keys Supported Operations: Insert, Search and Delete. The keys.
COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables.
Lecture 10: Search Structures and Hashing
Hashing General idea: Get a large array
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.
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
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.
David Luebke 1 10/25/2015 CS 332: Algorithms Skip Lists Hash Tables.
Hashing Sections 10.2 – 10.3 CS 302 Dr. George Bebis.
Hashing Hashing is another method for sorting and searching data.
Hashing as a Dictionary Implementation Chapter 19.
Hashing 8 April Example Consider a situation where we want to make a list of records for students currently doing the BSU CS degree, with each.
CSC 172 DATA STRUCTURES. SETS and HASHING  Unadvertised in-store special: SETS!  in JAVA, see Weiss 4.8  Simple Idea: Characteristic Vector  HASHING...The.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
October 6, Algorithms and Data Structures Lecture VII Simonas Šaltenis Aalborg University
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 1 Hashing. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
Midterm Midterm is Wednesday next week ! The quiz contains 5 problems = 50 min + 0 min more –Master Theorem/ Examples –Quicksort/ Mergesort –Binary Heaps.
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.
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:
M. Böhlen and R. Sebastiani 9/29/20161 Data Structures and Algorithms Roberto Sebastiani
1 What is it? A side order for your eggs? A form of narcotic intake? A combination of the two?
Hashing (part 2) CSE 2011 Winter March 2018.
CSC 172 DATA STRUCTURES.
Data Structures Using C++ 2E
Hashing, Hash Function, Collision & Deletion
Hash table CSC317 We have elements with key and satellite data
CSCI 210 Data Structures and Algorithms
Hashing CSE 2011 Winter July 2018.
Data Abstraction & Problem Solving with C++
CS 332: Algorithms Hash Tables David Luebke /19/2018.
Hashing Alexandra Stefan.
Hashing Alexandra Stefan.
Data Structures Using C++ 2E
Review Graph Directed Graph Undirected Graph Sub-Graph
Hash functions Open addressing
Hash Tables.
Data Structures and Algorithms
Dictionaries and Their Implementations
Introduction to Algorithms 6.046J/18.401J
Hash Tables “hash collision n. [from the techspeak] (var. ‘hash clash’) When used of people, signifies a confusion in associative memory or imagination,
Data Structures and Algorithms
Hash Tables – 2 Comp 122, Spring 2004.
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.
CS202 - Fundamental Structures of Computer Science II
Introduction to Algorithms
Hashing Sections 10.2 – 10.3 Lecture 26 CS302 Data Structures
EE 312 Software Design and Implementation I
CS 5243: Algorithms Hash Tables.
CS210- Lecture 16 July 11, 2005 Agenda Maps and Dictionaries Map ADT
Hashing.
What we learn with pleasure we never forget. Alfred Mercier
CS 3343: Analysis of Algorithms
EE 312 Software Design and Implementation I
Hash Tables – 2 1.
Lecture-Hashing.
Presentation transcript:

Algorithms and Data Structures Lecture VI Simonas Šaltenis Nykredit Center for Database Research Aalborg University simas@cs.auc.dk September 26, 2002

This Lecture Dictionary ADT Hashing the concept collision resolution chooing a hash function advanced collision resolution September 26, 2002

Dictionary Dictionary ADT – a dynamic set with methods: Search(S, k) – an access operation that returns a pointer x to an element where x.key = k Insert(S, x) – a manipulation operation that adds the element pointed to by x to S Delete(S, x) – a manipulation operation that removes the element pointed to by x from S An element has a key part and a satellite data part September 26, 2002

Dictionaries Dictionaries store elements so that they can be located quickly using keys A dictionary may hold bank accounts each account is an object that is identified by an account number each account stores a wealth of additional information including the current balance, the name and address of the account holder, and the history of deposits and withdrawals performed an application wishing to operate on an account would have to provide the account number as a search key September 26, 2002

Dictionaries (2) Supporting order (methods min, max, successor, predecessor ) is not required, thus it is enough that keys are comparable for equality September 26, 2002

Dictionaries (3) Different data structures to realize dictionaries arrays, linked lists (inefficient) Hash table (used in Java...) Binary trees Red/Black trees AVL trees B-trees In Java: java.util.Dictionary – abstract class java.util.Map – interface September 26, 2002

The Problem RT&T is a large phone company, and they want to provide caller ID capability: given a phone number, return the caller’s name phone numbers range from 0 to r = 108 -1 want to do this as efficiently as possible September 26, 2002

The Problem A few suboptimal ways to design this dictionary direct addressing: an array indexed by key: takes O(1) time, O(r) space - huge amount of wasted space a linked list: takes O(n) time, O(n) space (null) Jens Jensen 0000-0000 9635-8904 Jens Jensen 9635-8904 Ole Olsen 9635-9999 September 26, 2002

Another Solution We can do better, with a Hash table -- O(1) expected time, O(n+m) space, where m is table size Like an array, but come up with a function to map the large range into one which we can manage e.g., take the original key, modulo the (relatively small) size of the array, and use that as an index Insert (9635-8904, Jens Jensen) into a hashed array with, say, five slots 96358904 mod 5 = 4 (null) Jens Jensen 1 2 3 4 September 26, 2002

Another Solution (2) A lookup uses the same process: hash the query key, then check the array at that slot Insert (9635-8900, Leif Erikson) And insert (9635-8004, Knut Hermandsen). (null) Jens Jensen 1 2 3 4 September 26, 2002

Collision Resolution How to deal with two keys which hash to the same spot in the array? Use chaining Set up an array of links (a table), indexed by the keys, to lists of items with the same key Most efficient (time-wise) collision resolution scheme September 26, 2002

Analysis of Hashing An element with key k is stored in slot h(k) (instead of slot k without hashing) The hash function h maps the universe U of keys into the slots of hash table T[0...m-1] Assumption: Each key is equally likely to be hashed into any slot (bucket); simple uniform hashing Given hash table T with m slots holding n elements, the load factor is defined as a=n/m Assume time to compute h(k) is Q(1) September 26, 2002

Analysis of Hashing (2) To find an element Unsuccessful search using h, look up its position in table T search for the element in the linked list of the hashed slot Unsuccessful search element is not in the linked list uniform hashing yields an average list length a = n/m expected number of elements to be examined a search time O(1+ a) (this includes computing the hash value) September 26, 2002

Analysis of Hashing (3) Successful search assume that a new element is inserted at the end of the linked list upon insertion of the i-th element, the expected length of the list is (i-1)/m in case of a successful search, the expected number of elements examined is 1 more that the number of elements examined when the sought-for element was inserted! September 26, 2002

Analysis of Hashing (4) The expected number of elements examined is thus Considering the time for computing the hash function, we obtain September 26, 2002

Analysis of Hashing (5) Assuming the number of hash table slots is proportional to the number of elements in the table . searching takes constant time on average insertion takes O(1) worst-case time deletion takes O(1) worst-case time when the lists are doubly-linked September 26, 2002

Hash Functions Need to choose a good hash function quick to compute distributes keys uniformly throughout the table good hash functions are very rare – birthday paradox How to deal with hashing non-integer keys: find some way of turning the keys into integers in our example, remove the hyphen in 9635-8904 to get 96358904! for a string, add up the ASCII values of the characters of your string (e.g., java.lang.String.hashCode()) then use a standard hash function on the integers September 26, 2002

HF: Division Method Use the remainder Need to choose m m = be (bad) h(k) = k mod m k is the key, m the size of the table Need to choose m m = be (bad) if m is a power of 2, h(k) gives the e least significant bits of k all keys with the same ending go to the same place m prime (good) helps ensure uniform distribution primes not too close to exact powers of 2 September 26, 2002

HF: Division Method (2) Example 1 Further examples hash table for n = 2000 character strings we don’t mind examining 3 elements m = 701 a prime near 2000/3 but not near any power of 2 Further examples m = 13 h(3)? h(12)? h(13)? h(47)? September 26, 2002

HF: Multiplication Method Use h(k) = ëm (k A mod 1) û k is the key, m the size of the table, and A is a constant 0 < A < 1 The steps involved map 0...kmax into 0...kmax A take the fractional part (mod 1) map it into 0...m-1 September 26, 2002

HF: Multiplication Method(2) Choice of m and A value of m is not critical, typically use m = 2p optimal choice of A depends on the characteristics of the data Knuth says use (conjugate of the golden ratio) – Fibonaci hashing September 26, 2002

Universal Hashing For any choice of hash function, there exists a bad set of identifiers A malicious adversary could choose keys to be hashed such that all go into the same slot (bucket) Average retrieval time is Q(n) Solution a random hash function choose hash function independently of keys! create a set of hash functions H, from which h can be randomly selected September 26, 2002

Universal Hashing (2) A collection H of hash functions is universal if for any randomly chosen f from H (and two keys k and l), Pr{f(k) = f(l)} £ 1/m September 26, 2002

More on Collisions A key is mapped to an already occupied table location what to do?!? Use a collision handling technique We’ve seen Chaining Can also use Open Addressing Probing Double Hashing September 26, 2002

Open Addressing All elements are stored in the hash table (can fill up!), i.e., n £ m Each table entry contains either an element or null When searching for an element, systematically probe table slots Modify hash function to take the probe number i as the second parameter Hash function, h, determines the sequence of slots examined for a given key September 26, 2002

Open Adressing (2) Probe sequence for a given key k given by September 26, 2002

Linear Probing If the current location is used, try the next table location Lookups walk along the table until the key or an empty slot is found Uses less memory than chaining one does not have to store all those links Slower than chaining one might have to walk along the table for a long time LinearProbingInsert(k) 01   if (table is full) error 02   probe = h(k) 03   while (table[probe] occupied) 04   probe = (probe+1) mod m 05   table[probe] = k September 26, 2002

Linear Probing A real pain to delete from Example either mark the deleted slot or fill in the slot by shifting some elements down Example h(k) = k mod 13 insert keys: 18 41 22 44 59 32 31 73 September 26, 2002

Double Hashing Use two hash functions If m is prime, eventually will examine every position in the table Many of the same (dis)advantages as linear probing Distributes keys more uniformly than linear probing DoubleHashingInsert(k) 01   if (table is full) error 02   probe = h1(k) 03   offset = h2(k) 03   while (table[probe] occupied) 04   probe = (probe+offset) mod m 05   table[probe] = k September 26, 2002

Double Hashing (2) h2(k) must be relative prime to m Example h1(k) = k mod 13 h2(k) = 8 - k mod 8 insert keys: 18 41 22 44 59 32 31 73 September 26, 2002

Expected Number of Probes Load factor a < 1 for probing Analysis of probing uses uniform hashing assumpion – any permutation is equally likely What about linear probing and double hashing? unsuccessful successful chaining probing September 26, 2002

Expected Number of Probes (2) September 26, 2002

The Hashing Class in Java java.util.Hashtable Constructor public Hashtable(int size, float load); Accessor methods public Object put(Object key, Object value): maps the specified key to the specified value in this hashtable September 26, 2002

Next Week Binary Search Trees September 26, 2002