Depth First Search Algorithm

Depth First Search Algorithm

7 mins read1.6K Views Comment
Updated on Dec 26, 2022 12:00 IST

Data structures like trees and graphs are traversed or explored by the depth-first search algorithm, or DFS. The algorithm starts at the root node (in the case of a graph, you can choose any random node as the root node) and analyses each branch as far as it can go before backtracking. This article will briefly discuss DFS and how it works.

2022_09_MicrosoftTeams-image-44-1.jpg

The process of visiting each vertex (node) in a graph is known as graph traversal. A smaller graph is relatively simple to navigate. However, the process must be automated when navigating a graph with many nodes. Manual processes increase the likelihood of missing one or more vertices. And visiting nodes in a graph becomes crucial when you need to modify certain nodes, retrieve a value that was stored there, or do something else. Our graph traversing methods can help in this situation.

Two techniques exist for navigating a graph data structure:

  1. DFS, or the Depth-First Search algorithm
  2. BFS or the Breadth-First Search algorithm

Also Read: All you need to know about Data Structure and Algorithm

Also Read: Data Structures and Algorithms Online Courses & Certifications

Table of Content

Recommended online courses

Best-suited Data Structures and Algorithms courses for you

Learn Data Structures and Algorithms with these high-rated online courses

– / –
4 months
– / –
16 weeks
Free
– / –
– / –
– / –
– / –
6 months
– / –
4 months
– / –
8 weeks
β‚Ή4.24 K
6 weeks
– / –
12 weeks
– / –
4 weeks

What is Depth First Search Algorithm (DFS)

Data structures like trees and graphs are traversed or explored by the depth-first search algorithm, or DFS. The algorithm starts at the root node (in the case of a graph, you can choose any random node as the root node) and analyses each branch as far as it can go before backtracking.

Also Read: What are Algorithms

Also Read: Bubble Sort Algorithm

How does the DFS algorithm work?

This is how the DFS algorithm operates:

  • Start by keeping any vertex of the graph on top of the stack.
  • Add the item at the top of the stack to the visited list.
  • Make a list of the nodes that are close to that vertex. Place the items that haven’t been visited at the top of the stack.
  • Repeat actions 2 and 3 until the stack is completely gone.

Also Read: Selection Sort Algorithm

Also Read: Quick Sort Algorithm

Traversal of all Vertices

Now that you have learned about the basics of depth-first search. Let’s see one of the basic use cases of DFS, which is traversing all vertices in a graph.

Consider an example graph given below:

2022_12_image-62.jpg

Now the task is to start traversing this graph from node A and access each node using the DFS algorithm. We will cover it in the form of steps:

As mentioned above, we will use stack to complete our traversal.

Before proceeding, let’s have some assumptions throughout the process to keep the explanations simple.

  1. Each node is represented with a different color.

Also Read: Merge Sort Algorithm

Also Read: Insertion Sort Algorithm

Steps to traverse:

  1. Pick β€˜Aβ€˜ and put it into the stack. 
  2. Remove β€˜Aβ€˜ from the stack and put its adjacent nodes (β€˜Bβ€˜, β€˜Dβ€˜, β€˜Cβ€˜, and β€˜Gβ€˜) into the stack. Mark β€˜Aβ€˜ visited (the visited nodes are represented in red color) and print it in output.
2022_12_image-63.jpg
2022_12_image-64.jpg

3. Consider the top element of the stack β€˜Gβ€˜, remove it from the stack, and put the adjacent nodes of β€˜Gβ€˜ (β€˜Cβ€˜ and β€˜Aβ€˜) into the stack. Mark β€˜Gβ€˜ as visited and print it in output.

2022_12_image-65.jpg

4. The top of the stack is β€˜Aβ€˜; remove β€˜Aβ€˜ from the stack. Before traversing towards β€˜Aβ€˜, check if this node is visited or not. Since β€˜Aβ€˜ is already visited so you do not need to revisit it, and we can ignore it.

2022_12_image-66.jpg

5. Pop β€˜Cβ€˜ and check if it visited or not. Since β€˜Cβ€˜ is not visited, add the adjacent node of β€˜Cβ€˜ (β€˜Eβ€˜, β€˜Aβ€˜, and β€˜Gβ€˜) to the stack. Mark β€˜Cβ€˜ as visited and print it in output.

