 Hello, and welcome to the lecture on graph traversal algorithm depth first search, BFS. In this lecture, we will be discussing about graph traversal using depth first search method. The learning outcome of this video lecture is that student is able to demonstrate graph traversal using depth first search approach. What is graph traversal? Graph traversal is the process of visiting each node in the graph. As shown here, 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 the breadth first search, BFS, which we already discussed in previous video lecture. Second method is depth first search, DFS. For breadth first search method, refer to the previous video lecture. DFS depth first search method uses stack as a helping data structure to store nodes information for further processing. We will discuss DFS method in detail here. When we apply these algorithms on a graph, we can see non-visited, visited and explore nodes. Non-visited nodes means the nodes not yet visited. Explored nodes means the nodes which are visited. And explore nodes said to be explored when it was visited and all nodes which are adjacent or connected to that node is also visited. Those are concerned with the explored nodes. Explored nodes are the nodes which are visited and all the nodes connected to that node is also visited. In these algorithms, every node in the graph has the variable status, set to 1 or 2 or 3 to indicate its current status. The one value indicates ready status that is initial state of the node n. The value 2 indicates waiting status, it means the node n is placed in the queue and waiting to be processed. The value 3 indicates processed status which indicates node n has been completely processed. Depth first search algorithm begins at root node and explores all the neighbor nodes. The depth first search progresses by expanding the starting node of a graph and then going deeper and deeper until a goal node is found or until a node that has no children is encountered. When dead end is reached, the algorithm backtracks returning to the most recent node that has not been completely explored. In DFS algorithm, ages that lead to a new vertex or new node are called as a discovery edges and the ages which lead to an already visited node are called as a back edges. Now here we will discuss depth first search algorithm. Here the graph G with the nodes A, B, C, D is shown and the adjacency matrix representation of graph is displayed here. Along with that the status array for each node with the value 1 is set here and the stacked data structure will be used which will be used as a helping data structure during this algorithm is shown here. We will first basically see the steps in these algorithm and then we will go with one example. So first step is set the status 1 for each node in G, in the graph G, given graph as shown here the status of all these nodes is set to 1. Then push the starting node and onto the stack and update its status to 2. Then check is the stack is empty. If stack is not empty, we are going to pop the top node from the stack, process it and set its status to 3. Then push all the neighboring nodes of the node N which we have popped up from the stack onto the stack whose status is 1 and then set the status of these node to 2. Repeat this step till you will find the content in the stacks. Once you find the stack is empty, you are going to stop here. So this is overall the step of the DFS algorithm. Now we will see the DFS algorithm followed for the given graph G. So here we are having the adjacency metric representation of the graph G. So we will start traversing this graph with the first node A. So we are going to follow the first step that is setting the status of all these nodes to 1 that we have already done here, all the node status to 1. Then push the first element that is first node onto the stack that is the node A and then change the status of node A to from 1 to 2. Now we are checking is the stack is empty. Now stack is not empty. We are going to pop the node information from the stack that is node A is popped and the status of the node A is changed from 3 to 2 to 3. Means it indicates that node A is processed, visited. Now next is the adjacent nodes of the node A are B and C as you can see in this adjacency matrix. These nodes we are going to push on the stack. So these are pushed on the stacks means the status of B and C we are going to change from 1 to 2. Now check the stack is empty, no, so pop the top element of the stack. So C is popped up and it is processed. So status of the node C is changed from 2 to 3. Now find out the adjacent nodes of the node C is A and D. But out of these two, A is already processed status is not 1 it is 3 and D status is 1. So we can push the node D on the stack. Now check the top element or the stack is empty, yes, stack is not empty. So we can pop the top element from the stack that is node D, okay before that we have changed the status of the D to from 1 to 2 when we push the D information on the stack. Now pop the top element of the stack that is node D. So node D is processed node now so we are going to change the status from 2 to 3. Now adjacent nodes of the node D are C and E. So out of that C's status is 3 and E is 1. So E is we are going to push on the stack. E is pushed so that's why the status of node E is changed from 1 to 2. Now if your stack is empty, no, then pop the element from the stack that is node E information is popped and displayed and status of node E is changed from 2 to 3. Now adjacent nodes of the node E are B and D. So B status is 2 means it is in waiting and D status is 3 means it is processed. We don't have any node information to push on the stack. Now check the stack is empty, no, stack is not empty it is having one element B so that we are going to pop and that we are going to process. So node B is processed so status is changed from 2 to 3. Now check the adjacent nodes of the node B is 1 and E but the status of 1 and E is 3 means these two nodes are already processed, visited. Now check is stack is empty, yes, stack is empty so we are going to stop here. So in this sequence the given graph J is traverse using DFS algorithm. So the order is A, C, D and E and B. So this is the example by following DFS algorithm we have traverse the graph G. Now there is a time to reflect on what we have learned. So traverse the given graph G1 with the nodes P, Q, R, S, T by following the DFS algorithm. So you can pause here the video for a time and get the traversal sequence for this given graph G1. Let's see the solution for that. So we are having the adjacent matrix representation for this graph G1 and the status of all these nodes is set to 1 initially. So first we are starting traversing with the node P. So P is pushed on the stack so status of the P is changed from 1 to 2. Now if the stack is empty, no, so pop the element from the stack. So that is the node P. So that is processed so status of the node P is changed from the value 2 to 3. Now the adjacent nodes of the node P are Q, R, S. So these nodes are pushed on the stack Q, R, S and the status of these three nodes is changed from 1 to 2. Now the check the stack is empty, no, stack is not empty. So pop the top element of the stack so that is the S node S is processed. So status of node S is changed from value 2 to value 3. Now the adjacent nodes of the node S are P, R and T. Out of that P is processed, R is invading and T is not yet processed. So we can push the node T information on the stack. So status of the node T is changed from 1 to 2. Now stack is not empty, pop the top element of the stack that is node T that is processed. So that's why we are changing the status of the node T from 2 to 3. Now the adjacent nodes of the node T are Q and S. So out of that Q also waiting and S is processed. So we don't have any node information to push on the stack. So we are proceeding by checking the stack is empty, no, stack is not empty. So pop the top element of the stack that is the R. So R node information is processed, visited, R is visited so status is changed from the value 2 to value 3. Now the adjacent nodes of the node R are P and S. So P status is 3, S is 3, so both are already processed one, visited one. So we don't have any node information to push on the stack. So we are going to check is the stack is empty, no, stack is not empty. So pop the element from the stack that is node Q. So node Q is now processed one, visited one, so status is changed from 2 to 3. So adjacent nodes of the node Q are P and T. So status of the P and T is 3. So means these two nodes are visited one. So we don't have any information to push on the stack now. Now check this, the stack is empty, yes, stack is empty. So we are going to stop here. So we have traveled the graph G1 and the nodes visited R, P, S, T, R, Q. So this is a sequence of graph travels using DFS for graph G1. So you can check your answer here and if you find difference you can revise the concept again here by going through the video. Thank you for watching this video.