 What's up guys, my name is Michael and welcome to my YouTube channel today. We are going to go over We're gonna go over this Problem on spodge called is it a tree? So you're given an undirected graph and you need to write a program that to check if it's a tree topology Okay, so first let's actually read in the graph because that's Yeah, so if you don't know what a graph is just like Hold up So if you don't know what a graph is it's basically like a bunch of circles connected by a bunch of edges Right, so this technically this is a graph technically right and it's it's like If you like labels if you go on like a map Right, and you just label all the places you see with like circles and then for each of them There's like roads right for roads and like sidewalks. You just connect like your houses I don't know places that you need to connect your houses to or stuff like that you connect them with an edge That's basically what a graph is like this technically is a graph. So we need to check if it's a tree or not So given the graph Topology of these nodes. We need to check if it's a tree or not. Okay, so before we even do anything of that There's two ways to represent graphs adjacency list and adjacency matrix We are going to use an adjacency list because that's faster Okay, so let's actually just read in the graph So we have the input of the graph and then we could check if it's a tree or not And I'll go over what a tree topology is so So we read into integers n and m so n is a number of nodes m is a number of edges in the graph So and then the next m lines contains each of the pairs UV which means there's an edge between the node you and V So what does that mean? So that's going to mean this We read in n and m so Whoops N and m right and then this means that it's the N and m is the number nodes m is number of edges. So now I'm going to go through All edges and I'm going to read in a UNV Okay, and then UNV it just represents like each node that's connected to it. So here you and V. Okay, so now Yeah, we need to push this into an adjacency list so actually let's do that Yeah, I'll call this adjacency list. So each Each UNV is like each of these small circles and then adjacency list means that they're connected to each other So if I had like a list of I don't know Adjacency list just means it's like that all the neighbors of it So if I had if this was I don't know I've had number three, right? All right, and then this was four and five and six I don't know your adjacency list would be like three and it's going to have all the list of neighbors, which it would be like a four five and six Right because it's three and then the other neighbors four five and six. Whoops. I didn't circle it But yeah, that's basically what it's doing. Okay, so we're gonna an adjacency list is just that's just what it is, okay? So here we have adjacency list I'm going to do n plus one Right because I want to So n plus adjacency list is just like a list of the all the nodes, right? I'm gonna index it from one because I think It makes sense in this input statement because I don't want three to be to relate to two and one actually relates to zero That's too confusing. So I'm gonna index it from one. Okay, so that's why I do n plus one Okay. Now at this point, I'm going to do adjacency list at you which is going to be the The Yeah At you, which is going to represent the the value at you right and I'm going to push back So this is going to push back the node of the neighbor B. Okay. Um, so we know it's an undirected graph I think I think it's undirected undirected. So I think undirected means you could go both ways if I recall Let me just see undirected graph Versus directed. I think that's what it means Undirected graph dude does not have a direction. They could it go both ways two ways. Yeah, so yeah, so that means that the neighbors of you The node at you right its neighbor, we're gonna add a V V as its neighbor, but we also need to add V of its neighbor push back to have node you okay, that's basically what we need to do. Um Is that the gist of it? Let's see I actually should set these to have empty arrays actually Is this this work Oh, yeah, wait No, I don't have to initialize that. I don't think so. Okay. So now now we have this Now now we have the input graph Input it into our result. Remember long long just means long L. I'll just means long long But yeah, now we have the input graph. So now now we need to check if it's a tree topology or not So what is a tree? A tree by definition means that you have one root. So that means it's like If you were go back to here So a tree would mean like this would be like a tree. It's kind of like a Christmas tree. So there's like a starting root This is gonna be the root and then if you keep going down each of them has only like one parent. So Yeah, so the tree contains a single root That's the root and then we say the node P is a parent of you if you could reach from it, right? Similarity and as a child of P. Yeah, so this is just same definitions each note except the root have must have one parent okay, so this means that I cannot Connect both of these together. So like so this is this can this is a tree, right? We call this a tree I cannot connect this and this right because that means this child would have two parents here and here So that's not illegal. This is not allowed. So So because it's not allowed this is actually going to be this is actually going to be We have to make sure that it's not allowed. Okay. Um, so yeah Also, all the all the nodes have to be connected, right? So I can't just have At the end, I can't just have this a node right here I'll circle right here where it's not connected to any of them that I can't go because that this is just not connected, right? So, um, yeah, they have to be connected. So I have to be able to get from this root down to here and In this case, this would not be a tree because it's not connected but yeah, that's what we basically have to do so All of them has to be connected. Yeah, so let's actually go over the algorithm to how to check if it's a tree or not So right down here Okay, so over here on directed graph to check if it's a tree or not We're going to go to DFS run DFS and go through from any node to make sure that each node has exactly one parent so we have to check if all the nodes have exactly one parent because that that would If it doesn't it would violate the for the second rule of our tree that everything every every node has to have One single parent. So we have to run DFS on the node and check if it has a single parent right if not then Your turn falls Although also we have to check if all the nodes are visited So if the DFS if you can't visit all nodes and it's false. Otherwise, it's a tree So, yeah, so we have to visit all the nodes. Okay Yeah, all right. So, how would you do this? This is kind of the algorithm from CLRS So and it's another nodes. We're going to turn tree if it's true or false. Okay, so here Undirected DFS you have to visit all of them. If one of them is false, then it's not possible. Okay, so visit all the nodes then here Yeah, here you have to be able to visit all of them Yeah, actually, this is the this undirected DFS checks if it's there's a parent exactly one parent. Okay Called DFS pass one and pass a parent negative one indicate doesn't have any node will pass it true or false value Okay. All right, so I have not actually coded this before But let's actually do this Okay, so we're gonna actually code this algorithm from CLRS Okay so undirected DFS Let's see. So Okay, so what we're gonna do here is we hold up what do you want is print yes if it's a tree otherwise No, okay, so here I'm gonna print If it's is tree I pass in my agency list Then I print yes Else I print no Okay So is tree. Yeah. Oh wait, I think it has to be uppercase. Okay, so this has to be uppercase So a yes, and here we got a lowercase. Okay, so now what we're gonna do is that we're gonna create a function called Industry and here we're gonna call we're passing our agency list Okay, this is our agency list Pass by reference. Okay, so now we have the JCC list and let's go back to the algorithm So we're gonna run undirected DFS passing the current node And then a visited If you visited if it's false then we return false and yeah, so okay, so here we're gonna call Yeah, okay, so Visited it was false and we're gonna run if undirected directed DFS Actually, this should just be called has one parent Because that makes more and more sense. Okay, so if all nodes has one parent, okay That's what this function should return because I Don't know why why they're doing it this way. Okay. Um, if it's false then we return false Otherwise we need to visit all the nodes So here we're gonna do Yeah, so here we're gonna go from one to list dot size Minus one So this is this gonna be n. Oh crap. Do we have maybe we should pass an end? number of nodes and Plus one minus one. Yeah. Okay. So this is gonna go through One to n which is number of nodes Remember we indexed it from one. So that's why I have to do minus one to get number nodes Okay, so while we're going through all the number of nodes What do you have to do now oh? Yeah, so if it's If uh, if I didn't visit the node had I then I return false. Okay, so this is just Otherwise return truth. So this is just making sure you could visit all the nodes here. Okay. Um, so now let's actually go through has one parent Maybe we should do like I don't know. Okay, whatever. Okay, so here. Oh, oh, yeah, I forgot this should be a Boolean Representing all the nodes. So this should be a Vector Boolean The n plus one Okay, um n Let's see Maybe I should pass an end the number of nodes Yeah, I'm gonna pass an end The reason why I'm passing an ends is so that it doesn't get too confusing. So here I'm gonna pass an end Okay, so that's a number of nodes Okay, so we're gonna go through the number of nodes So visited if if I didn't visit a node I returned false So that means I visit all the nodes or visit all the nodes. I'm gonna return true Okay, so all notes if it's all nodes has one parent is false. So that means They don't have one parent then I return false. Okay now They say here Okay, so we start at one. So they're saying to start at one. Okay, so we're gonna start a node one Negative one, but the thing is we don't know if Do we know if Here's a problem. We don't know if it has if it all nodes start from one, right? Give a B between I don't know. Okay Anyway, um, so yeah node one they also said parent start the parent as one Haven't done this problem before guys. So I'm just telling you okay start one node one parent check Parent give parent negative one and then visited pass and visited. Okay, so I pass and visited Visited. Okay. So now here we're gonna create another function all nodes has one parent and Crap, I have to pass in the adjacency list also there, okay, so now So you have the current node that we're on we have the parent then we have the visitor array The nodes that we visited Maybe I should make this into like a global variable. I don't know We have our JCC matrix our JCC list Okay, um, what else do we have? You see lists Um Should we pass it in? Let's pass it in anyway. Okay. Why is this not working? Oh, what the what the? Oh Well, what the yeah, okay here now we're passing in. Okay. Okay. So now we have the list of nodes Okay, so now while we're going through DFS. What are we gonna do? We start the parent of the node if it's visited DFS not It's visited is true. Then we're gonna return false. What what oh if you already visited Yeah, yeah, okay. So if you already visited the node, you can't visit it twice. That means that the parent is There's no parent. There's two parents, right? So that means that Yeah, then you have to return false. So like if there's two parents, let's say I have node right here, right? And I started from the root and I went to here and then I went to here So I already visited so this is visited then I start from root. I went to here and Then I went to here again So this is visited twice. So if it was visited twice, that means there's two parents This parent this parent is two parents. So you can't do that. You cannot have that. So yeah, so that's why If you already visited you just returned false. So if current Visited at current node is already visited Then we return false because that means not possible. Okay Okay What else do you do? Uh? Otherwise, we're gonna set it to true. So we're gonna set whoops We are going to set that the current node to true. So that means we just visited. So we're gonna visit it Let's go through now. Let's go through every neighbors. Okay. Let's go through all the neighbors For every For every child Okay, okay for auto for every child of adjacency list Let's get all the neighbors of the current node so every child of all the neighbors of the node, right? So remember we stored it as a adjacency list. So each each index of the 2d array Represents the neighbors of it. Okay? so for every child of it if it's not equal to the parent so Um, it's not equal to parent It's not equal to the parent we're gonna run the result Okay, so we get the result of Dfs, okay, so if it's not equal to the parent. Ah So if it's not equal to the parent we are going to pass. We're gonna call it again So we're gonna call DFS again on the child. So we're gonna call Maybe we shouldn't call make it how all nodes has one fellas called DFS actually Because that's basically what it's doing. It's doing depth first search. Okay. Um, yeah Okay, so And I'll explain the code again So if it's So if the current child is not equal to the parent What we're gonna do is we're gonna call we get the results of DFS and we're going to call this on the child And Current node. Okay, so we're gonna call DFS on the child Passes visited passing just as a list passing and Technically, why don't you need in? Maybe I could get rid of that. I don't know If it's false return false. Okay, so if it's false return false Result is equal to false and we return false at the end return true Okay, so that's pretty much the gist of it We don't need n so we don't need the number of nodes for some reason because we have the jc list So let's actually get rid of that Yeah, we don't need number nodes Okay, my only issue with this is that starting at one and Yeah, what if there's no node at one wouldn't still be a graph though I'm saying all nodes are not connected or graphs. I don't know. All right, let's just see if this works Okay, let's run this 321223 321223, yes, okay, so that's right. Let's just I should actually just submit this now So Go here. Let's see if this works It's running running running running Running AC yeah, so yeah, that's basically just how to do this problem I'll go over it again. So essentially what we're doing is we're making sure that all the We start from a root here and we're making sure that we visit all the nodes just once so um to do that we go through the This we started a route and we go through its child. So we're going to go through this child right and then um Then we're going to go through its children. So that's first child right here So it's going to keep going down to one of its child children and then yeah, then um Once we're done visiting one of its children It's also going to go to this child as well and then it's going to keep going through every single of its child If we reach a child where it's visited twice by a node So like if there's two nodes here two parents visited twice Where this is visited and it was visited again. We know it's not a tree because there's two parents Right. So if it was visited twice There's two parents. So it's not possible. So that's that's why we return false At that point. So that's the reason why over here Over here we would return um false Yeah, so if if the current node if it was visited if it was already visited at the current node we return false That's right. We have here. Otherwise, we're just going to Set it that we visited this node to be true So that's going to mark this node true. This node is true. This node is true, right? So that's going to mark the nodes as true Afterwards what we're going to do is we're going to call Dfs we've gone through all its children So remember we have to go through its children and go through all of its children So that's what we have to do. So we're going to go through all of the its children of the nodes, right? We need to check if all each of the children is not equal to the parent. So if this These children right if they're not equal to the parent then we're going to run Dfs to it again, right? So to do that, we're going to run Dfs Again, so we're going to call On its children as again for each of these children, right? and we're going to pass in um Each uh, run Dfs on each of the children right here with that's what this does and we're going to pass in um A node representing the parent so that the parent is now changed to the current node Which is going to be parent for that um Yeah, so if if it actually returned false Then we just returned false if this result of the Dfs Where we didn't visit we visited the child twice then we're going to return false So that's why we do that here. Um in the end. We just returned true. Yeah, so that's what that does Okay, so after we run Dfs and if it's not equal to false we have to make sure we visit all the nodes So if any of the nodes was not visited we returned false So this is going to go through all the nodes and making sure that if you haven't visited one You're going to return false at the end. Otherwise we return true So that's pretty much just how to check if if it is a tree or not Hope you guys understand this video Right cop subscribe. I'll check you guys later. Peace