Iterative deepening depth-first search

Class Search algorithm Tree, Graph ${\displaystyle O(b^{d})}$, where ${\displaystyle b}$ is the branching factor and ${\displaystyle d}$ is the depth of the shallowest solution ${\displaystyle O(d)}$[1]:5

In computer science, iterative deepening search or more specifically iterative deepening depth-first search[2] (IDS or IDDFS) is a state space/graph search strategy in which a depth-limited version of depth-first search is run repeatedly with increasing depth limits until the goal is found. IDDFS is optimal like breadth-first search, but uses much less memory; at each iteration, it visits the nodes in the search tree in the same order as depth-first search, but the cumulative order in which nodes are first visited is effectively breadth-first.

Algorithm for Directed Graphs

The following pseudocode shows IDDFS implemented in terms of a recursive depth-limited DFS (called DLS) for directed graphs. This implementation of IDDFS does not account for already-visited nodes and therefore does not work for undirected graphs.

function IDDFS(root)
for depth from 0 to ∞
found, remaining ← DLS(root, depth)
if found ≠ null
return found
else if not remaining
return null

function DLS(node, depth)
if depth = 0
if node is a goal
return (node, true)
else

else if depth > 0
any_remaining ← false
foreach child of node
found, remaining ← DLS(child, depth−1)
if found ≠ null
return (found, true)
if remaining
any_remaining ← true    (At least one node found at depth, let IDDFS deepen)
return (null, any_remaining)


If the goal node is found, then DLS unwinds the recursion returning with no further iterations. Otherwise, if at least one nodes exist at that level of depth, the remaining flag will let IDDFS continue.

Notice the need to use 2-tuples as return value to signal IDDFS to continue deepening or stop, in case tree depth and goal membership are unknown a priori. Another solution could use sentinel values instead to represent not found or remaining level results.

Properties

IDDFS combines depth-first search's space-efficiency and breadth-first search's completeness (when the branching factor is finite). If a solution exists, it will find a solution path with the fewest number of arcs.[3]

Since iterative deepening visits states multiple times, it may seem wasteful, but it turns out to be not so costly, since in a tree most of the nodes are in the bottom level, so it does not matter much if the upper levels are visited multiple times.[4]

The main advantage of IDDFS in game tree searching is that the earlier searches tend to improve the commonly used heuristics, such as the killer heuristic and alpha-beta pruning, so that a more accurate estimate of the score of various nodes at the final depth search can occur, and the search completes more quickly since it is done in a better order. For example, alpha-beta pruning is most efficient if it searches the best moves first.[4]

A second advantage is the responsiveness of the algorithm. Because early iterations use small values for ${\displaystyle d}$, they execute extremely quickly. This allows the algorithm to supply early indications of the result almost immediately, followed by refinements as ${\displaystyle d}$ increases. When used in an interactive setting, such as in a chess-playing program, this facility allows the program to play at any time with the current best move found in the search it has completed so far. This can be phrased as each depth of the search corecursively producing a better approximation of the solution, though the work done at each step is recursive. This is not possible with a traditional depth-first search, which does not produce intermediate results.

Asymptotic analysis

Time complexity

The time complexity of IDDFS in a (well-balanced) tree works out to be the same as breadth-first search, i.e. ${\displaystyle O(b^{d})}$,[1]:5 where ${\displaystyle b}$ is the branching factor and ${\displaystyle d}$ is the depth of the goal.

Proof

In an iterative deepening search, the nodes at depth ${\displaystyle d}$ are expanded once, those at depth ${\displaystyle d-1}$ are expanded twice, and so on up to the root of the search tree, which is expanded ${\displaystyle d+1}$ times.[1]:5 So the total number of expansions in an iterative deepening search is

${\displaystyle b^{d}+2b^{d-1}+3b^{d-2}+\cdots +(d-1)b^{2}+db+(d+1)=\sum _{i=0}^{d}(d+1-i)b^{i}}$

where ${\displaystyle b^{d}}$ is the number of expansions at depth ${\displaystyle d}$, ${\displaystyle 2b^{d-1}}$ is the number of expansions at depth ${\displaystyle d-1}$, and so on. Factoring out ${\displaystyle b^{d}}$ gives

${\displaystyle b^{d}(1+2b^{-1}+3b^{-2}+\cdots +(d-1)b^{2-d}+db^{1-d}+(d+1)b^{-d})}$

Now let ${\displaystyle x={\frac {1}{b}}=b^{-1}}$. Then we have

