Depth-Limited and Iterative Deepening Search: Controlling How Deep AI Searches
Learn how Depth-Limited Search controls the depth of DFS and how Iterative Deepening Search gradually increases the limit to find solutions. Includes algorithms, Python implementations, complexity, comparisons, and applications.
In the previous article, we explored Depth-First Search (DFS). We saw how it follows one branch as deeply as possible before returning to explore another.
But imagine a search tree with a path that never ends.
DFS may keep moving deeper along that path without ever returning. Even when a solution exists somewhere else, the algorithm might never reach it.
So, how can we control how deep DFS is allowed to go?
One solution is to set a depth limit. Another is to gradually increase that limit and repeat the search.
These ideas lead us to two important uninformed search strategies: Depth-Limited Search (DLS) and Iterative Deepening Search (IDS).
1. Depth-Limited Search (DLS)
Depth-Limited Search (DLS) is a variation of Depth-First Search in which the search is restricted to a predefined maximum depth.
In ordinary DFS, the algorithm can continue exploring deeper and deeper. In DLS, it stops exploring a branch when it reaches the specified depth limit.
Consider this search tree:
A Depth 0
/ \
B C Depth 1
/ \ / \
D E F G Depth 2
/ \
H I Depth 3
Suppose we set the depth limit to 2.
The algorithm can explore A, B, C, D, E, F, and G. However, it will not explore H or I because they are beyond the permitted depth.
The limit controls how far the search can go.
How DLS works
The process is similar to DFS:
- Start from the initial node.
- Explore a branch using depth-first traversal.
- Keep track of the current depth.
- If the goal is found, return the solution.
- If the depth limit is reached, stop exploring that branch.
- Backtrack and explore other branches within the limit.
An important point: reaching the depth limit does not necessarily mean that no solution exists. It only means that the algorithm cannot explore beyond the permitted depth.
2. Understanding the Depth Limit
The depth limit is usually represented by (l).
For example:
- \(l = 0\): Only the starting node can be explored.
- \(l = 1\): The starting node and its immediate children can be explored.
- \(l = 2\): The search can reach nodes two levels below the starting node.
Choosing the limit matters.
If the limit is too small, the algorithm may miss a solution. If it is too large, the search may explore many unnecessary nodes.
This is one of the main challenges of Depth-Limited Search.
3. DLS Algorithm in Python
Let’s implement Depth-Limited Search using recursion.
graph = {
'A': ['B', 'C'],
'B': ['D', 'E'],
'C': ['F', 'G'],
'D': ['H', 'I'],
'E': [],
'F': [],
'G': [],
'H': [],
'I': []
}
def dls(graph, node, goal, limit):
if node == goal:
return True
if limit == 0:
return False
for neighbour in graph[node]:
if dls(graph, neighbour, goal, limit - 1):
return True
return False
print(dls(graph, 'A', 'H', 2))
print(dls(graph, 'A', 'H', 3))
Output:
False
True
Why?
Node H is at depth 3.
- With a limit of 2, the algorithm cannot reach H.
- With a limit of 3, it can reach H and return
True.
This implementation demonstrates the basic idea. In a general graph containing cycles, additional cycle-handling logic is needed to avoid repeatedly following the same nodes.
4. Limitations of Depth-Limited Search
DLS solves one problem of ordinary DFS: it prevents the algorithm from following a branch indefinitely.
However, it introduces another problem: choosing the correct depth limit.
Its main limitations are:
- Choosing the limit is difficult: We may not know how deep the solution is.
- Solutions beyond the limit are missed: A valid solution may exist just beyond the chosen depth.
- It is not generally optimal: It does not guarantee the shortest or least-cost solution.
- It may be incomplete: If the limit is smaller than the depth of the solution, the algorithm will not find it.
DLS is useful when we have a reasonable estimate of how deep a solution might be.
But what if we do not know the correct depth limit?
This is where Iterative Deepening Search becomes useful.
5. Iterative Deepening Search (IDS)
Iterative Deepening Search (IDS), also called Iterative Deepening Depth-First Search (IDDFS), repeatedly performs Depth-Limited Search while increasing the depth limit.
Instead of guessing one depth limit, it begins with a small limit and gradually increases it until the goal is found.
For example:
- First search: depth limit 0.
- Second search: depth limit 1.
- Third search: depth limit 2.
- Fourth search: depth limit 3.
The process continues until a solution is found or the search is otherwise terminated.
Example of IDS
Consider the same search tree:
A
/ \
B C
/ \ / \
D E F G
/ \
H I
Suppose the goal is H.
IDS searches as follows:
Iteration 1 — Limit 0
A
The goal is not found.
Iteration 2 — Limit 1
A → B → C
The goal is not found.
Iteration 3 — Limit 2
A → B → D → E → C → F → G
The goal is not found because H is at depth 3.
Iteration 4 — Limit 3
The algorithm explores deeper and reaches H.
The search stops when the goal is found.
Notice that IDS repeats some work. For example, node A is visited in every iteration. At first, this may seem inefficient, but it provides an important advantage: the algorithm does not need to know the solution’s depth in advance.
6. Implementing IDS in Python
We can implement IDS by repeatedly calling our DLS function with an increasing depth limit.
def ids(graph, start, goal, max_depth):
for limit in range(max_depth + 1):
if dls(graph, start, goal, limit):
return True
return False
print(ids(graph, 'A', 'H', 3))
Output:
True
The algorithm starts with a depth limit of 0 and increases it one step at a time until it finds H.
Here, max_depth is the maximum depth we are willing to search. If the goal is deeper than this value, the algorithm will not find it.
7. Properties of DLS and IDS
Let’s examine their performance using the standard search-algorithm properties.
Depth-Limited Search
| Property | Description |
|---|---|
| Completeness | Only if the limit reaches a solution depth, under suitable finite-search assumptions |
| Optimality | No, not generally |
| Time complexity | \(O(b^l)\) |
| Space complexity | \(O(bl)\) |
Here:
- \(b\) = branching factor.
- \(l\) = depth limit.
Iterative Deepening Search
| Property | Description |
|---|---|
| Completeness | Yes, when branching is finite and a solution exists at finite depth |
| Optimality | Yes, when all step costs are equal |
| Time complexity | \(O(b^d)\) |
| Space complexity | \(O(bd)\) |
Here, \(d\) is the depth of the shallowest goal.
IDS is optimal only under the equal-step-cost assumption. If actions have different costs, finding the shallowest goal does not necessarily mean finding the least-cost solution.
8. DLS vs IDS
Both algorithms are based on DFS, but they handle search depth differently.
| Feature | DLS | IDS |
|---|---|---|
| Main idea | Uses one fixed depth limit | Repeatedly increases the limit |
| Search process | One depth-limited search | Multiple depth-limited searches |
| Requires depth limit | Yes | Starts at 0 and increases it |
| Completeness | Depends on the chosen limit | Complete under standard finite-branching assumptions |
| Optimality | Not generally | Yes, with equal step costs |
| Time complexity | \(O(b^l)\) | \(O(b^d)\) |
| Space complexity | \(O(bl)\) | \(O(bd)\) |
IDS repeats some node visits, but it combines the low memory requirements of depth-first exploration with the ability to find a shallow solution without knowing its depth beforehand.
9. Advantages and Applications
Advantages of DLS
- Prevents unlimited exploration down a single branch.
- Uses relatively little memory.
- Useful when a reasonable depth limit is known.
- Can reduce unnecessary exploration in very deep search spaces.
Advantages of IDS
- Does not require prior knowledge of the solution depth.
- Complete under standard finite-branching conditions.
- Finds a shallowest solution when step costs are equal.
- Uses much less memory than BFS in many search-tree problems.
Applications
These methods can be useful in:
- Game-tree exploration: Limiting how many moves ahead a game-playing program searches.
- Puzzle solving: Restricting exploration to a certain number of moves.
- Planning: Searching for a sequence of actions within a controlled depth.
- Robotics: Exploring possible action sequences without allowing unlimited search.
- AI problem-solving: Finding shallow solutions in large search spaces.
Conclusion
Depth-Limited Search gives DFS a boundary. It prevents the algorithm from exploring beyond a chosen depth, but choosing that limit can be difficult.
Iterative Deepening Search addresses this issue by gradually increasing the depth limit until it finds a solution.
Together, these algorithms show how a small change in a search strategy can make it more controlled and useful.
In the next article, we will move from uninformed search to Heuristic Search and Informed Search Strategies, where AI begins using additional knowledge to decide which paths are more promising.
Quick quiz
5 questionsOriginally published at farhanpaal.com — https://farhanpaal.com/articles/depth-limited-and-iterative-deepening-search-controlling-how-deep-ai-searches/
Keep a copy
Read offline as a printout or PDF.
farhanpaal.com