- 7 hours ago
Transcript
00:00Next, we are going to study the Bipartite Graphs. First, we will understand this word by Bipartite. It seems very
00:09strange to hear this word, but I am going to throw it in small pieces. One will be by, one
00:16will be partite. By means two. Partite means parts, or partitions.
00:24Bipartite Graphs are such a graph which you can talk about in two circles. In two circles, I can talk
00:32about this graph that I can take any edge from the U to the V. If I take any edge
00:41from the U, I can take any edge from the U to the V. If I take any edge from
00:47the U, then I can divide my graph from the U.
00:54We will come in the first part and the V nodes will come in the second part. And I just
00:59need two sets of nodes that I can divide the whole graph in two parts. The definition says that all
01:09the vertices I can divide in two independent sets. Independent sets that are not dependent on the other one. There
01:17will be two independent sets.
01:30We need to divide U and V to every edge from U to V
01:39If you have a node connection here or if you have a node connection here or if you have a
01:44node connection here or if you have a node connection here or if you have a node connection here or
01:54if you have a node connection here
02:05or in this set, or in this set, or in this set. If any edge connects two nodes, then they
02:12should not come in the same set. I always have to make two sets. In this way, we can say
02:19that any edge, its connecting vertices will not come in the same set. So the same thing is, if I
02:25take any edge of my graph, then U and V should not come in the same set. Then we should
02:30come in different sets.
02:34In sets, I can break my graph in two pieces. If we can break my graph successfully, I can say
02:42that my graph is a bipartite graph. I have to return to true and if I can't break my graph,
02:49I have to return to false. If I can't break my graph, I have to return to false. If I
02:52can't break my graph, I have to return to false. First of all, I will talk about my first graph.
03:03I will consider this edge. In this edge, I will consider U and V. I will add 0 and 1
03:10to each set. Then I will add 0 and here I will add 1. After that, I will come to
03:16my second edge. In this edge, I will add 1 and 3. I will add 1 and 3. I will
03:22add 1 here. Then I will add 3 to the other side. Because I will not add the same. Where
03:28will 3 go? Where will 3 go?
03:30Now I am going to my third edge. I will consider this edge in which I will add 2 and
03:383. I will add 2 and 3. Then I will add 2 and 3. Then I will add 2 and
03:443. Then I will add 2 and 3. Then I will add 2 and 3. Then I will add 2.
03:51Then I will add this edge to this edge. I will add 0 and 2.
04:05So I can successfully say that this graph is a bipartite graph.
04:27each vertex which forms u and v in each edge is different sets and these are independent sets so this
04:35is a bipartite graph if I get this graph then I have to return true now let's talk about your
04:41second graph
04:42This is my second graph. In the second graph, first I will consider this edge. I will add 0 and
04:461. Then I will add 1 and 1. Then I will add 1 and 3. I will add 1 and
04:523. Then I will add 1 and 3. Then I will add 3 and 4. Then I will add 3
05:00and 4. Then I will add 4 and 2.
05:054 already done, I will add 2 in the second set. Now, the last thing I will talk about is
05:100 and 2. I will remove the rest of them. I want to add 0 and 2 in the second
05:15set. So, what will I do? I will see if 0 already done, 0 already done. Then, I will see
05:21if 2 already done, 2 already done. Where are they? Both are put in the same set. If both are
05:28put in the same set, then my condition is false. Plus, I will add 2 in the second set.
05:38So I can say that this is my non-bipartite graph. This is my non-bipartite graph. In this case,
05:47I will return false to this graph.
06:04So, this is non-bipartite and this is a bipartite graph. Now, let's see. We will use a very interesting
06:10algorithm to identify which graph is bipartite and which graph is not bipartite. For this interesting algorithm, we will use
06:20graph coloring. What I will do? I know that I have to take a set 1 and a set 2.
06:27So, I assign a set 1 to a color. Let's assume that the set 1 is yellow.
06:32and the color of set 2 is blue so in this way the nodes will be yellow automatically set 1
06:37and the nodes will be blue automatically set 2 I can say that if I gave a node 0 value
06:45that means its color is yellow and by default I gave a node 1 value that means its color is
06:50blue and if its value is minus 1 that means its color doesn't have any color in the beginning of
06:55all the nodes
07:00So what will I do? I will perform BFS and I will start giving all nodes all the nodes. Let's
07:06talk about this graph. In this graph, I first came to my 0. What did I do? In the beginning,
07:15everyone has a minus 1, minus 1, minus 1, minus 1. What will I do? I will give 0 to
07:21my 0. I will give 0 to my 0. I will give 0 to my 0. I will give 0
07:25to my 0. I will give 0 to my 0.
07:30Now I will come to the next node. Next node is 1. What do I need to do? I need
07:36to give opposite color. I need to give 1. I need to give 1.
07:46I will give the next node to the next node. I will give the next node to the next node.
07:50I will give the next node to the next node. I will give the next node to the next node.
07:52I will give the next node to the next node. I will give the next node to the next node.
07:57I will give the next node to the next node.
08:00So now I can see that when I go to 1, I already have a different color. So what do
08:04I do? With the help of the two colors, I have divided my whole graph into two parts. Can I
08:09say that the ones I have given the yellow color, they were 0 and 3, and the ones I have
08:14given the blue color, they were 1 and 2. So I have divided the edges of the edges into two
08:21parts, which are independent sets. So this is my bipartite graph. Now I will talk about the second graph.
08:30Here I can start with 0. What did I do at the beginning? I have also minus 1, I have
08:35minus 1, I have minus 1, I have minus 1, I have minus 1, I have minus 1, I have
08:38minus 1, I have minus 1, I have minus 1, I have minus 1, I have minus 1, I have
08:39minus 1, I have minus 1, I have minus 1, I have minus 1, I have minus 1, I have
08:40minus 1, I have minus 1, I have minus 1, I have minus 1, I have minus 1, I have
08:41minus 1, I have minus 1, I have minus 1, I have minus 1, I have minus 1, I have
08:53minus 1,
09:01Now I have to see that the next node of 2 should be blue because each edge should be a
09:08different color if it is bipartite. But what happened here? This color is already yellow and I have to give
09:16it yellow. So here I have a clash.
09:22Already color exists and the color is not opposite, it is the same color. So if neighbor exists before the
09:29same color, then my graph is not bipartite. So I have to return false to this. I am going to
09:37use graph coloring. I will start with the first notes. I will color one note. As I have colored, I
09:44will check my next neighbor. So neighbor has 2-3 conditions. Let's check these conditions.
09:50I will summarize that we will do a BFS-based technique in which we will color the nodes. There are
09:56three possible colors of nodes. First, minus one, which means that no color has not been given. One, zero, which
10:03means that we have given yellow. One, one, which means that I have given blue. So, when I created visited,
10:13I created a color. I will store it in every vertex.
10:22If I have this graph, then I will create a color in which I will create color. What are the
10:28conditions? If I have already given a color, if I have given a color, then there will be three cases
10:40in the neighbor. What is the case 1? The case 1 says that the neighbor has already color.
10:46Which color is? It has the same color. In this case, this is not my bipartite graph. If the neighbor
10:54has already color and the same color exists, it means that I can't divide in two parts. What should I
11:01return here? I can directly return here. False. Let's talk about case 2.
11:07Case 2 says that the neighbor already exists a color in your neighbor, but it is a different color. I
11:12wanted to say that if every neighbor has a different color, then I don't have any action in this case.
11:17It means that it can be a bipartite, it can also be a non-stop. But this is my continue
11:21statement. I don't have any concrete action. I don't have to do anything. Then my case 3 says that the
11:28neighbor says this is N, not H. I don't understand H. This is N.
11:35neighbor doesn't have any color that exists. So what will I do? I will give it a color and I
11:41will give it a opposite color. So I will traverse my notes in BFS fashion and I will give it
11:47a color. If someone doesn't have a color then I will give it a opposite color and add it in
11:52the queue. If someone already has a color then I will traverse it. I don't need to travel again.
12:00As we start, what do we do for DFS? Usually we create a Q, so why do we create a
12:05Q? Because this is my front part and rear part, I started with 0. Here we have the same funda
12:12here. What is it? First, I will create a main function like DFS and a utility function. In that way,
12:20I will create a proper is-bipartite name and is-bipartite
12:28I will also make a function of Util. I will also make my main work. What will I do in
12:33this? I will also make a loop and make a visitor. Visited is not necessary because it is BFS. We
12:37will also make a visitor's work. But here I will also make a loop. So if I have another component
12:45with this graph, then I will also check if it is bipartite. If this graph has multiple parts, then all
12:52the parts are bipartite.
12:56That's why I always use a loop for most of the graph. That's why we use a utility function. So
13:03I have to use all the things that we are looking at in the code. I understand the main logic.
13:08I have to use the main logic. I have to use the main logic. I have to use the main
13:09logic in the main logic. I have to use the main logic in the main logic. I have to use
13:13the main logic in the main logic. I have to use the main logic in the main logic. I have
13:14to use the main logic in the main logic. I have to use the main logic in the main logic.
13:23I have to use the main logic in the main logic.
13:24Initially, what is the color that is initialized with "-1". That means that nobody has no color. I will start
13:33with 0. Before we push our source to our queue, I will push 0 to equal. If there is no
13:45color, I will give it a yellow color.
13:52First of all, I will remove 0 and check the neighbors. Who is the neighbor? The neighbor is 1 and
13:58the neighbor is 2. What happens in BFS? Bread first search, so I will see all the neighbors as well.
14:06I will see 0 and 2. This is the question. That's why BFS and BFS have been studied earlier, because
14:12their applications have a lot of questions. There will be a lot of questions in which we will modify BFS
14:18or DFS.
14:23so I will talk about 1 and 2. What will I do for 1? There is no color assigned for
14:301, its value is minus 1. So I will give 1 to its opposite color. If 0 is 0, then
14:37I will give 1 to equal 1.
14:39I will give 2 colors as equal to 1 because this should be blue and this should also be blue.
14:43I am seeing all my neighbors in BFS. If my color is already decided, then my neighbors should be different
14:50in the bipartite graph. So I will remove 1 and 2 from here and assign them to 1 as equal
14:57to 1. I have given 1 and 2 from here. I have given 1 value in blue. Now I will
15:03push 1 and then push 2 from here.
15:05Now I will remove one. I will check the neighbors for Q.
15:11First of all, I will check for 0. I will check for 0 and see if it has already a
15:16color. Which color has already a color? It has a opposite color. In case 2, my neighbor has already a
15:23color and I have a different color. In case 2, my neighbor has already a color and I have a
15:25different color. In case 2, I have nothing to do with that. I mean, things are safe, situations are safe,
15:29because it should be different colors. Then, I will check one of the other neighbor. One of the other neighbor
15:34is three. Three of them has no color. If you don't have color, I will assign a color.
15:40and push in Q in Q. Opposite color assign plus push in Q. So 3 has no color. What was
15:49my color? What was my color? What was my color? One color is blue. So 3 will be removed from
15:55here. Yellow is equal to 0. And 3 will push in Q in Q. Then I will remove next element
16:032.
16:04Let's look at the two neighbors. Who's neighbor? Two's neighbor is zero. What color is zero? Yellow color and two
16:11has one color. This condition is already colored but different color. This condition is completely safe for me. I don't
16:19have to do anything. Now, two will look at the second neighbor. Two's second neighbor is four. What will you
16:23do?
16:24For 4, you will see that there is no color assigned to it because there is value minus 1. So,
16:29we will give opposite color and push it into it. For 4 to give opposite color, it means that if
16:342 was blue, then I have given the color for yellow. So, I have store it here. I have zero.
16:41And I will push 4 to my Q. Then the next Q will be 3. As the Q will be
16:473, you will check your neighbors. If you check your neighbors, you will see 1. What is 1 already color?
16:55What color is 1? Blue color. What color is 3? Yellow color. This is a safe condition. This is my
17:02case 2. Then we will see 3 and 4. What color is 4? Yellow color. What color is 3? Yellow
17:09color.
17:33that is non-bipartite.
17:38How much time will I have to do this? I will have time to do big of v plus e
17:42because I will modify my BFS in one way. So if I have modified BFS, I will have time complexity.
17:49I will have space for big of v because I have used a Q and this color array with it.
17:57So it will be big of v and it will be big of v.
18:00So the two big of v and big of v will be my space complexity in the code. So we
18:05are going to write our modified BFS to find out if my graph is bipartite or not. What did I
18:10do here? I have taken my graph. This is the same graph that I have done on the dry run.
18:16For the same graph, I need to have false print. To create this, I already have a code where I
18:22have connected all the edges. I have not taken any weight, which I have made in the edge class. I
18:27have taken only source and destination.
18:29Now I will make my function. I will name public static boolean type. I will name is bipartite. I will
18:43have a graph. Array list of edge.
18:52I am going to check for this graph if this is bipartite or not. So first I have to make
18:56a color name which I will initialize with minus 1. So in the beginning of all the vertex which will
19:03be color, that is equal to minus 1. So I will create int color is equal to new int of
19:11graph dot. I will run a loop which will initialize int i is equal to 0 and i less than
19:21color dot length i plus plus.
19:26I will give the value of the color of the eye to equal the color of the eye or minus
19:311 to equal the color of the eye to equal the color of the eye to equal the color of
19:33the eye to equal the color of the eye to equal the color of the eye to equal the color
19:33of the eye to equal the color of the eye to equal the color of the eye to equal the
19:34color of the eye to equal the color of the eye to equal the color of the eye
19:55So I will run a loop for int i equals 0 i less than my graph.length i++. I will
20:04check every time that my node has not given color. So if my color of i equals equals minus 1.
20:14I will run my inner loop and perform BFS. This is my if statement.
20:21Then I will perform BFS when the vertex is not assigned to any eye. I will visualize it a little
20:29bit. I have a single graph in a single graph. My graph looks like this is 0, this is 1,
20:41this is 2, this is 3, then I have 4, then I have 5 and here I have 6.
20:46So what will I do? If I have made my color array, then I will have a lot of indexes
20:51from 0 to 6. But as I know that I didn't have color assigned to 0, then what will I
20:59do? I will have color assigned to this case. And I will perform BFS first for this case. After that,
21:05the BFS has been performed, it has returned true. But now my graph is not bipartite. What will I do?
21:12I will check the rest of the other parts.
21:14So what will I do? I will see if the 4 has been assigned to the 4. Now I will
21:20see if the 4 has been assigned to the 4. So I will see if the 4 has been assigned
21:28to the 4.
21:28After that, if you know that it is bipartite, then I will check for 6. What is this node and
21:35graph? What is this bipartite? When I give 3, then I have to return true and false. If someone has
21:42given false, then I will return false. That's why I have an outer loop so that if I have multiple
21:48parts in the graph, then I can see all parts. If anyone has a starting node, then I can check
21:54all parts.
21:57So what do I want to perform BFS? First of all, I want to add the source and node to
22:04this node. I will start with this i-th node, which has no color. When I want to add it,
22:09I also want to give it a color here. By default, I want to give it a color here. You
22:14can also give it a blue. You can also give it a pink or green. You can also give it
22:22a gray or white. I can also give it a color here.
22:27Now I will run a loop. The if statement is going to be a BFS. It was external logic for
22:35coloring outside, but internal logic will show you a BFS. While my q.is empty is false, I will run
22:44the loop. First I will remove the current element from q.remove. First I will remove the q.remove.
22:56Then check the elements for that element I will check all the neighbors I will call for int i equals
23:030 i less than my graph of current dot size i plus plus i is equal to graph of i
23:14is equal to i is equal to i is equal to i is equal to i is equal to i
23:18is equal to i is equal to i is equal to i is equal to i is equal to i
23:20is equal to i is equal to i is equal
23:27I will find my neighbor. I will find my neighbor's destination. I will find my neighbor's destination. I will find
23:35my neighbor's destination. I will find my neighbor's destination. I will find my neighbor's destination. I will find my neighbor's
23:38destination. I will find my neighbor's destination. I will find my neighbor's destination.
23:49The third case is that the neighbor has a color, but it is the same color. I can directly return
23:53false because there is a false condition. Now, the graph is not bipartite. First, I write my case 1. If
24:02my color of e.destination neighbor's color is minus 1, what do I want to do in that case? I
24:09want to remove my color. What should I do in that case? Or I want to remove the other one.
24:14Now, what should I do in that case?
24:16So I take next color. What should be next color? If the color of current value is 0, if the
24:24current node is 0, then the next color is 1. No, then the next color is 0. This is a
24:29very logical thing. Or I have used the ternary operator. If the current node is 0, then what is the
24:36opposite? We will give it 1. And if it is 0, then obviously 1. Then what is the neighbor? We
24:42will give it 0. Then I have removed the next color.
24:45Now what I will do? I will assign the color of e.destination to the next color and q I
24:52will add the e.destination to the next node. So I have added the next node to the next node
24:57to the next node. What is the other case? If the color of e.destination already exists and its value
25:07is equal to my color. So what I will do? I will see with the color of current.
25:14If it is equal, we will return it from here. This is the condition in which your graph is not
25:21bipartite. I will return it directly to false code. This is the code for my BFS. What do I have
25:31to do here? I will return it to true. If there is false return, this means that my graph is
25:37bipartite, so I will return it to true value code. Let's save it.
25:42I will call the main function. I need false print this graph. I will call this bipartite for my graph.
25:51If I want to make a bipartite, what will I do? I can remove this cycle. By default, the graph
26:01will always be a bipartite graph. I will write it. We will summarize it.
26:12If graph doesn't have cycles, by default it is a bipartite graph. I will delete the cycles of 4 and
26:223. I will delete the edge from 4 and 3. I will delete the edge from 4 and 3. I
26:27will delete the edge from 4 and 3. I will delete the edge from 4. Let's save and run. In
26:33this case, it has returned true. Now it is bipartite. What did I do? I checked the bipartite. How did
26:39I check the BFS?
26:41I have run an outer loop for all components. Now I have seen that it has not been assigned color
26:45and then I put it in the queue. Then I have run a loop for BFS and then I have
26:52to check the color conditions and reverse it. This code turns out to be v plus e which is the
27:00same as my BFS time complexity.
27:02Now I will summarize some small things. I have checked the bipartite graph. I can also check the code in
27:11the help of the graph. If my graph is acyclic, which doesn't exist in the cycle, it will be true
27:21by default for bipartite.
27:24which is not a cycle in the graph it will always be a bipartite graph. What is the logic behind
27:29it? It is a simple logic. I have many nodes but there is no cycle. What can I do? If
27:36I give 0, then I give 1, then I give 1, then I give 1 and then I give 1.
27:43This way I can give alternate colors and it will never be the same because there is no cycle.
27:49condition is that if a cycle exists but there are even number of nodes such as 0, 1, 2, 3.
27:57In this cycle, the number of nodes are equal to 4. In the second case, I will take odd cycle
28:03which I have 0, 1 and 2. I have n is equal to 3. This is odd and even. When
28:10the cycle exists in the even cycle, it is true.
28:18If a cycle exists, then it is a bipartite graph. Here I can give 0, here I can give 1,
28:23here I can give 0, here I can give 0, here I can give 1, here I can give 1.
28:26But when the odd length comes, it is always false. My graph is not bipartite. If I have started with
28:320, then I can give 1. Here I have to give 0 again. But if this will clash with 0,
28:39then this will be my non bipartite graph.
28:41So we can write another code. I will give you some homework problem.
28:49We already know how the cycle detects in the graph. We already know how the cycle detects in the graph.
28:53We already know how the cycle detects in the graph. If it's undirected graph, then the cycle detects in the
28:57graph. Then I have to find out the length of the cycle. You have to have a little logic to
29:02track the length of the number of nodes. If it's even length, then it will be true. If it's even
29:09length, then it will be false. But if the cycle is false, then it will always be true.