Depth-first search with explicit visited state
Quick answer
Depth-first search (DFS) explores one branch before returning to alternatives. An explicit stack or recursive calls store the unfinished work; a visited state prevents cycles from repeating forever.
Working examples
Run as an independent script in Node.js v22.16.0. ByteMind editor support has not been verified.
const adj=[[1,2],[3],[],[0],[]],seen=new Set(),out=[];function dfs(u){if(seen.has(u))return;seen.add(u);out.push(u);for(const v of adj[u])dfs(v);}dfs(0);console.log(JSON.stringify(out));Expected console output:
[0,1,3,2]The first branch is explored deeply, and the return edge to zero is ignored.
Run as an independent script in Node.js v22.16.0. ByteMind editor support has not been verified.
const adj=[[1],[],[3],[]],seen=new Set(),starts=[];function visit(u){if(seen.has(u))return;seen.add(u);for(const v of adj[u])visit(v);}for(let u=0;u<adj.length;u++)if(!seen.has(u)){starts.push(u);visit(u);}console.log(JSON.stringify(starts),seen.size);Expected console output:
[0,2] 4Two start points are needed to reach every vertex in this graph.
How it works and cost model
For a graph with indexed vertices, recursive DFS marks a vertex before visiting neighbors. It reaches every vertex accessible from the start, in adjacency order, but does not guarantee a shortest path. Iterative DFS can use stack frames holding the vertex and next neighbor index to reproduce recursive ordering exactly, with at most O(V) frames. A simpler stack of pending vertices may contain duplicates if marking is delayed to pop; distinguish order and space trade-offs. For one recursive traversal, using a Boolean visited array and adjacency lists, time O(V+E) including initialization and O(V) auxiliary storage; deep recursion may exhaust the runtime stack. Start additional traversals for undiscovered vertices when full graph coverage is needed.
Common mistakes
A visited set proves reachability, not that a directed graph has a cycle. Directed-cycle detection needs active-path state as well as completed state.
Practice
Trace recursive DFS and BFS on [[1,2],[3],[],[0],[]] from 0. Compare order and explain why vertex 4 is not visited.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Depth-first search with explicit visited state — Knowledge Check
Reason about the algorithm or data structure, its invariants, trade-offs and deterministic outputs.
What prevents recursive DFS from looping around a cycle indefinitely?
Which property is not guaranteed by ordinary DFS from a start vertex?
How can a traversal cover disconnected or unreachable-from-start portions of a graph?
What is the exact console output in the Depth-first search preorder example 1?
const adj=[[1,2],[3],[],[0],[]],seen=new Set(),out=[];function dfs(u){if(seen.has(u))return;seen.add(u);out.push(u);for(const v of adj[u])dfs(v);}dfs(0);console.log(JSON.stringify(out));What is the exact console output in the Depth-first search preorder example 2?
const adj=[[1],[],[3],[]],seen=new Set(),starts=[];function visit(u){if(seen.has(u))return;seen.add(u);for(const v of adj[u])visit(v);}for(let u=0;u<adj.length;u++)if(!seen.has(u)){starts.push(u);visit(u);}console.log(JSON.stringify(starts),seen.size);