Speaker A
Let's start with an interesting problem that appears quite often in interviews. The question is given LeetCode 133 Clone Graph. The task in this question is to just make an exact copy of the graph. So let's understand the question and explore its intuition and dive deep into the solution step by step. Let me read the problem. The problem states that given a reference of a node in a connected undirected graph, this is an undirected graph, return a deep copy clone of the graph. Each node in the graph contains a value integer and a list of nodes as its neighbors. As the graph has its neighbors, the node has the value and the neighbors of it. Test case format for simplicity: each node's value is the same as the node index. For example, the first node value is equal to 1, the second node value is equal to 2, and so on. The graph is represented in the test case using an adjacency list. Okay, so that's what we have given. We have given a node and that node is connected to every other node, and this is a totally connected graph and each node has two things: the value and the list. Let us understand with given types of examples. There are a couple of examples given. As you can see, there is an example given as there is an original graph which is cloned to a second graph. You can't return the same graph. Okay, this is the instruction, and as the second graph, we have to return this type of graph, and in the third, we can't return that graph. You can see the input method adjacency of the graph. This is given as neighbors of the graph as values, totally the index, and here we have the relationships of that graph. Coming towards the constraint of the question, the number of nodes in the graph is in the range from 0 to 100, meaning the node is from 0 to 100. So we can have a time complexity of n squared. Let us understand the question. The question states it has a node. Okay, so let us understand how the node is formed in this graph. So in this graph, we have two things in a node: one is the value of the node, let us take it as one, and the other is the array of neighbors. It has the array of neighbors that store the reference of that node, not the exact value; it stores the reference exactly. We have another graph. So let us duplicate it and let me show it to you. We have this group. We can take another duplicate. Two duplicates we can take. Two duplicates we can take. Let me erase the values. So the value for this is three. The value for this is three, and the value for this is four. We can assign the references of the nodes as, let's say, it is a reference A, it is the reference B, and the third has the reference C, and the fourth has the reference D. As per the question, the one is connected to 2 and 4. So, we can insert the reference of 2 and 4 to A, 2 and 4 to A. The reference of two is B, and the reference of four is D. So, we have this, and two is connected to 1 and three. So the reference of one is A, and the reference of three is C. What about three? The three is connected to two and four. Two has B, and four has D. Now the D is connected to 1 and three. So one is having A, and three is having C. Let me just swap one and four for clear understanding of the question. Okay. Let us connect it from this. Connected like this. Like this. One is connected to four, and four is connected to three. This is the graph we have. Do you understand? All the things having their references like A, B, and C. So the thing we have to perform in this graph is to just change the references of every node and change the reference wherever the reference is written. So we need just a new graph as, let's say, we have this graph, we just have to let me change the reference. X, Y, Z, W. One is connected to 2 and 4. So two has Y, and four is having W. Two is connected to A and C. A means 1 and three. So X and Z, and B is connected to B and D. B and T. So here we will have Y and W. Four is connected to A and C, means X and Z. That's the thing we have to perform. All right, to do that, the first thing we should have in our mind is how to create a new reference. The new reference will be created by the thing like Node node takes new node. New reference is equal to new node. Here we can enter the old reference of the node. So we can have the old reference. Old reference. This will create a copy of the node. To store the copy of the node, we must require a hash set. So hash set will have two types of storing techniques. One is node, and another should be node. Let's create a hashmap. Hashmap of type node, and another will be type of node which is empty. All right. We have these types of things. The idea to solve this problem comes with an intuition. So let us move forward with the intuition of this problem. The intuition of this problem is to just transfer a node to its new reference and then transfer its neighbor to the new reference. It will require DFS. So as we transfer the A to X, then transfer, then go to the B and transfer its node also, go to its neighbor, transfer them also. So we are going depth to depth. It is the DFS. DFS requires depth every time. So we use DFS, we use hashmap, and we just use the node and make another node with the new reference. Let us do a dry run. Okay. So here we have value one. So we will take the first node as X. So we have X. Let it convert to another reference. Let us take it is converted as A. Now move to its neighbors as what its neighbors currently have. So currently its neighbors have Y and W. So we should move to the new neighbors, new neighbors. So convert the new neighbors to their new references. So take Y as when the first term we have converted, we can save it to our hashmap. So what our hashmap will have? Hashmap will have node of nodes. Okay. So for X, we have converted into A. So here we will mention the X is converted to A as old node, old node. It is new node. X is converted to A, and we have saved it to the hashmap. Now we move towards saving the neighbors. We will iterate to the neighbors and convert them to their new nodes. Pick up Y. Pick up Y and convert to their new node. So let me say Y is converted to B. Store this in the array. Y is converted to B. Now what are current of Y? What are current of Y? There is X and Z. To convert the X to its new node, we can first check if X is already converted or not. If X is already converted, we don't need to convert it again. So X has a new node reference as A. So just simply write it to convert for Z. To convert for Z, we should convert it to C. Okay. So let us save in the hashmap. So Z is converted to C. Now C, its current neighbors. What are current neighbors of C? Is Y and W. So what are new neighbors? New neighbors. First check Y is converted previously or not. So Y is converted, then replace it with its new reference. What about W? W is not converted. So let us convert W. W will be converted as D. So let us convert W as D and store it to the hashmap. All right. Now move to the current of W. Move to the current neighbors of W. The current neighbors of W is X and Z. So let's make it the new neighbors. So X is previously defined as A, and Z is also previously defined as C. So the function will end here. It will return to this as what is its new node is D. So we will mention the D here as W is converted to D, and also this C will return to this B. So we will write C. Now this B will return to this place. So Y is converted to B. And now we will check for W. Is W previously converted? Yes, W is previously converted. We can check it by the hashmap. So, replace it with D. All right. So, all things currently have their new node as this is having, this is also having, this is also having, and this is also having this new reference. So, this thing we must code it in a pseudocode. Let us first take how our pseudocode will work. The first thing in pseudocode is to check if the current node is already in map or not. If it is already in map, just take a return from it. If it is not in the map, you should create a new reference of it, store it in the map, and then traverse.