 Welcome to this course on data structures and algorithms. In this session, we will look at a computer program which implements breadth first search. You will recall that the basic principle of breadth first search was discussed in the last session. Let us look at the functions and some data structures that will require to implement the breadth first search. The first function of gate vertices works exactly as it did earlier. The breadth first search is a function which will traverse the graph in the BFS fashion. We will now use a list S, but this time we will use the list S, the list construct of STL as a queue. This is also used to store outgoing nodes, but the fundamental difference is instead of stack, we are going to use this list as queue. Consequently, we will use push back function which will push the nodes in the list and pop front function to pop the nodes out of the list. These are the two functions which are different from a stack operation they implement the queue operation. Please remember a queue will always push at the back of the queue and pop will always pop from the front of the queue. The simple difference makes the search from depth first search to breadth first search. Let us look at the demonstration. Initially, we insert A in the list, pop A from the list since A is not visited, so visited. When we visit A, we will find out the outgoing edges. There are two edges B and C, they will come in that order and they will get pushed onto the list S. Next, we have list S containing B and C. We pop from the list, but note the fundamental difference. This time the pop will happen from the front of the queue because we are treating list as a queue. So, B will get popped out and only C will remain on the list. Now, since B is not visited, we will visit it. When we visit B, we will find out there is one outgoing HD and this D will now be inserted in the list and because it is a queue it will be inserted at the back. So, now the list will contain C and D and we have visited A and B so far. Next, we pop from the list. Again C will be popped and not D because this is a queue. So, popping will happen from C. Since C is popped, only D remains on the list. Since C is not visited, we will visit it. C has only one outgoing HD and therefore D will get inserted on the list. Notice that the list contains now D twice once because it was already there earlier and next because C has an outgoing HD and therefore that will get pushed back onto the list. Please note that the visiting order is A, B and C so far. Now, we follow the same steps. The list contains D and D. We popped D from the list although both are same, but just we remember that it is a queue. Therefore, this D is going to be popped out which is in the front of the queue. The list still contains D. Now, since D is not visited, we visit it. What do we find when we visit D? D has an outgoing H to E and therefore the outgoing node E will be pushed onto the list. The visited order is A, B, C and D and my list contains D and E now. Next, we again pop the element D because it is a queue. Only E remains here. Since D is already visited, we forget it. We again go to the list. We examine the next node in the list which is E. We pop E from the list. E is not visited. So, we visited. It has no outgoing H. All nodes are now traversed successfully because the list is empty. There is nothing more to do and the order is A, B, C, D, E. I would like you to remember this fact that instead of stack by simply using it as queue, we are able to implement the braid first search in exactly the same fashion that we had implemented the depth first search. The only difference is because we are treating list as a queue, the pushing happens at the back of the queue and popping happens at the front of the queue. Here is another complex example which we had shown earlier also. Again, I request you to follow the BFS algorithm and the demonstration and confirm that the nodes which get visited in the first search order are A, B, C, D, E, F, E, G instead of the other order that we saw in the depth first search. The computer program that we write is amazingly similar to the one which we had written to implement the depth first search. For example, we have the class graph. Instead of the DFS, we have now a function called BFS. All other functions are same. We have create graph nodes which is similar to what we had written earlier. Next, we have get vertices which will also work in exactly the same fashion as we had done earlier. Now, we come to the template for BFS function. Please note that in the BFS function, we do exactly same thing that we did earlier. We defined the bool array, a visited. We have the get vertices function and so on. While size is greater than 0, we now do two things differently. First, we get the node from s dot front and second, we pop front. This is the only difference that we have from the earlier algorithm. After we do this, if the visited is false, we still have to write code to perform operations if the node is not visited. So, let us look at this code. In this code, we are doing exactly the same thing that we did earlier. The pushback will automatically do the same pushing back that we had done earlier, pushing all the outgoing edges of the node in the list. Let us look at the main program. Only difference in the main program is, instead of calling BFS, we are calling BFS. I would like to conclude this session by reminding you that the power of STL is such that I can use the list either as a stack or as a queue. In so far as graph traversal is concerned, the stack interpretation of the list permits me to implement the first search and the queue implementation permits me to implement the breadth first search. Please note that in our implementation, we have used cleverly a Boolean array to retain information about which node is visited and which node is not visited, which is required as per the algorithm whether it is depth first search or breadth first search. Thank you.