Lecture 18 Nov 3 Goals: hashing dictionary operations general idea of hashing hash functions chaining closed hashing.

Slides:



Advertisements
Similar presentations
David Luebke 1 6/7/2014 ITCS 6114 Skip Lists Hashing.
Advertisements

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
Nov 12, 2009IAT 8001 Hash Table Bucket Sort. Nov 12, 2009IAT 8002  An array in which items are not stored consecutively - their place of storage is calculated.
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 Techniques.
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.
Lecture 10 Sept 29 Goals: hashing dictionary operations general idea of hashing hash functions chaining closed hashing.
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.
Lecture 11 March 5 Goals: hashing dictionary operations general idea of hashing hash functions chaining closed hashing.
CSC 2300 Data Structures & Algorithms February 27, 2007 Chapter 5. Hashing.
1 CSE 326: Data Structures Hash Tables Autumn 2007 Lecture 14.
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)
Design and Analysis of Algorithms - Chapter 71 Hashing b A very efficient method for implementing a dictionary, i.e., a set with the operations: – insert.
CS2420: Lecture 33 Vladimir Kulyukin Computer Science Department Utah State University.
Hash Tables1 Part E Hash Tables  
COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables.
Lecture 10: Search Structures and Hashing
Hashing General idea: Get a large array
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.
COSC 2007 Data Structures II
ICS220 – Data Structures and Algorithms Lecture 10 Dr. Ken Cosh.
Hash Table March COP 3502, UCF.
Spring 2015 Lecture 6: Hash Tables
Data Structures and Algorithm Analysis Hashing Lecturer: Jing Liu Homepage:
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture8.
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.
CS212: DATA STRUCTURES Lecture 10:Hashing 1. Outline 2  Map Abstract Data type  Map Abstract Data type methods  What is hash  Hash tables  Bucket.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
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 Symbol Tables The symbol table contains information about –variables –functions –class names –type names –temporary variables –etc.
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,
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.
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.
CS201: Data Structures and Discrete Mathematics I Hash Table.
WEEK 1 Hashing CE222 Dr. Senem Kumova Metin
Lecture 17 April 11, 11 Chapter 5, Hashing dictionary operations general idea of hashing hash functions chaining closed hashing.
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,
1 Hashing - Introduction Dictionary = a dynamic set that supports the operations INSERT, DELETE, SEARCH Dictionary = a dynamic set that supports the operations.
Chapter 5: Hashing Part I - Hash Tables. Hashing  What is Hashing?  Direct Access Tables  Hash Tables 2.
Ihab Mohammed and Safaa Alwajidi. Introduction Hash tables are dictionary structure that store objects with keys and provide very fast access. Hash table.
Hash Table March COP 3502, UCF 1. Outline Hash Table: – Motivation – Direct Access Table – Hash Table Solutions for Collision Problem: – Open.
Hashing, Hashing Tables Chapter 8. Class Hierarchy.
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.
Midterm Midterm is Wednesday next week ! The quiz contains 5 problems = 50 min + 0 min more –Master Theorem/ Examples –Quicksort/ Mergesort –Binary Heaps.
Chapter 13 C Advanced Implementations of Tables – Hash Tables.
Data Structure & Algorithm Lecture 8 – Hashing JJCAO Most materials are stolen from Prof. Yoram Moses’s course.
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
CS6045: Advanced Algorithms Data Structures. Hashing Tables Motivation: symbol tables –A compiler uses a symbol table to relate symbols to associated.
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.
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:
CSE373: Data Structures & Algorithms Lecture 6: Hash Tables
CS 332: Algorithms Hash Tables David Luebke /19/2018.
CS223 Advanced Data Structures and Algorithms
Advanced Associative Structures
Lecture 17 April 11, 11 Chapter 5, Hashing dictionary operations
Searching Tables Table: sequence of (key,information) pairs
CSCE 3110 Data Structures & Algorithm Analysis
EE 312 Software Design and Implementation I
Data Structures and Algorithm Analysis Hashing
EE 312 Software Design and Implementation I
Presentation transcript:

Lecture 18 Nov 3 Goals: hashing dictionary operations general idea of hashing hash functions chaining closed hashing

Dictionary operations search insert delete Applications: compilers web searching game playing, state space search spell checking etc.

Hashing oAn important and widely useful technique for implementing dictionaries oConstant time per operation (on the average) oWorst case time proportional to the size of the set for each operation (just like array and linked list implementation)

General idea U = Set of all possible keys: (e.g. 9 digit SS #) If n = |U| is not very large, a simple way to support dictionary operations is: map each key e in U to a unique integer h(e) in the range 0.. n – 1. Boolean array H[0.. n – 1] to store keys.

General idea

Ideal case not realistic U the set of all possible keys is usually very large so we can’t create an array of size n = |U|. Create an array H of size m much smaller than n. Actual keys present at any time will usually be smaller than n. mapping from U -> {0, 1, …, m – 1} is called hash function. Example: D = students currently enrolled in courses, U = set of all SS #’s, hash table of size = 1000 Hash function h(x) = last three digits

Example (continued) Insert Student “Dan” SS# = h( ) = hash table buckets 871 Dan NULL

Example (continued) Insert Student “Tim” SS# = h( ) = 871, same as that of Dan. Collision hash table buckets 871 Dan NULL

Hash Functions If h(k 1 ) =  = h(k 2 ): k 1 and k 2 have collision at slot  There are two approaches to resolve collisions.

Collision Resolution Policies Two ways to resolve: (1) Open hashing, also known as separate chaining (2) Closed hashing Chaining: keys that collide are stored in a list (which we can implement as a vector in Matlab). Closed hashing: find an alternate location to store the key.

Previous Example Insert Student “Tim” SS# = h( ) = 871, same as that of Dan. Collision hash table buckets 871 Dan NULL Tim

Open Hashing The hash table is a pointer to the head of a linked list All elements that hash to a particular bucket are placed on that bucket’s linked list Records within a bucket can be ordered in several ways by order of insertion, by key value order, or by frequency of access order, in the order of most recent access etc.

Open Hashing Data Organization D-1...

Choice of hash function A good hash function should: be easy to compute distribute the keys uniformly to the buckets use all the fields of the key object.

Example: key is a string over {a, …, z, 0, … 9, _ } Suppose hash table size is n = (Choose table size to be a prime number.) Good hash function: interpret the string as a number to base 37 and compute mod h(“word”) = ? “w” = 23, “o” = 15, “r” = 18 and “d” = 4. h(“word”) = (23 * * * ) % 10007

Computing hash function for a string Horner’s rule: (( … (a 0 x + a 1 ) x + a 2 ) x + … + a n-2 )x + a n-1 ) function indx = hash1(key, size) indx = 0; for i = 1:length(key) indx = 37 * indx + key(i); end; indx = mod(indx, size);

Implementation of open hashing – search function hash_search(table, key) indx = myhash(key, table.size); lst = table.keys{indx}; any(strcmp(lst,key));

Insert into an open hash table Key x is inserted in front of the list to which h hashes function [out, succ] = hash_insert(hash, key) indx = myhash(key, hash.size) succ = false; sindx = find(hash.keys{indx}, key); if isempty(sindx) hash.keys{indx} = [key]; succ = true; end; out = hash;

Implementation of open hashing - delete function [out, succ] = hash_delete(table, x) % removes x if it is present in hash table indx = myhash(x, table.size) arr = table.keys{indx} indx1 = find(arr==x) if isempty(indx1) succ = false; else arr(indx1) = []; table.keys{indx} = arr; succ = true; end; out = table;