 Welcome to this course on data structures and algorithms. We continue our discussion on graphs. Last time we looked at the depth first search algorithm to traverse that graph. Today we shall continue that particular depth first search traversal and actually look at a C plus plus program to implement DFS. We will need several functions in order to implement the depth first search. A function which says get vertices, we are already familiar with this. We get vertices of the graph and store them in an array and this will be called by the DFS. The second function we require is actual DFS search for traversal of the graph. We will also be using a list S. This list is used to store adjacent nodes which are outgoing from the current node. We use the list construct of STL as a stack. It is important to note that for implementing depth first search we are going to use list as a stack. So, therefore we use the pushback and popback facilities of STL. The pushback will push the nodes in the list and popback will pop the nodes out of the list. Please note because we are using it as a stack it will always push on top and pop from top. Let us demonstrate this process by again relooking at the graph which has these 5 nodes. The steps are initially we start with A which is the first node. We insert A in the list. So, list has contained A. Now, we pop A from the list. Since A is not visited, we visit it. We find that outgoing edges from A are to B and C. So, we will push back the outgoing nodes in the list. Next, we pop the next node from the list as a stack. Since this is being treated as a stack, we pop C from the list. When we pop C from the list, the list contains only B. Now, we look at C which we have just popped. It is not visited. So, we will visit it. When we visit C, we find that C has an outgoing H to D. So, we will push back the outgoing nodes of C which is D on to the list. The list will now contain B and D. We continue. We now pop the next element from the list which happens to be D. When D is popped, list contains only B. Since D is not visited, we visit it and we push back all outgoing edges from the D which happens to be only one H to E and therefore, the outgoing node E is now pushed on to the list. The list now contains only B and E. Next, we pop E from the list because E is at this position. The list now contains only B. Since E is not visited, we visit it. Of course, there is no outgoing H from E. So, there is nothing else to be done. Since there is nothing else to be done, we have to continue to examine the back element of the list. The only element on the list that is remaining now is B. So, we now have to visit B. Let us look at this element B. We pop B from the list. Since B is not visited, we visit it. Now, we have to push back all the outgoing edges from B. The outgoing nodes from the B is only one that is D. So, we push D on the list. So, the list still contains D. Now, next, we examine what is there in the list. We pop D out, but notice that it is already visited. Now, there is nothing left in the list. That means all nodes are traveled successfully because the list is empty. Please note that the visited nodes in the order in which they are visited, which we have been showing on every slide, will have A, C, D, E, B as the sequence which is the proper sequence for a deaf first search. Here is an example of a slightly more complex graph with so many edges. It has A, B, C, D, E, F, G as the nodes and it has multiple edges between these nodes. I request you to do exactly the same kind of demonstration that you have seen in the previous slides to actually construct the sequence in which these nodes are visited and confirm that the deaf first search order will give you A, D, G, F, E, C, B in that order. Let us now look at the program to implement the deaf first search. As before, we use the class graph. We introduce a new variable called size. We shall see how it is used. As public elements of this class, we have functions which we are already familiar with. For example, create graph nodes, add edge, remove edge, print outgoing, print incoming. We have seen all these functions. Two new functions which we add to implement the deaf first search are one, get vertices. As we saw in the algorithms, this one will be called from the DFS and the main function DFS which implements the deaf first search. Look at this. The only change here in the create graph nodes and other thing is size equal to A, which we capture based on the input that we have been given. Next, let us look at get vertices. We are already familiar with this. It will get all the vertices through the iteration. Next, let us look at DFS. This is the main deaf first search function. Let us see what we are doing. We create a T class instance with this size that we have given which we have got that is the total number of nodes. We get vertices. Now, we introduce a bool array called visited. This is the one which will permit us to keep track of which node is visited and which node is not visited. Please note that this visited array will work in tandem with the array of nodes that we have. This will actually will be a Boolean variable corresponding to the node in the same position in the other array. All that we do is for i equal to 0 to size, we set all the visited elements as false. Now, we look at this pushback. When we say s dot pushback on the list that we would actually start the process of DFS here. While the size is greater than 0, what are we doing? We are seeing s dot back goes to that node and we say pop back. This implements the procedure that we had outlined in the demonstration where a node is popped and the pop node is now looked at depending upon whether that node is visited or not. However, we need to first check where this particular node is. Now, this is done by saying position of the node and we have to find that position. Now, this position will be in that array that we have from start to finish somewhere else. Starting point is C, end point is C plus size and node is what we are trying to find out. Now, this minus C will actually give me the location differential from the starting point for the node that I am looking at. I will check if the position of the node in the visited array indicates a false. That means this node has not been visited. Now, you will see the importance of using this Boolean array visited. This is the main purpose to keep track of whether a node has been visited or not visited. Now, if the visited is false that means the node has not been visited, I need to write code to perform operation which is as per the DFS logic. If not, then end where I come out and I will go to the end of while doing the same thing keeping on visiting thing. Please note that the while loop ensures that successively I will pop back and keep visiting the pop back node as per this logic. Let us look at code to perform operations if the node is not visited. Now, if the node is not visited we have to first mark that it is visited. So, the visited array we convert from false to true that means we are visiting it. Now, what we are doing? We output that visiting node what we are visiting and we run an iteration now. What are we doing in that iteration? We are going through that list and in that list we set iterator to begin. Now, if that point is equal to the node then we set list iterator plus plus and we will run an iteration on that list associated that. What are we going to do? We are going to do push back because all outgoing edges of the node in the list have to be pushed back and they are being pushed on to the stack. So, this is the operation which does that pushing on the stack. This will complete the operation to find out all visits from a particular node and this is the code that we are writing in the outer iteration that we have seen earlier. The main program is now very simple. I set the vertex again this is an arbitrary example a, b, c, d, e. I create graph nodes, I add edges, I print outgoing. Now, I want to print DFS. So, I first execute g dot DFS. This DFS will actually give me the depth first search with this starting node a that is all the main program is. To conclude in this particular session we saw the depth first search. We cleverly used the list as a stack because the stack permitted us for pushing at the top and popping from the top. We looked at the DFS algorithm and implemented that algorithm. Two special thing that we used was a Boolean array to represent the status of a node whether it is visited or not. The implementation of depth first search is actually a straightforward thing. You can confirm using the demonstration that when you execute this algorithm will actually get the depth first search order printed for the graph traversal. Thank you.