Search for a command to run...
Graph playground
Edit undirected edges and replay the algorithm from a new start node.
Initialize the DFS stack
Start at node 0 and push it onto the stack.
Depth-First Search
1function dfs(graph, start) {2 const stack = [start];3 const visited = new Set([start]);//start: 0stack: [0]visited: [0]4 5 while (stack.length > 0) {6 const current = stack.pop();7 for (const neighbor of graph[current]) {8 if (!visited.has(neighbor)) {9 visited.add(neighbor);10 stack.push(neighbor);11 }12 }13 }14}