COSC 1030 Lecture 10 Hash Table. Topics Table Hash Concept Hash Function Resolve collision Complexity Analysis.

Slides:



Advertisements
Similar presentations
Hash Tables.
Advertisements

CSCE 3400 Data Structures & Algorithm Analysis
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.
ICOM 4035 – Data Structures Lecture 12 – Hashtables & Map ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico,
© 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.
Hashing Techniques.
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.
© 2006 Pearson Addison-Wesley. All rights reserved13 A-1 Chapter 13 Hash Tables.
Hash Tables and Associative Containers CS-212 Dick Steflik.
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.
Data Structures Using Java1 Chapter 8 Search Algorithms.
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  
COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables.
Hashing General idea: Get a large array
Dictionaries 4/17/2017 3:23 PM Hash Tables  
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
COSC 2007 Data Structures II
Hash Table March COP 3502, UCF.
Data Structures Using Java1 Chapter 8 Search Algorithms.
Hashtables David Kauchak cs302 Spring Administrative Talk today at lunch Midterm must take it by Friday at 6pm No assignment over the break.
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.
CS121 Data Structures CS121 © JAS 2004 Tables An abstract table, T, contains table entries that are either empty, or pairs of the form (K, I) where K is.
Hashing Sections 10.2 – 10.3 CS 302 Dr. George Bebis.
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 as a Dictionary Implementation Chapter 19.
Lecture 12COMPSCI.220.FS.T Symbol Table and Hashing A ( symbol) table is a set of table entries, ( K,V) Each entry contains: –a unique key, K,
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.
Chapter 5: Hashing Part I - Hash Tables. Hashing  What is Hashing?  Direct Access Tables  Hash Tables 2.
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.
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.
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
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.
H ASH TABLES. H ASHING Key indexed arrays had perfect search performance O(1) But required a dense range of index values Otherwise memory is wasted Hashing.
Hashing Suppose we want to search for a data item in a huge data record tables How long will it take? – It depends on the data structure – (unsorted) linked.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
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 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.
Hashing O(1) data access (almost) -access, insertion, deletion, updating in constant time (on average) but at a price… references: Weiss, Goodrich & Tamassia,
Hash Tables ADT Data Dictionary, with two operations – Insert an item, – Search for (and retrieve) an item How should we implement a data dictionary? –
Hash Tables Ellen Walker CPSC 201 Data Structures Hiram College.
1 Hash Tables Chapter Motivation Many applications require only: –Insert –Search –Delete Examples –Symbol tables –Memory management mechanisms.
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.
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.
Hashing CSE 2011 Winter July 2018.
Hashing Alexandra Stefan.
Hashing Alexandra Stefan.
Advanced Associative Structures
Hash Table.
CS202 - Fundamental Structures of Computer Science II
Collision Handling Collisions occur when different elements are mapped to the same cell.
CS210- Lecture 16 July 11, 2005 Agenda Maps and Dictionaries Map ADT
Presentation transcript:

COSC 1030 Lecture 10 Hash Table

Topics Table Hash Concept Hash Function Resolve collision Complexity Analysis

Table Table – A collection of entries – Entry : – Insert, search and delete – Update, and retrieve Array representation – Indexed – Maps key to index

Hash Table – A table – Key range >> table size – Many-to-one mapping (hashing) – Indexed – hash code as index Tabbed Address Book – Map names to A:Z – Multiple names start with same letter Same tab, sequential slots

Hash Table ADT Interface Hashtable { void insert(Item anItem); Item search(Key aKey); boolean remove(Key aKey); boolean isFull(); boolean isEmpty(); }

Hash Function Maps key to index evenly For any n in N, hash(n) = n mod M where M is the size of hash table. hash(k*M + n) = n, where n < M, k: integer Map to integer first if key is not an integer – A:Z  0:25 String s  h(s[0]) + h(s[1])*26 +…+ h(s[n-1])*26^(n-1) String s  h(s[0])*26^(n-1) + …+h(s[n-1])

