Robbie CSCI2100A Data Structures Tutorial

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
Skip List & Hashing CSE, POSTECH.
Hashing as a Dictionary Implementation
Hashing: Collision Resolution Schemes
Log Files. O(n) Data Structure Exercises 16.1.
Hashing Techniques.
Hashing CS 3358 Data Structures.
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.
Hash Tables1 Part E Hash Tables  
Hash Tables1 Part E Hash Tables  
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
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.
§3 Separate Chaining ---- keep a list of all keys that hash to the same value struct ListNode; typedef struct ListNode *Position; struct HashTbl; typedef.
COSC 2007 Data Structures II
ICS220 – Data Structures and Algorithms Lecture 10 Dr. Ken Cosh.
CS 221 Analysis of Algorithms Data Structures Dictionaries, Hash Tables, Ordered Dictionary and Binary Search Trees.
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:
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.
Hash Tables1   © 2010 Goodrich, Tamassia.
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.
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.
Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.
WEEK 1 Hashing CE222 Dr. Senem Kumova Metin
Chapter 5: Hashing Part I - Hash Tables. Hashing  What is Hashing?  Direct Access Tables  Hash Tables 2.
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.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
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.
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.
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
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 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.
Fundamental Structures of Computer Science II
CE 221 Data Structures and Algorithms
Hash In-Class Quiz.
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
Data Structures and Algorithm Analysis Hashing
Collision Resolution: Open Addressing Extendible Hashing
Presentation transcript:

Robbie CSCI2100A Data Structures Tutorial Hash in C Robbie CSCI2100A Data Structures Tutorial

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

Hashing - 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; }

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

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

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

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

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

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

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

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; }

Hashing - Open addressing if collision occurs, alternative cells are tried. h0(X), h1(X), h2(X), ..., hk(X) = (Hash(X) + F(k) ) mod TableSize Linear probing : F(k) = k Quadratic probing : F(k) = k2 Double hashing : F(k) = k * Hash2(X) By modifying the output of previous hashing function

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

Hashing - Open addressing Linear probing F(k) = k hk(X) = (Hash(X) + k ) mod TableSize h0(X) = (Hash(X) + 0) mod TableSize, h1(X) = (Hash(X) + 1) mod TableSize, h2(X) = (Hash(X) + 2) mod TableSize, ...

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

Hashing - Open addressing Linear probing example Initial hash table

Hashing - Open addressing Linear probing example Insert 7 at h0(7) (7 mod 17) = 7

Hashing - Open addressing Linear probing example Insert 36 at h0(36) (36 mod 17) = 2

Hashing - Open addressing Linear probing example Insert 24 at h1(24) (24 mod 17) = 7

Hashing - Open addressing Linear probing example Insert 75 at h2(75) (75 mod 17) = 7

Hashing - Open addressing Linear probing example Delete 24 Should not be used to terminate the search

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

Hashing - Open addressing Quadratic probing F(k) = k2 hk(X) = (Hash(X) + k2 ) mod TableSize h0(X) = (Hash(X) + 02) mod TableSize, h1(X) = (Hash(X) + 12) mod TableSize, h2(X) = (Hash(X) + 22) mod TableSize, ...

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

Hashing - Open addressing Quadratic probing example Initial hash table

Hashing - Open addressing Quadratic probing example Insert 5 at h0(5) (5 mod 17) = 5

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

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

Hashing - Open addressing Quadratic probing example Insert 124 at h3(124) (124 mod 17) = 5 ((124 + 3*3) mod 17) = 6

Hashing - Open addressing Random probing Randomize(X) h0(X) = Hash(X), h1(X) = (h0(X) + RandomGen()) mod TableSize, h2(X) = (h1(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

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);

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

Hashing - Open addressing Double hashing : F(k) = k * Hash2(X) hk(X) = (Hash(k) + i * Hash2(X) ) mod TableSize h0(X) = (Hash(X) + 0 * Hash2(X)) mod TableSize, h1(X) = (Hash(X) + 1 * Hash2(X)) mod TableSize, h2(X) = (Hash(X) + 2 * Hash2(X)) mod TableSize, ... Sum up the outputs of two hashing functions

Midterm Programming Exam Time: 6:30 pm - 9:30 pm, Thursday, Apr. 2, 2015 Venue: Rm 924, SHB Please arrive to the venue before 5:30 pm since you need time to register, test your disk, and have a quick tutorial on the judge system. Scope: to sort algorithms You can bring print outs of previous programming homework assignments or standard code for some data structures. No electronic devices are allowed during the exam, e.g., mobile phone, portable disk, etc.

Environment The operation system will be Ubuntu Linux 12.04 LTS. And the computer configuration will have these basic editors: Vi/VIM Emacs Gedit Nano How to compile: gcc -g progname.c -o progname