Algorithm

boolean iddfs(Node root, Node target) {
  for (int depth = 0; depth < MAX; depth++) {
    if (dls(root, target, depth)) return true;
  }
  return false;
}
boolean dls(Node n, Node target, int limit) {
  if (n == target) return true;
  if (limit == 0) return false;
  for (Node child : children(n))
    if (dls(child, target, limit - 1)) return true;
  return false;
}
Advertisement

Time analysis

Level d visited N × more times over iterations. Total: O(b^d) — same as BFS asymptotically, way less memory.

Advertisement

Space O(d)

DFS stack only. Vs BFS's O(b^d) queue.

Applications

Game trees (chess). AI search when depth unknown. Memory-constrained environments.