2022_12_image-67.jpg

6. The top of the stack is β€˜Gβ€˜; remove β€˜Gβ€˜ from the stack. Before traversing towards β€˜Gβ€˜, check if this node is visited or not. Since β€˜Gβ€˜ is already visited so, you do not need to revisit it, and we can ignore it.

2022_12_image-68.jpg

7. Now the top of the stack is β€˜Aβ€˜; remove β€˜Aβ€˜ from the stack. Before traversing towards β€˜Aβ€˜, check if this node is visited or not. Since β€˜Aβ€˜ is already visited so, you do not need to revisit it, and we can ignore it. The stack looks like this.

2022_12_image-69.jpg

8. Now come back to β€˜Cβ€˜ and check if any edge is left; we have β€˜Eβ€˜ left. Pop β€˜Eβ€˜ and check if it visited or not. Since β€˜Eβ€˜ is not visited, add the adjacent node of β€˜Eβ€˜ (β€˜Fβ€˜, β€˜Bβ€˜, β€˜Dβ€˜, and β€˜Cβ€˜) and add it to the stack. Mark β€˜Eβ€˜ as visited and print it in output.

2022_12_image-70.jpg

9. The top of the stack is β€˜Cβ€˜; remove β€˜Cβ€˜ from the stack. Before traversing towards β€˜Cβ€˜, check if this node is visited or not. Since β€˜Cβ€˜ is already visited so, you do not need to revisit it, and we can ignore it.

2022_12_image-71.jpg

10. The top of the stack is β€˜Dβ€˜; remove β€˜Dβ€˜ from the stack. Pop β€˜Dβ€˜ and check if visited or not. Since β€˜Dβ€˜ is not visited, add the adjacent node of β€˜Dβ€˜ (β€˜Eβ€˜ and β€˜Aβ€˜) and add it to the stack. Mark β€˜Dβ€˜ as visited and print it in output.

2022_12_image-72.jpg

11. Now the top of the stack is β€˜Aβ€˜; remove β€˜Aβ€˜ from the stack. Before traversing towards β€˜Aβ€˜, check if this node is visited or not. Since β€˜Aβ€˜ is already visited so, you do not need to revisit it, and we can ignore it.

12. The top of the stack is β€˜Eβ€˜; remove β€˜Eβ€˜ from the stack. Before traversing towards β€˜Eβ€˜, check if this node is visited or not. Since β€˜Eβ€˜ is already visited so, you do not need to revisit it, and we can ignore it. The stack looks like this.

2022_12_image-73.jpg

13. Now come back to β€˜Eβ€˜ and check if any edge is left; we have β€˜Bβ€˜ and β€˜Fβ€˜ left, as seen in the stack. Pop β€˜Bβ€˜ and check if it visited or not. Since β€˜Bβ€˜ is not visited, add the adjacent node of β€˜Bβ€˜ (β€˜Fβ€˜, β€˜Eβ€˜, and β€˜Aβ€˜) and add it to the stack. Mark β€˜Bβ€˜ as visited and print it in output.

2022_12_image-74.jpg

14. Now the top of the stack is β€˜Aβ€˜; remove β€˜Aβ€˜ from the stack. Before traversing towards β€˜Aβ€˜, check if this node is visited or not. Since β€˜Aβ€˜ is already visited so, you do not need to revisit it, and we can ignore it. The stack looks like this.

15. The top of the stack is β€˜Eβ€˜; remove β€˜Eβ€˜ from the stack. Before traversing towards β€˜Eβ€˜, check if this node is visited or not. Since β€˜Eβ€˜ is already visited so, you do not need to revisit it, and we can ignore it.

2022_12_image-75.jpg

16. The top of the stack is β€˜Fβ€˜; remove β€˜Fβ€˜ from the stack. Pop β€˜Fβ€˜ and check if it visited or not. Since β€˜Fβ€˜ is not visited, add the adjacent node of β€˜Fβ€˜ (β€˜Eβ€˜ and β€˜Bβ€˜) and add it to the stack. Mark β€˜Fβ€˜ as visited and print it in output.