${\displaystyle b^{d}(1+2x+3x^{2}+\cdots +(d-1)x^{d-2}+dx^{d-1}+(d+1)x^{d})}$

This is less than the infinite series

${\displaystyle b^{d}(1+2x+3x^{2}+4x^{3}+\cdots )=b^{d}\left(\sum _{n=1}^{\infty }nx^{n-1}\right)}$

which converges to

${\displaystyle b^{d}(1-x)^{-2}=b^{d}{\frac {1}{(1-x)^{2}}}}$, for ${\displaystyle abs(x)<1}$

That is, we have

${\displaystyle b^{d}(1+2x+3x^{2}+\cdots +(d-1)x^{d-2}+dx^{d-1}+(d+1)x^{d})\leq b^{d}(1-x)^{-2}}$, for ${\displaystyle abs(x)<1}$

Since ${\displaystyle (1-x)^{-2}}$ or ${\displaystyle \left(1-{\frac {1}{b}}\right)^{-2}}$ is a constant independent of ${\displaystyle d}$ (the depth), if ${\displaystyle b>1}$ (i.e., if the branching factor is greater than 1), the running time of the depth-first iterative deepening search is ${\displaystyle O(b^{d})}$.

Example

For ${\displaystyle b=10}$ and ${\displaystyle d=5}$ the number is

${\displaystyle \sum _{i=0}^{5}(5+1-i)10^{i}=6+50+400+3000+20000+100000=123456}$

All together, an iterative deepening search from depth ${\displaystyle 1}$ all the way down to depth ${\displaystyle d}$ expands only about ${\displaystyle 11\%}$ more nodes than a single breadth-first or depth-limited search to depth ${\displaystyle d}$, when ${\displaystyle b=10}$.[5]

The higher the branching factor, the lower the overhead of repeatedly expanded states,[1]:6 but even when the branching factor is 2, iterative deepening search only takes about twice as long as a complete breadth-first search. This means that the time complexity of iterative deepening is still ${\displaystyle O(b^{d})}$.

Space complexity

The space complexity of IDDFS is ${\displaystyle O(d)}$,[1]:5 where ${\displaystyle d}$ is the depth of the goal.

Proof

Since IDDFS, at any point, is engaged in a depth-first search, it need only store a stack of nodes which represents the branch of the tree it is expanding. Since it finds a solution of optimal length, the maximum depth of this stack is ${\displaystyle d}$, and hence the maximum amount of space is ${\displaystyle O(d)}$.

In general, iterative deepening is the preferred search method when there is a large search space and the depth of the solution is not known.[4]

Example

For the following graph:

a depth-first search starting at A, assuming that the left edges in the shown graph are chosen before right edges, and assuming the search remembers previously-visited nodes and will not repeat them (since this is a small graph), will visit the nodes in the following order: A, B, D, F, E, C, G. The edges traversed in this search form a Trémaux tree, a structure with important applications in graph theory.

Performing the same search without remembering previously visited nodes results in visiting nodes in the order A, B, D, F, E, A, B, D, F, E, etc. forever, caught in the A, B, D, F, E cycle and never reaching C or G.

Iterative deepening prevents this loop and will reach the following nodes on the following depths, assuming it proceeds left-to-right as above:

• 0: A
• 1: A, B, C, E

(Note that iterative deepening has now seen C, when a conventional depth-first search did not.)

• 2: A, B, D, F, C, G, E, F

(Note that it still sees C, but that it came later. Also note that it sees E via a different path, and loops back to F twice.)

• 3: A, B, D, F, E, C, G, E, F, B

For this graph, as more depth is added, the two cycles "ABFE" and "AEFB" will simply get longer before the algorithm gives up and tries another branch.

Related algorithms

Similar to iterative deepening is a search strategy called iterative lengthening search that works with increasing path-cost limits instead of depth-limits. It expands nodes in the order of increasing path cost; therefore the first goal it encounters is the one with the cheapest path cost. But iterative lengthening incurs substantial overhead that makes it less useful than iterative deepening.[4]

Iterative deepening A* is a best-first search that performs iterative deepening based on "f"-values similar to the ones computed in the A* algorithm.

Bidirectional IDDFS

IDDFS has a bidirectional counterpart[1]:6, which alternates two searches: one starting from the source node and moving along the directed arcs, and another one starting from the target node and proceeding along the directed arcs in opposite direction (from the arc's head node to the arc's tail node). The search process first checks that the source node and the target node are same, and if so, returns the trivial path consisting of a single source/target node. Otherwise, the forward search process expands the child nodes of the source node (set ${\displaystyle A}$), the backward search process expands the parent nodes of the target node (set ${\displaystyle B}$), and it is checked whether ${\displaystyle A}$ and ${\displaystyle B}$ intersect. If so, a shortest path is found. Otherwise, the search depth is incremented and the same computation takes place.

