Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.

Slides:



Advertisements
Similar presentations
1 Designing Hash Tables Sections 5.3, 5.4, Designing a hash table 1.Hash function: establishing a key with an indexed location in a hash table.
Advertisements

Preliminaries Advantages –Hash tables can insert(), remove(), and find() with complexity close to O(1). –Relatively easy to program Disadvantages –There.
Hash Tables CS 310 – Professor Roch Weiss Chapter 20 All figures marked with a chapter and section number are copyrighted © 2006 by Pearson Addison-Wesley.
Hashing General idea Hash function Separate Chaining Open Addressing
© 2004 Goodrich, Tamassia Hash Tables1  
Log Files. O(n) Data Structure Exercises 16.1.
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
CSE 250: Data Structures Week 12 March 31 – April 4, 2008.
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 Text Read Weiss, §5.1 – 5.5 Goal Perform inserts, deletes, and finds in constant average time Topics Hash table, hash function, collisions Collision.
Introduction to Hashing CS 311 Winter, Dictionary Structure A dictionary structure has the form: (Key, Data) Dictionary structures are organized.
Hashing General idea: Get a large array
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.
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.
Hashing. Hashing as a Data Structure Performs operations in O(c) –Insert –Delete –Find Is not suitable for –FindMin –FindMax –Sort or output as sorted.
§3 Separate Chaining ---- keep a list of all keys that hash to the same value struct ListNode; typedef struct ListNode *Position; struct HashTbl; typedef.
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).
1 Chapter 5 Hashing General ideas Methods of implementing the hash table Comparison among these methods Applications of hashing Compare hash tables with.
Data Structures and Algorithm Analysis Hashing Lecturer: Jing Liu Homepage:
1.  We’ll discuss the hash table ADT which supports only a subset of the operations allowed by binary search trees.  The implementation of hash tables.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 7 Prepared by İnanç TAHRALI.
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.
1 Symbol Tables The symbol table contains information about –variables –functions –class names –type names –temporary variables –etc.
1 CSE 326: Data Structures: Hash Tables Lecture 12: Monday, Feb 3, 2003.
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 HASHING Course teacher: Moona Kanwal. 2 Hashing Mathematical concept –To define any number as set of numbers in given interval –To cut down part of.
Hash Tables - Motivation
CS201: Data Structures and Discrete Mathematics I Hash Table.
WEEK 1 Hashing CE222 Dr. Senem Kumova Metin
Data Structures and Algorithms Hashing First Year M. B. Fayek CUFE 2010.
Hash Tables CSIT 402 Data Structures II. Hashing Goal Perform inserts, deletes, and finds in constant average time Topics Hash table, hash function, collisions.
Chapter 11 Hash Tables © John Urrutia 2014, All Rights Reserved1.
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 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.
CSE 373 Data Structures and Algorithms Lecture 17: Hashing II.
CS261 Data Structures Hash Tables Open Address 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.
Hash Tables © Rick Mercer.  Outline  Discuss what a hash method does  translates a string key into an integer  Discuss a few strategies for implementing.
Chapter 13 C Advanced Implementations of Tables – Hash Tables.
1 the hash table. hash table A hash table consists of two major components …
Hash Tables ADT Data Dictionary, with two operations – Insert an item, – Search for (and retrieve) an item How should we implement a data dictionary? –
Hashing Goal Perform inserts, deletes, and finds in constant average time Topics Hash table, hash function, collisions Collision handling Separate 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.
1 Resolving Collision Although collisions should be avoided as much as possible, they are inevitable Need a strategy for resolving collisions. We look.
CSC2100B Tutorial 6 Hashing Hao Ma Yi LIU Mar 4, 2004.
1 Designing Hash Tables Sections 5.3, 5.4, 5.5, 5.6.
Chapter 11 (Lafore’s Book) Hash Tables Hwajung Lee.
DS.H.1 Hashing Chapter 5 Overview The General Idea Hash Functions Separate Chaining Open Addressing Rehashing Extendible Hashing Application Example: Geometric.
Fundamental Structures of Computer Science II
Chapter 27 Hashing Jung Soo (Sue) Lim Cal State LA.
Hashing Problem: store and retrieving an item using its key (for example, ID number, name) Linked List takes O(N) time Binary Search Tree take O(logN)
Hashing CSE 2011 Winter July 2018.
Hash Tables (Chapter 13) Part 2.
Advanced Associative Structures
Hash Table.
Chapter 28 Hashing.
Chapter 21 Hashing: Implementing Dictionaries and Sets
Collision Resolution Neil Tang 02/18/2010
Searching Tables Table: sequence of (key,information) pairs
Tree traversal preorder, postorder: applies to any kind of tree
Collision Resolution Neil Tang 02/21/2008
Collision Handling Collisions occur when different elements are mapped to the same cell.
Podcast Ch21b Title: Collision Resolution
Data Structures and Algorithm Analysis Hashing
CSE 373: Data Structures and Algorithms
Presentation transcript:

Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6

Designing a hash table Hash function: establishing a key with an indexed location in a hash table.  Index = hash(key) % table_size; Resolve conflicts:  Need to handle multiple keys that may be mapped to the same index.  Two representative solutions Linear probe open addressing (will discuss more later) Chaining with separate lists.

Separate Chaining Each table entry stores a list of items So we don’t need to worry about multiple keys mapped to the same entry.

Separate Chaining (contd.) Type Declaration for Separate Chaining Hash Table

Separate Chaining (contd.)

Hash Tables Without Chaining Try to avoid buckets with separate lists How  use Probing Hash Tables  If collision occurs, try another cell in the hash table.  More formally, try cells h 0 (x), h 1 (x), h 2 (x), h 3 (x)… in succession until a free cell is found. h i (x) = (hash(x) + f(i)) And f(0) = 0

Insert(k,x) // assume unique keys 1.index = hash(key) % table_size; 2. if (table[index]== NULL) table[index]= new key_value_pair(key, x); 3.Else { index++; index = index % table_size; goto 2; } Linear Probing: f(i) = i

Search (key) 1.Index = hash(key) % table_size; 2.If (table[index]==NULL) return –1; // Item not found 3.Else if (table[index].key == key) return index; 4.Else { Index ++; index = index % table_size; goto 2; 5.} Linear Probing: Search

Linear Probing Example Insert 89, 18, 49, 58, 69

Linear Probing: Delete Can be tricky... How to maintain the consistency of the hash table What is the simplest deletion strategy you can think of?

Quadratic Probing f(i) = i 2

Double Hashing f(i) = i*hash 2 (x) E.g.: hash 2 (x) = 7 – (x % 7) What if hash 2 (x) == 0 for some x ?

Rehashing Hash Table may get full  No more insertions possible Hash table may get too full  Insertions, deletions, search take longer time Solution: Rehash  Build another table that is twice as big and has a new hash function  Move all elements from smaller table to bigger table Cost of Rehashing = O(N)  But happens only when table is close to full  Close to full = table is X percent full, where X is a tunable parameter

Rehashing Example After Rehashing Original Hash Table After Inserting 23

Rehashing Implementation