Hash table separate chaining

WilliamFiset · Beginner ·⚡ Algorithms & Data Structures ·9y ago

Key Takeaways

This video discusses hash tables and separate chaining, a hash collision resolution technique that uses an auxiliary data structure, typically a linked list, to handle collisions in a hash table.

Full Transcript

okay let's talk about something super super cool and that is hash tables and separate chaining all rights dive right in what is separate chaining so separate chaining is just one of many many hash collision resolution techniques and how it works is when there's a hash collision meaning two keys hash to the same value we need to have some sort of way of handling that within our hash table so it's still functional well what separate chaining does is it maintains an auxilary data structure to essentially hold all the collisions and so we can go back and look up inside that bucket or that data structure of values for the item we're looking for and usually we use the linked list for this but it's not limited to only linked lists can use arrays binary trees cell balancing trees or even a hybrid approach okay so suppose we have the following hash table it's just a fancy name for an array of key value pairs of age and names and we associate an arbitrary hash value that has been computed with some hash function so those are our hash values they don't really matter for now we're just going to see how we can use separate chaining to handle the collisions okay so on the left is our hash table so our array and I'm going to be inserting all of these key value pairs into this hash table via separate chaining and you'll see how easy it is okay so our first person is will his ages 21 and he hashed to 3 so we're going to put him in that slot 3 Leah age 18 hash to 4 since she has to 4 we're going to put her at index for so the hash is a way to index into the array okay Rick age 61 hash to going to put them there and Ray age 25 hash one okay we're starting to get a little bit full in our hash table here so make some collisions pretty soon okay lera age 34 hash to four but we say oh shoot leah is already there what do we do well in separate chaining we just chain along so essentially each each position in the array is actually a linked list data structure so we're going to scan through the linked list and see if Alera exists and if Lehrer does not exist then we're going to add lera at the very end of the chain okay so Ryan also hash to one but then we look and Ryan is not equal to R a so we need to add a new entry at position one al era age 34 hash to four so nope and Oh layer already existed in our hash table so we're good and she has the same age so we don't need to update it so thin age 21 hash to 3 so easy our hash collision with will who has also hash to 3 so what we're going to do is we're going to append sin to the end of the length less chain so note that even though that will and Finn both hashed to same value that it'll index 3 oh and they have the same age we tell them apart because we store both the key and the value as an entry in our linked list block so that's how we're able to tell them apart okay now I want insert mark whose age is 10 and who has to four so scan through the link list at index 4 for mark and he's not found so we have to append mark at the very end all right now let's have a look at how we can do lookups in this structure so it's basically the same thing so what we're going to do is give it a name we want to find well what the person's age is so suppose we have Ryan Ryan when we hash and we get one so we suspect that Ryan should be in bucket one when I say a bucket I just mean whatever data structure we're using at index one and in our case a linked list so I just scan this linked list for Ryan so start right now so here we're comparing the key so we're comparing the key Ryan to the key array and there's not a match to keep going compare right Ryan there's a match okay we found Ryan and then inside that and true say oh his age is 56 so returned 56 let's do another one find the age of Mark hash mark and since our hash functions are deterministic we know that if there's a mark then it's going to be fine in position four okay so we look in the bucket for scan through oh last one is a mark so we return age 10 so it might happen that the value or the key looking for doesn't exist and so it doesn't exist you turn now okay so here's a good question how do we maintain a constant time insertion and lookup time complexity if the hash table gets really full and I have some long linked list chains good question and the answer is that once there's a lot of elements within your hash table you'll actually want to create a new hash table with the larger capacity and rehash all your items and reinsert them into this new a larger table because tables are fixed size okay and another good question how do I remove key value pairs from my hash table with separate chaining well the answer is basically the same procedure you hash your key and instead of doing a lookup well you would remove particular entry and the length list that's it another question how do I use can I use another data structure to model the bucket behavior of such a mean yes of course and clumb data structures that are used inside a linked list include arrays binary trees up by self balancing trees java uses a hybrid approach in our hash map so once we get to a certain chain length they switch to a binary tree or maybe a self balance by a tree I'm not too sure however these alternative methods are a bit more memory intensive and complex to implement which is wighting maybe less popular but they might be a lot faster - I haven't actually implemented them myself so have a look at those okay so that's it for this video next video we're going to be going over hash tables with open addressing which I'm really excited about the guys if you want an implementation of a hash table with separate chaining there's joint be one at github.com slash will infuse a slash dated - structures and I will also be going this or some source code in the next video so stay tuned for that thanks so much for watching catch you later

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 =================================== 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 · 60 of 60

