Difference Between Depth First Search, Breadth First Search and Depth Limit Search in AI
Last Updated :
10 Apr, 2024
In AI, search algorithms like Depth First Search (DFS), Breadth First Search (BFS), and Depth Limit Search (DLS) are essential for systematically exploring a search space to find solutions. In this article, we’ll compare these algorithms, detailing their features, benefits, and uses.
Depth-First Search vs Breadth-First Search vs Depth-Limit Search
The differences between Depth First Search (DFS), Breadth First Search (BFS), and Depth Limit Search (DLS) in AI are as follows:
Aspect
| Depth First Search (DFS)
| Breadth First Search (BFS)
| Depth Limit Search (DLS)
|
---|
Exploration Strategy
| Depth-first exploration
| Breadth-first exploration
| Depth-limited exploration
|
---|
Data Structure
| Stack
| Queue
| Stack
|
---|
Completeness
| Not guaranteed
| Guaranteed
| Complete if depth limit >= depth of the shallowest goal
|
---|
Optimal Solution
| Not guaranteed
| Guaranteed
| Not guaranteed especially with a low depth limit
|
---|
Memory Usage
| Less memory required
| More memory required
| For the shallow depth limit, it will be memory-efficient
|
---|
Time Complexity
| O(b^m) (b: branching factor, m: maximum depth)
| O(b^d) (d: depth of solution)
| O(b^m)
|
---|
Space Complexity
| O(bd)
| O(b^d) (d: depth of solution)
| O(bd)
|
---|
Number of nodes inspected
| [Tex]N_{DFS}\approx \frac {b^d}{2}[/Tex]
| [Tex]N_{BFS} \approx \frac{b^d (b+1)}{2(b-1)}[/Tex]
| [Tex]N_{DLS} \approx \frac{b_{limit}^d (b+1)}{2(b+1)}[/Tex]
|
---|
Terminations
| Can Continue infinitely in graphs with infinite paths
| Continues until goal state is found or all nodes have been explored
| Terminate when reaching the depth limit
|
---|
Suitable For
| Suitable for solutions deep in search space
| Optimal pathfinding, web crawling
| Optimal pathfinding, web crawling
|
---|
Backtracking
| Utilizes backtracking
| Doesn’t backtrack
| Utilizes backtracking within depth limit
|
---|
Depth First Search (DFS):
DFS investigates each branch as far as it can go before turning around. To keep track of which nodes should be visited next, it employs a stack data structure.
This is an implementation in Python:
Python3
def dfs(graph, start, visited=None):
if visited is None:
visited = set()
visited.add(start)
print(start, end=' ')
for neighbor in graph[start]:
if neighbor not in visited:
dfs(graph, neighbor, visited)
# Example usage:
graph = {
'A': ['B', 'C'],
'B': ['D', 'E'],
'C': ['F'],
'D': [],
'E': ['F'],
'F': []
}
print("Depth First Traversal:")
dfs(graph, 'A')
OutputDepth First Traversal:
A B D E F C
Breadth First Search (BFS):
Before advancing to the nodes at the subsequent depth level, BFS investigates every neighbor node at the current depth. It keeps track of the nodes that will be visited next using a queue data structure.
This is an implementation in Python:
Python3
from collections import deque
def bfs(graph, start):
visited = set()
queue = deque([start])
visited.add(start)
while queue:
node = queue.popleft()
print(node, end=' ')
for neighbor in graph[node]:
if neighbor not in visited:
queue.append(neighbor)
visited.add(neighbor)
# Example usage:
graph = {
'A': ['B', 'C'],
'B': ['D', 'E'],
'C': ['F'],
'D': [],
'E': ['F'],
'F': []
}
print("Breadth First Traversal:")
bfs(graph, 'A')
OutputBreadth First Traversal:
A B C D E F
Depth Limited Search (DLS):
While DFS and DLS are comparable, DLS restricts the depth of inquiry. When endless loops in DFS are feasible, it is helpful.
This is an implementation in Python:
Python3
def dls(graph, start, depth, visited=None):
if visited is None:
visited = set()
if depth == 0:
return
visited.add(start)
print(start, end=' ')
for neighbor in graph[start]:
if neighbor not in visited:
dls(graph, neighbor, depth-1, visited)
# Example usage:
graph = {
'A': ['B', 'C'],
'B': ['D', 'E'],
'C': ['F'],
'D': [],
'E': ['F'],
'F': []
}
print("Depth Limited Traversal:")
dls(graph, 'A', 2)
OutputDepth Limited Traversal:
A B C
Conclusions
It is essential to comprehend the differences between DFS, BFS, and DLS in order to choose the best search technique for the given issue requirements. Each method caters to distinct circumstances in AI applications and has its own set of benefits and trade-offs. AI professionals may enhance the effectiveness of their search algorithms by using this comparative study to optimize them.
Share your thoughts in the comments
Please Login to comment...