CS201: Data Structures and Discrete Mathematics I Hash Table.

Slides:



Advertisements
Similar presentations
Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach to the.
Advertisements

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.
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.
CSCE 3400 Data Structures & Algorithm Analysis
Searching “It is better to search, than to be searched” --anonymous.
CSC212 Data Structure - Section AB Lecture 20 Hashing Instructor: Edgardo Molina Department of Computer Science City College of New York.
© 2004 Goodrich, Tamassia Hash Tables1  
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
Hashing Techniques.
Lecture 18 Nov 3 Goals: hashing dictionary operations general idea of hashing hash functions chaining closed hashing.
hashing1 Hashing It’s not just for breakfast anymore!
© 2006 Pearson Addison-Wesley. All rights reserved13 A-1 Chapter 13 Hash Tables.
Lecture 11 March 5 Goals: hashing dictionary operations general idea of hashing hash functions chaining closed hashing.
1 CSE 326: Data Structures Hash Tables Autumn 2007 Lecture 14.
hashing1 Hashing It’s not just for breakfast anymore!
L l Chapter 11 discusses several ways of storing information in an array, and later searching for the information. l l Hash tables are a common approach.
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.
COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
Hashing General idea: Get a large array
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Aree Teeraparbseree, Ph.D
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.
CS 206 Introduction to Computer Science II 04 / 06 / 2009 Instructor: Michael Eckmann.
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.
COSC 2007 Data Structures II
Hashing CS 105. Hashing Slide 2 Hashing - Introduction In a dictionary, if it can be arranged such that the key is also the index to the array that stores.
CS212: DATA STRUCTURES Lecture 10:Hashing 1. Outline 2  Map Abstract Data type  Map Abstract Data type methods  What is hash  Hash tables  Bucket.
Hashing Table Professor Sin-Min Lee Department of Computer Science.
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.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 4 Search Algorithms.
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.
CSC211 Data Structures Lecture 20 Hashing Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
P p Chapter 11 discusses several ways of storing information in an array, and later searching for the information. p p Hash tables are a common approach.
P p Chapter 11 discusses several ways of storing information in an array, and later searching for the information. p p Hash tables are a common approach.
Hashing Hashing is another method for sorting and searching data.
HASHING PROJECT 1. SEARCHING DATA STRUCTURES Consider a set of data with N data items stored in some data structure We must be able to insert, delete.
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.
1 Hashing - Introduction Dictionary = a dynamic set that supports the operations INSERT, DELETE, SEARCH Dictionary = a dynamic set that supports the operations.
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.
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 10 Hashing. The search time of each algorithm depend on the number n of elements of the collection S of the data. A searching technique called.
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.
Hashing CS 110: Data Structures and Algorithms First Semester,
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.
Hash Tables © Rick Mercer.  Outline  Discuss what a hash method does  translates a string key into an integer  Discuss a few strategies for implementing.
Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 9 – Hash Tables Presentation copyright 2010 Addison Wesley Longman,
Hash Tables ADT Data Dictionary, with two operations – Insert an item, – Search for (and retrieve) an item How should we implement a data dictionary? –
CS 206 Introduction to Computer Science II 04 / 08 / 2009 Instructor: Michael Eckmann.
CSC 143T 1 CSC 143 Highlights of Tables and Hashing [Chapter 11 p (Tables)] [Chapter 12 p (Hashing)]
Hashtables.
Hashing CSE 2011 Winter July 2018.
Search by Hashing.
Hash Table.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
CSCE 3110 Data Structures & Algorithm Analysis
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
CSC212 Data Structure - Section KL
Collision Handling Collisions occur when different elements are mapped to the same cell.
Lecture-Hashing.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
Presentation transcript:

CS201: Data Structures and Discrete Mathematics I Hash Table

Hashing Dictionary: in many tasks, one needs store a collection of data and find item in it An important and widely useful technique for implementing dictionaries –Insert, delete, and search Constant time per operation (on average) Worst case time proportional to the size of the set for each operation (just like array and chain/list implementation)

Data collection Data collection is a set of records (static or dynamic) Each record consists of two parts –A key: a unique identifier of the record. –Data item: it can be arbitrarily complex. The key is usually a number, but can be a string or any other data type. –Non-numbers are converted to numbers when applying hashing.

Basic ideas of hashing Use hash function to map keys into positions in a hash table Ideally If data item or element e has key k and h is hash function, then e is stored in position h(k) of table To search for e, compute h(k) to locate position. If no element/item, dictionary does not contain e.

