Uninformed (blind) search strategies use only the information in the problem definition: they can generate successors and test for the goal, but cannot tell whether one state is more promising than another.
Tree Search and the Frontier
Search builds a search tree rooted at the initial state. A node holds a state, its parent, the action that produced it, and the path cost g(n). The set of nodes generated but not yet expanded is the frontier (open list). The algorithm repeatedly removes a node from the frontier, tests whether it is a goal, and otherwise expands it, adding the children. Strategies differ only in the order in which they choose frontier nodes. On graphs we also keep an explored set (closed list) to avoid repeated states and loops.
Breadth First Search
BFS expands the shallowest node first, using a FIFO queue as the frontier: all nodes at depth d are expanded before any at depth d + 1. It is complete (finds a solution if the branching factor is finite) and optimal when all step costs are equal, since the shallowest goal is found first. Its drawback is memory: with branching factor b and shallowest goal depth d, time and space are both O(bᵈ), because every node is kept. At b = 10, a depth of 12 would need about a thousand terabytes; memory, not time, is the real limit.
Depth First Search
DFS expands the deepest node first, using a LIFO stack (or recursion). It plunges down one path until a leaf is reached and then backtracks. Its memory need is only O(b·m) (a single path with unexpanded siblings), where m is the maximum depth, a great advantage. But it is not complete in infinite or looping spaces and not optimal: it may find a deep goal on the left while a shallow one exists on the right. Time is O(bᵐ), which can be far worse than bᵈ when m is much larger than d. A depth-limited variant stops at a given depth limit L.
Definition: Uninformed search. A search strategy that has no information about states beyond that given in the problem definition, so it only distinguishes goal from non-goal states.
Choosing Between Them
Use BFS when the shortest solution matters and memory is available; use DFS when memory is tight and many solutions exist at a similar depth. Uniform-cost search, a relative, expands the node with the lowest path cost g(n) and is optimal for varying step costs.
Table 2.1: BFS and DFS compared
| Property | BFS | DFS |
|---|---|---|
| Frontier | FIFO queue | LIFO stack |
| Complete | Yes | No (infinite or cyclic spaces) |
| Optimal | Yes, for equal step costs | No |
| Time | O(bᵈ) | O(bᵐ) |
| Space | O(bᵈ) | O(b·m) |
Example 2.1: BFS and DFS on a tree
Problem: Tree: S has children A, B; A has C, D; B has E, F; E has G (goal). Give the visiting order of BFS and DFS (left to right).
Solution:
- BFS: S; then A, B; then C, D, E, F; then G found at depth 3.
- BFS order: S, A, B, C, D, E, F, G.
- DFS: S, A, C (leaf), D (leaf), back to S, B, E, G found.
- DFS order: S, A, C, D, B, E, G.
Result: Both find G at depth 3; DFS visits 7 nodes and BFS 8, but BFS guarantees the shallowest goal.
from collections import deque
tree = {"S": "AB", "A": "CD", "B": "EF", "E": "G"}
def bfs():
q, order = deque("S"), []
while q:
n = q.popleft(); order.append(n)
if n == "G": return order
q.extend(tree.get(n, ""))
def dfs(n="S", order=None):
order = [] if order is None else order
order.append(n)
if n == "G": return order
for c in tree.get(n, ""):
r = dfs(c, order)
if r and r[-1] == "G": return r
print("BFS:", bfs())
print("DFS:", dfs())
Output
BFS: ['S', 'A', 'B', 'C', 'D', 'E', 'F', 'G']
DFS: ['S', 'A', 'C', 'D', 'B', 'E', 'G']
Common mistakes
- Using DFS and expecting the shortest path.
- Forgetting the explored set, so graph search loops forever.
- Believing BFS is time-limited when its real limit is memory.
- Using a queue where a stack is needed, or the reverse.
Key takeaways
- BFS: FIFO queue, complete, optimal for equal costs, O(bᵈ) time and space.
- DFS: LIFO stack, not complete or optimal, O(b·m) space.
- The frontier is the set of generated but unexpanded nodes.
- Strategies differ only in the order of expanding frontier nodes.
Practice questions
- Which data structure does BFS use for the frontier?
- Why is DFS memory-efficient?
- When is BFS optimal?
Answers to the practice questions
- A FIFO queue.
- It stores only the current path and the unexpanded siblings, O(b·m).
- When all step costs are equal.
Sources
- Official BCA syllabus Artificial Intelligence, course code 26BCA404DS04, Unit II
