CSC2100B Tutorial 6 Hashing Hao Ma Yi LIU Mar 4, 2004.

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

An Introduction to Hashing. By: Sara Kennedy Presented: November 1, 2002.
Hashing General idea Hash function Separate Chaining Open Addressing
CSCE 3400 Data Structures & Algorithm Analysis
Hashing: Collision Resolution Schemes
Log Files. O(n) Data Structure Exercises 16.1.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A15 – Hash Tables: Collision Resolution.
© 2006 Pearson Addison-Wesley. All rights reserved13 A-1 Chapter 13 Hash Tables.
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.
CS 206 Introduction to Computer Science II 11 / 17 / 2008 Instructor: Michael Eckmann.
CS2420: Lecture 33 Vladimir Kulyukin Computer Science Department Utah State University.
CSE 326 Hashing Richard Anderson (instead of Martin Tompa)
Aree Teeraparbseree, Ph.D
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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.
CS 206 Introduction to Computer Science II 04 / 06 / 2009 Instructor: Michael Eckmann.
§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).
ICS220 – Data Structures and Algorithms Lecture 10 Dr. Ken Cosh.
HASHING Section 12.7 (P ). HASHING - have already seen binary and linear search and discussed when they might be useful (based on complexity)
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:
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Hashing CS 202 – Fundamental Structures of Computer Science II Bilkent.
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.
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 CSE 326: Data Structures: Hash Tables Lecture 12: Monday, Feb 3, 2003.
Hash Tables: Collision Resolution. 2 Overview Hash Tables Collisions Linear Probing Problems with Linear Probing Chaining.
Robbie CSCI2100A Data Structures Tutorial
Hash Tables - Motivation
CS201: Data Structures and Discrete Mathematics I Hash Table.
Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.
WEEK 1 Hashing CE222 Dr. Senem Kumova Metin
Data Structures and Algorithms Hashing First Year M. B. Fayek CUFE 2010.
Chapter 5: Hashing Part I - Hash Tables. Hashing  What is Hashing?  Direct Access Tables  Hash Tables 2.
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.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
CSE 373 Data Structures and Algorithms Lecture 17: Hashing II.
Chapter 5: Hashing Collision Resolution: Open Addressing Extendible Hashing Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova,
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.
COSC 1030 Lecture 10 Hash Table. Topics Table Hash Concept Hash Function Resolve collision Complexity Analysis.
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 Hashing by Adlane Habed School of Computer Science University of Windsor May 6, 2005.
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.
1 Data Structures CSCI 132, Spring 2014 Lecture 33 Hash Tables.
Searching Tables Table: sequence of (key,information) pairs (key,information) pair is a record key uniquely identifies information, so no duplicate records.
Hash Tables Ellen Walker CPSC 201 Data Structures Hiram College.
1 Designing Hash Tables Sections 5.3, 5.4, 5.5, 5.6.
Chapter 11 (Lafore’s Book) Hash Tables Hwajung Lee.
Fundamental Structures of Computer Science II
Hashing: Collision Resolution Schemes
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)
Richard Anderson (instead of Martin Tompa)
Hash In-Class Quiz.
Collision Resolution Neil Tang 02/18/2010
Resolving collisions: Open addressing
Searching Tables Table: sequence of (key,information) pairs
Hash Tables Computer Science and Engineering
A Hash Table with Chaining
Tree traversal preorder, postorder: applies to any kind of tree
Collision Resolution Neil Tang 02/21/2008
Data Structures and Algorithm Analysis Hashing
Hashing: Collision Resolution Schemes
Presentation transcript:

CSC2100B Tutorial 6 Hashing Hao Ma Yi LIU Mar 4, 2004

CSC2100B Tutorial2 Hashing - overview Hash function Collision resolution  Separate Chaining (Open hashing)  Open addressing (Closed Hashing) Linear probing Quadratic probing Double hashing

CSC2100B Tutorial3 Hashing - hash function Hash function  A mapping function that maps a key to a number in the range 0 to TableSize -1 int hashfunc(int integer_key) { return integer_key % HASHTABLESIZE; }

CSC2100B Tutorial4 Hashing - separate chaining If two keys map to same value, the elements are chained together.

CSC2100B Tutorial5 Hashing - example Insert the following four keys into hash table of size 10 using separate chaining. The hash function is key % 10 Initial hash table

CSC2100B Tutorial6 Hashing - example Insert the following four keys into hash table of size 10 using separate chaining. The hash function is key % 10 After insert % 10 = 2

CSC2100B Tutorial7 Hashing - example Insert the following four keys into hash table of size 10 using separate chaining. The hash function is key % 10 After insert % 10 = 4

CSC2100B Tutorial8 Hashing - example Insert the following four keys into hash table of size 10 using separate chaining. The hash function is key % 10 After insert % 10 = 5

CSC2100B Tutorial9 Hashing - example Insert the following four keys into hash table of size 10 using separate chaining. The hash function is key % 10 After insert % 10 = 2

CSC2100B Tutorial10 Hashing Open addressing  Open addressing hash tables store the records directly within the array.  A hash collision is resolved by probing, or searching through alternate locations in the array. Linear probing Quadratic probing Random probing Double hashing

