Class: | Search algorithm |
Data: | Tree, Graph |
Time: | O(bd) b d |
Space: | O(d) |
Optimal: | yes (for unweighted graphs) |
Complete: | yes |
In computer science, iterative deepening search or more specifically iterative deepening depth-first search[1] (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, meaning that it finds the shallowest goal. Since it visits all the nodes in the search tree down to depth
d
d+1
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.
function IDDFS(root) is for depth from 0 to ∞ do found, remaining ← DLS(root, depth) if found ≠ null then return found else if not remaining then return null function DLS(node, depth) is if depth = 0 then if node is a goal then return (node, true) else return (null, true) (Not found, but may have children) else if depth > 0 then any_remaining ← false foreach child of node do found, remaining ← DLS(child, depth−1) if found ≠ null then return (found, true) if remaining then any_remaining ← true (At least one node found at depth, let IDDFS deepen) return (null, any_remaining)
If the goal node is found by DLS, IDDFS will return it without looking deeper. Otherwise, if at least one node exists at that level of depth, the remaining flag will let IDDFS continue.
2-tuples are useful 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.
IDDFS achieves breadth-first search's completeness (when the branching factor is finite) using depth-first search's space-efficiency. If a solution exists, it will find a solution path with the fewest arcs.[2]
Iterative deepening visits states multiple times, and it may seem wasteful. However, if IDDFS explores a search tree to depth
d
d
d
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.
A second advantage is the responsiveness of the algorithm. Because early iterations use small values for
d
d
The time complexity of IDDFS in a (well-balanced) tree works out to be the same as breadth-first search, i.e.
O(bd)
b
d
In an iterative deepening search, the nodes at depth
d
d-1
d+1
bd+2bd-1+3bd-2+ … +(d-1)b2+db+(d+1)=
d | |
\sum | |
i=0 |
(d+1-i)bi
where
bd
d
2bd
d-1
bd
bd(1+2b-1+3b-2+ … +(d-1)b2+db1+(d+1)b-d)
Now let
x=
1 | |
b |
=b-1
bd(1+2x+3x2+ … +(d-1)xd+dxd+(d+1)xd)
This is less than the infinite series
bd(1+2x+3x2+4x3+ … )=bd\left(
infty | |
\sum | |
n=1 |
nxn-1\right)
which converges to
bd(1-x)-2=bd
1 | |
(1-x)2 |
abs(x)<1
That is, we have
bd(1+2x+3x2+ … +(d-1)xd+dxd+(d+1)xd)\leqbd(1-x)-2
abs(x)<1
Since
(1-x)-2
\left(1-
1 | |
b |
\right)-2
d
b>1
O(bd)
For
b=10
d=5
5 | |
\sum | |
i=0 |
(5+1-i)10i=6+50+400+3000+20000+100000=123456
All together, an iterative deepening search from depth
1
d
11\%
d
b=10
The higher the branching factor, the lower the overhead of repeatedly expanded states,[1] 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
O(bd)
The space complexity of IDDFS is
O(d)
d
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
d
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.
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:
(Iterative deepening has now seen C, when a conventional depth-first search did not.)
(It still sees C, but that it came later. Also it sees E via a different path, and loops back to F twice.)
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.
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.
Iterative deepening A* is a best-first search that performs iterative deepening based on ""-values similar to the ones computed in the A* algorithm.
IDDFS has a bidirectional counterpart,[1] 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
A
B
A
B
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
\langles,u,v,t\rangle.
u
v
v
u
What comes to space complexity, the algorithm colors the deepest nodes in the forward search process in order to detect existence 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,
s\inS,t\inT
S
T
The running time of bidirectional IDDFS is given by
and the space complexity is given by
where
n
s,t
n | |
\sum | |
k=0 |
bk
function Build-Path(s, μ, B) is π ← Find-Shortest-Path(s, μ) (Recursively compute the path to the relay node) remove the last node from π return π
\circ
function Depth-Limited-Search-Forward(u, Δ, F) is if Δ = 0 then F ← F
\cup
function Depth-Limited-Search-Backward(u, Δ, B, F) is prepend u to B if Δ = 0 then if u in F then return u (Reached the marked node, use it as a relay node) remove the head node of B return null foreach parent of u do μ ← Depth-Limited-Search-Backward(parent, Δ − 1, B, F) if μ
≠
function Find-Shortest-Path(s, t) is if s = t then return <s> F, B, Δ ← ∅, ∅, 0 forever do Depth-Limited-Search-Forward(s, Δ, F) foreach δ = Δ, Δ + 1 do μ ← Depth-Limited-Search-Backward(t, δ, B, F) if μ
≠