Hash table open addressing
Key Takeaways
The video discusses hash table open addressing, a collision resolution technique for hash tables, and covers topics such as load factor, probing sequences, and probing functions like linear probing and quadratic probing.
Full Transcript
I'm pretty excited we're going to be talking about the open addressing collision resolution technique for hash tables so let's get going first let's just do a quick recap on hash table so that everyone's on the same page so the goal of the hash table is to construct a mapping from a set of keys to a set of values and the keys need to be hashable now what we do is we define a hash function on the keys to convert them into numbers then we use the number obtained through the hash function as a way to index into the array or the hash table however this is a foolproof method because from time to time we're going to have hash collisions that is two keys that hash to the same value so we need a way to resolve this an open addressing is a solution for that alright so whenever you're going to be using the open addressing collision resolution technique the one thing you need to keep in mind is the actual key value pairs themselves are going to be stored in the table itself so as opposed to say an auxilary data structure like in the separate chaining method we saw in the last video so this means that we care a great deal about the size of the hash table and how many elements are currently in the hash table because once there are too many elements inside the hash table will also be really hard to find an open slot or a position to place our element so just an important piece of terminology we say that the load factor is the ratio between the number of items in the table and the size of the table so this means we need to keep tabs on the load factor here's a neat chart from Wikipedia so on the chart what you can see are two different methods one of them is chaining that is separate chaining and linear probing and open addressing a technique and we can see from the linear probing one is that once it gets to a certain threshold it gets exponentially bad so you don't want to go anywhere near that say point eight mark in fact we're going to be keeping it a lot lower than that usually and what this says is we always need to keep the load factor which we denote by the Greek letter alpha below a certain threshold and we need to grow the size of our table once that threshold is met all right so when we want to insert a key value pair into our hash table here's what we do we take the key we use our hash function to find out our key and we hash the value and this gives us an original position inside the hash table for where the key should go but suppose there's a hash collision and there's already a key in that slot well we can't have two keys in the same slot that's not how arrays work so what we do is we use a probing sequence which I will define as P of X so now on to tell us where to go next so we hashed to the value H of K the original position and now we're going to probe along using this probing function in such a way that we're going to eventually find an open spot along the way so as it turns out there are actually an infinite amount of probing sequences to choose from so here are a few of them we have linear probing which probes via a linear function so given an input parameter X so when we're probing we start usually x at 0 or 1 and as we're unable to find free slots then we just increment X by 1 and it works the same for all of these probing functions for linear probing we use a linear function for quadratic probing we use a quadratic function and then there's double hashing which is super neat actually what we do is we define a secondary hash function on our key find its value and then use that inside the probing function and the last one is the pseudo random number generator probing function that we can use so given a random number generator we're going to seed it using the hash value of our key which we know is deterministic so it's always going to be the same thing and then we can use that inside our probing function which is pretty neat and increment by X each time and we know x increments by one so we're just getting the next number in the random number generator then the next one after that all right so here's a general insertion method for open addressing suppose we have a table size n and here's how the algorithm goes first we initialize X to be 1 so X is a constant or sorry a variable that we're going to use for the probing and we're going to increment X each time we fail to hit a free position then we get the key hash just by hashing our key and that is actually going to be the index where we're going to look at a table first so while the table index is occupied meaning it's not equal to null we're going to say our new index is the key hash or the original position we hash to plus the probing function mod n so that we always land back inside the table and then we're going to increment X so the next time we run this loop while we probe at a different position and then eventually we're going to find a free position we always set up our probing function in such a way that we will always find problems a sorry we will always find a free slot because we know that the load factors been kept below a certain amount alright so here's the big issue with open addressing and it's that most probing sequences that we choose modulo n we're going to end up producing some sort of cycle shorter than the table size itself so imagine you're probing sequence just hops between three different values and cycles and your table is of size 10 but you're only ever able to hit three slots because it's stuck in a cycle and on all of those three slots are full well you're stuck in an infinite loop so this is very problematic and something we absolutely absolutely need to handle all right so somebody looking at an example so right here I have a hash table and that using open addressing and it's got some key value pairs already inserted and assume that the circle of the bar through it is the null token further assume that we're using the probing sequence P of x equals 4x and suppose you want insert a new key value pair into the table and that the key hashes to eight so that means we want to insert that key value pair at position eight but oh it's already occupied because there's key five value five already there so what we do well we probe so we compute P of 1 which is 4 X at 1 so we get 8 plus 4 my 12 well that's 0 so then we go slot 0 and we see oh that is also occupy because the key 1 and value 1 is already there so now we compute P of 2 and then that gives us 16 ma 12 which is 4 then oh that cell is already occupied and then we keep probing and as you see right now we've entered a cycle so we would keep probing and probing and probing and we'd always be getting back to the same position so although we have a proving function it does not work in this particular situation the probing function is flawed so that's quite concerning because not all probing functions are viable they produce cycles which are shorter than the table size how do we handle this and in general the consensus is that we don't handle this issue instead we try to avoid it altogether by restricting the domain of probing functions that we choose to be those which produce a cycle of exactly length n and those probing functions do exist I have a little asterisks here and it says there are a few exceptions and this is true there are some probing functions we can use which don't produce a full cycle but still work and we're going to have a look at I think one of those in the quadratic probing video all right so just to recap technique such as linear probing and quadratic probing double hashing they're all subject to this issue of the cycles and what we need to do is redefine probing functions which are very specific that produce a cycle of length and to avoid not being able to insert an element and being stuck in an infinite loop so this is a bit of an issue with the open addressing scheme but it's something we can handle although notice that this isn't something you have to worry about if you're in the separate chaining world just because we have that auxilary data structure that just captures all our collisions so the next video we're going to be going to great detail on linear probing so guys if you learn something please like this video and subscribe I'll catch you in the next video and dropper comment I always love reading those thank you
Original Description
Related Videos:
Hash table intro/hash function: https://www.youtube.com/watch?v=2E54GqF0H4s
Hash table separate chaining: https://www.youtube.com/watch?v=T9gct6Dx-jo
Hash table separate chaining code: https://www.youtube.com/watch?v=Av9kwXkuQFw
Hash table open addressing: https://www.youtube.com/watch?v=xIejolxzZS8
Hash table linear probing: https://www.youtube.com/watch?v=Ma9XOInZJWM
Hash table quadratic probing: https://www.youtube.com/watch?v=b0858c55TGQ
Hash table double hashing: https://www.youtube.com/watch?v=H5e9V5x92vI
Hash table open addressing removing: https://www.youtube.com/watch?v=7eLDTtbzX4M
Hash table open addressing code: https://www.youtube.com/watch?v=7eLDTtbzX4M
Data Structures Source Code:
https://github.com/williamfiset/algorithms
My website: http://www.williamfiset.com
===============================================================================
Developer tools I used in the creation/testing of the content in these videos:
1) Sublime text, my favorite lightweight code editor (https://www.sublimetext.com).
NOTE: I'm often asked about the color scheme I use, find it here: https://github.com/williamfiset/dotfiles/tree/master/sublime
2) Kite, a free AI-powered coding assistant that provides smart code completions while typing:
https://www.kite.com/get-kite/?utm_medium=referral&utm_source=youtube&utm_campaign=williamfiset&utm_content=description-only
=============================================================================== ===================================
Practicing for interviews? I have used, and recommend `Cracking the Coding Interview` which got me a job at Google. Link on Amazon: https://amzn.to/3cvMof5
A lot of the content on this channel is inspired by the book `Competitive Programming` by Steven Halim which I frequently use as a resource and reference. Link on Amazon: https://amzn.to/3wC2nix
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from WilliamFiset · WilliamFiset · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
JES Image Manipulation - 2 - Installation
WilliamFiset
JES Image Manipulation - 3 - User Interface
WilliamFiset
JES Image Manipulation - 5 - Negative
WilliamFiset
JES Image Manipulation - 6 - Black & White
WilliamFiset
JES Image Manipulation - 4 - Grayscale
WilliamFiset
JES Image Manipulation - 8 - Blur
WilliamFiset
JES Image Manipulation - 7 - Edge Detection
WilliamFiset
JES Image Manipulation - 9 - Blend
WilliamFiset
JES Image Manipulation - 10 - Matte
WilliamFiset
JES Image Manipulation - 13 - Rotate90
WilliamFiset
JES Image Manipulation - 12 - Mirroring Picture
WilliamFiset
JES Image Manipulation - 11 - Crop Image
WilliamFiset
JES Image Manipulation - 14 - Stretch picture
WilliamFiset
Java Fractal Explorer [6/8]
WilliamFiset
Java Fractal Explorer [4/8]
WilliamFiset
Java Fractal Explorer [8/8]
WilliamFiset
Java Fractal Explorer [5/8]
WilliamFiset
Java Fractal Explorer [2/8]
WilliamFiset
Java Fractal Explorer [7/8]
WilliamFiset
Java Fractal Explorer [1/8]
WilliamFiset
Java Fractal Explorer [3/8]
WilliamFiset
Introduction [Programming Competition Problems]
WilliamFiset
String Manipulation 1 [Programming Competition Problems]
WilliamFiset
String Manipulation 2 [Programming Competition Problems]
WilliamFiset
Graph Theory 1 [Programming Competition Problems]
WilliamFiset
Logic 1 [Programming Competition Problems]
WilliamFiset
Grid Problems 1 [Programming Competition Problems]
WilliamFiset
Dynamic Programming 1 [Programming Competition Problems]
WilliamFiset
Introduction to Big-O
WilliamFiset
Dynamic and Static Arrays
WilliamFiset
Dynamic Array Code
WilliamFiset
Linked Lists Introduction
WilliamFiset
Doubly Linked List Code
WilliamFiset
Stack Introduction
WilliamFiset
Stack Implementation
WilliamFiset
Stack Code
WilliamFiset
Queue Introduction
WilliamFiset
Queue Implementation
WilliamFiset
Queue Code
WilliamFiset
Priority Queue Introduction
WilliamFiset
Priority Queue Min Heaps and Max Heaps
WilliamFiset
Priority Queue Inserting Elements
WilliamFiset
Priority Queue Removing Elements
WilliamFiset
Priority Queue Code
WilliamFiset
Union Find Introduction
WilliamFiset
Union Find Kruskal's Algorithm
WilliamFiset
Union Find - Union and Find Operations
WilliamFiset
Union Find Path Compression
WilliamFiset
Union Find Code
WilliamFiset
Binary Search Tree Introduction
WilliamFiset
Binary Search Tree Insertion
WilliamFiset
Binary Search Tree Removal
WilliamFiset
Binary Search Tree Traversals
WilliamFiset
Binary Search Tree Code
WilliamFiset
Fenwick Tree range queries
WilliamFiset
Fenwick Tree point updates
WilliamFiset
Fenwick Tree construction
WilliamFiset
Fenwick tree source code
WilliamFiset
Hash table hash function
WilliamFiset
Hash table separate chaining
WilliamFiset
More on: LLM Foundations
View skill →Related Reads
📰
📰
📰
📰
O(N) Manacher's Algorithm with Mirror Boundary Optimization
Dev.to · Dipaditya Das
Building a Power Grid Inside Minecraft with BFS Algorithms
Dev.to · Carlos Cortez 🇵🇪 [AWS Hero]
The Run-Length Encoding Trick: How Simple Strings Get Compressed
Medium · Programming
75 Days of Leetcode — Day 4: #238 — Product of Array Except Self
Medium · AI
🎓
Tutor Explanation
DeepCamp AI