LearnViz CompSci
Home / Learn / Algorithms / Depth-First Search

Intermediate Topic 32 of 51 Algorithms

Depth-First Search

Exploring a graph as deep as possible before backtracking.

How it works

Depth-first search explores as far as possible along each branch before backtracking, using recursion or an explicit stack. It records discovery and finishing times, runs in O(V + E) and classifies edges as tree, back, forward or cross edges, which reveals cycles and structure. Memory is proportional to the path depth.

Where it is used

Detecting cycles, topological sorting, finding connected and strongly connected components, solving mazes, backtracking search, finding articulation points and bridges and traversing file systems use DFS.

Common pitfall

Recursive DFS can overflow the stack on deep graphs, so use an explicit stack. Forgetting a visited set causes infinite loops on cyclic graphs. DFS does not find shortest paths. Handle disconnected graphs by starting a search from every unvisited vertex.

Where it fits in the course

Depth-First Search is topic 32 of 51 in Algorithms, rated Intermediate. It follows Breadth-First Search. The next topic is Topological Sort.

Practice and explore

How to study this topic

  1. Read the explanation above and restate the definition in your own words.
  2. Open the interactive lesson and answer its mini quiz.
  3. Read the common pitfall, then write a short example that avoids it.
  4. Check your understanding in the Algorithms quiz, and revisit the topic if you miss a question.

Frequently asked questions

What is Depth-First Search?
Exploring a graph as deep as possible before backtracking. It belongs to Algorithms and is an intermediate-level topic.
Where do people use Depth-First Search?
Detecting cycles, topological sorting, finding connected and strongly connected components, solving mazes, backtracking search, finding articulation points and bridges and traversing file systems use DFS.
What is the most common mistake with Depth-First Search?
Recursive DFS can overflow the stack on deep graphs, so use an explicit stack.
What should I learn before this topic?
Start with Breadth-First Search, the topic just before it in Algorithms.
How can I practice it?
Answer the mini quiz in the interactive lesson, then take the Algorithms quiz. You can also explore Graph Traversal in the Visual Lab.
Is Depth-First Search hard to learn?
It is an Intermediate topic. Be comfortable with the Beginner topics in this area first, then read the common pitfall carefully, since it is where most mistakes happen.
How long does it take to learn Depth-First Search?
Most learners can read the explanation and finish the interactive lesson in under an hour. Fluency comes from applying the idea, so plan a few short practice sessions with the quiz.
Which topics are related to Depth-First Search?
Breadth-First Search, Topological Sort, N-Queens Problem are closely connected. The full list is in the related topics section of this page.
What should I study after Depth-First Search?
Continue with Topological Sort, the next topic in Algorithms.

Related topics