Design & Analysis of Algorithm Hashing

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
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.
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)
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.
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.
Symbol Tables Symbol tables are used by compilers to keep track of information about variables functions class names type names temporary variables etc.
D ESIGN & A NALYSIS OF A LGORITHM 01 – H ASHING Informatics Department Parahyangan Catholic University.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture8.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
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 Sections 10.2 – 10.3 CS 302 Dr. George Bebis.
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.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
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.
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,
CPSC 252 Hashing Page 1 Hashing We have already seen that we can search for a key item in an array using either linear or binary search. It would be better.
Hash Tables © Rick Mercer.  Outline  Discuss what a hash method does  translates a string key into an integer  Discuss a few strategies for implementing.
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,
CS6045: Advanced Algorithms Data Structures. Hashing Tables Motivation: symbol tables –A compiler uses a symbol table to relate symbols to associated.
CSC 143T 1 CSC 143 Highlights of Tables and Hashing [Chapter 11 p (Tables)] [Chapter 12 p (Hashing)]
1 What is it? A side order for your eggs? A form of narcotic intake? A combination of the two?
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
Course Developer/Writer: A. J. Ikuomola
Data Structures Using C++ 2E
Design & Analysis of Algorithm Hashing (Contd.)
Hash table CSC317 We have elements with key and satellite data
CSE373: Data Structures & Algorithms Lecture 6: Hash Tables
CSCI 210 Data Structures and Algorithms
LEARNING OBJECTIVES O(1), O(N) and O(LogN) access times. Hashing:
Data Abstraction & Problem Solving with C++
CS 332: Algorithms Hash Tables David Luebke /19/2018.
Hashing Alexandra Stefan.
Hashing - Hash Maps and Hash Functions
Subject Name: File Structures
Hashing Alexandra Stefan.
Data Structures Using C++ 2E
Review Graph Directed Graph Undirected Graph Sub-Graph
Hashing Exercises.
Hash functions Open addressing
Introduction to Hashing & Hashing Techniques
Hash tables Hash table: a list of some fixed size, that positions elements according to an algorithm called a hash function … hash function h(element)
Advanced Associative Structures
Hash Table.
Computer Science 2 Hashing
Hash Tables.
Introduction to Algorithms 6.046J/18.401J
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
CSE 373: Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Data Structures – Week #7
Hashing Alexandra Stefan.
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
CS202 - Fundamental Structures of Computer Science II
Hashing Sections 10.2 – 10.3 Lecture 26 CS302 Data Structures
Space-for-time tradeoffs
EE 312 Software Design and Implementation I
Introduction to Hashing & Hashing Techniques
Data Structures – Week #7
Hashing.
CS 3343: Analysis of Algorithms
EE 312 Software Design and Implementation I
Lecture-Hashing.
Presentation transcript:

Design & Analysis of Algorithm Hashing Informatics Department Parahyangan Catholic University

Can we achieve O(1) performance ? Motivation We have seen many data structures: array, linked list, stack, and queue. Each has its own strength and weaknesses Consider the case when we want to find an element in a data structure Unsorted array  sequential search O(n) Sorted array  binary search O(lg n) Linked list  sequential search O(n) Can we achieve O(1) performance ?

Analogy Remembering == our brain maps each item to some location, so finding an item is fast (O(1)) After hours of playing the same hidden object game, we remember where each item is located, thus our search is no longer “sequential”. Moreover, we can find them right away

Analogy We remember which rack sells our favorite item in a supermarket, thus we directly goes to that rack without checking the other racks.

Analogy We know where to find “Universitas Parahyangan” in Yellow Pages – it must be around the beginning of “P” section.

O(1) for insertion and lookup How does hashing work ? Associate keys with values Given a key (e.g. a company’s name), retrieve the value (e.g. phone number, address, etc.) for the given key A hash function is defined to map the key to an index of a table where the value is stored e.g. “Universitas Parahyangan” is stored at section “P” O(1) for insertion and lookup Is it really O(1)?

Another Example Dairy products are barcoded (given a key) and put into “Dairy” rack (mapped to a table)

Another Example J (74) K (75) L (76) … S (83) John Smith +1-555-1234 Lisa Smith +1-555-8976 Sam Doe +1-555-5030 DATA = KEY + VALUE John Smith Sam Doe Lisa Smith KEY HASH TABLE

Direct Addressing If the size of universe of keys is small, and the keys are unique, then we can set up a table whose size is the same as the universe’s size. Each slot with index k in the table stores element with key k. If no element with key k, then slot with index k is empty

Direct Addressing :: example Student’s NPM : XXXXYYZZZZ XXXX = year YY = faculty and department’s number ZZZZ = student’s number Key’s universe is 0000000000 – 9999999999 (10,000,000,000 keys)  very big ! Let’s say we only want to store the data of Informatics Department (YY=73). Key’s universe is 0000730000 – 9999739999 (100,000,000 keys)  still a lot !