2022_12_image-76.jpg

17. Now the top of the stack is β€˜Bβ€˜; remove β€˜Bβ€˜ from the stack. Before traversing towards β€˜Bβ€˜, check if this node is visited or not. Since β€˜Bβ€˜ is already visited so, you do not need to revisit it, and we can ignore it. 

18. The top of the stack is β€˜Eβ€˜; remove β€˜Eβ€˜ from the stack. Before traversing towards β€˜Eβ€˜, check if this node is visited or not. Since β€˜Eβ€˜ is already visited so, you do not need to revisit it, and we can ignore it.

2022_12_image-77.jpg

19. All nodes present in the stack have been visited, you can check it in sequence, and at the end stack gets empty, and the final transversal output looks like this:

2022_12_image-78.jpg

Pseudo Code

 
//Pseudo code for DFS
DFS(adjacent[][], source, visited[], key) {
if(source == key) return true //We found the key
visited[source] = True
FOR node in adjacent[source]:
IF visited[node] == False:
DFS(adjacent, node, visited)
END IF
END FOR
return false // If it reaches here, then all nodes have been explored and we still havent found the key.
}
Copy code

Following is the java implementation of the DFS algorithm:

 
import java.io.*;
import java.util.*;
//Representing directed graph using adjacency
// list representation
class Graph {
private int V; // No. of vertices
private LinkedList
\n \n <integer>\n \n
adjacency[];\n \n
\n \n
// Constructor\n \n
Graph(int v)\n \n
{\n \n
V = v;\n \n
adjacency = new LinkedList[v];\n \n
for (int i = 0; i < v; ++i)\n \n
adjacency[i] = new LinkedList();\n \n
}\n \n
\n \n
// Function to add an edge into the graph\n \n
void addEdge(int v, int w)\n \n
{\n \n
adjacency[v].add(w); // Add w to v's list.\n \n
}\n \n
\n \n
// A function used by DFS\n \n
void dfs(int v, boolean visited[])\n \n
{\n \n
// Mark the current node as visited and print it\n \n
visited[v] = true;\n \n
System.out.print(v + " ");\n \n
\n \n
// Recur for all the vertices adjacent to this\n \n
// vertex\n \n
Iterator\n \n
\n \n <integer>\n \n
i = adjacency[v].listIterator();\n \n
while (i.hasNext()) {\n \n
int n = i.next();\n \n
if (!visited[n])\n \n
dfs(n, visited);\n \n
}\n \n
}\n \n
\n \n
// The function to do DFS traversal. It uses recursive\n \n
// dfs()\n \n
void depthFirst()\n \n
{\n \n
// Mark all the vertices as not visited(set as\n \n
// false by default in java)\n \n
boolean visited[] = new boolean[V];\n \n
\n \n
// Call the recursive helper function to print DFS\n \n
// traversal starting from all vertices one by one\n \n
for (int i = 0; i < V; ++i)\n \n
if (visited[i] == false)\n \n
dfs(i, visited);\n \n
}\n \n
\n \n
public static void main(String args[])\n \n
{\n \n
Graph grp = new Graph(4);\n \n
\n \n
grp.addEdge(0, 1);\n \n
grp.addEdge(0, 2);\n \n
grp.addEdge(1, 2);\n \n
grp.addEdge(2, 3);\n \n
grp.addEdge(3, 4);\n \n
grp.addEdge(4, 1);\n \n
\n \n
// Function call\n \n
grp.depthFirst();\n \n
}\n \n
}\n \n
\n \n
\n \n </integer>\n \n
\n \n </integer>
Copy code

Application of DFS Algorithm

The following uses for the DFS algorithm are listed:

  • The topological sorting can be implemented using the DFS algorithm.
  • It can be applied to determine the routes connecting two vertices.
  • It can also be used to find graph cycles.
  • DFS technique is also applied to puzzles with a single solution.

Also Read: All about Heap Sort Technique

Also Read: Divide and Conquer Algorithm

Conclusion

In this article, we have briefly discussed how Depth First Search Algorithm works and its application.

Hope you will like the article.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio