• 1 day ago
Transcript
00:00Now we are going to learn about Graphs in Connected Components. Graphs is a group of many connected components. When
00:09we talked about Graphs, we had some kind of structure to visualize the Graphs, where one node from a lot
00:16of nodes
00:18connected. If I tell you what this is a graph, then you will say yes, this is a graph structure
00:24which is correct. After that, if I tell you another structure and I tell you what this is a graph,
00:30then you will say
00:32It looks like a binary tree. It can be a binary tree, but the binary trees are indirectly graphed. Yes,
00:40they are indirectly graphed. Therefore, this structure is also a graph. If I tell you what this structure is a
00:47graph structure, then you will say, yes, this is a graph structure. This is a graph where there is a
00:52single node available. Now, this is my graph 1. This is my graph 2. This is my graph 3. Now,
00:59I will ask you,
01:02This is a single graph. How do you think this was a different graph? This was a different graph. This
01:08was a different graph. This was a different graph. How do you think this is a different graph? This is
01:12a different graph because there is no condition in the graph. There is no compulsion. The components of your graph
01:21are connected. It can happen that you don't have a concept of connected components.
01:30It can be that these three structures are made by making a single graph. A single graph in which one
01:37part looks like, the other part looks like, and the third part looks like. It can be that this graph
01:42has two connected components, one component and one component. It can be that if there is a third edge here,
01:51it can be a single connected component.
01:54But here there are no two edges. Then we can call them a single graph, which is the edges of
02:01the edges.
02:08foreign foreign foreign foreign foreign foreign foreign
02:31I am traversing through any source. But it is not necessary that you have picked a source within a graph.
02:38You can traverse all the graph. Because if there are multiple components within the graph, this is a component, this
02:45is a component, this is a component. If I took it as a source, then I would say DFS. First
02:50I will cover it, then it will cover it, then it will cover it, then it will cover it, then
02:53it will cover it, then it will cover it, then it will cover it. But now I will cover the
02:58graph in two parts.
03:02So, when we write normal DFS or normal BFS, it is not necessary. If we start with single source, we
03:09traverse one node. It will be that we traverse a component and if there are disjoint components, we will not
03:20traverse them. So, what do we do in such cases? In such cases, existing code was exactly right. We were
03:29just small in that code.
03:30I will tell you how to use these small modifications. If we were to make a single function before DFS,
03:38then we will make two functions. What will we do in one function? We will put a loop in one
03:44function. Int i is equal to 0 to our graph.length. I mean, I will do my whole function.
03:53I will travel all my vertices until I travel. And my visited, which I had a Boolean type, I will
04:02make it ready for the first time. It means that I have my V-Size array ready for the first
04:10time and initialized the false values.
04:15In that loop, I will call my helper function every time. Helper function means that such a function that has
04:23no individual utility has no other function to call it. So this function will become my DFS function and I
04:35will call it DFS helper function. Helper functions usually
04:40It is a convention that we write the name main function and it will be util. This is my utility
04:46function, the extra helper function. What will I do? I will create Boolean or loop. If my vertex is non
04:59-visited, if my eye's visited is false, then what will I do? I will call DFSUtil. And inside DFSUtil,
05:08I have written the original code of DFS, which I have already written, I will write the code. This is
05:14the advantage of doing this. I think there are two parts in the graph. One is a part of this
05:20kind and one is my part.
05:22Here is this part. These two parts are forming a single graph. Let's say that I have started from the
05:27beginning of the source. So I have traversed this node. So how do I take this node as a source?
05:33I will run a loop that will start from 0 to the length of the graph. Let's say that this
05:37node is 0, this is 1, this is 2, this is 3, this is 4, this is 5, this is
05:416. If I run DFS, first of all, we will see who visited falls. First of all, there will be
05:46visited falls. So what will I do?
05:49I will give you true and visit it. After that, I will call DFS Util. DFS Util is performing its
05:56entire DFS. As it will perform DFS, it will also be visited, it will also be visited, it will also
06:02be visited, it will also be visited, it will also be visited. In DFS, there is a normal DFS function
06:07which will visit my entire component. When it comes back, you will see that it will also be visited, 2
06:13will also be visited, 3 will also be visited.
06:19DFS is false because it was not able to visit the last time. So what do we do? We will
06:23call DFS again for our second component. I will call for the second component, then we will visit 4, 5,
06:316. In this way, my second component will also be visited. And in a similar way, if I have multiple
06:38components, then I will call multiple sources so that DFS can visit all components. And this is the same thing.
06:49that my normal function will be BFS, which I will take Boolean Visited and run a loop. And I will
06:56write a helper function within BFS, which will be my main BFS. If I have small parts in my graph,
07:07then I will traverse that whole graph. Why? Because I have run a loop and I have tracked the Visited
07:15first.
07:17And we will not only do these questions in DFS or BFS, but we will do many questions. So now
07:22we will get to see the questions inside the questions. Usually we run a loop and then call helper function.
07:30So the logic behind the helper function is that there are different connected components inside my graph. And my work
07:37is that I can reach all parts of my graph and traverse all parts. So what do we do in
07:43our old DFS and BFS code?
07:57function function function function function function function function function function function function function function function function function function function function
08:00function function function
08:11This is the same graph. And the work of this function is to create Visited and track all components. So
08:17these Boolean Visited I have created here, I will actually remove it from here and define it in BFS and
08:24pass it here as a parameter. So here I have Boolean Visited.
08:29So this array has come to me and I will use this boolean visited in my entire BFS. So in
08:34this way, my BFS util code will be small. And in my BFS function, I have defined visited. Now I
08:41will run a loop for int i equals 0, i less than mygraph.length, i plus plus. Every time I
08:47will see if not of visited of i, meaning if my visited is false code, so in that case I
08:53will call bfs.
08:55I will pass my graph and my visited. So what will I do with BFS Util? Within my visited notes,
09:03which are connected to my source, I will give them to visit and then I will see the next Unvisited
09:08for the next component. So I will divide my BFS and the same way we are dividing DFS. So this
09:15is my DFS code. Now I will make DFS Util code. And here I will also call DFS Util.
09:24So this will become my utility function. And now I will make a main DFS function. Public Static Void DFS.
09:31What will be in the DFS? I will have a graph which will traverse the whole way through DFS. Now
09:39what am I going to do? I will make a visitor here. I will say boolean visited is equal to
09:45new boolean of my total number of vertices which I will have graph.length. And I will run a loop
09:53for int i equal 0.
09:55I less than, until we don't traverse my graph, I++. Every time I will call my DFS util, which I
10:02will call? Graph, or my current will become my I, and I will pass my visitor. You will see, these
10:08lines in the DFS are the same code, and the lines in my BFS are the same code. So what
10:14do we usually do? Traverse first, or we have to traverse through all the different parts of graph. So we
10:20will create this outer function, and we will create our main function code.
10:25we transfer our main work into the utility function so that we can traverse all the different parts. So in
10:31the next few questions, we need to observe the same thing.