DFS (Depth First Search) algorithmIn this article, we will discuss the DFS algorithm in the data structure. It is a recursive algorithm to search all the vertices of a tree data structure or a graph. The depth-first search (DFS) algorithm starts with the initial node of graph G and goes deeper until we find the goal node or the node with no children. Because of the recursive nature, stack data structure can be used to implement the DFS algorithm. The process of implementing the DFS is similar to the BFS algorithm. The step by step process to implement the DFS traversal is given as follows -
Applications of DFS algorithmThe applications of using the DFS algorithm are given as follows -
AlgorithmStep 1: SET STATUS = 1 (ready state) for each node in G Step 2: Push the starting node A on the stack and set its STATUS = 2 (waiting state) Step 3: Repeat Steps 4 and 5 until STACK is empty Step 4: Pop the top node N. Process it and set its STATUS = 3 (processed state) Step 5: Push on the stack all the neighbors of N that are in the ready state (whose STATUS = 1) and set their STATUS = 2 (waiting state) [END OF LOOP] Step 6: EXIT PseudocodeExample of DFS algorithmNow, let's understand the working of the DFS algorithm by using an example. In the example given below, there is a directed graph having 7 vertices. Now, let's start examining the graph starting from Node H. Step 1 - First, push H onto the stack. Step 2 - POP the top element from the stack, i.e., H, and print it. Now, PUSH all the neighbors of H onto the stack that are in ready state. Step 3 - POP the top element from the stack, i.e., A, and print it. Now, PUSH all the neighbors of A onto the stack that are in ready state. Step 4 - POP the top element from the stack, i.e., D, and print it. Now, PUSH all the neighbors of D onto the stack that are in ready state. Step 5 - POP the top element from the stack, i.e., F, and print it. Now, PUSH all the neighbors of F onto the stack that are in ready state. Step 6 - POP the top element from the stack, i.e., B, and print it. Now, PUSH all the neighbors of B onto the stack that are in ready state. Step 7 - POP the top element from the stack, i.e., C, and print it. Now, PUSH all the neighbors of C onto the stack that are in ready state. Step 8 - POP the top element from the stack, i.e., G and PUSH all the neighbors of G onto the stack that are in ready state. Step 9 - POP the top element from the stack, i.e., E and PUSH all the neighbors of E onto the stack that are in ready state. Now, all the graph nodes have been traversed, and the stack is empty. Complexity of Depth-first search algorithmThe time complexity of the DFS algorithm is O(V+E), where V is the number of vertices and E is the number of edges in the graph. The space complexity of the DFS algorithm is O(V). Implementation of DFS algorithmNow, let's see the implementation of DFS algorithm in Java. In this example, the graph that we are using to demonstrate the code is given as follows - Output ConclusionIn this article, we have discussed the depth-first search technique, its example, complexity, and implementation in the java programming language. Along with that, we have also seen the applications of the depth-first search algorithm. So, that's all about the article. Hope it will be helpful and informative to you. Next TopicSpanning Tree |