← Previous Next →
1 JES Image Manipulation - 2 - Installation
JES Image Manipulation - 2 - Installation
WilliamFiset
2 JES Image Manipulation - 3 - User Interface
JES Image Manipulation - 3 - User Interface
WilliamFiset
3 JES Image Manipulation - 5 - Negative
JES Image Manipulation - 5 - Negative
WilliamFiset
4 JES Image Manipulation - 6 - Black & White
JES Image Manipulation - 6 - Black & White
WilliamFiset
5 JES Image Manipulation - 4 - Grayscale
JES Image Manipulation - 4 - Grayscale
WilliamFiset
6 JES Image Manipulation - 8 - Blur
JES Image Manipulation - 8 - Blur
WilliamFiset
7 JES Image Manipulation - 7 - Edge Detection
JES Image Manipulation - 7 - Edge Detection
WilliamFiset
8 JES Image Manipulation - 9 - Blend
JES Image Manipulation - 9 - Blend
WilliamFiset
9 JES Image Manipulation - 10 - Matte
JES Image Manipulation - 10 - Matte
WilliamFiset
10 JES Image Manipulation - 13 - Rotate90
JES Image Manipulation - 13 - Rotate90
WilliamFiset
11 JES Image Manipulation - 12 - Mirroring Picture
JES Image Manipulation - 12 - Mirroring Picture
WilliamFiset
12 JES Image Manipulation - 11  - Crop Image
JES Image Manipulation - 11 - Crop Image
WilliamFiset
13 JES Image Manipulation - 14 - Stretch picture
JES Image Manipulation - 14 - Stretch picture
WilliamFiset
14 Java Fractal Explorer [6/8]
Java Fractal Explorer [6/8]
WilliamFiset
15 Java Fractal Explorer [4/8]
Java Fractal Explorer [4/8]
WilliamFiset
16 Java Fractal Explorer [8/8]
Java Fractal Explorer [8/8]
WilliamFiset
17 Java Fractal Explorer [5/8]
Java Fractal Explorer [5/8]
WilliamFiset
18 Java Fractal Explorer [2/8]
Java Fractal Explorer [2/8]
WilliamFiset
19 Java Fractal Explorer [7/8]
Java Fractal Explorer [7/8]
WilliamFiset
20 Java Fractal Explorer [1/8]
Java Fractal Explorer [1/8]
WilliamFiset
21 Java Fractal Explorer [3/8]
Java Fractal Explorer [3/8]
WilliamFiset
22 Introduction [Programming Competition Problems]
Introduction [Programming Competition Problems]
WilliamFiset
23 String Manipulation 1 [Programming Competition Problems]
String Manipulation 1 [Programming Competition Problems]
WilliamFiset
24 String Manipulation 2 [Programming Competition Problems]
String Manipulation 2 [Programming Competition Problems]
WilliamFiset
25 Graph Theory 1 [Programming Competition Problems]
Graph Theory 1 [Programming Competition Problems]
WilliamFiset
26 Logic 1 [Programming Competition Problems]
Logic 1 [Programming Competition Problems]
WilliamFiset
27 Grid Problems 1 [Programming Competition Problems]
Grid Problems 1 [Programming Competition Problems]
WilliamFiset
28 Dynamic Programming 1 [Programming Competition Problems]
Dynamic Programming 1 [Programming Competition Problems]
WilliamFiset
29 Introduction to Big-O
Introduction to Big-O
WilliamFiset
30 Dynamic and Static Arrays
Dynamic and Static Arrays
WilliamFiset
31 Dynamic Array Code
Dynamic Array Code
WilliamFiset
32 Linked Lists Introduction
Linked Lists Introduction
WilliamFiset
33 Doubly Linked List Code
Doubly Linked List Code
WilliamFiset
34 Stack Introduction
Stack Introduction
WilliamFiset
35 Stack Implementation
Stack Implementation
WilliamFiset
36 Stack Code
Stack Code
WilliamFiset
37 Queue Introduction
Queue Introduction
WilliamFiset
38 Queue Implementation
Queue Implementation
WilliamFiset
39 Queue Code
Queue Code
WilliamFiset
40 Priority Queue Introduction
Priority Queue Introduction
WilliamFiset
41 Priority Queue Min Heaps and Max Heaps
Priority Queue Min Heaps and Max Heaps
WilliamFiset
42 Priority Queue Inserting Elements
Priority Queue Inserting Elements
WilliamFiset
43 Priority Queue Removing Elements
Priority Queue Removing Elements
WilliamFiset
44 Priority Queue Code
Priority Queue Code
WilliamFiset
45 Union Find Introduction
Union Find Introduction
WilliamFiset
46 Union Find Kruskal's Algorithm
Union Find Kruskal's Algorithm
WilliamFiset
47 Union Find - Union and Find Operations
Union Find - Union and Find Operations
WilliamFiset
48 Union Find Path Compression
Union Find Path Compression
WilliamFiset
49 Union Find Code
Union Find Code
WilliamFiset
50 Binary Search Tree Introduction
Binary Search Tree Introduction
WilliamFiset
51 Binary Search Tree Insertion
Binary Search Tree Insertion
WilliamFiset
52 Binary Search Tree Removal
Binary Search Tree Removal
WilliamFiset
53 Binary Search Tree Traversals
Binary Search Tree Traversals
WilliamFiset
54 Binary Search Tree Code
Binary Search Tree Code
WilliamFiset
55 Fenwick Tree range queries
Fenwick Tree range queries
WilliamFiset
56 Fenwick Tree point updates
Fenwick Tree point updates
WilliamFiset
57 Fenwick Tree construction
Fenwick Tree construction
WilliamFiset
58 Fenwick tree source code
Fenwick tree source code
WilliamFiset
59 Hash table hash function
Hash table hash function
WilliamFiset
Hash table separate chaining
Hash table separate chaining
WilliamFiset

