Data Structures - CSCI 102. CS102 Hash Tables. Prof. Tejada. Copyright Sheila Tejada

Size: px
Start display at page:

Download "Data Structures - CSCI 102. CS102 Hash Tables. Prof. Tejada. Copyright Sheila Tejada"

Transcription

1 CS102 Hash Tables Prof. Tejada 1

2 Vectors, Linked Lists, Stack, Queues, Deques Can t provide fast insertion/removal and fast lookup at the same time The Limitations of Data Structure Binary Search Trees, Heaps Provide consistently fast operations, but must maintain an internal ordering How can we further improve the performance of lookup, add & removal? What if we didn t care about the ordering of the elements at all? 4

3 Lookup Tables For operations where we only care about fast add/remove/search, not fast traversal, we create a table structure to optimize for fast lookup Each value in the table has a unique key The key is used as a short identifier to lookup an entire value in the table Example Your student ID is used to look up your student record (e.g. name, GPA, etc.) 5

4 Lookup Tables What kind of operations do we need to perform on a lookup table? Search(key) See if a particular value identified by key is in the table Insert(key,value) Insert a new value identified by key into the table Remove(key) Remove the value identified by key from the table We don t care as much about traversal (visiting all elements) in this scenario 6

5 Sample Object We want to keep a directory of all the students at USC and be able to look them up by their student ID Let s assume ID is a unique integer struct Student { string name; double gpa; int id; }; 7

6 Direct Address Table If we can guarantee that student IDs will always range from 0 to N (e.g. 0 to 4999), we could just store them in an array: Student data[4999]; Then when we want to grab a particular student, we know Student N is at index N: int id = 3285; Student s = data[id]; 8

7 Direct Address Table Student IDs Data Student Objects John Doe Jane Doe ID 3.7 GPA Some Guy Name

8 Direct Address Table Direct Addressing Maps keys directly to the indexes in an array Unused array indexes need to be marked Generally use NULL Operations are fast O(1) worst case 10

9 Direct Address Table Direct Addressing Issues Key Restrictions Keys must fall into a nice, uniform range Keys must be numeric Array Size If there are N possible keys, then data[] must be of size N Our array could get HUGE What if we re only using a small numbers of keys? Tons of space is wasted How can we get around these limitations? 11

10 Hash Functions Hash Functions A function that maps key values to array indexes Input records all have a unique key The hash function maps key to an array index Records are stored at data[hash(key)] Ideally every unique key also has unique hash(key) Direct Addressing essentially uses a hash function that does nothing int directaddresshash(int studentid) { return studentid; } 12

11 Hash Tables Data Student Objects Student IDs (Keys) hash(4) hash(0) John Doe Jane Doe Hash Function hash(2) 4 ID 3.7 GPA Some Guy Name 13

12 Hash Tables Hash Functions How can we avoid having to make our array gigantic to hold all possible keys? 14

13 Hash Tables Hash Functions How can we avoid having to make our array gigantic to hold all possible keys? Simple solution: use modular arithmetic Size of the backing array is no longer dependent on the number of unique keys int modularhash(int studentid) { return studentid % ARRAY_SIZE; } Recall direct addressing: int directaddresshash(int studentid) { return studentid; } 15

14 Hash Functions What makes a good hash function? Fast Hashing is supposed to be faster than a binary search tree. hash(key) needs to be O(1) Deterministic If we have a key K, then hash(k) must always give the same result Uniform distribution The hash function should uniformly distribute keys across all of the available indexes in the storage array Making a good hash function is hard 16

15 Hash Functions Making a hash function Map your data into the set of natural numbers N = {0, 1, 2,...} For strings, use things like ASCII letter codes Prime numbers are your friend Prime table sizes tend to yield better results Handle variants of the same pattern E.g. make sure "get" and "gets" hash differently Try to be independent of any patterns that may exist in the data You won t usually have to write your own, but you should know what the default hash function does 17

16 Sample Hash Function The Java String hash Changing the multiplier to 32 from 31 results in significantly worse performance Primes numbers good, powers of 2 bad hash(s) = s[0] 31 n-1 + s[1] 31 n s[n-2] 31 + s[n-1] The Java Default hash function Return the address in memory of the object Works better in Java because everything is actually a pointer/reference behind the scenes int hash(string s) { int h = 0; for (i=0; i < s.size(); i++) { h = (h*31)+s[i]; } return h; } 18

17 Hash Tables Hashing Issues Hash Tables do not maintain any ordering of their internal elements Creating a perfect hash function is almost impossible Collisions When two distinct keys generate the same hash value it s called a collision hash(k1) == hash(k2) 19

18 Collision Handling Possible Collision Handling Solutions Ideal hash function Table Resizing Open Addressing Linear probing Quadratic probing Double hashing Chaining Universal Hashing Perfect Hashing 20

19 Collision Handling Ideal Hash Function Create a hash function so good that it will never cause a collision You can get close if you use a cryptographic hash algorithm like sha1 or md5 You will pay a performance penalty that will affect your insertion speed You will not get a collision if the size of your hash table is really really really large Most likely, this cannot be achieved 21

20 Collision Handling Table Resizing If your hash function uses modular arithmetic, make the array bigger when you get a collision When a collision happens... Dynamically create a new, larger array (usually double the size) Re-hash all input values into the new larger array With a little luck, no more collisions A helpful strategy, but not a very good one on its own 22

21 Collision Handling Open Addressing If we try to insert a new element and there s a collision, keep probing the hash table until we find a vacant space Probing If a collision occurs, use a deterministic algorithm to calculate the next array index to check (based on the initial hash result) All data is stored directly in the hash table. No extra data structures are needed. 23

22 Open Addressing Linear Probing If there is no collision, insert the element into the table as normal If a collision occurs, start walking through the hash table index by index looking for an empty slot First check data[hash(key)] If it s taken, check data[hash(key)+1] If it s taken, check data[hash(key)+2] Etc. If you walk off the end of the array, wrap around back to the beginning of the array 24

23 Open Addressing (Linear Probing) Start with an empty Hash Table Data

24 Open Addressing (Linear Probing) Insert "John Doe" with ID = 123 Data Student 4 Name GPA ID John Doe

25 Open Addressing (Linear Probing) Insert "John Doe" with ID = 123 hash(123) = 1 Data 0 Student hash() hash(123) = Name GPA ID John Doe

26 Open Addressing (Linear Probing) Insert "John Doe" with ID = 123 hash(123) = 1 data[1] is empty, no collision Data 0 Student hash() hash(123) = Name GPA ID John Doe

27 Open Addressing (Linear Probing) Insert "John Doe" with ID = 123 hash(123) = 1 data[1] is empty, no collision store it there Student hash() hash(123) = 1 Data John Doe Name GPA ID John Doe

28 Open Addressing (Linear Probing) Hash Table contains one item Data 0 1 John Doe

29 Open Addressing (Linear Probing) Insert "Jane Doe" with ID = 202 Name GPA ID Student Jane Doe Data John Doe

30 Open Addressing (Linear Probing) Insert "Jane Doe" with ID = 202 hash(202) = 3 Data 0 1 John Doe Name GPA ID Student Jane Doe hash() hash(202) =

