How iterative deepening DFS saves the frontier

Short answer: Iterative deepening depth-first search runs a depth-limited depth-first search with limit 0, then 1, then 2, and so on, until the target is found. On an unweighted graph the first success is a shortest path. The space is O(d) for the current path, not O(b^d) for a breadth-first frontier. The repeated work sums to a small constant factor, because the last depth dominates, and the time stays O(b^d).


What the problem actually asks

Breadth-first search finds a minimum-hop path and stores every vertex on the frontier. In a game tree or a state graph with branching factor b, that frontier is the thing that runs out of memory. Depth-first search stores only the current path and may find a very long route first.

Repeating a depth-first search with a growing cap combines the two. Each capped search is cheap relative to the next one. The successful cap equals the hop distance.


What the algorithm guarantees

A depth-limited search with limit L expands nodes at depth up to L and uses O(L) stack space. Iterating L from 0 upward, the first time the target is generated the path on the stack has L edges and is a shortest path in an unweighted graph. The total number of expansions is b^0 + b^1 + … + b^d, which is at most b^{d+1}/(b−1) for b > 1, a constant factor times the size of the last level. Extra memory beyond the stack is whatever duplicate detection the state graph requires.

Without a visited set, the algorithm can rewalk exponential DAGs of states and can loop only if the depth cap fails to stop a cycle. A visited set that persists across depths reintroduces BFS-sized memory. A visited set that lives only inside one depth-limited search is the usual compromise, and it must be undone on backtracking if the same state can be reached by a different path of equal depth.


The working steps

  1. For limit from 0 upward, start a depth-first search at the source that refuses to extend a path of length limit.
  2. If the target is generated, return the path on the stack. Its length is the minimum hop count.
  3. If a depth-limited search generates no new vertex at all, the target is unreachable and the iteration stops.
  4. Bound the limit by a known maximum, such as the number of vertices, when a missing target must be reported and no other termination test exists.
  5. Keep parent pointers or an explicit stack of the current path so the successful search can emit the route without a second pass.

The shortest-path claim is the unweighted one. A weighted graph needs iterative deepening on path cost, which is IDA* when a heuristic sets the next bound. The constant-factor overhead assumes a branching factor bounded away from 1. A long path graph has b = 1, and the repeated scans cost Θ(d²) rather than Θ(d).


Approaches that look simpler and fail

Approach Why it fails when the depth cap is mistaken for a weight cap
Return the first path a plain DFS happens to find That path can be longer than the hop distance. The growing limit is what forces the first success to be shortest.
Keep a global visited set across increasing limits A vertex first reached by a long path is then blocked, and a later shallower search cannot use it. Visited marks have to respect the current limit.
Use the same iteration on unequal edge weights A path with more edges can weigh less. The cap has to be on weight, or on f, as in IDA*.
Ignore the repeated work and quote the space bound as a time improvement over BFS Time stays proportional to the BFS tree. The improvement is the O(d) stack.

A frontier that fits in memory belongs to BFS, which avoids the repeated expansions. A heuristic and a cost bound belong to IDA*.


A working example

This Python fragment deepens a depth-first search one limit at a time. The first time the goal is reached, the depth is the unweighted distance. It prints 2.

adj = {0: [1, 2], 1: [3], 2: [3], 3: []}

def dls(u, goal, limit, seen):
    if u == goal:
        return True
    if limit == 0:
        return False
    for v in adj[u]:
        if v not in seen:
            seen.add(v)
            if dls(v, goal, limit - 1, seen):
                return True
            seen.remove(v)
    return False

depth = 0
while not dls(0, 3, depth, {0}):
    depth += 1
assert depth == 2
print(depth)

Where it is used

Iterative deepening depth-first search explores a game tree or a puzzle when a breadth-first frontier will not fit. Each pass repeats the shallow work and goes one ply deeper. IDA* adds the f-cost bound on top of the same idea.


When you can drop this algorithm

Drop iterative deepening when the frontier fits in memory. BFS is faster by the constant the repeated levels spend. Keep it when an unweighted shortest path is required and the frontier of a BFS will not fit, which is the usual case in large state spaces.


Why this stays published

We publish this so a search for iterative deepening returns the growing depth cap, the O(d) stack, and the constant-factor repeated work. Iterative deepening DFS finds an unweighted shortest path with O(d) memory by repeating a depth-limited search, and the last depth dominates the running time.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *