Interview concept

What is depth-first search (DFS)?

Depth-first search explores one branch of a graph fully before backtracking, using recursion or an explicit stack. It suits path finding, cycle detection, and topological ordering.

Interview concept

How it works

Recursive or stack-based

Implement DFS with recursion or an explicit stack plus a visited set to avoid revisiting nodes.

Uses

DFS supports cycle detection, connected components, topological sort, and exhaustive path search.

When to use it

Prefer DFS when you must explore full paths or structure, not the shortest one.

Interview concept

Responsible use

Use any live AI assistant only where interview, workplace, school, and platform rules allow it. Do not use generated answers to misrepresent your skills, experience, or authorship.

FAQ

Common questions.

When should I use DFS?

Use DFS for exhaustive path exploration, cycle detection, connected components, and topological sorting.

What is the complexity of DFS?

DFS runs in O(V + E) time and O(V) space for the visited set and recursion or stack.