31 Open Addressing (Linear Probing) Insert "Jane Doe" with ID = 202 hash(202) = 3 data[3] is empty, no collision Data 0 1 John Doe Name GPA ID Student Jane Doe hash() hash(202) =

32 Open Addressing (Linear Probing) Insert "Jane Doe" with ID = 202 hash(202) = 3 data[3] is empty, no collision store it there Data 0 1 John Doe Student hash() hash(202) = Jane Doe Name Jane Doe GPA 3.4 ID

33 Open Addressing (Linear Probing) Hash Table contains two items Data John Doe Jane Doe

34 Open Addressing (Linear Probing) Insert "Some Guy" with ID = 401 Name GPA ID Student Some Guy Data John Doe Jane Doe

35 Open Addressing (Linear Probing) Insert "Some Guy" with ID = 401 hash(401) = 1 Student hash() hash(401) = 1 Data John Doe Jane Doe Name Some Guy GPA 3.5 ID

36 Open Addressing (Linear Probing) Insert "Some Guy" with ID = 401 hash(401) = 1 data[1] is non-empty, collision! Student hash() hash(401) = 1 Data John Doe Jane Doe Name Some Guy GPA 3.5 ID

37 Open Addressing (Linear Probing) Insert "Some Guy" with ID = 401 hash(401) = 1 data[1] is non-empty, collision! Name hash(401)+1 = 2 Student Some Guy hash() hash(401) = 1 Data John Doe Jane Doe GPA ID

38 Open Addressing (Linear Probing) Insert "Some Guy" with ID = 401 hash(401) = 1 data[1] is non-empty, collision! Name hash(401)+1 = 2 data[2] is empty, no collision Student Some Guy hash() hash(401) = 1 Data John Doe Jane Doe GPA ID

39 Open Addressing (Linear Probing) Insert "Some Guy" with ID = 401 hash(401) = 1 Name data[1] is non-empty, collision! hash(401)+1 = 2 data[2] is empty, no collision store it there Student Some Guy GPA 3.5 hash() hash(401) = 1 Data John Doe Some Guy Jane Doe ID

40 Open Addressing (Linear Probing) Hash Table contains three items Data John Doe Some Guy Jane Doe

41 Open Addressing (Linear Probing) How do the following operations work now? Search(key) 1) Look for the value at data[hash(key)] If data[hash(key)] is empty, search fail If data[hash(key)] has value, search success If data[hash(key)] has different value, start linear probing 2) Repeatedly look at data[hash(key)+i] If data[hash(key)+i] is empty, search fail If data[hash(key)+i] has value, search success If data[hash(key)+i] has different value, increment i and repeat #2 If you eventually return back to the original hash(key) index, search fail 43

42 Open Addressing (Linear Probing) How do the following operations work now? Insert(key,value) Almost identical to search Start at hash(key) in the array and walk through until you find an empty slot Insert the item at the empty slot What if there are no empty slots in the table (the table is full)? Make a new, bigger table Rehash all the existing entries Go back to business as usual 44

43 Open Addressing (Linear Probing) How do the following operations work now? Remove(key) Search for the desired value Remove the value once you find it Complications You can t just set the removed value to NULL You might break other linear probe searches Have to mark elements as deleted or something similar instead 45

44 Open Addressing (Linear Probing) What is the Big O of each of these operations? Search(key) Insert(key,value) Remove(key) 46

45 Open Addressing (Linear Probing) The world s worst hash function int hash(t key) { return 0; } What would this hash function cause to happen if we were using an open addressing approach with linear probing? 47

46 Open Addressing (Linear Probing) What is the Big O of each of these operations? Search(key) Average: O(1), Worst Case: O(N) Insert(key,value) Average: O(1), Worst Case: O(N) Remove(key) Average: O(1), Worst Case: O(N) Operations depend on the table s load factor How big is the table? How many slots are taken already? load factor = (# of elements) / (size of array) "Utilization" 48

47 Open Addressing (Linear Probing) Other issues with Linear Probing Promotes clustering Long runs of occupied slots build up and increase average search time Potential Solutions Quadratic Probing Use a polynomial to determine where to look for empty slots Double Hashing Use a secondary hash function to determine where to look for empty slots 49

48 Collision Handling Chaining Each slot in the Hash Table can now contain a list of elements instead of a single element When multiple items hash to the same slot, they are placed in the list at that slot This requires the overhead of an extra list for each slot that contains one or more elements 50

49 Hash Table contains two items Chaining Data John Doe Jane Doe

50 Insert "Some Guy" with ID = 401 Chaining Data 0 John Doe Name GPA Student Some Guy Jane Doe ID

51 Chaining Insert "Some Guy" with ID = 401 hash(401) = 1 Data 0 John Doe hash(401) = 1 1 Name GPA Student Some Guy 3.5 hash() Jane Doe ID

52 Chaining Insert "Some Guy" with ID = 401 hash(401) = 1 data[1] is non-empty, collision! Data 0 John Doe hash(401) = 1 1 Name GPA Student Some Guy 3.5 hash() Jane Doe ID

53 Insert "Some Guy" with ID = 401 hash(401) = 1 data[1] is non-empty, collision! Chaining says to add the new entry to the list at data[1] Name GPA Student Some Guy 3.5 hash() Chaining hash(401) = 1 Data John Doe Jane Doe ID

54 Insert "Some Guy" with ID = 401 hash(401) = 1 data[1] is non-empty, collision! Chaining says to add the new entry to the list at data[1] Name GPA Student Some Guy 3.5 hash() Chaining Insert Some Guy in the list at data[1] hash(401) = 1 Data John Doe Jane Doe ID

55 Hash Table contains three items Chaining Some Guy Data John Doe Jane Doe

56 Chaining How do the following operations work now? Search(key) 1) Retrieve the list at data[hash(key)] If data[hash(key)] is empty, search fail 2) Linear search for the element with key in the list that was retrieved If the list contains the value, search success If the list does not contain the value, search fail 58

57 Chaining How do the following operations work now? Insert(key,value) Almost identical to search Retrieve the list at data[hash(key)] in the array If there is no list, make a one item long list containing value and re-insert it If there is a list already, add value to the list Does the table ever need to be resized? Theoretically, no...chains can grow forever In reality, sometimes it s good to resize so the chains don t become prohibitively long 59

58 Chaining How do the following operations work now? Remove(key) Almost identical to search Retrieve the list at data[hash(key)] Remove the value from the list (if it exists) 60

59 Chaining What is the Big O of each of these operations? Search(key) Insert(key,value) Remove(key) 61

60 Chaining The world s worst hash function int hash(t key) { return 0; } What would this hash function cause to happen if we are using a chaining approach? 62

61 Chaining What is the Big O of each of these operations? Search(key) Average: O(1), Worst Case: O(N) Insert(key,value) Average: O(1), Worst Case: O(1) Remove(key) Average: O(1), Worst Case: O(N) Operations depend on the average length of a chain (except for insert) 63

62 Chaining Other issues with Chaining Searching chains is still O(N) worst case Linear Search Potential Solutions Binary Search Trees Store a balanced binary search tree at each element in the Hash Table instead of a list Insertion becomes O(log(N)) Worst case lookup becomes O(log(N)) 64

