 Hello, and welcome to the lecture on graph traversal algorithm, breadth first search algorithm. In this lecture, we will be discussing about graph traversal using breadth first search algorithm. The learning outcome of this video lecture is that the student is able to demonstrate graph traversal using breadth first search approach. What is graph traversal? Graph traversal is the process of visiting each node in the graph as shown here. So through the graph traversal, we will be visiting each node in the graph at least once. This graph traversal process is classified based on the order in which the nodes are visited while traversing the graph. There are two standard methods of graph traversal. First one is breadth first search, we are calling it as BFS. Second method is depth first search that is DFS. Bread first search method uses Q as a helping data structure to store nodes for further processing. Depth first search method uses stack as a helping data structure to store nodes for further processing. Let us see BFS method in detail. In these algorithms, every node in the graph have the variable status to set to value one or two or three to indicate its current status. So the one value indicates ready status that is initial state of the node n. The value two indicates waiting status. It means the node n is placed in the queue and waiting to be processed and whereas the value three indicates processed status which indicates node n has been completely processed. Bread first search algorithm begins at root node and explores all the neighboring nodes. Here is the time to reflect on the content what we have learned. State the following statement is true or false. Bread first search algorithm uses Q data structure to store nodes for the further processing and the second statement is graph traversal is the process of inserting a new node in the graph. We can pause the video for some time and think on the answer for this question. The first statement is true. Bread first search algorithm uses Q data structure to store nodes for further processing. The second statement is false because graph traversal is the process of visiting each node in the graph and not inserting a node in the graph. Now we will discuss the bread first search algorithm here in detail. As you can see here we are considered the graph G with the nodes A, B, C, D, E and this is the adjacency matrix representation of the graph G which will be the input to the bread first search algorithm and this is the array status which will show the status of these individual nodes and the Q data structure which will be used for storing the nodes for further processing. So first step is we are going to set the status for each node to 1 as shown here. Second one and Q is the starting node into the Q and update it is the status 2. We will first go through the complete algorithm and then we will see one example. Now we are checking Q is empty. If Q is empty we are going to stop this process. If Q is not empty we are going to dequeue a node from that particular Q and process means you are informing that that particular node is visited and set that node status to 3. Then enqueue all the neighboring nodes of the node n which we have dequeued just now and those node status should be 1 and then set their status to 2. We are going to repeat this procedure till you will find the Q is empty. Once Q is empty we are going to stop. So we will see now the BFS algorithm with example. So here we are continuing the same example of graph G with the nodes A, B, C, D. So adjacency matrix representation is shown here. Now we are starting with the first node A. So all the node status is set to 1 as the first step we can see here. Then we are enqueuing the first node into the Q and setting the status to 2. So first node we are assuming is the A node from where we are going to start the traversal. So first node is enqueued into the Q. So accordingly we have updated the status to 2. Now the next is Q is empty. No, Q is having the node A. So we are going to dequeue that node and we are marking that node as a processed one or visited one. So we are changing the status of node A to 3. And we are enqueuing all the neighboring nodes of node A. So how we are going to find out the neighboring nodes of node A? That is from this matrix wherever you will find the value 1 that is a B and C. So these nodes are enqueued into the Q. Then accordingly we have to update that node status to 2. So we are going to update the node status to 2. Now next we are going to repeat the again the same procedure is the Q is empty. No, so first node in the Q is B. So we are going to dequeue that. So that node is processed, visited. So we are going to update the status to 3 and that is marked as a visited node or processed node. Then next we are enqueuing the neighboring node of the node B. That is you can see here the neighboring node of the node B is E. So that is enqueued into the Q. And the status of that particular node is going to be updated to 2. Then again we are going to repeat that is your Q is empty? No. So the node in the Q is now C. So we are going to dequeue the node C that will be marked as a process 1. So status is updated to 3 and that will be displayed as a process node C. Now we are going to check is this node C is neighboring nodes are there with the value 1. Yes, the D is with the status 1. So that we are going to enqueue into the Q and the status of the D is updated to values 2. Now again we are going to check is your Q is empty? No. Q is not at empty. So dequeue the element E, mark it as a process 1. So status is changed to from 2 to 3 and that will be displayed as a process node. Now we are going to check the neighboring nodes of node E that is B and D. But B and D's value is now status is not 1. It is 3 and 2. It means that B is processed 1 and D is in waiting. So there is no further nodes to enqueue into the Q. So we are going to check now, this is the Q is empty, no, Q is not empty, D is there in the Q. So we are going to dequeue that, mark it as a process 1 with the value 3 then displayed it as a process node. Then we are going to check the D nodes, neighboring node that is node C and node E. But node C and node E is already marked as a process node with the status value 3. So there are no further nodes to enqueue into the Q and then after this we are checking is Q is empty? Yes. Q is empty. So we are going to stop here. All nodes are processed from this graph. So this is a sequence of graph traverser for the graph G, A, B, C, E and D. So this is overall the BFS search algorithm. So now this is the time to reflect by following the BFS algorithm. You are going to write the graph traverser for the given graph with the nodes P, Q, R, S, T. Here you can pause the video for some time and write the sequence of nodes for this graph using BFS method. Let us see the graph traverser for this graph. First node we are starting with the node P. So that is enqueue into the Q. Then we are going to dequeue that node from the Q and marked as a process one. I am not showing here the status array. We are going to assume it. Now what are the neighboring nodes of the node P is Q and R. So these two nodes are enqueued into the Q. Q, R and S sorry, S is also connected to the node P. So these three nodes we have enqueued into the Q. And that status is now changed of the Q, R, S is to waiting. Now we are enqueuing the first node from the Q that is node Q that will be marked as a processed one. Now the neighboring node of Q is P and T out of which P is already processed one. So T is the one which is not at process that we are going to enqueue into the Q. So T is enqueued. Now which is the next element into the Q that is the R that we are going to dequeue. So R is dequeued, marked as a processed node. Now we are going to check which are the neighboring nodes of the R that is P and S out of which P is already processed one and S is in the waiting. So there is no further node which we can enqueue into the Q. So we are going to now dequeue the first element from the Q that is the S and that is marked as a processed node. Now neighboring nodes of the node S is P, R and T. After this P and R are already processed one and T is in waiting. So there is no further neighboring nodes of S which we can enqueue into the Q. So next in the Q is the Q is empty node. There is still one node information in the Q that is T. So dequeue that node information marked it as a processed one. Now after processing the node T check the neighboring nodes of node T that is a Q and S. So out of these two nodes Q and S already processed. So there is no further nodes which are neighboring to the node T which we can enqueue into the Q. Now we are checking is Q is empty? Yes. Q is empty. So we are going to stop here the traversal algorithm. So this is a sequence for this example graph traversal sequence that is P, Q, R as T. Thank you for watching this video.