Hash Function String s  h(s[0])*26^(n-1) + …+h(s[n-1]) int toInt(String s) { assert(s != null); int c = 0; for (int I = 0; I < s.length(); I ++) { c = c*26 + toInt(s.charAt(I)); } return c; } int hash(String s) { return hash(toInt(s)); }

Example Table[7] – HASHTABLE_SIZE = 7 Insert ‘B 2 ’, ‘H 7 ’, ‘M 12 ’, ‘D 4 ’, ‘Z 26 ’ into the table 2, 0, 5, 4, 5 Collision – The slot indexed by hash code is already occupied A simple solution – Sequentially decreases index until find an empty slot or table is full

Collision Possibility How often collision may occur? Insert 100 random number into a table of 200 slots 1 –  ((200 – I)/200), I=0:99 = 1 – 6.66E-14 > Load factor – 100/200 = 0.5 = 50%  – 20/ 200 = 0.1 = 10%  0.63 – 10/200 = 0.05 = 5%  0.2 Default load factor is 75% in java Hashtable

Primary Cluster The biggest solid block in hash table Join clusters The bigger the primary cluster is, the easier to grow Distributed evenly to avoid primary cluster

Probe Method What we can do when collision occurred? – A consistent way of searching for an empty slot – Probe Linear probe – decrease index by 1, wrap up when 0 Double hash – use quotient to calculate decrement –Max(1, (Key / M) % M) Separate chaining – linked list to store collision items Hash tree – link to another hash table (A4)

Probe sequence coverage Ensure probe sequence cover all table – Utilizes the whole table – Even distribution – M and probe decrement are relative prime No common factor except 1 – Makes M a prime number M and any decrement (< M) are relative prime

Probe Method void insert(Item item) { if(!isFull()) { int index = probe(item.key); assert(index >=0 && index < M); table[index] = item; count ++; }

Linear Probe Method int probe(int key) { int hashcode = key % HASHTABLE_SIZE; if(table[hashcode] == null) { return hashcode; } else { int index = hashcode; do { index--; if(index < 0) index += HASHTABLE_SIZE; } while (index != hashcode && table[index] != null); if(index == hashcode) return –1; else return index; }

Double Hash Probe Method int probe(int key) { int hashcode = key % HASHTABLE_SIZE; if(table[hashcode] == null) { return hashcode; } else { int index = hashcode; int dec = (key / HASHTABLE_SIZE) % HASHTABLE_SIZE; dec = Math.max(1, dec); do { index -= dec; if(index < 0) index += HASHTABLE_SIZE; } while (index != hashcode && table[index] != null); if(index == hashcode) return –1; else return index; }

Search Method Item search(int key) { int hashcode = key % HASHTABLE_SIZE; int dec = max(1, (key / HASHTABLE_SIZE) % HASHTABLE_SIZE); while(table[hashcode] != null) { if(table[hashcode].key == key) break; hashcode -= dec; } return table[hashcode]; }

Delete Method Difficulty with delete when open addressing – Destroy hash probe chain Solution – Set a deleted flag – Search takes it as occupied – Insert takes it as deleted – Forms primary cluster Separate chaining – Move one up from chained structure

Efficiency Successful search – Best case – first hit, one comparison – Average Half of average length of probe sequence Load factor dependent O(1) if load factor < 0.5 – Worst case – longest probe sequence Load factor dependent Unsuccessful search – Average - average length of probe sequence – Worst case - longest probe sequence

Advanced Topics Choosing Hash Functions – Generate hash code randomly and uniformly – Use all bits of the key – Assume K=b0b1b2b3 – Division h(k) = k % M; p(k) = max (1, (k / M) % M) – Folding h(k) = b1^b3 % M; p(k) = b0^b2 % M; // XOR – Middle squaring h(k) = (b1b2) ^ 2 – Truncating h(k) = b3;

Advanced Topics Hash Tree – Separate chained collision resolution – Recursively hashing the key Hash Table

Hash Tree void insert(int key, Item item) { Int h = h(key); Int k = g(key); // one-to-one mapping Key  Key If(table[h] == null) { table[h] = item; } else { if(table[h].link == null) table[h].link = new HashTree(); table[h].link.insert(k, item); }