One limitation of the algorithm is that the shortest path consisting of an odd number of arcs will not be detected. Suppose we have a shortest path ${\displaystyle \langle s,u,v,t\rangle .}$ When the depth will reach two hops along the arcs, the forward search will proceed to ${\displaystyle v}$ from ${\displaystyle u}$, and the backward search will proceed from ${\displaystyle v}$ to ${\displaystyle u}$. Pictorially, the search frontiers will go through each other, and instead a suboptimal path consisting of an even number of arcs will be returned. This is illustrated in the below diagrams:

Bidirectional IDDFS

What comes to space complexity, the algorithm colors the deepest nodes in the forward search process in order to detect existing of the middle node where the two search processes meet.

Additional difficulty of applying bidirectional IDDFS is that if the source and the target nodes are in different strongly connected components, say, ${\displaystyle s\in S,t\in T}$, if there is no arc leaving ${\displaystyle S}$ and entering ${\displaystyle T}$, the search will never terminate.

Time and space complexities

The running time of bidirectional IDDFS is given by

${\displaystyle 2\sum _{k=0}^{n/2}b^{k}}$

and the space complexity is given by

${\displaystyle b^{n/2},}$

where ${\displaystyle n}$ is the number of nodes in the shortest ${\displaystyle s,t}$-path. Since the running time complexity of iterative deepening depth-first search is ${\displaystyle \sum _{k=0}^{n}b^{k}}$, the speedup is roughly

${\displaystyle {\frac {\sum _{k=0}^{n}b^{k}}{2\sum _{k=0}^{n/2}b^{k}}}={\frac {\frac {1-b^{n+1}}{1-b}}{2{\frac {1-b^{n/2+1}}{1-b}}}}={\frac {1-b^{n+1}}{2(1-b^{n/2+1})}}={\frac {b^{n+1}-1}{2(b^{n/2+1}-1)}}\approx {\frac {b^{n+1}}{2b^{n/2+1}}}=\Theta (b^{n/2}),}$

which is exponential in shortest path length.

Pseudocode

function Build-Path(s, μ, B)
π ← Find-Shortest-Path(s, μ) (Recursively compute the path to the relay node)
remove the last node from π
return π ${\displaystyle \circ }$ B (Append the backward search stack)

function Depth-Limited-Search-Forward(u, Δ, F)
if Δ = 0
F ← F ${\displaystyle \cup }$ {u} (Mark the node)
return
foreach child of u
Depth-Limited-Search-Forward(child, Δ - 1, F)

function Depth-Limited-Search-Backward(u, Δ, B, F)
prepend u to B
if Δ = 0
if u in F
return u (Reached the marked node, use it as a relay node)
remove the first node in B
return null
foreach parent of u
μ ← Depth-Limited-Search-Backward(parent, Δ - 1, B, F)
if μ ${\displaystyle \neq }$ null
return μ
remove the first node in B
return null

function Find-Shortest-Path(s, t)
if s = t:
return <s>
F, B, Δ ← ∅, ∅, 0
forever
Depth-Limited-Search-Forward(s, Δ, F)
foreach δ = Δ, Δ + 1
μ ← Depth-Limited-Search-Backward(t, δ, B, F)
if μ ${\displaystyle \neq }$ null
return Build-Path(s, μ B) (Found a relay node)
B ← ∅
F, Δ ← ∅, Δ + 1


References

1. KORF, Richard E. (1985). "Depth-first iterative deepening" (PDF).
2. ^ Korf, Richard (1985). "Depth-first Iterative-Deepening: An Optimal Admissible Tree Search". Artificial Intelligence. 27: 97–109. doi:10.1016/0004-3702(85)90084-0.
3. ^ David Poole; Alan Mackworth. "3.5.3 Iterative Deepening‣ Chapter 3 Searching for Solutions ‣ Artificial Intelligence: Foundations of Computational Agents, 2nd Edition". artint.info. Retrieved 29 November 2018.
4. ^ a b c d Russell, Stuart J.; Norvig, Peter (2003), Artificial Intelligence: A Modern Approach (2nd ed.), Upper Saddle River, New Jersey: Prentice Hall, ISBN 0-13-790395-2
5. ^ Russell; Norvig (1994). Artificial Intelligence: A Modern Approach.