AlgoPrecision

Graph Visualizer

Graph Traversal

Trace BFS, DFS, and Dijkstra across a weighted directed graph with frontier, visited, settled, and distance state.

O(V + E)
4251731ABCDEF

Step Note

Start DFS with the chosen node on the stack.

Details
function dfs(graph: Graph, start: Node) {
  const stack = [start]
  while (stack.length > 0) {
    const node = stack.pop()
    visit(node)
    for (const neighbor of graph.neighbors(node)) {
      stack.push(neighbor)
    }
  }
}