Direct Addressing :: example First year of Parahyangan’s Informatics Department is 1996. Let’s say we want to store student’s information only up to year 2020. Key’s universe is 1996730000 – 2020739999 (24,009,999 keys)  still a lot ! But the “73” part is always the same, so let’s cut it out ! Then the key’s universe becomes 19960000 – 20209999 (249,999 keys)  better ! We can save even more by considering that each year’s student never exceed 999 (doesn’t need the 4th digit) and write the year in 2 digits format.

Problems in Direct Addressing Only implementable if the size of universe is small What if we want to store IP addresses ? 000.000.000.000 to 255.255.255.255 = 256^4 = 4,294,967,296 = 4GB space What if we want to store 10 characters names ? = 26^10 = 141,167,095,653,376 What if we want to store 16 digits KTP numbers ? = 10^16 = 10,000,000,000,000,000 What if 50 characters address ? Solution: Use hash table with size |K| = the number of keys stored e.g. in the previous example, we don’t need to prepare a space for data before year 1996 Requires fewer storage space but still O(1) time complexity for lookup When the size is big: Requires too much memory space Inefficient if only a small portion of the keys are stored

Hash Function & Hash Table A hash function h(k) is defined to map the key k to an index of a table where the element with key k is stored J (74) K (75) L (76) … S (83) John Smith Hash function e.g. take the ASCII number of the first character Sam Doe Lisa Smith The value h(k) is called hash value KEY HASH TABLE

Example :: NPM 1996730023 2000730055 2010730111 Hash function : Extract the last 2 digits of year Extract the last 3 digits of student number Concatenate the two of them 96023 00055 10111

e.g. take the ASCII number of the first character Collision Since the storage size is reduced, two distinct keys k1 and k2 may be mapped to the same index h(k1) = h(k2) This condition is known as collision  resolution strategy is required (we shall see later) Example: J (74) K (75) L (76) … Hash function e.g. take the ASCII number of the first character John Smith Jane Smith

Choosing Hash Function Deterministic h(k) always gives the same result for the same k Easy to compute needs to be O(1), otherwise insertion and lookup become expensive The range has to agree with table size must not map any value outside the hash table

Types of Hash Function Modular/Division Truncation Multiplicative Folding Length-dependent

Hash Function Modular/Division Define the table size M h(k) = k mod M M should be prime numbers, since prime numbers provide better distribution in the table Why should M be prime ?

Hash Function Modular/Division Suppose we want to store NPM into a hash table with hash function h(k) = k mod 100 So, only the last 2 digits of NPM determine the hash value Why should M be prime ? Observe that there are more students with small NPM than students with large NPM. Additionally, NPM ≥100 are also hashed to index 0..99, thus the smaller indexes have more collisions Using prime number for M gives a better distribution (thus less collisions) because every digits of the key contribute to the hash value.

Hash Function Truncation Take the last n digits/characters as table index e.g. taking the last 3 digits of your NPM Fast, but often cannot evenly distribute the keys in the table What is the difference with Modulo/Division method ? Similar reason as the previous example

Hash Function Multiplicative Suppose we have a floating point key k, 0 ≤ k < 1 And a hash table of size M Define M = 10 1 2 3 4 5 6 7 8 9 k = 0.7237378 k = 0.3562319

Hash Function Multiplicative What if key’s domain is not a floating point ? Choose a floating point A in the range 0 < A < 1 Define floating point floating point ranged 0..1

Hash Function Multiplicative Does the value of M matter ? M doesn’t matter  Usually M is a power of 2 since it’s easier to implement on most computer Does the value of A matter ? This method works practically with any valid A, but some works better than the other Knuth suggest that A ≈ (√5 – 1)/2 = 0.6180339887… (golden ratio) is likely to work reasonably well Disadvantage ? Computing hash value is slower than modular method See “Introduction to Algorithms book

Hash Function Folding/Shifting Just like folding a paper 1 2 3 1 2 3 4 5 6 4 5 6 7 8 9 7 8 9 0 9 8 0 9 8 3 2 1 4 5 6 9 8 7 0 9 8 + 6 4 2

Hash Function Folding/Shifting Like cutting the paper and stacks them up 1 2 3 1 2 3 4 5 6 4 5 6 7 8 9 7 8 9 0 9 8 0 9 8 + 2 4 6

Hash Function Length-dependent Useful when the keys do not have the same length  use the length of the key as one of the hashing function’s parameter E.g. the keys are names of people, take the sum of first 5 characters plus its length to get the table index (can be combined with modular method if needed)

String to integer key What if the type of the key is not a number ? (e.g. string) Treat the string as a base n number Base 26 if string consist of A..Z only e.g. A is digit 0, B is 1, …, and Z is 25 Base 52 if string consist of A..Z, a..z only e.g. a = 0, … z = 25, A = 26, … Z = 51 Base 256 if string consist of all possibly ASCII characters Similar approach can be used to encode other key types

String to integer key Be careful when choosing number’s base and M ! Both numbers should be coprime to each other (do not have common factor other than 1) Example : String is treated as base 26 number M = 13 ABC26 = (Cx260)mod 13+ (Bx261)mod 13 + (Ax262)mod 13 C C multiply of 13 multiply of 13 = 1 = 2 x 13 = (2 x 13)2 Only the last digit which is not 0, thus not every digit contributes to the hash value