CSC2100B Tutorial11 Hashing - Open addressing #define HASHTABLESIZE 51 typedef struct { int key[HASHTABLESIZE]; char state[HASHTABLESIZE]; /* -1=lazy delete, 0=empty, 1=occupied */ } hashtable; /* The hash function */ int hash(int input) { return input % HASHTABLESIZE; }

CSC2100B Tutorial12 Hashing - Open addressing Open addressing  if collision occurs, alternative cells are tried.  h 0 (X), h 1 (X), h 2 (X),... h i (X) = (Hash(X) + F(i) ) mod TableSize  Linear probing:F(i) = i  Quadratic probing:F(i) = i 2  Double hashing:F(i) = i * Hash 2 (X)

CSC2100B Tutorial13 Hashing - Open addressing void open_addressing_insert(int item, hashtable * ht ) { hash_value = hash(item); i = hash_value; while (ht->state[i]!= 0) { if (ht->key[i] == item) { fprintf(stderr,”Duplicate entry\n”); exit(1); } i = (i + F(i)) % HASHTABLESIZE; if (i == hash_value) { fprintf(stderr, “The table is full\n”); exit(1); } ht->key[i] = item; }

CSC2100B Tutorial14 Hashing - Open addressing Linear probing  F(i) = i h i (X) = (Hash(X) + i ) mod TableSize h 0 (X) = (Hash(X) + 0) mod TableSize, h 1 (X) = (Hash(X) + 1) mod TableSize, h 2 (X) = (Hash(X) + 2) mod TableSize,...

CSC2100B Tutorial15 Hashing - Open addressing Linear probing /* The h function */ int h(int i, int input) { return (hash(input) + i)% HASHTABLESIZE; }

CSC2100B Tutorial16 Hashing - Open addressing Linear probing example  Initial hash table

CSC2100B Tutorial17 Hashing - Open addressing Linear probing example  Insert 7 at h 0 (7)(7 mod 17) = 7

CSC2100B Tutorial18 Hashing - Open addressing Linear probing example  Insert 36 at h 0 (36)(36 mod 17) = 2

CSC2100B Tutorial19 Hashing - Open addressing Linear probing example  Insert 24 at h 1 (24)(24 mod 17) = 7

CSC2100B Tutorial20 Hashing - Open addressing Linear probing example  Insert 75 at h 2 (75)(75 mod 17) = 7

CSC2100B Tutorial21 Hashing - Open addressing Linear probing example  Delete 24

CSC2100B Tutorial22 Hashing - Open addressing Linear probing example  Find 75, found !

CSC2100B Tutorial23 Hashing - Open addressing Quadratic probing  F(i) = i 2 h i (X) = (Hash(X) + i 2 ) mod TableSize h 0 (X) = (Hash(X) ) mod TableSize, h 1 (X) = (Hash(X) ) mod TableSize, h 2 (X) = (Hash(X) ) mod TableSize,...

CSC2100B Tutorial24 Hashing - Open addressing Quadratic probing /* The h function */ int h(int i, int input) { return (hash(input) + i * i) % HASHTABLESIZE; }

CSC2100B Tutorial25 Hashing - Open addressing Quadratic probing example  Initial hash table

CSC2100B Tutorial26 Hashing - Open addressing Quadratic probing example  Insert 5 at h 0 (5)(5 mod 17) = 5

CSC2100B Tutorial27 Hashing - Open addressing Quadratic probing example  Insert 56 at h 1 (56)(56 mod 17) = 5 ((56 + 1*1) mod 17) = 6

CSC2100B Tutorial28 Hashing - Open addressing Quadratic probing example  Insert 73 at h 2 (56)(73 mod 17) = 5 ((73 + 2*2) mod 17) = 9

CSC2100B Tutorial29 Hashing - Open addressing Quadratic probing example  Insert 124 at h 3 (124)(124 mod 17) = 5 (( *3) mod 17) = 6

CSC2100B Tutorial30 Hashing - Open addressing Random probing Randomize(X) h 0 (X) = Hash(X), h 1 (X) = (h 0 (X) + RandomGen()) mod TableSize, h 2 (X) = (h 1 (X) + RandomGen()) mod TableSize,...  Use Randomize(X) to ‘seed’ the random number generator using X  Each call of RandomGen() will return the next random number in the random sequence for seed X

CSC2100B Tutorial31 Hashing - Open addressing Implement random probing using random number generator in C  pseudo-random number generator: rand()  returns an integer between 0 and RAND_MAX  ‘Seed’ the randomizer srand(unsigned int);  Use time as a ‘seed’ time(time_t *); time(NULL);

CSC2100B Tutorial32 Hashing - Open addressing  random number generation in C srand(time(NULL)); for (i = 0; i < 10; i++) printf("%d\n", rand() );

CSC2100B Tutorial33 Hashing - Open addressing Double hashing:F(i) = i * Hash 2 (X) h i (X) = (Hash(X) + i * Hash 2 (X) ) mod TableSize h 0 (X) = (Hash(X) + 0 * Hash 2 (X)) mod TableSize, h 1 (X) = (Hash(X) + 1 * Hash 2 (X)) mod TableSize, h 2 (X) = (Hash(X) + 2 * Hash 2 (X)) mod TableSize,...

Thanks!