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
| N_{DFS}\approx \frac {b^d}{2}
| N_{BFS} \approx \frac{b^d (b+1)}{2(b-1)}
| N_{DLS} \approx \frac{b_{limit}^d (b+1)}{2(b+1)}
|
---|
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.
Similar Reads
Difference Between Machine Learning and Deep Learning
If you are interested in building your career in the IT industry then you must have come across the term Data Science which is a booming field in terms of technologies and job availability as well. In this article, we will explore the Difference between Machine Learning and Deep Learning, two major
8 min read
Illustrate the difference in peak memory consumption between DFS and BFS
To understand this let's take a binary tree Binary tree If we conduct BFS in this tree: In level 0 there is one node in the memoryLevel-0 In level 1 there are two nodes in the memoryLevel-1In level 2 there are four nodes in the memoryLevel-2In level 3 there are eight nodes in the memoryLevel-3 But i
1 min read
Difference between Informed and Uninformed Search in AI
What is an Informed Search in AI? algorithms have information on the goal state which helps in more efficient searching. This information is obtained by a function that estimates how close a state is to the goal state. Informed search in AI is a type of search algorithm that uses additional informat
5 min read
Breadth-first Search is a special case of Uniform-cost search
In AI there are mainly two types of search techniques: Un-informed/blind search techniquesInformed search techniques Search algorithms under the Uninformed category are: Breadth-first searchUniform cost searchDepth-first searchDepth limited searchIterative deepening depth-first searchBidirectional s
6 min read
Difference Between Greedy Best First Search and Hill Climbing Algorithm
AI algorithms, or Artificial Intelligence algorithms, are computational procedures or methods designed to solve problems and perform tasks that would typically require human intelligence. These algorithms are fundamental components of artificial intelligence systems and play a crucial role in variou
4 min read
Difference between Shallow and Deep Neural Networks
Neural networks have become a cornerstone of modern machine learning, with their ability to model complex patterns and relationships in data. They are inspired by the human brain and consist of interconnected nodes or neurons arranged in layers. Neural networks can be broadly categorized into two ty
6 min read
Difference between a Neural Network and a Deep Learning System
Since their inception in the late 1950s, Artificial Intelligence and Machine Learning have come a long way. These technologies have gotten quite complex and advanced in recent years. While technological advancements in the Data Science domain are commendable, they have resulted in a flood of termino
7 min read
What is the difference between a strong AI and a weak AI?
Artificial Intelligence (AI) has revolutionized various aspects of modern life, from personal assistants like Siri and Alexa to more complex systems like autonomous vehicles and advanced robotics. However, AI is a broad field that encompasses different levels of intelligence and capabilities. The pr
4 min read
Difference between BFS and Dijkstra's algorithms when looking for shortest path?
What is Dijkstra's Algorithm? Dijkstra's Algorithm is used for finding the shortest path between any two vertices of a graph. It uses a priority queue for finding the shortest path. For more detail about Dijkstra's Algorithm, you can refer to this article. What is BFS Algorithm? Breadth First Search
2 min read
Memory-bounded search ( Memory Bounded Heuristic Search ) in AI
Search algorithms are fundamental techniques in the field of artificial intelligence (AI) that let agents or systems solve challenging issues. Memory-bounded search strategies are necessary because AI systems often encounter constrained memory resources in real-world circumstances. The notion of mem
10 min read