What is a Hash Table ? The simplest kind of hash table is an array of records (elements). This example array has 701 cells. [ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ]... [ 700] [ 4 ] An array of cells

Following the example We want to store a dictionary of Object Records, no more than 701 objects Keys are Object ID numbers, e.g., Hash function: h(k) maps k(=ID) into distinct table positions Operations: insert, delete, and search

Complexity (ideal case) Why hashing and hash table? –It is very efficient. O(D) time to initialize hash table (D number of positions or cells in hash table) O(1) time to perform insert, remove, search

Use the Hash Table Each record has a special field, i.e., its key. In this example, the key is a long integer field called Number. [ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ]... [ 700] [ 4 ] [ 4 ]

Use the Hash Table The number is a object's identification number, and the rest of the record has information about the object. [ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ]... [ 700] [ 4 ] [ 4 ]

Use the Hash Table When a hash table is in use, some spots contain valid records, and other spots are "empty". [ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ] [ 700]

Inserting a New Record In order to insert a new record, the key must somehow be mapped to an array index using a hash function. The index is called the hash value of the key. [ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ] [ 700]

Hash functions Popular hash functions: hashing by division h(k) = k mod D, where D is number of cells in hash table Example: hash table with 701 cells h(k) = k mod 701 h(80) = 80 mod 701 = 80 h(1000) = 1000 mod 701 = 299

Inserting a New Record Let us find the hash value for What is ( mod 701) ? [ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ] [ 700]

Inserting a New Record Let us find the hash value for mod 701 = 3 [ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ] [ 700]

Inserting a New Record The hash value is used to find the location of the new record. [ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ] [ 700]

Collisions Here is another new record to insert, with a hash value of 2. [ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ] [ 700]

Collisions This is called a collision, because there is already another valid record at [2]. [ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ] [ 700]

Collision Resolution Policies Two strategies: –(1) Open hashing, a.k.a. separate chaining –(2) Closed hashing, a.k.a. open addressing Difference has to do with whether collisions are stored outside the table (open hashing) or whether collisions result in storing one of the records at another slot in the table (closed hashing)

Closed hashing Associated with closed hashing is a rehash strategy: “If we try to place x in cell h(x) and find it occupied, find alternative location h 1 (x), h 2 (x), etc. Try each in order. If none empty, table is full,” h(x) is called home cell Simplest rehash strategy is called linear hashing h i (x) = (h(x) + i) mod D

Collisions: closed hashing Collision, there is already another valid record at [2]. [ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ] [ 700] When a collision occurs, move forward until we find an empty spot.

Collisions: closed hashing Collision, there is already another valid record at [2]. [ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ] [ 700] When a collision occurs, move forward until we find an empty spot.

Collisions: closed hashing Collision, there is already another valid record at [2]. [ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ] [ 700] When a collision occurs, move forward until we find an empty spot.

Collisions: closed hashing Collision, there is already another valid record at [2]. [ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ] [ 700] The new record goes in the empty spot.

Searching for a Key The data that's attached to a key can be found fairly quickly. [ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ] [ 700]

Searching for a Key Calculate the hash value. Check that location of the array for the key. [ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ] [ 700] Not me. The hash value of is 2

Searching for a Key Keep moving forward until you find the key, or you reach an empty spot. [ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ] [ 700] Not me. The hash value of is 2

Searching for a Key Keep moving forward until you find the key, or you reach an empty spot. [ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ] [ 700] Not me. The hash value of is 2

Searching for a Key Keep moving forward until you find the key, or you reach an empty spot. [ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ] [ 700] Yes. The hash value of is 2

Searching for a Key When the item is found, the information can be copied to the necessary location. [ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ] [ 700] Yes. The hash value of is 2

Deleting a Record Records may also be deleted from a hash table. [ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ] [ 700] Please delete me.

Deleting a Record Records may also be deleted from a hash table. But the location must not be left as an ordinary "empty spot" since that could interfere with searches. [ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ] [ 700]

Deleting a Record Records may also be deleted from a hash table. But the location must not be left as an ordinary "empty spot" since that could interfere with searches. The location must be marked in some special way so that a search can tell that the spot used to have something in it. [ 0 ][ 1 ][ 2 ][ 3 ][ 4 ][ 5 ] [ 700]

Open hashing Each cell in the hash table is the head of a linked list All records or elements that hash to a particular cell are placed on that cell’s linked list Records within a cell can be ordered in several ways –by order of insertion, by key value order, or by frequency of access order

Open hashing: same example [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 700] …

Summary Hash tables store a collection of records with keys. The location of a record depends on the hash value of the record's key. When a collision occurs, use a collision resolution strategy to deal with it. Hash table is a very efficient way to store and to search for data