Exam answer
Searching process: searching is the systematic exploration of a problem's state space to find a sequence of actions (a path) that leads from the initial state to a goal state. A search problem is defined by the initial state, the legal operators (actions) and their results, a goal test, and a path cost. The search algorithm repeatedly picks a state, checks whether it is the goal, and if not, expands it by applying the operators to generate successor states.
Parameters to evaluate a search technique:
- Completeness: does the algorithm always find a solution when one exists?
- Optimality: does it find the best (lowest-cost) solution?
- Time complexity: how many nodes does it generate or expand?
- Space complexity: how many nodes must it keep in memory at once?
Formulating a problem for search
Search can only start once the problem is written in these terms. For the 8-puzzle:
| Component | 8-puzzle |
|---|---|
| State | Position of the eight tiles and the blank on the 3×3 board |
| Initial state | Any given scrambled board |
| Operators | Move the blank up, down, left or right (when possible) |
| Goal test | Does the board match the target arrangement? |
| Path cost | 1 per move, so the cost is the number of moves |
The same five components describe route finding, puzzle solving like the water jug problem, or planning a robot's moves. Choosing a good formulation, especially what counts as a "state", often matters more than the choice of algorithm.
The general search loop
Almost every search algorithm follows the same skeleton. Only the rule for choosing the next node from the frontier (the set of generated but not yet expanded nodes) changes:
frontier = {initial state}
explored = empty set
loop:
if frontier is empty: return failure
node = choose and remove a node from frontier # the strategy
if goal_test(node): return path to node
add node to explored
for each successor of node:
if successor not in explored: add it to frontier
- Frontier as a queue (first in, first out) gives breadth-first search.
- Frontier as a stack (last in, first out) gives depth-first search.
- Frontier as a priority queue by path cost gives uniform-cost search.
- Priority by a heuristic gives greedy best-first search; by cost plus heuristic gives A*.
The explored set prevents the algorithm from expanding the same state twice, which matters on graphs with cycles.
The four parameters, explained
Complexity is described with three quantities: b, the branching factor (maximum successors of a node); d, the depth of the shallowest goal; and m, the maximum depth of the state space.
Completeness
An algorithm is complete if it always finds a solution when one exists (in a finite space, or with finite b). Breadth-first search is complete. Depth-first search is not complete in infinite or cyclic spaces without a visited check, because it can follow one branch forever.
Optimality
An optimal algorithm returns the lowest-cost solution. BFS is optimal only when every step costs the same. Uniform-cost search is optimal for any positive step costs. DFS is not optimal.
Time complexity
Usually counted as nodes generated or expanded. BFS takes O(bd); DFS takes O(bm), which can be far worse than BFS if m is much larger than d.
Space complexity
The largest number of nodes stored at one time. BFS stores the whole frontier, O(bd), which is usually its real bottleneck. DFS stores only the current path and its siblings, O(b·m). This trade-off is why iterative deepening, which combines DFS-like memory with BFS-like completeness, exists.
Measuring the parameters on one example
A small weighted graph makes the differences concrete. From S, the path S–B–G has only two steps but costs 5 + 3 = 8; the longer path S–A–C–D–G costs only 1 + 1 + 1 + 1 = 4. The code runs BFS, DFS and uniform-cost search (UCS) using the loop above, and counts expanded nodes and the largest frontier size:
import heapq
from collections import deque
graph = { # node: [(neighbour, step cost), ...]
"S": [("A", 1), ("B", 5)],
"A": [("C", 1), ("D", 7)],
"B": [("G", 3)],
"C": [("D", 1)],
"D": [("G", 1)],
"G": [],
}
def search(strategy):
frontier = deque([(0, "S", ["S"])]) if strategy != "UCS" else [(0, "S", ["S"])]
explored, expanded, max_frontier = set(), 0, 1
while frontier:
if strategy == "BFS":
cost, node, path = frontier.popleft()
elif strategy == "DFS":
cost, node, path = frontier.pop()
else:
cost, node, path = heapq.heappop(frontier)
if node == "G":
return path, cost, expanded, max_frontier
if node in explored:
continue
explored.add(node)
expanded += 1
for nxt, step in graph[node]:
if nxt not in explored:
item = (cost + step, nxt, path + [nxt])
heapq.heappush(frontier, item) if strategy == "UCS" else frontier.append(item)
max_frontier = max(max_frontier, len(frontier))
return None
for s in ("BFS", "DFS", "UCS"):
path, cost, expanded, peak = search(s)
print(f"{s}: path={'-'.join(path):<11} cost={cost:<2} expanded={expanded} max_frontier={peak}")
Output:
BFS: path=S-B-G cost=8 expanded=5 max_frontier=3
DFS: path=S-B-G cost=8 expanded=2 max_frontier=2
UCS: path=S-A-C-D-G cost=4 expanded=4 max_frontier=3
What the numbers show:
- Completeness: all three find a solution, because the graph is finite and the explored set prevents loops.
- Optimality: only UCS returns the cheapest path (cost 4). BFS finds the path with the fewest steps, which is not the cheapest when costs differ. DFS happens to find the same path as BFS here only because of the order it explores neighbours; with a different order it could return a worse one.
- Time: DFS expanded just 2 nodes on this graph by luck of ordering; UCS expanded 4 and BFS 5. On a large space these counts grow as O(bd) or O(bm).
- Space: the frontier never exceeded 3 nodes here; on large problems the BFS and UCS frontiers grow exponentially while DFS's stays proportional to depth.
BFS here tests for the goal when a node is removed from the frontier, so that one loop serves all three strategies. Testing when a node is generated lets BFS stop a little earlier, but would break UCS's optimality.
Comparison table
| Strategy | Complete? | Optimal? | Time | Space |
|---|---|---|---|---|
| Breadth-first | Yes (finite b) | Yes, if all step costs are equal | O(bd) | O(bd) |
| Uniform-cost | Yes (positive step costs) | Yes | Grows with the optimal cost C* and the smallest step cost | Same as time |
| Depth-first | No (infinite or cyclic spaces) | No | O(bm) | O(b·m) |
| Depth-limited (limit l) | No, if l < d | No | O(bl) | O(b·l) |
| Iterative deepening | Yes | Yes, if all step costs are equal | O(bd) | O(b·d) |
Informed strategies add a fifth practical consideration: the quality of the heuristic. They are compared in informed vs uninformed search and heuristic search techniques, and BFS and DFS are traced in detail in BFS and DFS explained.
Sources: S. Russell and P. Norvig, Artificial Intelligence: A Modern Approach, ch. 3 (problem formulation, search strategies and their evaluation); E. Rich and K. Knight, Artificial Intelligence, ch. 2