63 Collision Handling The Problem If a malicious user knows what hash function you re using, they can intentionally cause your worst-case behavior Universal Hashing When the Hash Table is created, randomly choose a hash function independent of the keys that are going to be stored No single input gives worst-case behavior (just like randomized Quicksort) 66

64 Collision Handling Multi-Level Hashing Like chaining, but each element in the hash table holds another hash table with a different hash function By hashing multiple times, we can greatly decrease the odds of a collision Perfect Hashing If the set of possible keys is static (never changes), we can develop a perfect multi-level hash to give O(1) worst case performance e.g. The reserved keywords in a programming language are a static set of keys 67

65 Hash Tables Other Notes Hash Tables generally do provide a way for you to retrieve a list of the known keys Just keep in mind there is no guaranteed ordering of the keys C++ currently has no built-in hash table There s a proposal for unordered_map in the STL is on the table Google Sparse Hash provides C++ hash tables Boost C++ Libraries provides hash tables 68

66 CS102 Sets & Maps 1

67 Types of Containers Containers generally fall into 2 major categories Sequential Containers Elements are linearly arranged Each element has a position relative to the others Items are accessed by position Examples: vector, linked list, etc. Associative Containers Elements are not guaranteed to follow an ordering Designed for retrieval based on a unique identifier Items are accessed by key or value Examples: Hashtable, Binary Search Tree 3

68 Types of Containers Container Adapters Provide a specific & often restricted interface Rely on existing container to manage elements Interface is independent of underlying container Sequential Container Adapters Stack, Queue, Deque, Heaps, etc. Associative Container Adapters Set, MultiSet, Map, MultiMap, etc. 4

69 Types of Containers Sequential Container Adapters Stack, Queue, Deque We implemented them as linked lists Would they still work properly if we wanted to use arrays or vectors underneath? Would the performance suffer? Heap We implemented it using an array/vector Would it still work properly if we wanted to use a linked list underneath? Would the performance suffer? 5

70 Types of Containers Associative Container Adapters Set MultiSet (a.k.a. Bag) Map MultiMap Generally implemented using Hashtables or binary search trees (or variants) underneath 6

71 Sets Set An unordered collection of objects All elements in a set are unique (no duplicates) Each element is its own key Example Usage Given two lists of words, return a single list containing only the words that exist in both lists 7

72 Set Example Sets Initial set S = { 1, 3, 5, 8, 16, 22 } New values are added as normal Add 18 S = { 1, 3, 5, 8, 16, 22, 18 } Add 0 S = { 1, 3, 5, 8, 16, 22, 18, 0 } Duplicate values are rejected Add 8 S = { 1, 3, 5, 8, 16, 22, 18, 0 } Add 16 S = { 1, 3, 5, 8, 16, 22, 18, 0 } 8

73 Set Operations Set Operations search(value) See if the specified value is in the Set insert(value) Add a new value to the Set Return a value indicating whether or not anything was actually added to the Set remove(value) Remove an item from the set 9

74 Set Operations Set Operations (cont...) Union Combine two sets removing all redundant elements {1, 3, 5, 7} {2, 3, 4, 5} = {1, 2, 3, 4, 5, 7} Intersection Combine two sets keeping all shared elements {1, 3, 5, 7} {2, 3, 4, 5} = {3, 5} Difference Remove the elements in one set from another set (order matters!) {1, 3, 5, 7} - {2, 3, 4, 5} = {1, 7} {2, 3, 4, 5} - {1, 3, 5, 7} = {2, 4} 10

75 Sets Other Notes Set elements must be comparable Must be able to determine if something is already present in the set Either overload operators or pass comparison functions Sets are iterable Although sets have no specific ordering, you can still iterate over them Sets are not subscriptable You cannot index directly into a set like you would a vector (e.g. set[5] is not valid syntax) 11

76 Sets Given what you know, how would you go about implementing a Set? 12

77 Sets Given what you know, how would you go about implementing a Set? Binary Search Tree variants or Hashtables are both acceptable answers Many languages will give you both options and use names like TreeSet or HashSet to differentiate A TreeSet will generally be ordered while a HashSet will not C++ STL implements Sets with Red/Black Trees This means that the C++ STL set is actually an ordered set 13

78 MultiSet MultiSet An unordered collection of objects Duplicates are allowed Each element is its own key Works just like a Set, but allows repeated values Also known as a Bag Example Usage For each word that exists in a document, store the number of occurrences of that word 14

79 MultiSet Example MultiSet Initial set S = { 1, 3, 5, 8, 16, 22 } New values are added as normal Add 18 S = { 1, 3, 5, 8, 16, 22, 18 } Add 0 S = { 1, 3, 5, 8, 16, 22, 18, 0 } Duplicate values are added as normal Add 8 S = { 1, 3, 5, 8, 16, 22, 18, 0, 8 } Add 16 S = { 1, 3, 5, 8, 16, 22, 18, 0, 8, 16 } 15

80 MultiSet Operations MultiSet Operations search(value) See if the specified value is in the MultiSet Potentially also return the number of occurrences of a value in the MultiSet insert(value) Add a new value to the MultiSet (always succeeds) Duplicates are allowed remove(value) Remove all occurrences of the specified item from the MultiSet N inserts can be undone by 1 remove 16

81 MultiSets Given what you know, how would you go about implementing a MultiSet? 17

82 MultiSets Given what you know, how would you go about implementing a MultiSet? Binary Search Trees that allow duplicates Option 1: Allow duplicate elements in the tree Option 2: Each node in the tree contains a counter Hashtables where the key is the element and the value is a counter of how many of it exist Many other options Many languages will give you many options and use names like TreeMultiSet, HashMultiSet, TreeBag or HashBag to differentiate C++ STL implements MultiSets with Red/Black Trees 18

83 Map Map A Set of pairs of the form (key,value) The key must be unique The value can be non-unique Each key maps to exactly one value Each value can be mapped to by one or more keys Example Usage A dictionary. A word can be used as a key and its value could hold the word s definition, etymology, etc. 19

84 Map Example Maps M = { (32,John), (22,Jane), (40,Joe), (56,Jeff), (35,Joe) } Keys John Jane Joe Jeff Values 20

85 Map Operations Map Operations search(key) See if the specified key has an existing mapping insert(key,value) Add a new mapping from key to value If key already exists, reject the insert remove(key) Remove the value identified by key (and remove the key as well) Be careful if a value is pointed to by more than one key 21

86 Maps Other Notes Map keys must be comparable Must be able to determine if something is already present in the map Maps are iterable Although maps have no specific ordering, you can still iterate over them Most languages let you iterate over the set of keys or over the set of values 22

87 Maps Given what you know, how would you go about implementing a Map? 23

88 Maps Given what you know, how would you go about implementing a Map? Binary Search Tree variants or Hashtables are both acceptable answers Many languages will give you both options and use names like TreeMap or HashMap to differentiate A TreeMap will generally be ordered while a HashMap will not C++ STL implements Maps with Red/Black Trees This means that the C++ STL map is actually an ordered set 24

