Skip to content

Resource · Guide or article

Depth-First Search (DFS)

Gives a short definition of depth-first search: from a root, or any node picked as root in a graph, follow each branch as far as it goes and then backtrack.

Reviewed

Open the original: Depth-First Search (DFS) (opens the original site in a new tab)

Practice this topicOpens the practice interviewer with a question on Graphs.

Add to a roadmap

Keep this resource with a roadmap you follow. It appears as your own extra resource; the roadmap's steps don't change and it doesn't count toward your tracked steps.

Source content: English

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.

PrepKit overview

PrepKit editors wrote and reviewed this overview from the source repository. It is not text from the resource itself.

Suited to
The source does not state a level
Format
Reference

What it covers

  • Branch-first exploration with backtracking, shown in an animated diagram
  • Links to tree traversal orders (inorder, preorder, postorder), a BFS versus DFS comparison and an interactive DFS visualizer

How to use it

PrepKit study advice, not a claim made by the source.

Write DFS recursively and with an explicit stack, confirm both give a valid order, then use it to count connected components.

Scope and limitations

Idea only. Pseudocode, complexity, recursion depth limits and applications are absent, so you will need the linked pages or another source.

Access and format

Free English README on GitHub in the MIT-licensed trekhleb/javascript-algorithms repository.

Reviewed by PrepKit on Oct 2, 2026. Listing source commit: 85293e3e2b88

Evidence behind this overview
  • src/algorithms/graph/depth-first-search/README.md

Related preparation

These are PrepKit study suggestions, not employer endorsements or predictions of interview questions.

Pages that include this

Get the PrepKit newsletter

Optional. You don't need it to use PrepKit.

How we handle your email