• 1 day ago
Transcript
00:00Next time we are going to talk about graph traversals. We have already learned what happens on any data structure.
00:06If we travel, we call traversal. We have talked about three traversals. There are four ways to traverse. One is
00:15our level order traversal. One is our pre-order. One is in order. And one is post-order. There are
00:23two ways to traverse. First is our breadth-first search.
00:29This we call BFS and Depth to First Search. This we call DFS. So first we will study BFS and
00:36then we will study DFS. BFS and DFS code directly asked you in interviews. No, we don't know how BFS
00:44and DFS perform on graph. Because there are many algorithms. Some algorithms use DFS and some algorithms use BFS. So
00:54we need to remember the class to see BFS and DFS actually.
00:59If I have a graph of normal, I have a graph of data structure with nodes 0, 1, 2, 3,
01:084, 5, and 6. And in this way, I have some edges. When we are traversing, usually we start with
01:17a source. Source is a starting point. I can say that my starting point is 0.
01:23In a traversal, there is no starting point. In a graph, like in a tree, it is defined that the
01:29traversal is always from the root. Because a tree is such a structure in which all nodes and branches are
01:37out of root. But in a graph, there is no starting point in a graph. So in a graph, we
01:41can also take our starting point in a graph. I want to take our starting point in 3. I want
01:47to take our starting point in 5. I want to take our starting point in 5. But I want to
01:51take our starting point in ease.
01:53If I travel to 0, then I have two choices. I can go to 1 or 2. If I go
02:01to 0, then I travel to 0. Then I have to travel to 1. Now I have to travel to
02:091. I have two choices. If I go to 1, then I can go to 3, then I can go
02:13to 0. If I go to 1, then I can go to 3.
02:19So, I will print 3. If I go to 3 to 5, then I will print 5. If I go
02:26to 4, then I will print 4. If I traverse the graph, there are many many different ways. But we
02:33will talk about two fixed ways. One is BFS Traversal. One is BFS Traversal. One is BFS Traversal. First is
02:40BFS Traversal. Let's talk about BFS Traversal. BFS Traversal is called BFS Traversal.
02:49means if we are standing on any node then we will go to the immediate neighbors which are all near
02:56one and two so first we will travel in two of them and then we will travel in two of
03:00them and then we will travel in two of them and then we will travel in three of them and
03:06then we will not travel in three of them and then we will travel in two of them first we
03:08will travel in two of them and then we will travel in two of them and then we will travel
03:11in two of them and then we will travel in two of them and then we will travel in two
03:11of them and then we will travel in two of them and then we will travel in two of them
03:12and then we will travel in two of them
03:15Now Bread First Search is very similar to Level Order Traversal. What happens in Level Order Traversal? If I have
03:23a given tree, like I have such a given tree in which there are many nodes. What do I do?
03:29Level wise, I can print a value of each node. I will travel to this level, then this level, then
03:35this level. This was my Level Order Traversal, which I would implement in Q in the form. That's how my
03:42Bread First Search is.
03:44How do my levels become? This becomes my level 0. This becomes my level 1. This becomes my level 2.
03:53This becomes my level 3. And this becomes my level 4. So I'm going to travel my nodes level-wise
04:00in my breadth-first search. Breadth is called Chaudhai. That means you will travel in the first place. Chaudhai means
04:08that if I go down here,
04:12So I'm going to the depth, I'm going to the inside, first I'm going to the inside, first I'm going
04:17to the spread of the breadth. First I'm going to the immediate neighbors. So the immediate neighbors are 1 and
04:242, then I'm going to the next level of the nodes. Then I'm going to implement the breadth-first search.
04:30For the breadth-first search, we need a queue data structure.
04:35We know that this logic we have already studied. We used to use Q data structure to implement the level
04:38order traversal. Bread first will also use Q data structure. So why do I create a Q? This will be
04:48my front and rear. Why do I create a normal Q? I have FIFO type. FIFO type means first in
04:54first out. The node will be printed first. Bread first search has a rule that the node has come first.
05:01We will travel first and then 1 and 2 will come first. So, 1 and 2 will travel first and
05:07then 3, 4, 5, 6 will travel first. Now, I have to implement FIFO logic. So, why do I use
05:13the data structure? Because first of all, I will add my source. Let's say that my starting point is the
05:19source that is equal to 0. So, I will add 0. And with this, I will add another Boolean type
05:25array. I will make Boolean array. I will give it a name. Visit it.
05:32This index will be 0, 1, 2, 3, 4, 5, and 6. In the beginning, all the indexes are false.
05:42By default Java, this is false. This is false. This is false. This is false. This is false. This is
05:48false. Visited is false. It means that I haven't traveled yet. And as this value is true, it means that
05:56I have traveled.
05:57What is the logic of this extra visited? In BFS, I have to take visited in this way, because in
06:04graphs, there are cycles. When we had a level audit of trees, there was no visited extra boolean type of
06:11array, which tells us which node is going to be visited or not.
06:14Because in the graph, we travel from the bottom to the bottom. So, I know that from the bottom to
06:20the bottom, I'm going to travel again to the upper node. This means that in trees, there are no cycles.
06:27But in graphs, there are cycles. For 3, I'm going to 5. For 5, I'm going to 4. For 4,
06:34I'm going to 3 again. So, what will happen when I'm going to 3?
06:38So what will this neighbor be? This neighbor will become 5 and 4. So I will visit them to 5
06:42and 4. Then when I come to 5 and I will print them to 5, I will print them again
06:48to 4 and again to 3. Why? Because there exists a cycle here. And because there exists a cycle in
06:57the graph, I have to track that I will visit one node one time. I have taken the visited node
07:02so that each node is visited.
07:09Once. I will visit one node one time. That's why I have visited one node. So, let's start our BFS.
07:17What will we do here? I have taken a source of zero and added it in the queue. Now, I
07:22have to run a loop. It's a level order reversal. I will remove the element from the queue. I will
07:28print it. I will print it. And as I have printed it, I will visit it. Print it means that
07:35I have visited it. It means that I have visited it in the visitor.
07:40So, I will remove it from the queue. So, I will remove it from the queue. So, I will remove
07:43it from the queue. So, I will remove it from the queue. And I will visit it from the queue.
07:48I will give it to the current element. So, I will give it to the current element. So, I will
07:54give it to the current element. So, I will print it from the current element. Now, I will see the
07:58current neighbors. Current neighbors will be 1 and 2.
08:04So, why do I add 1 and 2? First, 1 will add 1, then my 2 will add. Neighbors will
08:09come to me. I run a loop in which I will remove each edge and the destination of my neighbor
08:15will become my neighbor. This one I have already done. 1 and 2 I have added. Then, I will remove
08:201. Now, I have 0 which is already printed. Now, I will remove 1. 1 I have removed 1, I
08:26have printed 1, I have printed 1, I have printed 1, I have printed 1. So, 1 I have printed
08:301, I have printed 1, I have printed 1 and I have printed 1.
08:34Now what I will do? I will push all the immediate neighbors after 2. In this graph, there is only
08:413 of 1. What I will do? I will push my Q in 3. Then I will remove 2. I
08:49will remove 2. I will print 2 for 2. Now I will push my immediate neighbors after 2. I will
08:57add 4 to 4. I will add 4 to my Q in 3. Now it comes to 3.
09:03As I have removed 3, I will print 3 and I will print 3. Now I will add 3 to
09:09the immediate neighbors. The immediate neighbor will become 1, 1 will be 4, 1 will be 5. So I will
09:17add 3 to the value. 1 will be 1, 1 will be 4 and 1 will be equal. This is
09:24my Q which is 1, 4 and 5 will be equal. Next, the Q will be 4.
09:31For just like this, I will do print for 4 and for visited to do true. This is the way
09:37to remove the queue. After that, I have to check whether my node is already visited.
09:44If my node didn't visit before, meaning visited of current is false, then I have to print it and now
09:52I have to visit it. If it was before visit, then I don't want to visit it again. So I
09:57have to print these nodes when they are un-visited. So I have to print all my nodes un-visited.
10:04So 4 is un-visited. Now I have to print it for 4. Now we will add the neighbors to
10:104. For 4, the neighbors are who are?
10:121-4 neighbor is 2, 1-4 neighbor is 3, and 1-4 neighbor is 5. So, I will add
10:18in my queue 2, 3, and 5. Now, what will come from the queue? I will remove all these nodes.
10:284 was removed. Now, in my queue, I have all these elements. Now, the queue will come from 1. Just
10:35like 1 came out, I will check if 1 is already visited. I have already visited 1.
10:42This means that one already visited means that you don't want to visit again. If you don't want to visit
10:48again, you don't want to print it. You don't want to add neighbors to it. You don't want to visit
10:54again. Visit means that you don't want to visit again. If you don't want to visit again, you don't want
11:02to print again. Second, you want to print again. Third, you want to print again. Third, you want to print
11:10again. Third, you want to
11:11Add it in Q. So, these three work will be only for those notes, which are un-visited. For one,
11:20I already have visited and true. I mean, one for three work has already been done. I won't do it
11:24again. So, what do I do? Why do I remove one simply? Next, I will remove four. I will remove
11:31four and see if four is visited and true. Yes, four is also visited and true. So, four will not
11:36be done again here. After that, I will remove five.
11:42If I remove 5, then I will see if it is true or not. So, I will visit 5. I
11:48will print 5 to 5. Now, I will add 5 to 5. I will add 5 to 5. Now, I
11:55will add 5 to 5. Now, I will add 5 to 5. Now, I will add 5 to 5. Now,
12:05I will add 5 to 5.
12:08I will see that 2 is already visited. I just want to remove it. Then I will remove it 3.
12:14I will see that 3 is already visited. I don't want to remove it. Then I will remove it 5.
12:20I will check that 5 is already visited. I will remove it. Then I will remove it 3. I will
12:29see that 3 is already visited. Then I will remove it 4. I will see that 4 is already visited.
12:35Then I will remove it 6.
12:376 removed, so not visited for 6. So, what do I do? I will print it. I will do visited
12:43for 6. And I will add the neighbors to 6. 6 neighbor is 5. So, 5 will come here. After
12:50all, these are all gone. Last, which will come out of my queue, will be my 5 node. 5 node
12:58will come out. 5 is already visited for 5. So, 5 will not be printed again.
13:03And finally, I will see the sequence of BFS traversal. I will print 0, 1, 2, 3, 4, 5, and
13:096. And what did I do? I used to travel all the vertex. I mean, I printed all the notes
13:15in BFS. 0, 1, 2, 3, 4, 5, 6. All the BFS parts are in BFS. So we are going
13:23to write code for BFS. What did I do? I made a classroom class. I already made my edge class.
13:30And I...
13:32I made a function called create graph. It was a big work to create a graph and add an edge
13:37to that. I made a function called function. This is the same work we discussed. This time my graph shows
13:44something like this. Now we have seen dry run on this graph. The total number of vortices are 7 to
13:50equal. So I took V to 7 to equal. I defined my graph. Then I called create graph. What does
13:57create graph do?
13:59Each graph defines an empty array list and all the edges I created. By default, I have given the weight
14:08of each one to equal each one. If you want, you can completely remove your code base. So, this line
14:14will remove, this line will remove, and this line will remove all the weights from here. But since I am
14:20trying to follow standard, the unweighted graph is
14:24But then I have given the weight to each one edge. No difference between the code or the answer. So
14:30I have already created the graph. What can you do? You have to write the code time. You have to
14:35write that part. Usually when we ask the question of graphs, we don't have to create the graph from scratch.
14:41But then we need to create the graph in case there is an interviewer to ask us to ask us.
14:48So the process of creation, we don't do every question every time. Usually, we write functions in graphs. So, here
14:55we write BFS function. Create, I have already done graph. What will I do? I will write a function. Public,
15:02Static, Void, BFS. In BFS, I will have my array of array list of edge, graph name. And I will
15:16print this graph.
15:18Why do we take Q in the BFS traversal? Because I will store our integer. I will name Q is
15:25equal to new linked list. Besides, I am going to take a boolean type of visited array. I am going
15:32to name visited is equal to new boolean and its size will be equal to V. What is V? V
15:38is the graph size. Graph.length will be equal to V. Now my source
15:46In the beginning, I'm going to start from 0. If you want to start by default, you can start from
15:513, from 5, from 4. We can also start from BFS. I'm going to take my source from 0 to
16:00equal. So what will I do? I will add my source to 0. So my source is equal to 0.
16:07This is my starting node. I will run a loop until my queue is empty. Until my queue is not
16:13empty.
16:15This loop will be the same as the level audit reversal in trees. I will remove the node from the
16:20queue. Int current is equal to q.remove. Then I will check that if my current is not visited, that
16:33means the value of visited for current is false, then I will visit it. Then visit current. What do I
16:41want to do? I don't want to do anything. Just leave it to remove and continue.
16:45In this case, I am going to visit my node in the same case. We are going to visit the
16:50node in three steps. The first step is to print it. What I am going to do is print it.
16:56I am going to print current. The second step is to print it. It means that now we have visited
17:03it. Visited of current will be true to equal. The third step is to add all the neighbors to Q.
17:12I am going to add the loop. For int i equals 0.
17:15i less than graph of current i less than graph of current i less than graph of current i less
17:19than graph of current i less than graph of current
17:44This is the part of the code that we have already written when we created our graph. We write this
17:51for any node or vertex to remove all neighbors. So what did I do? My current node was the name
17:57of all neighbors. So in the graph of current, I got an array list. I traversed that array list and
18:05then I removed one edge. The destination of the edge is my neighbor. So why did I do that?
18:12I have added my destination to my edge. So this is my BFS code complete. What did I do? I
18:19will summarize again. A queue is a boolean visitedly. Why did I add 0 to 0? Why did I add
18:250 to the queue in the beginning? Why did I run a loop when my entire queue is empty? Why
18:29do I remove the queue from my element? Then I will check whether it has already been visited. If it
18:35hasn't, I will visit it. There are three steps to visit.
18:38The first step is to print it. The second step is to true. The third step is to add all
18:43the neighbors. How do we remove the neighbors? We have already written code. I will remove each neighbor's edge and
18:49the destination will be a neighbor. I will add all the elements in the queue. We will remove all the
18:54elements from the queue and they will come and print. We will do save. I will call the main function.
19:00BFS, which I will pass the graph. We will do save and now run.
19:10I have my output in BFS fashion which is 0, 1, 2, 3, 4, 5 and 6. This is the
19:17output that I had to dry run. And BFS will give you a lot of attention to level order traversal.
19:23You should also use level order traversal as well as BFS traversal. So we will ask a lot of questions
19:30that will implement BFS. That's why BFS code is such a function that we should remember very well. And if
19:37I talk about the time complexity of this function,
19:40a time complexity big of n. Big of n is a linear time function. As I have nodes and vertices
19:48in graph, I have time to take the BFS code. And the BFS code of time complexity becomes big of
19:57v plus e. The vertices and edges control the time complexity of v plus e.
20:04This is important to remember, you need to remember that in Java, if we use the adjacency list, like we
20:12have done in this implementation, if we use BFS, then the time complexity is big of v plus e. And
20:19here, if I make the graph, I create a matrix, then it becomes big of v square. So, this time
20:27complexity is more and we don't use the matrix so we use the adjacency list.
20:32So BFS time complexity is V plus A. How did V plus E come? Because we will see Q when
20:38we run, we will traverse all the edges. We will travel only once and twice, plus all the vertices we
20:48will go to all the vertices. That's why time complexity is the sum of both. So whoever the value will
20:54control BFS time complexity.