- 2 days ago
Transcript
00:00Now I have seen that put function, get method, contains, remove, size, all these operations are working in constant time.
00:08So what are we going to do? In this lecture, we will implement all the hashmap functions in this time
00:14complexity. And as we implement it, it will be very similar to Java already inbuilt. In this lecture, we will
00:23also learn how to implement Java in which we will run and simulate it.
00:29Now, if I take an example of a map, let's take a example of a country and population map, in
00:37which I have a lot of data, these are my notes, in which one side I have India which is
00:43equal to 100, one side I have China which is equal to 150, one side I have US which is
00:51equal to 50 and Nepal which is equal to 5.
00:56Here the name of my country will be key and the value of population will be equal to my value
01:02and this will become my hash map. Now this data is implemented in the form of array of linked list.
01:09What did I say? I said array of linked list. Array of linked list means that I have an array
01:19of linked list. This array is a fixed size. Let's say it's size is equal to 4. What do I
01:25have?
01:274 parts. This will be index 0, this will be index 1, index 2, index 3. And on this single
01:33index, there is a link list. Like the first link list, I can create something like this. Here, here. This
01:43will be head node. This will be my link list next node. And here, this node is attached with my
01:48null value. Besides, I can create another node. Here, I can create another node. Here, I have a single node.
02:05in the form of array of link list and these nodes are in the form of link list
02:16Here my data is stored in India, which I store 100. Here my data is in China, which I store
02:22150. Here my data is in Nepal, which I store 5. Here my data is in US, which I store
02:2850. I take one more data. I think I have data from Bhutan, which I store 1. So my pairs
02:35are linked list notes in this way. And where is it stored? It is stored on an array of index.
02:44Actually, we don't call Array, we call Bucket. In implementation form, we call Buckets. Buckets means Balty.
02:53This is my one vault which has a lot of nodes. This is my second vault which has a lot
02:58of nodes. This is my third vault which has nodes. This is my fourth vault which has nodes. So I
03:05have buckets. And with all these buckets, there is a fixed size which is equal to my size. And in
03:11these buckets, where is the data linked list?
03:15that it is stored within the link list. So, for each bucket, it becomes a link list. So, I have
03:21basically visualized and converted this form into internal implementation. So, I will remove this visualization and now we will talk
03:30about this form, meaning about this data. Now, let's assume the first function which we want to implement is my
03:38put function. Put, that means I have a pair of key values
03:44Add to this hash map. For example, I will store the population for Canada which is let's say 30. So,
03:54this pair I will store in my bucket. So, there are two cases. Put in two cases. The first case
04:02is that my key already exists. The second case is that my key doesn't exist.
04:10So, first of all, I have to find out that my key, do I exist in my buckets or do
04:15I not? Now, the most new brute force way in my mind is that go to one index and check
04:22every place. I mean, this index, I checked in this node, I checked in this node, then I checked in
04:26this node, then I checked in this node, then I checked in this node, then I checked in this node,
04:29then I checked in this node, then I checked in this node, then I checked in this node, then I
04:30checked in this node, then I checked in this node, then I checked in this node, then I checked in
04:30this node, then I checked in this node, then I checked in this node, then I checked in this node,
04:31then I checked in this node, then I checked in this node, then I checked in this node, then I
04:32checked in this node, then I checked in this node, then I checked in this node, then I checked in
04:35this node, then I checked in this node, then I checked in
04:38I will travel to 4, 5, all nodes. Let's write some values here. One is N value. N means I
04:46have nodes or pairs. Besides, I have capital N which is equal to the size of my array or my
04:55total number of buckets. How many I have buckets? These are my buckets.
05:00So if I went to every place, then I travel to every node. So my time complexity will become big
05:07of n which is not a good time complexity. What do I do? I have to solve our put function
05:11in constant time complexity. So for that, we use hashing. For hashing, a code exists already in Java.
05:23we name the hash code. Hash code is a function. It takes input of any data and hashing it and
05:30hashing it and hashing it and hashing it. Like if I pass this data in India
05:37So in this output, I can return any number in the output. This can be a positive number, negative, or
05:45any form. So hash code has to be hashing. Hashing is to change the form of data. So this can
05:52be a string type, integer type, boolean type, float type, long type. Change the form of data in the hashed
06:00form.
06:01Now, how do you do it? There are different hashing algorithms. There are many popular algorithms that are used in
06:07hacking. When your passwords are hacked, I have already given this example. Your passwords are made in companies. If you
06:15have made an Instagram or Facebook account, you have put your password in ABCD.
06:20So, if your data has leaked from that company, meaning that someone has hacked the Facebook account and the data
06:26has leaked, then you think that your password will go to the hacker. But actually, it doesn't happen. Companies hashing
06:32algorithm use. So, companies also have a function called hash function. What does hash function do? Input, you get data
06:41and output, you get data to change its form. So, let's say, output has given 1589.
06:49This data has changed the form of data.
06:52When the hacker has hacked the data, it will get hashed form which is 1589. So, it will directly tag
06:58your password directly. It will only get hashed form. Now, there are also many salting techniques that can be used
07:05to make the password more safe in companies. But it is a separate thing. How do the hashing implement? There
07:13are many algorithms. In Java, C++, there are multiple languages. And in multiple languages, there are multiple algorithms.
07:22which we don't know in detail. We know in detail about data structures and algorithms. So we just know how
07:28to use data structures and how to implement it and how to use it and how to make it. We
07:33will just focus on that. How hashing is already in Java. No one will ask you in interview. Just the
07:39basics, which concept can be asked to you. But if you have curiosity, you can go straight and Google and
07:44learn hashing. It's very interesting if you have interested in hacking. So what will we do?
07:51Hash function already exists and it becomes a function within the hash code name. For now, I remove hash code
07:58and assume that I have a black box. Black box is that I don't know how the work is in
08:05it, but I know that it becomes a work. This black box is my hash function.
08:10Hash function or any function of any function, take some input or output. We have learned in basic functions methods.
08:17Hash function takes a bucket index from its bucket index.
08:28I will not check all the buckets. I know that my particular key will go into a particular bucket. This
08:36means India, which has a particular logic in this index 0, has a particular logic. What is it? It is
08:42in the hash function. This which has been stored in China,
08:46On the index 3, there is a logic. What is the logic? It is in the hash function. So, don't
08:50give attention to the logic. I just want to think that if I have passed the hash function, then it
08:56will give me the right bucket index. Like if I have passed India here, then my hash function will return
09:050. If I have passed US here, then my hash function
09:09I will return to the US because the US is stored in the US, so the US is stored in
09:14my bucket index. So the hash function's work is to take a key call and return to the bucket index.
09:21And we are going to use this thing again. Now let's talk about put.
09:27What should I do? Canada and its corresponding value. I store this pair, this node in my bucket. So first,
09:36where should I store this key? Which one will tell me? This will tell me my hash function. In the
09:42hash function, I will pass Canada and for Canada, I will return a bucket index for Canada. I mean, in
09:48the hash function, I have passed my string Canada and this hash function will give me a bucket index by
09:54return.
09:57bucket index is equal to 2. Where should Canada store? Canada should go to this 2 index. Now there are
10:04two cases. Or Canada already exists there. Because hash function always gives the same value for one key. Canada should
10:11store after this value or return it. Now Canada has the value or bucket index returned.
10:20If I pass Canada again, I will return the same bucket index. What does the hash function do? It returns
10:27the same value for the same value. It is not that if you pass another key you will pass the
10:32same bucket index. I have passed Canada so I have returned the bucket index is equal to 2. I have
10:39to store Canada's node in 2 bucket index.
10:42So what will I do? First of all, I will go to this two bucket index. As I have come
10:47here, I will run a loop. Who will run a loop? There are many linked lists here. If this is
10:53a linked list, there is a link list in the bucket. So I will run a loop on all the
10:58nodes. Let's assume that there is already another node. I have a value stored in which I have Indonesia and
11:076.
11:08So, first of all, I will check this node if there is Canada stored or not. After that, I will
11:14go to the next node to the linked list. Then, what is Canada stored or not. If I don't get
11:21Canada in both places, then what will I do? I will create a new node in which I will store
11:26my key and value which is equal to 30. So, in this way, I have stored a new node. In
11:33the linked list, how much time it takes to store a new node? Because of one time.
11:37Now, let's take one time Canada 30 passed key doesn't exist, so what did I do? I created a new
11:42node. But if I pass Canada again along with key 50.
11:54So what will happen in this case? First of all, I have to pass the hash function to Canada to
11:58the hash function. Then I have to pass the bucket index to the hash function. What will happen in Canada?
12:03I will return the bucket index to the two. So I will go to the two bucket. Two bucket means
12:08this bucket. I will go to the first node and search if Canada is not here. So I will go
12:15to the next node. Then I will search if Canada is here. Then I will go to the next node
12:19and search if Canada is here. Then I will go to the next node and search if Canada is here.
12:21I will find Canada.
12:23So, as I got Canada, if my key already exists, what do we do? We update the value. So, the
12:31value is my 50. So, I cut this previous 30 and store it as 50. Now, I will summarize all
12:37these cases. If I have a pair of key value, like here, then I will do hash function.
12:49I will remove the bucket index from the bucket index. I will get to know that the key value pair
12:53is in which bucket is added or in which bucket is already added. It will be the same bucket. Then
12:59remove the index. After that, we will put the loop on our link list.
13:05in the bucket, so in the bucket we have to put a loop in the bucket. We have to go
13:12to the notes and search the value. Which value we will search? We will search our key value because we
13:19always search key through. Key is unique, value can be used here, US is 50, Indonesia is 50 and Canada
13:27is 50. In this case, you cannot check 50 value, so we will always check key because it is unique.
13:35if found, if found, if found, if found, if found, if found, if found, if found, if found, if found,
13:40if found,
13:52Here the first question that comes in our mind is that the loop has been launched on the linked list.
13:57So how does our time complexity come in the hash map? I have the hash map created within the hash
14:03map of my bucket size. The value of n is equal to 4 which is equal to my size and
14:10my small n is equal to 7.
14:13So if I put some element, calculate its time complexity. So what will the hash function directly do for my
14:19element? Hash function will take my key and return my bucket index. Now hash function works in constant time. So
14:27I assume that in constant time, I know that my data will add in this bucket. So it will be
14:32constant operation. Time which is saved, is saved only from here to travel the link list. Now if I spread
14:40the link list evenly,
14:42I mean these all the 7 notes, how will we divide? They will divide if equally in these 4 buckets.
14:50So what can I say? I have a value of lambda. Lambda signal which students don't come from math background,
14:56we call it lambda.
14:58It's a symbol. Lambda ki value ho jayegi meri paas N divided by N ke equal. N matlab meri total
15:03number of nodes. N matlab meri total number of buckets. May nai kya kiya? Nodes ko buckets ke andar equally
15:09divide karne ki kooshas kar di. Aur usse mujhe ek lambda value mil jayegi. Ye jo value hogi this will
15:15be equal to 7 divided by 4. Which is equal to 1.75.
15:19So I will add the number of operations to one value actually in n or the total number of nodes.
15:28Or the size of the bucket. I will add the time to one element to the big of lambda. And
15:36what is big of lambda here? Big of 1.75. Can I say that this is a constant value?
15:42And mathematically, we will see why this is happening. Hash function can be implemented in this way. Hash map can
15:49be implemented in this way. That my lambda value is less than or equal to some constant value. This value
15:57is called a constant threshold. Threshold is the highest value. So this threshold value is constant value. We call it
16:06k. So lambda value is less than a constant fixed value.
16:12Therefore, the time in operation is not a single operation, but if we divide the notes evenly in buckets, in
16:21the average cases, the time in most of the cases, in fact, the majority of the cases, the time is
16:28a constant value, which is lambda value, which is equal to less than equal to some constant, and the value
16:35of constant is less than equal to, that is also constant. Therefore, the put function takes constant time.
16:41So the amazing performance of the hash map in terms of time complexity comes with the hash function. And this
16:47hash function is due to which I get directly my bucket index. If I get the bucket index, I don't
16:53have to check all the notes. I have to check all the notes. How many notes in the bucket? On
16:59an average, there are lambda number of notes and lambda also has constant value. Therefore, my number of operations are
17:05constant. So put function is constant time operation.
17:10point is that my lambda will increase. Why will the lambda increase? Let's say that I have added some node
17:16for Sri Lanka here. Here I have taken some value of 150 or 180. Now what will happen? Slowly the
17:22value of my n will increase.
17:24N means my total number of nodes. In this map, I will add more nodes. What will happen every time?
17:31My value will be plus plus. Lambda means N divided by capital N. Now, if this value is increasing, I
17:38can say that my lambda will increase slowly. If lambda will increase slowly, then the value of lambda will increase
17:45in one point. That's why we track the threshold value first. Let's keep a fixed value.
17:51If I have decided before that K is my threshold value and K is my value that I have to
17:55keep 2. Now my lambda value is 1.75, so that's okay. But the value of n will increase, which
18:01means lambda will increase and lambda will cross the threshold. If lambda value is more than 2, then at that
18:08point, we perform some action which we call rehashing. That means you have to correct your hash again.
18:16As the value of n will increase, my hash map will lose a little time complexity. But as the threshold
18:23comes, I will fix it again. And as the new threshold comes, I will fix it again. So, in this
18:28way, we give re-hashing the term. Re-hashing which we perform on our hash map. As in my map,
18:37we add more nodes.
18:38What do I have to do in the rehashing? In the rehashing, I will recreate this entire structure which I
18:44have created a linked list of nodes. I will recreate it. I will create a new array. We will create
18:52a new array. We will create a new array. Usually there is a fixed logic that I want to create
18:58a new array. I want to create a new array 2 into old array size or old size.
19:07We have rehashing on the hash map. So what was the size here? Size means my array size. My total
19:12number of buckets. Size is 1, 2, 3, 4 to equal. So now I will create a new array. So
19:19I will create a big array of 8 size. This will be my index. 0, 1, 2, 3, 4, 5,
19:276, 7, 8.
19:31What is my map? What is lambda value? It is n divided by capital N. What is n value? 1,
19:372, 3, 4, 5, 6, 7, 8. I will write here 8. And here, what is size value? Size value
19:44is 4 which is equal to 2. Now, this value is equal to this value. It is not big. But
19:50if there is another node, like I have added another node here, which we believe in Sri Lanka, I have
19:57added Pakistan here.
19:58Here I can put the population of Pakistan. So what will happen here? Here I will have 9 divided by
20:064. 9 divided by 4 is 2.2 something. So the value will be greater than my threshold value. So
20:15now I have to rehash. What will I do for rehashing? I will create a new array. Double size means
20:208 size. I have created 8 buckets. Now how do the elements come in each bucket?
20:27I will remove one node and call the put function here. I mean, first of all, I will remove this
20:34India node. What will India do? India node will ask me to tell my bucket index. So I will give
20:42this bucket index. Which bucket? This new bucket. Let's say India's bucket index is equal to 4. So India node
20:50will be added here.
20:51Then what will we do? This node will be removed from here. Now if I remove this value from here,
20:56what will I do? What will I do? The node will ask its bucket index. Let's assume its bucket index
21:01is equal to 7. So the node will be removed from here.
21:07foreign foreign foreign foreign foreign
21:33for a different bucket index. So what will happen to you? I will push my new array into my new
21:44array. This will become a new hash map for me. So what did the rehashing? Rehashing has now rearranged the
21:52objects within the new data structure. So what will happen to the value of lambda? Let's say 9 and 9
21:58will transfer all nodes here. So now the value of n is equal to 9.
22:03i mean n divided by capital N here n is equal to 9. What is capital N? What is my
22:08size? What is my size? Size is equal to 8. My array size has doubled. Buckets have doubled. So here
22:149 divided by 8. Which is one point something.
22:17Now this one point something is less than my threshold value. So now my hash map is doing the right
22:23job. That means if the value of lambda will come, that will be one point something which is less than
22:29equal to my value k which is my threshold value. That's why we say that the operations of the hash
22:33map are constant time. Why? Because the rehashing work is not always over time. This rehashing work is very low.
22:42First you have made 4 size, then 8 size, then 16 size.
22:48time complexity we calculate time complexity when the input size is very big we don't talk about 10, 12, 16,
22:5632 we don't talk about 1,2,000,000,000,000,000,000,000,000,000,000,000,000,000
23:00,000,000,000,000,000
23:15we will rehash it, so rehashing process is long. This is big of n, but it is not very small,
23:22it is very small. Which process happens every time? It happens every time that we put in big of lambda.
23:27That's why constant time comes to hash map. What do we do? We implement a basic hash map. We write
23:34the put operation in it. After that, we will talk about the rest of the operations.