89 MultiMap MultiMap A Set of pairs of the form (key,value) Works just like a Map, but allows repeated keys Each key maps to exactly one or more values Each value can be mapped to by one or more keys Example Usage For each word that exists in a document, store the line number in the document for each occurrence of that word. 25

90 MultiMap Example MultiMap M = { (32,John), (22,Jane), (22,Joe), (56,Jeff), (35,Joe) } Keys John Jane Joe Jeff Values 26

91 MultiMap Operations MultiMap Operations search(key) See if the specified key has any existing mappings and return them all insert(key,value) Add a new mapping from key to value It does not matter if key already has a mapping remove(key) Remove all the values identified by key (and remove the key as well) Be careful if a value is pointed to by more than one key 27

92 MultiMap Given what you know, how would you go about implementing a MultiMap? 28

93 MultiMap Given what you know, how would you go about implementing a MultiMap? Binary Search Trees Option 1: Allow duplicate elements in the tree Option 2: Each node in the tree contains a list Hashtables where the key is the element and the value is a list of mapped values Many other options C++ STL implements MultiMaps with Red/Black Trees 29

94 The C++ STL "map" Class 3

HashTable CISC5835, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang Fall 2018

HashTable CISC5835, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang Fall 2018 HashTable CISC5835, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang Fall 2018 Acknowledgement The set of slides have used materials from the following resources Slides for textbook by Dr. Y.

More information

CMSC 341 Lecture 16/17 Hashing, Parts 1 & 2

CMSC 341 Lecture 16/17 Hashing, Parts 1 & 2 CMSC 341 Lecture 16/17 Hashing, Parts 1 & 2 Prof. John Park Based on slides from previous iterations of this course Today s Topics Overview Uses and motivations of hash tables Major concerns with hash

More information

Introducing Hashing. Chapter 21. Copyright 2012 by Pearson Education, Inc. All rights reserved

Introducing Hashing. Chapter 21. Copyright 2012 by Pearson Education, Inc. All rights reserved Introducing Hashing Chapter 21 Contents What Is Hashing? Hash Functions Computing Hash Codes Compressing a Hash Code into an Index for the Hash Table A demo of hashing (after) ARRAY insert hash index =

More information

Acknowledgement HashTable CISC4080, Computer Algorithms CIS, Fordham Univ.

Acknowledgement HashTable CISC4080, Computer Algorithms CIS, Fordham Univ. Acknowledgement HashTable CISC4080, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang Spring 2018 The set of slides have used materials from the following resources Slides for textbook by Dr.

More information

Understand how to deal with collisions

Understand how to deal with collisions Understand the basic structure of a hash table and its associated hash function Understand what makes a good (and a bad) hash function Understand how to deal with collisions Open addressing Separate chaining

More information

Algorithms with numbers (2) CISC4080, Computer Algorithms CIS, Fordham Univ.! Instructor: X. Zhang Spring 2017

Algorithms with numbers (2) CISC4080, Computer Algorithms CIS, Fordham Univ.! Instructor: X. Zhang Spring 2017 Algorithms with numbers (2) CISC4080, Computer Algorithms CIS, Fordham Univ.! Instructor: X. Zhang Spring 2017 Acknowledgement The set of slides have used materials from the following resources Slides

More information

Algorithms with numbers (2) CISC4080, Computer Algorithms CIS, Fordham Univ. Acknowledgement. Support for Dictionary

Algorithms with numbers (2) CISC4080, Computer Algorithms CIS, Fordham Univ. Acknowledgement. Support for Dictionary Algorithms with numbers (2) CISC4080, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang Spring 2017 Acknowledgement The set of slides have used materials from the following resources Slides for

More information

Chapter 5 Hashing. Introduction. Hashing. Hashing Functions. hashing performs basic operations, such as insertion,

Chapter 5 Hashing. Introduction. Hashing. Hashing Functions. hashing performs basic operations, such as insertion, Introduction Chapter 5 Hashing hashing performs basic operations, such as insertion, deletion, and finds in average time 2 Hashing a hash table is merely an of some fixed size hashing converts into locations

More information

Hash Tables. Hashing Probing Separate Chaining Hash Function