This video teaches how to implement hash tables with separate chaining, a technique used to handle collisions in a hash table. It covers the basics of hash tables, hash functions, and collision resolution using auxiliary data structures like linked lists.

Key Takeaways
  1. Create a hash table with a fixed size
  2. Implement a hash function to map keys to indices
  3. Use separate chaining to handle collisions
  4. Insert key-value pairs into the hash table
  5. Perform lookups and deletions in the hash table
💡 Separate chaining allows for efficient handling of collisions in a hash table by using an auxiliary data structure to store colliding elements.

Related Reads

📰
Day 29/100 Koko Eating Bananas (Binary Search)
Learn to implement binary search to solve the Koko Eating Bananas problem and improve your algorithmic skills
Medium · Programming
📰
O(N) Manacher's Algorithm with Mirror Boundary Optimization
Learn to optimize palindrome detection using Manacher's Algorithm with mirror boundary optimization, reducing time complexity to O(N)
Dev.to · Dipaditya Das
📰
Building a Power Grid Inside Minecraft with BFS Algorithms
Learn to build a power grid inside Minecraft using BFS algorithms and understand its relevance to cloud services
Dev.to · Carlos Cortez 🇵🇪 [AWS Hero]
📰
The Run-Length Encoding Trick: How Simple Strings Get Compressed
Learn how Run-Length Encoding (RLE) compresses simple strings, a fundamental technique in programming and data compression, and apply it to optimize storage and transmission of data
Medium · Programming
Up next
Stump Grinder Carbide Wheel Grinds Hardwood To Chips
Innoforge Studio
Watch →