 What's up guys, we're going to go over another problem on spodge called bugs life. Basically, Professor Hopper is researching a sexual behavior of a rare case of bugs. He assumes that they feature two different genders and they only interact with bugs of the opposite gender. In his experiment, the individual bugs and their interactions are easy to identify because their numbers are painted on their backs. You're now given a list of bug interactions and you need to decide whether the experiment supports his assumption of two genders with no homosexual bugs or if it contains any some bugs interactions that falsify it. Basically, you're given the interactions between two bugs and you want to say if there's any homosexual bugs. If there's a relationship between two people of the same gender. Yeah, so that's basically what you want to see. If there's a relationship between two people of the same gender. Okay, so this is actually not a hard problem. If you understand what bipartite graph is, then you would be able to do this problem very easily. So basically a bipartite graph is just a graph where you have you could split the graph into two sets where they don't share a similar vertex and every vertex from one of the set connects to a vertex to another set, right? That's what a bipartite graph is. And I think I'll show you like a picture here right now. But yeah, that's basically what a bipartite graph is. So now basically, all we have to do is just check to see if is just read in the graph and check if it's bipartite or not. So one way to check if it's bipartite or not is actually just to run BFS breadth first search, and then just color every single vertex with its neighbor with its opposite with the opposite color, right? So with the neighbor, so like you're gonna what you're gonna do is you're gonna have like in this case, there's in this case, this problem we're trying to find like homosexual, non homosexual bugs, right? So what you could do is you could have like, you start out with a vertex with a bunch of you pick one vertex, you start out in your graph, and then you label this color, let's say H, right, for like, I don't know, homosexual. And then every every other neighbor that that's rounding your homosexual bug, you say it like, I don't know, G, I don't know, you label G. And then then then after that, you go to every neighbor, and then you color its surrounding area with the opposite color. Again, so you basically just keep you, you color one color, and then you color the surrounding neighbors with a different color, and then you basically repeat this color, there's one color, color the surrounding neighbors of different color. So then in the end, after all your vertices are colored, if one of them, if one of them actually has the exact same color of one of the same neighbors, if one of the same neighbors have like the same color, then it's not, it's the graph is not bipartite. So you can't actually, I can't actually have a bipartite graph. So yeah, that would tell if you could actually have a homosexual bug or not. So yeah, basically, that's basically how you solve this problem. I'll just show you guys the code now. So here's the code. This part, we have we have the main, and then we have the care, the, the variables input, and then the number of test cases. Okay, so what I did here is for every test case, I read in a test case, I'm going to read an N and M, and an N and M, and is like the number of vertices and M is the number of edges. So what I do next, I'm going to use an adjacency matrix, because for this current implementation that I did, I use a adjacency matrix for to check if it's bipartite or not. Because I think it's easier in this case, but yeah, you could use the adjacency list. But yeah, so here I have a a g is equal to a 2d matrix with the size n by n. That's what I do here. And then here what I'm doing is I'm going to loop through for every edge for number of edges, I'm going to read in x and y. So x and y are the two two vertices that we're reading in. And then these are the two vertices that we're going to connect edges to, right? So then here now what I do is I subtract x and y by one, both of them, because I'm actually going to index my adjacency matrix by zero instead, right? I don't want to index it by one. I'm going to index it by zero instead. So I subtract one from both of them. So now then to connect an edge from one vertex to another, I do g of x y is equal to one. So that says that there's an edge connecting between x and y. And I set g of y x is equal to one. So that connects a edge from y to x. Okay, so now once we have that, in this loop, I called solved, and I'm going to pass an I and I is basically just the the current test case number. And the reason why I do this is because in the actual output, you have to for every test case, you have to print it out if it's suspicious or no suspicious, right? In case if there's a if there's homosexual bug or no homosexual bug, right? So that's why I have to pass an I for the current test case number. Okay, so here I'm going to go in my solution. I solve method here. Yeah, so here, like I said before, we have to print out the scenario and then the number and then print out number and then a colon, right? So this prints out the scenario number, right? So now here, here's what I do. I have this Boolean called done is equal to true. And I set that in the first place to basically check if it's bipartite or not, right? And what I'm going to do is I'm going to actually go through every single vertex in my graph. So for every i zero to n, and I'm going to check if from this point on coloring it, is it bipartite or not? So then if it's bipartite, then I just, I just break, right? I break out. And then I just print out suspicious bugs are found. So that means, uh, yeah, wait, yeah, wait, okay, so yeah, yeah, wait. So here, if it's not bipartite, right, then I said done is false. And then break. So then that means no bugs were found, right? That means that there's no homosexual bugs that were found. Otherwise, I have to print out suspicious bugs are found. So that means that you can have a homosexual bug in this case. But yeah, that's basically what you do. And then I'll just show you guys the bipartite method. So here's a bipartite method. So here we have a the source vertex, it's long, long source, which is like where you're starting from. And then here what I do is I create, um, this is basically the size of a number of vertices, I create an array of color array, and it's filled with the size of number vertices, and I set it to negative one first. The so all the values in my array have negative one. Okay, then what I do is I color the first one one. So the first vertex here, I color it one, right from the source node node is one, then I create instead of doing recursively, I use a queue. So this is like breath research, but with a queue. So I use a queue, and then I push the starting vertex onto it. Then what do I do? I check while it's not empty. So if it's not empty, I'm going to take the first vertex in my queue, and I pop it and I remove it. That's what I do. Now if the, this is just like a base case. If you haven't, so this is a case if like you have an edge, if you have a vertex and it's connected to itself, then it's not bipartite because remember, a bipartite graph cannot have an edge connecting to itself, right? So that's where you have to return false. So this is if if the current vertex has another has an edge connecting from itself to itself, then return false. Okay, now this is what I'm going to do. I'm going to loop through all the values of my all the vertices, right? Then if there's an edge between you and V, right? So there's an edge, this basically is to check in the neighbors that go through all the neighbors. If there's an edge between you and me, what am I going to do? What I'm going to do is and yeah, and it's uncolored. So if it's a there's an edge between you and V, and the current current, current vertex is an uncolored, so equal negative one. Then what I do is I set my current color is equal to one minus color r at you. So the color at V is going to equal to one minus color r at you. So what does this do this basically saying like, if I if I'm at my current color of r u is one right, then the surrounding neighbors that I colored it is going to be zero. So basically, I'm just alternating between one and zero every single time when I'm coloring it. So then that's basically just going to color all the it's going to color the current vertex with one and then all the surrounding neighbors going to be zero. It's going to go to the next one, color it if it's zero colors color all surrounding one, so on and so forth. Okay. All right, then I have to push the current current neighbor into V, because I'll go to the next one, the current neighbor. So that's why I do q dot push V. Okay, so that pushes the neighbor so you could continue going forward. All right, now what I'm going to do is if if there if if I already colored you and V a color between you and V, and the color at V V is equal to the color at you, right? So if basically what's what's what's the saying is that if you have an edge between two nodes, and they're the same color, then it's not possible. It's false. So you have to return false. So yeah, and then that's basically the end of it and then just return true. So yeah, that's basically the gist of the code. Hope you guys enjoy this video. If you can't subscribe, I'll just check you guys later. Peace