Open In App

Applications, Advantages and Disadvantages of Depth First Search (DFS)

Last Updated : 15 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Depth First Search is a widely used algorithm for traversing a graph. Here we have discussed some applications, advantages, and disadvantages of the algorithm.

Applications of Depth First Search:

1. Detecting cycle in a graph: A graph has a cycle if and only if we see a back edge during DFS. So we can run DFS for the graph and check for back edges.

2. Path Finding: We can specialize the DFS algorithm to find a path between two given vertices u and z. 

  • Call DFS(G, u) with u as the start vertex. 
  • Use a stack S to keep track of the path between the start vertex and the current vertex. 
  • As soon as destination vertex z is encountered, return the path as the contents of the stack

3. Topological Sorting: Topological Sorting is mainly used for scheduling jobs from the given dependencies among jobs. In computer science, applications of this type arise in instruction scheduling, ordering of formula cell evaluation when recomputing formula values in spreadsheets, logic synthesis, determining the order of compilation tasks to perform in makefiles, data serialization, and resolving symbol dependencies in linkers.

4. To test if a graph is bipartite: We can augment either BFS or DFS when we first discover a new vertex, color it opposite its parents, and for each other edge, check it doesn’t link two vertices of the same color. The first vertex in any connected component can be red or black.

5. Finding Strongly Connected Components of a graph: A directed graph is called strongly connected if there is a path from each vertex in the graph to every other vertex. (See this for DFS-based algo for finding Strongly Connected Components)

6. Solving puzzles with only one solution: such as mazes. (DFS can be adapted to find all solutions to a maze by only including nodes on the current path in the visited set.).

7. Web crawlers: Depth-first search can be used in the implementation of web crawlers to explore the links on a website.

8. Maze generation: Depth-first search can be used to generate random mazes.

9. Model checking: Depth-first search can be used in model checking, which is the process of checking that a model of a system meets a certain set of properties.

10. Backtracking: Depth-first search can be used in backtracking algorithms.

Advantages of Depth First Search:

  • Memory requirement is only linear with respect to the search graph. This is in contrast with breadth-first search which requires more space. The reason is that the algorithm only needs to store a stack of nodes on the path from the root to the current node.
  • The time complexity of a depth-first Search to depth d and branching factor b (the number of children at each node, the outdegree) is O(bd) since it generates the same set of nodes as breadth-first search, but simply in a different order. Thus practically depth-first search is time-limited rather than space-limited.
  •  If depth-first search finds solution without exploring much in a path then the time and space it takes will be very less.
  • DFS requires less memory since only the nodes on the current path are stored. By chance DFS may find a solution without examining much of the search space at all.

Disadvantages of Depth First Search:

  • The disadvantage of Depth-First Search is that there is a possibility that it may down the left-most path forever. Even a finite graph can generate an infinite solution to this problem is to impose a cutoff depth on the search. Although ideal cutoff is the solution depth d and this value is rarely known in advance of actually solving the problem. If the chosen cutoff depth is less than d, the algorithm will fail to find a solution, whereas if the cutoff depth is greater than d, a large price is paid in execution time, and the first solution found may not be an optimal one.
  •  Depth-First Search is not guaranteed to find the solution.
  •  And there is no guarantee to find a minimal solution, if more than one solution.

Previous Article
Next Article

Similar Reads

Time and Space Complexity of Depth First Search (DFS)
The time complexity of Depth First Search (DFS) is O(V + E), where V is the number of vertices and E is the number of edges. The space complexity is O(V) for a recursive implementation. Below are the detailed explanation of Time and Space Complexity of Depth First Search (DFS): Time Complexity of Depth First Search (DFS):Best Case of Depth First Se
2 min read
Depth First Search or DFS for disconnected Graph
Given a Disconnected Graph, the task is to implement DFS or Depth First Search Algorithm for this Disconnected Graph. Example: Input: Output: 0 1 2 3 Algorithm for DFS on Disconnected Graph:In the post for Depth First Search for Graph, only the vertices reachable from a given source vertex can be visited. All the vertices may not be reachable from
7 min read
Top 10 Interview Questions on Depth First Search (DFS)
Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. One starts at the root (selecting some arbitrary node as the root in the case of a graph) and explores as far as possible along each branch before backtracking. Here are some important DFS problems asked in Technical Interviews: Find number of island
1 min read
Depth First Search or DFS for a Graph
Depth First Traversal (or DFS) for a graph is similar to Depth First Traversal of a tree. The only catch here is, that, unlike trees, graphs may contain cycles (a node may be visited twice). To avoid processing a node more than once, use a boolean visited array. A graph can have more than one DFS traversal. Example:  Input: n = 4, e = 6 0 -> 1,
8 min read
Applications, Advantages and Disadvantages of Breadth First Search (BFS)
We have earlier discussed Breadth First Traversal Algorithm for Graphs. Here in this article, we will see the applications, advantages, and disadvantages of the Breadth First Search. Applications of Breadth First Search: 1. Shortest Path and Minimum Spanning Tree for unweighted graph: In an unweighted graph, the shortest path is the path with the l
4 min read
Applications, Advantages and Disadvantages of Binary Search Tree
A Binary Search Tree (BST) is a data structure used to storing data in a sorted manner. Each node in a Binary Search Tree has at most two children, a left child and a right child, with the left child containing values less than the parent node and the right child containing values greater than the parent node. This hierarchical structure allows for
2 min read
Applications, Advantages and Disadvantages of Binary Search
Binary Search is defined as a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary search is a versatile algorithm with numerous applications in various fields. Applications of Bin
2 min read
Depth First Traversal ( DFS ) on a 2D array
Given a 2D array grid[][] of dimension N * M, the task is to perform the Depth - First Search traversal on the given 2D array. Examples: Input: grid[][] = {{-1, 2, 3}, {0, 9, 8}, {1, 0, 1}}Output: -1 2 3 8 1 0 9 0 1Explanation: The sequence of traversal of matrix elements using DFS is -1, 2, 3, 8, 1, 0, 9, 0, 1. Input: grid[][] = {{1, 2, 3}, {5, 6,
10 min read
Iterative Deepening Search(IDS) or Iterative Deepening Depth First Search(IDDFS)
There are two common ways to traverse a graph, BFS and DFS. Considering a Tree (or Graph) of huge height and width, both BFS and DFS are not very efficient due to following reasons. DFS first traverses nodes going through one adjacent of root, then next adjacent. The problem with this approach is, if there is a node close to root, but not in first
10 min read
Applications, Advantages and Disadvantages of Branch and Bound Algorithm
Branch and bound algorithm is a method used in computer science to find the best solution to optimization problems. It systematically explores all potential solutions by breaking the problem down into smaller parts and then uses limits or rules to prevent certain parts from being considered. Applications of Branch and Bound:Combinatorial Optimizati
2 min read
Article Tags :
Practice Tags :