Hash Tables. Hashing Probing Separate Chaining Hash Function Hash Tables Hashing Probing Separate Chaining Hash Function Introduction In Chapter 4 we saw: linear search O( n ) binary search O( log n ) Can we improve the search operation to achieve better than O(

More information

Hash Open Indexing. Data Structures and Algorithms CSE 373 SP 18 - KASEY CHAMPION 1

Hash Open Indexing. Data Structures and Algorithms CSE 373 SP 18 - KASEY CHAMPION 1 Hash Open Indexing Data Structures and Algorithms CSE 373 SP 18 - KASEY CHAMPION 1 Warm Up Consider a StringDictionary using separate chaining with an internal capacity of 10. Assume our buckets are implemented

More information

Table ADT and Sorting. Algorithm topics continuing (or reviewing?) CS 24 curriculum

Table ADT and Sorting. Algorithm topics continuing (or reviewing?) CS 24 curriculum Table ADT and Sorting Algorithm topics continuing (or reviewing?) CS 24 curriculum A table ADT (a.k.a. Dictionary, Map) Table public interface: // Put information in the table, and a unique key to identify

More information

Hash[ string key ] ==> integer value

Hash[ string key ] ==> integer value Hashing 1 Overview Hash[ string key ] ==> integer value Hash Table Data Structure : Use-case To support insertion, deletion and search in average-case constant time Assumption: Order of elements irrelevant

More information

AAL 217: DATA STRUCTURES

AAL 217: DATA STRUCTURES Chapter # 4: Hashing AAL 217: DATA STRUCTURES The implementation of hash tables is frequently called hashing. Hashing is a technique used for performing insertions, deletions, and finds in constant average

More information

Cpt S 223. School of EECS, WSU

Cpt S 223. School of EECS, WSU Hashing & Hash Tables 1 Overview Hash Table Data Structure : Purpose To support insertion, deletion and search in average-case constant t time Assumption: Order of elements irrelevant ==> data structure

More information

HASH TABLES. Hash Tables Page 1

HASH TABLES. Hash Tables Page 1 HASH TABLES TABLE OF CONTENTS 1. Introduction to Hashing 2. Java Implementation of Linear Probing 3. Maurer s Quadratic Probing 4. Double Hashing 5. Separate Chaining 6. Hash Functions 7. Alphanumeric

More information

Introduction. hashing performs basic operations, such as insertion, better than other ADTs we ve seen so far

Introduction. hashing performs basic operations, such as insertion, better than other ADTs we ve seen so far Chapter 5 Hashing 2 Introduction hashing performs basic operations, such as insertion, deletion, and finds in average time better than other ADTs we ve seen so far 3 Hashing a hash table is merely an hashing

More information

Lecture 12 Notes Hash Tables

Lecture 12 Notes Hash Tables Lecture 12 Notes Hash Tables 15-122: Principles of Imperative Computation (Spring 2016) Frank Pfenning, Rob Simmons 1 Introduction In this lecture we re-introduce the dictionaries that were implemented

More information

Introduction hashing: a technique used for storing and retrieving information as quickly as possible.

Introduction hashing: a technique used for storing and retrieving information as quickly as possible. Lecture IX: Hashing Introduction hashing: a technique used for storing and retrieving information as quickly as possible. used to perform optimal searches and is useful in implementing symbol tables. Why

More information

Standard ADTs. Lecture 19 CS2110 Summer 2009

Standard ADTs. Lecture 19 CS2110 Summer 2009 Standard ADTs Lecture 19 CS2110 Summer 2009 Past Java Collections Framework How to use a few interfaces and implementations of abstract data types: Collection List Set Iterator Comparable Comparator 2

More information

Lecture 16. Reading: Weiss Ch. 5 CSE 100, UCSD: LEC 16. Page 1 of 40

Lecture 16. Reading: Weiss Ch. 5 CSE 100, UCSD: LEC 16. Page 1 of 40 Lecture 16 Hashing Hash table and hash function design Hash functions for integers and strings Collision resolution strategies: linear probing, double hashing, random hashing, separate chaining Hash table

More information

Lecture Notes on Hash Tables

Lecture Notes on Hash Tables Lecture Notes on Hash Tables 15-122: Principles of Imperative Computation Frank Pfenning Lecture 13 February 24, 2011 1 Introduction In this lecture we introduce so-called associative arrays, that is,

More information

Hash table basics mod 83 ate. ate. hashcode()

Hash table basics mod 83 ate. ate. hashcode() Hash table basics ate hashcode() 82 83 84 48594983 mod 83 ate Reminder from syllabus: EditorTrees worth 10% of term grade See schedule page Exam 2 moved to Friday after break. Short pop quiz over AVL rotations

More information

COMP171. Hashing.

COMP171. Hashing. COMP171 Hashing Hashing 2 Hashing Again, a (dynamic) set of elements in which we do search, insert, and delete Linear ones: lists, stacks, queues, Nonlinear ones: trees, graphs (relations between elements

More information

Lecture 12 Hash Tables

Lecture 12 Hash Tables Lecture 12 Hash Tables 15-122: Principles of Imperative Computation (Spring 2018) Frank Pfenning, Rob Simmons Dictionaries, also called associative arrays as well as maps, are data structures that are

More information

CS 310 Advanced Data Structures and Algorithms

CS 310 Advanced Data Structures and Algorithms CS 310 Advanced Data Structures and Algorithms Hashing June 6, 2017 Tong Wang UMass Boston CS 310 June 6, 2017 1 / 28 Hashing Hashing is probably one of the greatest programming ideas ever. It solves one

More information

Hashing. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Hashing. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University Hashing CptS 223 Advanced Data Structures Larry Holder School of Electrical Engineering and Computer Science Washington State University 1 Overview Hashing Technique supporting insertion, deletion and

More information

CIS 190: C/C++ Programming. Lecture 12 Student Choice

CIS 190: C/C++ Programming. Lecture 12 Student Choice CIS 190: C/C++ Programming Lecture 12 Student Choice Outline Hash Maps Collisions Using Open Addressing Collisions Chaining Collisions In C++ C++ STL Containers C++ GUI Resources Hash Maps (AKA Hash Tables)

More information

Hash tables. hashing -- idea collision resolution. hash function Java hashcode() for HashMap and HashSet big-o time bounds applications

Hash tables. hashing -- idea collision resolution. hash function Java hashcode() for HashMap and HashSet big-o time bounds applications hashing -- idea collision resolution Hash tables closed addressing (chaining) open addressing techniques hash function Java hashcode() for HashMap and HashSet big-o time bounds applications Hash tables

More information

CSCI 104 Hash Tables & Functions. Mark Redekopp David Kempe

CSCI 104 Hash Tables & Functions. Mark Redekopp David Kempe CSCI 104 Hash Tables & Functions Mark Redekopp David Kempe Dictionaries/Maps An array maps integers to values Given i, array[i] returns the value in O(1) Dictionaries map keys to values Given key, k, map[k]

More information

Open Addressing: Linear Probing (cont.)

Open Addressing: Linear Probing (cont.) Open Addressing: Linear Probing (cont.) Cons of Linear Probing () more complex insert, find, remove methods () primary clustering phenomenon items tend to cluster together in the bucket array, as clustering

More information

CMSC 341 Hashing (Continued) Based on slides from previous iterations of this course

CMSC 341 Hashing (Continued) Based on slides from previous iterations of this course CMSC 341 Hashing (Continued) Based on slides from previous iterations of this course Today s Topics Review Uses and motivations of hash tables Major concerns with hash tables Properties Hash function Hash

More information

Unit #5: Hash Functions and the Pigeonhole Principle

Unit #5: Hash Functions and the Pigeonhole Principle Unit #5: Hash Functions and the Pigeonhole Principle CPSC 221: Basic Algorithms and Data Structures Jan Manuch 217S1: May June 217 Unit Outline Constant-Time Dictionaries? Hash Table Outline Hash Functions

More information

COSC160: Data Structures Hashing Structures. Jeremy Bolton, PhD Assistant Teaching Professor

COSC160: Data Structures Hashing Structures. Jeremy Bolton, PhD Assistant Teaching Professor COSC160: Data Structures Hashing Structures Jeremy Bolton, PhD Assistant Teaching Professor Outline I. Hashing Structures I. Motivation and Review II. Hash Functions III. HashTables I. Implementations

More information

CS 350 : Data Structures Hash Tables

CS 350 : Data Structures Hash Tables CS 350 : Data Structures Hash Tables David Babcock (courtesy of James Moscola) Department of Physical Sciences York College of Pennsylvania James Moscola Hash Tables Although the various tree structures

More information

9/24/ Hash functions

9/24/ Hash functions 11.3 Hash functions A good hash function satis es (approximately) the assumption of SUH: each key is equally likely to hash to any of the slots, independently of the other keys We typically have no way

More information

Topic HashTable and Table ADT

Topic HashTable and Table ADT Topic HashTable and Table ADT Hashing, Hash Function & Hashtable Search, Insertion & Deletion of elements based on Keys So far, By comparing keys! Linear data structures Non-linear data structures Time

More information

! A Hash Table is used to implement a set, ! The table uses a function that maps an. ! The function is called a hash function.

! A Hash Table is used to implement a set, ! The table uses a function that maps an. ! The function is called a hash function. Hash Tables Chapter 20 CS 3358 Summer II 2013 Jill Seaman Sections 201, 202, 203, 204 (not 2042), 205 1 What are hash tables?! A Hash Table is used to implement a set, providing basic operations in constant

More information

5. Hashing. 5.1 General Idea. 5.2 Hash Function. 5.3 Separate Chaining. 5.4 Open Addressing. 5.5 Rehashing. 5.6 Extendible Hashing. 5.

5. Hashing. 5.1 General Idea. 5.2 Hash Function. 5.3 Separate Chaining. 5.4 Open Addressing. 5.5 Rehashing. 5.6 Extendible Hashing. 5. 5. Hashing 5.1 General Idea 5.2 Hash Function 5.3 Separate Chaining 5.4 Open Addressing 5.5 Rehashing 5.6 Extendible Hashing Malek Mouhoub, CS340 Fall 2004 1 5. Hashing Sequential access : O(n). Binary

More information

ECE 242 Data Structures and Algorithms. Hash Tables I. Lecture 24. Prof.

ECE 242 Data Structures and Algorithms.  Hash Tables I. Lecture 24. Prof. ECE 242 Data Structures and Algorithms http//www.ecs.umass.edu/~polizzi/teaching/ece242/ Hash Tables I Lecture 24 Prof. Eric Polizzi Motivations We have to store some records and perform the following

More information

Hash table basics. ate à. à à mod à 83

Hash table basics. ate à. à à mod à 83 Hash table basics After today, you should be able to explain how hash tables perform insertion in amortized O(1) time given enough space ate à hashcode() à 48594983à mod à 83 82 83 ate 84 } EditorTrees

More information

Hash table basics mod 83 ate. ate

Hash table basics mod 83 ate. ate Hash table basics After today, you should be able to explain how hash tables perform insertion in amortized O(1) time given enough space ate hashcode() 82 83 84 48594983 mod 83 ate 1. Section 2: 15+ min

More information

stacks operation array/vector linked list push amortized O(1) Θ(1) pop Θ(1) Θ(1) top Θ(1) Θ(1) isempty Θ(1) Θ(1)

stacks operation array/vector linked list push amortized O(1) Θ(1) pop Θ(1) Θ(1) top Θ(1) Θ(1) isempty Θ(1) Θ(1) Hashes 1 lists 2 operation array/vector linked list find (by value) Θ(n) Θ(n) insert (end) amortized O(1) Θ(1) insert (beginning/middle) Θ(n) Θ(1) remove (by value) Θ(n) Θ(n) find (by index) Θ(1) Θ(1)

More information

Hash Tables. Gunnar Gotshalks. Maps 1

Hash Tables. Gunnar Gotshalks. Maps 1 Hash Tables Maps 1 Definition A hash table has the following components» An array called a table of size N» A mathematical function called a hash function that maps keys to valid array indices hash_function:

More information

Announcements. Submit Prelim 2 conflicts by Thursday night A6 is due Nov 7 (tomorrow!)

Announcements. Submit Prelim 2 conflicts by Thursday night A6 is due Nov 7 (tomorrow!) HASHING CS2110 Announcements 2 Submit Prelim 2 conflicts by Thursday night A6 is due Nov 7 (tomorrow!) Ideal Data Structure 3 Data Structure add(val x) get(int i) contains(val x) ArrayList 2 1 3 0!(#)!(1)!(#)

More information

STANDARD ADTS Lecture 17 CS2110 Spring 2013

STANDARD ADTS Lecture 17 CS2110 Spring 2013 STANDARD ADTS Lecture 17 CS2110 Spring 2013 Abstract Data Types (ADTs) 2 A method for achieving abstraction for data structures and algorithms ADT = model + operations In Java, an interface corresponds

More information

TABLES AND HASHING. Chapter 13

TABLES AND HASHING. Chapter 13 Data Structures Dr Ahmed Rafat Abas Computer Science Dept, Faculty of Computer and Information, Zagazig University arabas@zu.edu.eg http://www.arsaliem.faculty.zu.edu.eg/ TABLES AND HASHING Chapter 13

More information

Hash table basics mod 83 ate. ate

Hash table basics mod 83 ate. ate Hash table basics After today, you should be able to explain how hash tables perform insertion in amortized O(1) time given enough space ate hashcode() 82 83 84 48594983 mod 83 ate Topics: weeks 1-6 Reading,

More information

HASH TABLES cs2420 Introduction to Algorithms and Data Structures Spring 2015

HASH TABLES cs2420 Introduction to Algorithms and Data Structures Spring 2015 HASH TABLES cs2420 Introduction to Algorithms and Data Structures Spring 2015 1 administrivia 2 -assignment 9 is due on Monday -assignment 10 will go out on Thursday -midterm on Thursday 3 last time 4

More information

Hash Tables. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Data Dictionary Revisited

Hash Tables. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Data Dictionary Revisited Unit 9, Part 4 Hash Tables Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Data Dictionary Revisited We've considered several data structures that allow us to store and search for data

More information

Worst-case running time for RANDOMIZED-SELECT

Worst-case running time for RANDOMIZED-SELECT Worst-case running time for RANDOMIZED-SELECT is ), even to nd the minimum The algorithm has a linear expected running time, though, and because it is randomized, no particular input elicits the worst-case

More information

CSE 214 Computer Science II Searching

CSE 214 Computer Science II Searching CSE 214 Computer Science II Searching Fall 2017 Stony Brook University Instructor: Shebuti Rayana shebuti.rayana@stonybrook.edu http://www3.cs.stonybrook.edu/~cse214/sec02/ Introduction Searching in a

More information

Symbol Table. Symbol table is used widely in many applications. dictionary is a kind of symbol table data dictionary is database management

Symbol Table. Symbol table is used widely in many applications. dictionary is a kind of symbol table data dictionary is database management Hashing Symbol Table Symbol table is used widely in many applications. dictionary is a kind of symbol table data dictionary is database management In general, the following operations are performed on

More information

Hashing. 5/1/2006 Algorithm analysis and Design CS 007 BE CS 5th Semester 2

Hashing. 5/1/2006 Algorithm analysis and Design CS 007 BE CS 5th Semester 2 Hashing Hashing A hash function h maps keys of a given type to integers in a fixed interval [0,N-1]. The goal of a hash function is to uniformly disperse keys in the range [0,N-1] 5/1/2006 Algorithm analysis

More information

CSC 321: Data Structures. Fall 2016

CSC 321: Data Structures. Fall 2016 CSC : Data Structures Fall 6 Hash tables HashSet & HashMap hash table, hash function collisions Ø linear probing, lazy deletion, primary clustering Ø quadratic probing, rehashing Ø chaining HashSet & HashMap

More information

Linked lists (6.5, 16)

Linked lists (6.5, 16) Linked lists (6.5, 16) Linked lists Inserting and removing elements in the middle of a dynamic array takes O(n) time (though inserting at the end takes O(1) time) (and you can also delete from the middle

More information

CSE100. Advanced Data Structures. Lecture 21. (Based on Paul Kube course materials)

CSE100. Advanced Data Structures. Lecture 21. (Based on Paul Kube course materials) CSE100 Advanced Data Structures Lecture 21 (Based on Paul Kube course materials) CSE 100 Collision resolution strategies: linear probing, double hashing, random hashing, separate chaining Hash table cost

More information

ECE 242 Data Structures and Algorithms. Hash Tables II. Lecture 25. Prof.

ECE 242 Data Structures and Algorithms.  Hash Tables II. Lecture 25. Prof. ECE 242 Data Structures and Algorithms http://www.ecs.umass.edu/~polizzi/teaching/ece242/ Hash Tables II Lecture 25 Prof. Eric Polizzi Summary previous lecture Hash Tables Motivation: optimal insertion

More information

Abstract Data Types (ADTs) Queues & Priority Queues. Sets. Dictionaries. Stacks 6/15/2011

Abstract Data Types (ADTs) Queues & Priority Queues. Sets. Dictionaries. Stacks 6/15/2011 CS/ENGRD 110 Object-Oriented Programming and Data Structures Spring 011 Thorsten Joachims Lecture 16: Standard ADTs Abstract Data Types (ADTs) A method for achieving abstraction for data structures and

More information

General Idea. Key could be an integer, a string, etc e.g. a name or Id that is a part of a large employee structure

General Idea. Key could be an integer, a string, etc e.g. a name or Id that is a part of a large employee structure Hashing 1 Hash Tables 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 is called hashing. Hashing is a technique

More information

Lecture Notes on Interfaces

Lecture Notes on Interfaces Lecture Notes on Interfaces 15-122: Principles of Imperative Computation Frank Pfenning Lecture 14 October 16, 2012 1 Introduction The notion of an interface to an implementation of an abstract data type

More information

CSCI 104. Rafael Ferreira da Silva. Slides adapted from: Mark Redekopp and David Kempe

CSCI 104. Rafael Ferreira da Silva. Slides adapted from: Mark Redekopp and David Kempe 1 CSCI 104 Rafael Ferreira da Silva rafsilva@isi.edu Slides adapted from: Mark Redekopp and David Kempe HASH TABLES 2 3 Dictionaries/Maps An array maps integers to values Given i, array[i] returns the

More information

Hashing. Dr. Ronaldo Menezes Hugo Serrano. Ronaldo Menezes, Florida Tech

Hashing. Dr. Ronaldo Menezes Hugo Serrano. Ronaldo Menezes, Florida Tech Hashing Dr. Ronaldo Menezes Hugo Serrano Agenda Motivation Prehash Hashing Hash Functions Collisions Separate Chaining Open Addressing Motivation Hash Table Its one of the most important data structures

More information

Hash Tables and Hash Functions

Hash Tables and Hash Functions Hash Tables and Hash Functions We have seen that with a balanced binary tree we can guarantee worst-case time for insert, search and delete operations. Our challenge now is to try to improve on that...

More information

CMSC 341 Hashing. Based on slides from previous iterations of this course

CMSC 341 Hashing. Based on slides from previous iterations of this course CMSC 341 Hashing Based on slides from previous iterations of this course Hashing Searching n Consider the problem of searching an array for a given value q If the array is not sorted, the search requires

More information

Topic 22 Hash Tables

Topic 22 Hash Tables Topic 22 Hash Tables "hash collision n. [from the techspeak] (var. `hash clash') When used of people, signifies a confusion in associative memory or imagination, especially a persistent one (see thinko).

More information

HO #13 Fall 2015 Gary Chan. Hashing (N:12)

HO #13 Fall 2015 Gary Chan. Hashing (N:12) HO #13 Fall 2015 Gary Chan Hashing (N:12) Outline Motivation Hashing Algorithms and Improving the Hash Functions Collisions Strategies Open addressing and linear probing Separate chaining COMP2012H (Hashing)

More information

Data Structure. A way to store and organize data in order to support efficient insertions, queries, searches, updates, and deletions.

Data Structure. A way to store and organize data in order to support efficient insertions, queries, searches, updates, and deletions. DATA STRUCTURES COMP 321 McGill University These slides are mainly compiled from the following resources. - Professor Jaehyun Park slides CS 97SI - Top-coder tutorials. - Programming Challenges book. Data

More information

CS 261 Data Structures

CS 261 Data Structures CS 261 Data Structures Hash Tables Open Address Hashing ADT Dictionaries computer kəәmˈpyoōtəәr noun an electronic device for storing and processing data... a person who makes calculations, esp. with a

More information

MIDTERM EXAM THURSDAY MARCH

MIDTERM EXAM THURSDAY MARCH Week 6 Assignments: Program 2: is being graded Program 3: available soon and due before 10pm on Thursday 3/14 Homework 5: available soon and due before 10pm on Monday 3/4 X-Team Exercise #2: due before

More information

Chapter 20 Hash Tables

Chapter 20 Hash Tables Chapter 20 Hash Tables Dictionary All elements have a unique key. Operations: o Insert element with a specified key. o Search for element by key. o Delete element by key. Random vs. sequential access.

More information

Hashing Techniques. Material based on slides by George Bebis

Hashing Techniques. Material based on slides by George Bebis Hashing Techniques Material based on slides by George Bebis https://www.cse.unr.edu/~bebis/cs477/lect/hashing.ppt The Search Problem Find items with keys matching a given search key Given an array A, containing

More information

Algorithms and Data Structures

Algorithms and Data Structures Lesson 4: Sets, Dictionaries and Hash Tables Luciano Bononi http://www.cs.unibo.it/~bononi/ (slide credits: these slides are a revised version of slides created by Dr. Gabriele D Angelo)

More information

More on Hashing: Collisions. See Chapter 20 of the text.

More on Hashing: Collisions. See Chapter 20 of the text. More on Hashing: Collisions See Chapter 20 of the text. Collisions Let's do an example -- add some people to a hash table of size 7. Name h = hash(name) h%7 Ben 66667 6 Bob 66965 3 Steven -1808493797-5

More information

SETS AND MAPS. Chapter 9

SETS AND MAPS. Chapter 9 SETS AND MAPS Chapter 9 The set Functions Required methods: testing set membership (find) testing for an empty set (empty) determining set size (size) creating an iterator over the set (begin, end) adding

More information

Announcements. Container structures so far. IntSet ADT interface. Sets. Today s topic: Hashing (Ch. 10) Next topic: Graphs. Break around 11:45am

Announcements. Container structures so far. IntSet ADT interface. Sets. Today s topic: Hashing (Ch. 10) Next topic: Graphs. Break around 11:45am Announcements Today s topic: Hashing (Ch. 10) Next topic: Graphs Break around 11:45am Container structures so far Array lists O(1) access O(n) insertion/deletion (average case), better at end Linked lists

More information

Hashing for searching

Hashing for searching Hashing for searching Consider searching a database of records on a given key. There are three standard techniques: Searching sequentially start at the first record and look at each record in turn until

More information

CS61B Lecture #24: Hashing. Last modified: Wed Oct 19 14:35: CS61B: Lecture #24 1

CS61B Lecture #24: Hashing. Last modified: Wed Oct 19 14:35: CS61B: Lecture #24 1 CS61B Lecture #24: Hashing Last modified: Wed Oct 19 14:35:49 2016 CS61B: Lecture #24 1 Back to Simple Search Linear search is OK for small data sets, bad for large. So linear search would be OK if we

More information

Question Bank Subject: Advanced Data Structures Class: SE Computer

Question Bank Subject: Advanced Data Structures Class: SE Computer Question Bank Subject: Advanced Data Structures Class: SE Computer Question1: Write a non recursive pseudo code for post order traversal of binary tree Answer: Pseudo Code: 1. Push root into Stack_One.

More information

CSC 427: Data Structures and Algorithm Analysis. Fall 2004

CSC 427: Data Structures and Algorithm Analysis. Fall 2004 CSC 47: Data Structures and Algorithm Analysis Fall 4 Associative containers STL data structures and algorithms sets: insert, find, erase, empty, size, begin/end maps: operator[], find, erase, empty, size,

More information

Selection, Bubble, Insertion, Merge, Heap, Quick Bucket, Radix

Selection, Bubble, Insertion, Merge, Heap, Quick Bucket, Radix Spring 2010 Review Topics Big O Notation Heaps Sorting Selection, Bubble, Insertion, Merge, Heap, Quick Bucket, Radix Hashtables Tree Balancing: AVL trees and DSW algorithm Graphs: Basic terminology and

More information

Hash Tables. CS 311 Data Structures and Algorithms Lecture Slides. Wednesday, April 22, Glenn G. Chappell

Hash Tables. CS 311 Data Structures and Algorithms Lecture Slides. Wednesday, April 22, Glenn G. Chappell Hash Tables CS 311 Data Structures and Algorithms Lecture Slides Wednesday, April 22, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks CHAPPELLG@member.ams.org 2005

More information

CSE373: Data Structures & Algorithms Lecture 17: Hash Collisions. Kevin Quinn Fall 2015

CSE373: Data Structures & Algorithms Lecture 17: Hash Collisions. Kevin Quinn Fall 2015 CSE373: Data Structures & Algorithms Lecture 17: Hash Collisions Kevin Quinn Fall 2015 Hash Tables: Review Aim for constant-time (i.e., O(1)) find, insert, and delete On average under some reasonable assumptions

More information

Hashing. Hashing Procedures

Hashing. Hashing Procedures Hashing Hashing Procedures Let us denote the set of all possible key values (i.e., the universe of keys) used in a dictionary application by U. Suppose an application requires a dictionary in which elements

More information

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1 Asymptotics, Recurrence and Basic Algorithms 1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1 2. O(n) 2. [1 pt] What is the solution to the recurrence T(n) = T(n/2) + n, T(1)

More information

Dynamic Dictionaries. Operations: create insert find remove max/ min write out in sorted order. Only defined for object classes that are Comparable

Dynamic Dictionaries. Operations: create insert find remove max/ min write out in sorted order. Only defined for object classes that are Comparable Hashing Dynamic Dictionaries Operations: create insert find remove max/ min write out in sorted order Only defined for object classes that are Comparable Hash tables Operations: create insert find remove

More information

Hashing. 1. Introduction. 2. Direct-address tables. CmSc 250 Introduction to Algorithms

Hashing. 1. Introduction. 2. Direct-address tables. CmSc 250 Introduction to Algorithms Hashing CmSc 250 Introduction to Algorithms 1. Introduction Hashing is a method of storing elements in a table in a way that reduces the time for search. Elements are assumed to be records with several

More information

Data Structures. Topic #6

Data Structures. Topic #6 Data Structures Topic #6 Today s Agenda Table Abstract Data Types Work by value rather than position May be implemented using a variety of data structures such as arrays (statically, dynamically allocated)

More information

Hash Table and Hashing

Hash Table and Hashing Hash Table and Hashing The tree structures discussed so far assume that we can only work with the input keys by comparing them. No other operation is considered. In practice, it is often true that an input

More information

Data Structures And Algorithms

Data Structures And Algorithms Data Structures And Algorithms Hashing Eng. Anis Nazer First Semester 2017-2018 Searching Search: find if a key exists in a given set Searching algorithms: linear (sequential) search binary search Search

More information

Hashing. Manolis Koubarakis. Data Structures and Programming Techniques

Hashing. Manolis Koubarakis. Data Structures and Programming Techniques Hashing Manolis Koubarakis 1 The Symbol Table ADT A symbol table T is an abstract storage that contains table entries that are either empty or are pairs of the form (K, I) where K is a key and I is some

More information

Lecture 4. Hashing Methods

Lecture 4. Hashing Methods Lecture 4 Hashing Methods 1 Lecture Content 1. Basics 2. Collision Resolution Methods 2.1 Linear Probing Method 2.2 Quadratic Probing Method 2.3 Double Hashing Method 2.4 Coalesced Chaining Method 2.5

More information

Announcements. Hash Functions. Hash Functions 4/17/18 HASHING

Announcements. Hash Functions. Hash Functions 4/17/18 HASHING Announcements Submit Prelim conflicts by tomorrow night A7 Due FRIDAY A8 will be released on Thursday HASHING CS110 Spring 018 Hash Functions Hash Functions 1 0 4 1 Requirements: 1) deterministic ) return

More information

Announcements. Today s topic: Hashing (Ch. 10) Next topic: Graphs. Break around 11:45am

Announcements. Today s topic: Hashing (Ch. 10) Next topic: Graphs. Break around 11:45am Announcements Today s topic: Hashing (Ch. 10) Next topic: Graphs Break around 11:45am 1 Container structures so far Array lists O(1) access O(n) insertion/deletion (average case), better at end Linked

More information

Today: Finish up hashing Sorted Dictionary ADT: Binary search, divide-and-conquer Recursive function and recurrence relation

Today: Finish up hashing Sorted Dictionary ADT: Binary search, divide-and-conquer Recursive function and recurrence relation Announcements HW1 PAST DUE HW2 online: 7 questions, 60 points Nat l Inst visit Thu, ok? Last time: Continued PA1 Walk Through Dictionary ADT: Unsorted Hashing Today: Finish up hashing Sorted Dictionary

More information

DATA STRUCTURES AND ALGORITHMS

DATA STRUCTURES AND ALGORITHMS LECTURE 11 Babeş - Bolyai University Computer Science and Mathematics Faculty 2017-2018 In Lecture 9-10... Hash tables ADT Stack ADT Queue ADT Deque ADT Priority Queue Hash tables Today Hash tables 1 Hash

More information

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

CSE373 Fall 2013, Second Midterm Examination November 15, 2013 CSE373 Fall 2013, Second Midterm Examination November 15, 2013 Please do not turn the page until the bell rings. Rules: The exam is closed-book, closed-note, closed calculator, closed electronics. Please

More information

CSC 321: Data Structures. Fall 2017

CSC 321: Data Structures. Fall 2017 CSC : Data Structures Fall 7 Hash tables HashSet & HashMap hash table, hash function collisions Ø linear probing, lazy deletion, clustering, rehashing Ø chaining Java hashcode method HW6: finite state

More information

CSCI-1200 Data Structures Fall 2018 Lecture 22 Hash Tables, part 2 & Priority Queues, part 1

CSCI-1200 Data Structures Fall 2018 Lecture 22 Hash Tables, part 2 & Priority Queues, part 1 Review from Lecture 21 CSCI-1200 Data Structures Fall 2018 Lecture 22 Hash Tables, part 2 & Priority Queues, part 1 the single most important data structure known to mankind Hash Tables, Hash Functions,

More information

Lecture 5 Data Structures (DAT037) Ramona Enache (with slides from Nick Smallbone)

Lecture 5 Data Structures (DAT037) Ramona Enache (with slides from Nick Smallbone) Lecture 5 Data Structures (DAT037) Ramona Enache (with slides from Nick Smallbone) Hash Tables A hash table implements a set or map The plan: - take an array of size k - define a hash funcion that maps

More information

The dictionary problem

The dictionary problem 6 Hashing The dictionary problem Different approaches to the dictionary problem: previously: Structuring the set of currently stored keys: lists, trees, graphs,... structuring the complete universe of

More information