Iterative deepening DFS: Examples & Code

Algorithm Visualizations November 16, 2026 javascript

Published by Dimitris Kougioumtzis · Updated

Search from node 0 to the highest numbered node. Ignore weights; revisit a node if a shallower path is discovered.

Repeat depth-limited DFS at increasing limits until success or the supported depth bound is exhausted.

Open the 3D visualization

Iterative deepening DFS example state preview
Preview of the example's final state. Complete values are available in the worked example and interactive timeline.

When to use Iterative deepening DFS

Comparing repeated shallow work with BFS layer exploration.

Choosing an alternative

BFS visits layers once; iterative deepening revisits earlier work.

Worked example: input and state changes

Example input

0,1,4; 0,2,2; 2,1,1; 1,3,5; 2,3,8; 3,4,2

These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.

  1. Step 0: Start DFS with depth limit 0.

    State at this checkpoint
    {
      "values": [
        0,
        "∞",
        "∞",
        "∞",
        "∞"
      ],
      "active": [],
      "kind": "graph",
      "visited": [],
      "chosen": []
    }
  2. Step 6: Visit node 1 at depth 1.

    State at this checkpoint
    {
      "values": [
        0,
        1,
        1,
        "∞",
        "∞"
      ],
      "active": [
        1
      ],
      "kind": "graph",
      "visited": [
        0,
        1
      ],
      "chosen": []
    }
  3. Step 13: Push node 3 at depth 2.

    State at this checkpoint
    {
      "values": [
        0,
        1,
        1,
        2,
        "∞"
      ],
      "active": [
        3
      ],
      "kind": "graph",
      "visited": [
        0,
        1
      ],
      "chosen": []
    }
  4. Step 19: Push node 1 at depth 1.

    State at this checkpoint
    {
      "values": [
        0,
        1,
        1,
        "∞",
        "∞"
      ],
      "active": [
        1
      ],
      "kind": "graph",
      "visited": [
        0
      ],
      "chosen": []
    }
  5. Step 25: Path length = 3; path 0 → 1 → 3 → 4.

    State at this checkpoint
    {
      "values": [
        0,
        1,
        1,
        2,
        3
      ],
      "active": [
        0,
        1,
        3,
        4
      ],
      "kind": "graph",
      "visited": [
        0,
        1,
        3,
        4
      ],
      "chosen": [],
      "path": [
        0,
        1,
        3,
        4
      ],
      "result": 3
    }

Expected final state

Path length = 3; path 0 → 1 → 3 → 4.

Complexity and input bounds

Family: Graphs. Time complexity: O(DVE) across depth passes.

Space complexity: O(V) state per pass, excluding trace history.

Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.

Directed edges as source,target,weight; separated by semicolons. Nodes 0–7, positive weights 1–99. Start node is 0.

The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.

Common mistakes and limitations

Reset per-pass state; this graph-pruning variant is not the same implementation as an unpruned search tree.

Further reading

For background on algorithm analysis and the JavaScript function interface:

Editorial and testing approach · Browse all algorithm guides

Complete JavaScript implementation

javascript
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Directed graph with 1–8 vertices and up to 16 edges; weights ignored. Source 0, goal vertices-1. Revisit nodes on shallower paths. Writes path indices and returns moves or -1. Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation.
function algorithm(from, to, weight, edges, vertices, output) {
    let limit = vertices - 1;
    let best = Array(256).fill(0); let parent = Array(256).fill(0); let stack = Array(256).fill(0); let depth = Array(256).fill(0);
    for (let bound = 0; bound <= limit; bound++) {
        for (let v = 0; v < vertices; v++) { best[v] = 1000000; parent[v] = -1; }
        best[0] = 0; let top = 1; stack[0] = 0; depth[0] = 0;
        while (top > 0) {
            top--; let u = stack[top]; let d = depth[top];
            if (d != best[u]) { continue; }
            if (u == vertices - 1) {
                let path = Array(256).fill(0); let count = 0;
                for (let v = u; v >= 0; v = parent[v]) { path[count++] = v; }
                for (let i = 0; i < count; i++) { output[i] = path[count - 1 - i]; }
                return count - 1;
            }
            if (d == bound) { continue; }
            for (let i = edges - 1; i >= 0; i--) {
                let v = to[i];
                if (from[i] == u && d + 1 < best[v]) { best[v] = d + 1; parent[v] = u; stack[top] = v; depth[top++] = d + 1; }
            }
        }
    }
    return -1;
}

Source

Korf is the primary research source for iterative deepening; the playground uses a bounded graph-search variant.