 Okay, I'm here again with James and we've been talking about hash tables and last time we ended with saying that we're going to come back with a visualization on a white board, but unfortunately our dry erase marker is dead. So hopefully we'll have visualizations in future videos, but we're going to continue talking about hash tables. And we're going to start off with my question on where we are knowing very little about hash tables. I kind of get the concept of not really how they work. My initial thought while listening to you in the last video, and as an example you gave off camera, was that let's say you had all these houses and you want to put people in each house. And John Smith came along and you're going to put John Smith in house number five. My initial thought is okay, well then you have all these values, all these people in these houses and then you have a separate list of what house they are in. But I know that's not right and you don't have a list, you figure out where they are mathematically based on the names value. Yeah. Explain that some more. So let's try to use an example that is probably an easier example to work with. We'll talk in terms of C programming and try to use those sorts of words this time. And we'll talk about just what we would do, like let's suppose we have a dictionary, or not a dictionary, let's suppose we have a big book and we want to identify all the words in the book and sort of keep track of how many times each word is counted. So what we might do is we start inputting these words and first we try to make them all uniform so we get rid of any case sensitivity, we make them all lower case. And then what we want to do is we want to actually start organizing in our data structure. So what we want to do is we want to come up with a way that obviously words are just letters, combinations of letters, or arrays of letters, right, each one has a number. And those numbers are, you can't just add them up because, you know, if it was 1 and 2, or 3 and, if it was 2 and 3, it'd be 5 because it would be 4 and 1 and 5, right? So, you know, you would have non-unique numbers for each word. So that's why we do something like a MD5 hash, right? Which we discussed earlier, not on camera, but the, which theoretically can be unique, but if the value you're putting into it gets large enough, eventually the numbers start coming in again. But it's supposed to be so large that it's unlikely. Yeah, yeah, it's going to compute for at least all the words we have available to us. It's going to keep them very unique. So what we can do is then we can translate that word, that combination of characters into a number, okay? But we won't consider that, even though there is a little bit of overlap on MD5, we don't even have to worry about that because we won't consider that number of the key. We'll consider the string key. Okay. And the value will consider the count of that word. Okay. So what we'll do though is the hash function, what it's going to do is it's going to take that word and it's going to MD5 it, okay? So now it has a number and we'll have made an array that is our hash table. So for this example, maybe we'll make an array that has like a million spaces in it, right? We probably don't have a million words available. Maybe that's a little overkill. Maybe we'll only do 100,000. Sure. Because that's probably most, most books, you know, and it's a good developer. I probably would have looked up how many, what the average number of words used in it and what volume are and try to keep the epic with table. So anyways, MD5 is going to start making numbers for each of these words. Obviously, these numbers can be far bigger than my 100,000 size table or array, right? So what I'm going to do is I'm going to use a modulus, okay? Find the remainder of dividing that number with the number the size of my table. So again, it's going to be very similar to what we were talking about before where the word and, okay, it's going to give me a number, the remainder of dividing it by the size. So you can kind of think of if the number was like 7,832,000, you can think of every time I get 100,000, it wraps around once, right, and it wraps around again. And finally, it's going to finish wrapping and then the remainder is going to be how many times it goes through to get to the location, okay? But now, what happens is two different numbers might have the same remainder, okay? So we get two numbers on the same location. So that's why the hash function doesn't just figure out where in the array it goes. It actually has to look and see if something's there. And if something's there, it has to see if it's the same key. If it's the same key, it's good. It doesn't have to do anything else. If it's not, well, it kind of depends how you write hash tables, but it either does, like I said last time, it'll check the next one and the next one. There are some implementations that are slightly different. I think Microsoft wrote a version where the cell actually can itself be divided up. And I think there's other versions where you start, you don't just look at the next one, but you skip and you look around a certain area. So I was going to ask if there was like sub keys, if it were, but that kind of sounds like that the Microsoft one is doing, sort of. It's not a sub key as much as what it starts to do if you're inside that bin is it'll start going back to the original, like just checking each value one at a time. Like what you would do as a non-hash to just a regular array or a linked list. Okay. Okay. And then if that, other than that, you could do hash tables and hash tables though and have two keys. It doesn't solve the problem we talked about earlier, which are hash collisions. It does not solve hash collisions, right? For hash collisions you have to compare your original key and just keep moving. Right. But what it does is, what you could do is you could have groups within groups. You know, that would be perfectly acceptable. Okay. Yeah. Yeah, there's comments on this YouTube video right now. Chris looks like he's lost. Well, yeah, that's, I'm sorry, we should, yeah, go ahead. No, I'm just trying to, what did you call it? Finding the remainder with the percentile sum? Modulus. The modulus. Yeah. Yeah, I'm just not grasping how that's being used properly. Yeah, so. Talk to me like I'm an idiot. Okay. So. Like you normally talk to me. So my array is only, you know, we were saying I think a hundred thousand for a little bit. My array is only a hundred thousand. Can we make it a small number just for an example? I know that's. Let's say ten. My array is size ten. Okay. So, so when my number, my MD5 number gets made, and that number is in thousands, right? One thousand one hundred and fifteen. I can't put it on that array, right? Okay, right. Right, because what I'm going to get is, what do you, it's that fault, right? You're right, so even if it's, if it's eleven, it can't put it on there. That's a great question. We shouldn't use that example, yeah? So if it's eleven, right, it can't put it on that. In fact, if it's ten, it can't put it on there because it's zero through nine, right? Right, so the, the value is bigger than the number of spaces. Yes. So that's why you're getting the remainder. Yes. Okay, okay. That'll, that'll remap it on what you haven't failed. I got it now. Okay. See, you just use smaller numbers. Yeah. And we should have gone to three. You have, no, let's do one. But, and then you have two, so you get the remainder. Why do you always get remainder one, so you get the remainder one? See, you're going to get a very simple equation. Yeah. No, okay, I understand it more now. I got it. I got, I got why you're doing that. That's what I was missing before. Yeah, yeah. So that'll always tell you where to start. And then if you're a very good developer and you're writing a custom sort of hashtag thing, well, you know, if you're, if you know, again, people use, if you use a dictionary, you've used a hashtag but you didn't even know it, right? Because how does that work? You think every time you put in a word and a value associated with that word in the dictionary, do you think it just stores them in some list and checks those one at a time? It doesn't. That's how I would have done it. I know. What it does is it's, it's, it's going to be doing some sort of, and I'm not a developer for Python. So if someone corrects me, they're probably right. But what it's going to be doing, it's going to be mapping that string to a location and then it's going to verify that key and it'll be able to get any values that are there, right? No, you're saying, okay, I'm, you're not a really a Python developer. I mean, I know what developed the core interpreter for Python. Right, right. Yeah. So you're saying that you're pretty sure that dictionaries are hash tables or you're positive? Yeah, I mean, as a matter of, you can see this because, well, yeah, go ahead. I'm going to, when you say, when you say someone correct me, is that what you were saying, referring to or something? Well, I mean, I don't know the fine details of the implementation, but yeah. Okay, I guess, I guess, I forget that. My question was going to be, you're, you do a lot of stuff in C. Yeah, yeah. Okay. I do a lot of Python, too. Okay. Yeah. But in C, is there anything like dictionaries or you have to create it yourself? You've kind of created it yourself. I mean, I guess there's probably something out there, but in general. And there's C, so most people create stuff. So, you know, a dictionary is a very specialized type of hash table, right? Because a dictionary is, you have a word that is your key. And you can put whatever you want as a value, though. You can put an object, you can put another list, you can put another dictionary, right? But for C, you could make something more abstract. You know, for example, we were talking about facial recognition software earlier. And the way facial recognition software works, right? It's going to actually, you can see this. Yeah, it's going to map all these locations, right? And it's going to take that very complex object. And it's going to hash that into a table, right? So it's going to compute, first thing it's going to do is it's going to take that key, which is all of that data about your face. And it's going to convert it into a number, okay? And then it's going to, you know, find the remainder on the side of that table. It's going to check that location in the table. And it's going to then compare all your values to that, okay? And that actually is, it's an oversimplification. It actually works a little bit more complicated than that. That's kind of interesting, because I think actually the way it works is each distance gets hashed and sort of the combination of all of the locations in the half table kind of represent your face. But again, you can just take a very complex object, a very complex idea. And you can create a hash table for it, which is kind of the power of doing that in the scene. Sure. Okay. Well, thank you for talking with me about this. And we'll do some more chats soon, some more tech talks. And as always, please visit FilmsByChris.com. That's Chris with the K. Should be linked in the description. And James, thanks again for stopping by. Okay, this is an introduction to FilmsByChris.com. I'm Chris. That's Chris the K. That's me right there. My daughter, Amber. And my wife, Jennifer. We pretty much live in the swamps of Florida. I'm a firefighter by day, as well as by night. We work long hours. But that's not why you're here. You're here about the videos I put up on YouTube. These videos are mainly about computers and programming, which means most of my videos look something like this. And if that's what you're interested in, great. If not, that's all right. I do videos on other topics, too, such as video editing, special effects, photo editing, 3D design, and music creation. If you are one of my viewers and you enjoy my videos, my Patreon page is a place where you can go to help support my videos. So I ask that you take the time to go to my Patreon page and look at different levels of rewards you can receive for different levels of backing. There should be a link in the description of this video if you were watching it on YouTube. Otherwise, you can visit patreon.com forward slash metalx1000. And I thank you for your time and your support. Have a great day.