1 Introduction to Hashing - Hash Functions Sections 5.1 and 5.2.

Slides:



Advertisements
Similar presentations
David Luebke 1 6/7/2014 ITCS 6114 Skip Lists Hashing.
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 CS 310 – Professor Roch Weiss Chapter 20 All figures marked with a chapter and section number are copyrighted © 2006 by Pearson Addison-Wesley.
Hashing.
Dictionaries Again Collection of pairs.  (key, element)  Pairs have different keys. Operations.  Search(theKey)  Delete(theKey)  Insert(theKey, theElement)
CS202 - Fundamental Structures of Computer Science II
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.
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,
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 18: Hash Tables.
Hashing CS 3358 Data Structures.
1 Hashing (Walls & Mirrors - end of Chapter 12). 2 I hate quotations. Tell me what you know. – Ralph Waldo Emerson.
Lecture 11 March 5 Goals: hashing dictionary operations general idea of hashing hash functions chaining closed hashing.
Hash Tables and Associative Containers CS-212 Dick Steflik.
Hashing COMP171 Fall Hashing 2 Hash table * Support the following operations n Find n Insert n Delete. (deletions may be unnecessary in some applications)
CS2420: Lecture 33 Vladimir Kulyukin Computer Science Department Utah State University.
COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
COSC 2007 Data Structures II
ICS220 – Data Structures and Algorithms Lecture 10 Dr. Ken Cosh.
Symbol Tables Symbol tables are used by compilers to keep track of information about variables functions class names type names temporary variables etc.
1 Chapter 5 Hashing General ideas Methods of implementing the hash table Comparison among these methods Applications of hashing Compare hash tables with.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Hashing CS 202 – Fundamental Structures of Computer Science II Bilkent.
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 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.
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,
Hashing Sections 10.2 – 10.3 CS 302 Dr. George Bebis.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
Can’t provide fast insertion/removal and fast lookup at the same time Vectors, Linked Lists, Stack, Queues, Deques 4 Data Structures - CSCI 102 Copyright.
1 Introduction to Hashing - Hash Functions Sections 5.1, 5.2, and 5.6.
Hash Tables - Motivation
Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.
1 Hashing - Introduction Dictionary = a dynamic set that supports the operations INSERT, DELETE, SEARCH Dictionary = a dynamic set that supports the operations.
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 is a method to store data in an array so that sorting, searching, inserting and deleting data is fast. For this every record needs unique key.
Hashing Fundamental Data Structures and Algorithms Margaret Reid-Miller 18 January 2005.
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.
Hashing 1 Hashing. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
Midterm Midterm is Wednesday next week ! The quiz contains 5 problems = 50 min + 0 min more –Master Theorem/ Examples –Quicksort/ Mergesort –Binary Heaps.
CHAPTER 9 HASH TABLES, MAPS, AND SKIP LISTS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++,
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.
Hash Tables ADT Data Dictionary, with two operations – Insert an item, – Search for (and retrieve) an item How should we implement a data dictionary? –
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.
Hashing & Hash Tables. Sets/Dictionaries Set - Our best efforts to date:
Hash Functions Andy Wang Data Structures, Algorithms, and Generic Programming.
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:
1 Designing Hash Tables Sections 5.3, 5.4, 5.5, 5.6.
Duke CPS Faster and faster and … search l Binary search trees ä average case insert/search/delete = O( ) ä worst case = O( ) l balanced search.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
CSE373: Data Structures & Algorithms Lecture 6: Hash Tables
CS 332: Algorithms Hash Tables David Luebke /19/2018.
Introduction to Hashing - Hash Functions
Searching.
Hash Functions Sections 5.1 and 5.2
CS223 Advanced Data Structures and Algorithms
Advanced Associative Structures
Hashing Sections 10.2 – 10.3 Lecture 26 CS302 Data Structures
EE 312 Software Design and Implementation I
Hashing.
EE 312 Software Design and Implementation I
Presentation transcript:

1 Introduction to Hashing - Hash Functions Sections 5.1 and 5.2

2 Hashing Data items are stored in an array of some fixed size –Hash table Search performed using some part of the data item –key Used for performing insertions, deletions, and finds in constant average time Operations requiring ordering information not supported efficiently –Such as findMin, findMax

3 An example of hash table

4 Applications of hash tables Comparing search efficiency of different data structures: –Vector, list: O(N) –Binary search tree: O(log(N)) –Hash table: O(1) Compilers to keep track of declared variables –Symbol tables Mapping from name to id Game programs to keep track of positions visited –Transposition table On-line spelling checkers BTW, a fast way to solve the word puzzle problem is to use hash table to maintain the valid words –Examples/r7

5 Hashing functions Map keys to integers (which represent table indices) –Hash(Key) = Integer –Evenly distributed index values Even if the input data is not evenly distributed What happens if multiple keys mapped to the same integer (same position)? –Collision management (discussed in detail later)

6 Simple Hash Functions Assumptions: –K: an unsigned 32-bit integer –M: the number of buckets (the number of entries in a hash table) Goal: –If a bit is changed in K, all bits are equally likely to change for Hash(K) –So that items evenly distributed in hash table

7 A Simple Function What if –Hash(K) = K % M –Where M is of any integer value What is wrong? Values of K may not be evenly distributed –But Hash(K) needs to be evenly distributed Suppose –M = 10, –K = 10, 20, 30, 40 Then K % M = 0, 0, 0, 0, 0…

8 Another Simple Function If –Hash(K) = K % P, P = prime number Suppose –P = 11 –K = 10, 20, 30, 40 K % P = 10, 9, 8, 7 More uniform distribution… So hash tables have prime number of entries

9 Hashing a Sequence of Keys K = {K 1, K 2, …, K n ) E.g., Hash(“test”) = Design Principles –Use the entire key –Use the ordering information

10 Use the Entire Key unsigned int Hash(const string& Key) { unsigned int hash = 0; for (string::size_type j = 0; j != Key.size(); ++j) { hash = hash ^ Key[j] // exclusive or } return hash; } Problem: Hash(“ab”) == Hash(“ba”)

11 Use the Ordering Information unsigned int Hash(const string &Key) { unsigned int hash = 0; for (string::size_type j = 0; j != Key.size(); ++j) { hash = hash ^ Key[j]; hash = hash * (j%32); } return hash; }

12 Better Hash Function unsigned int Hash(const string& S) { string::size_type i; long unsigned int bigval = S[0]; for (i = 1; i < S.size(); ++i) bigval = ((bigval & 65535) * 18000) // low16 * magic_number + (bigval >> 16) // high16 + S[i]; bigval = ((bigval & 65535) * 18000) + (bigval >> 16); // bigval = low16 * magic_number + high16 return bigval & 65535; // return low16 } /* some values: f(a) = f(b) = f(abcd) = f(bacd) = f(dcba) = f(x) = f(xx) = f(xxx) = f(xxxx) = f(xxxxx) = */