Nexus Learn

Lecture 12: Depth-First Search and Iterative Deepening

Lecture Notes CS-511 Artificial Intelligence 5 min read Updated 8 Nov 2026

↓ Download notes (Word)

On this page
  1. Learning Outcomes
  2. 1. Depth-First Search
  3. 2. DFS on the Road Map
  4. 3. Depth-Limited Search and Iterative Deepening
  5. 4. Common Mistakes
  6. Word Bank
  7. Self-Check Questions

Learning Outcomes

SLO

After this lecture you will be able to...

CLO

Bloom

SLO 12.1

Trace depth-first search with a LIFO stack and compare its order with BFS

CLO3

C3 Apply

SLO 12.2

Explain why DFS uses little memory but is neither optimal nor always complete

CLO3

C2 Understand

SLO 12.3

Apply depth-limited search and iterative deepening, and compare all uninformed methods

CLO3

C3 Apply

§1.1 The idea

Slide L12-S06

DFS expands the deepest unexpanded node first. Its frontier is a LIFO stack (last in, first out) - like a stack of plates. It follows one road as far as possible; at a dead end it backtracks to the most recent node that still has unexplored children, exactly as Prolog does (Lecture 9). In Python a list works as a stack: append() and pop().

§1.2 The code

Slide L12-S07

def dfs(graph, start, goal):

"""Depth-first GRAPH search: frontier is a LIFO stack; children pushed in reverse so the first neighbour is tried first.

Returns path, expansion order and the stack after each expansion."""

stack, explored, steps = [[start]], set(), []

while stack:

path = stack.pop(); node = path[-1]

if node in explored: continue

if node == goal: return path, steps

explored.add(node)

kids = [n for n, _ in graph[node] if n not in explored]

for n in reversed(kids): stack.append(path + [n])

steps.append((node, kids, [p[-1] for p in reversed(stack)]))

return None, steps

def dls(graph, node, goal, limit, path=None, counter=None):

"""Depth-limited search (tree search, no repeats on the current path). Returns path, 'cutoff' or None."""

path = path or [node]; counter = counter if counter is not None else [0]

counter[0] += 1

if node == goal: return path

if limit == 0: return "cutoff"

cut = False

for n, _ in graph[node]:

if n in path: continue

r = dls(graph, n, goal, limit - 1, path + [n], counter)

if r == "cutoff": cut = True

elif r: return r

return "cutoff" if cut else None

2. DFS on the Road Map

§2.1 Trace: Burewala to Sahiwal

Slide L12-S09

step expand children pushed stack (top ... bottom)

1 Burewala Vehari, Arifwala, Mian Channu Vehari, Arifwala, Mian Channu

2 Vehari Mailsi, Multan Mailsi, Multan, Arifwala, Mian Channu

3 Mailsi - (dead end) Multan, Arifwala, Mian Channu

4 Multan Khanewal, Lahore Khanewal, Lahore, Arifwala, Mian Channu

5 Khanewal Mian Channu Mian Channu, Lahore, Arifwala, Mian Channu

6 Mian Channu - (dead end) Lahore, Arifwala, Mian Channu

7 Lahore Sahiwal Sahiwal, Arifwala, Mian Channu

Sahiwal popped -> goal.

DFS path: Burewala -> Vehari -> Multan -> Lahore -> Sahiwal | 4 roads | 645 km | expanded: 7

BFS path: Burewala -> Arifwala -> Sahiwal | 2 roads | 105 km | expanded: 3

DFS dived through Vehari, Mailsi (dead end), Multan, Khanewal, Mian Channu (dead end) and Lahore before reaching Sahiwal: 4 roads and 645 km, while BFS found Burewala - Arifwala - Sahiwal (2 roads, 105 km).

§2.2 Properties

Slide L12-S10

Property

DFS

Reason

Complete

No (yes in finite spaces with repeat checks)

Can dive forever down an infinite or looping path

Optimal

No

Returns the first goal it reaches

Time

O(b^m)

m = maximum depth

Space

O(bm)

Keeps one path plus siblings - its big advantage

start BFS moves DFS moves DFS expanded

1 2 3 / 4 _ 6 / 7 5 8 2 49,284 135,586

_ 2 3 / 1 4 5 / 7 8 6 4 22,646 23,538

8 1 3 / 4 _ 2 / 7 6 5 14 63,906 83,659

On the 8-puzzle plain DFS found solutions tens of thousands of moves long where BFS needed 2, 4 or 14 moves.

3. Depth-Limited Search and Iterative Deepening

§3.1 Depth-limited search

Slide L12-S12

Depth-limited search (DLS) is DFS that does not expand nodes at depth l. It returns 'cutoff' when the limit stopped it (a solution may exist deeper) and 'failure' when there is no solution at all. The difficulty is choosing l.

§3.2 Iterative deepening

Slide L12-S13

Iterative deepening (IDDFS) runs DLS with l = 0, 1, 2, ... until it finds a goal. It keeps DFS's small memory (O(bd)) and, like BFS, is complete and optimal when step costs are equal. Repeating the top levels costs little because most nodes are at the bottom: for b = 10, d = 5 IDDFS generates 123,456 nodes and BFS 111,111 (11% more).

Road map, Burewala to Lahore:

limit 0: nodes 1 result: cutoff

limit 1: nodes 4 result: cutoff

limit 2: nodes 9 result: cutoff

limit 3: nodes 6 result: Burewala -> Vehari -> Multan -> Lahore (475 km)

8-puzzle 8 1 3 / 4 _ 2 / 7 6 5:

IDDFS found 14 moves at limit 14; nodes generated in all iterations: 24,028

BFS: 14 moves; nodes expanded 3,633; largest frontier 2,327 | IDDFS stores at most 15 states on its path

§3.3 Comparison of uninformed methods

Slide L12-S14

Criterion

BFS

UCS

DFS

DLS

IDDFS

Complete?

Yes*

Yes

No

If l ≥ d

Yes*

Optimal?

Equal costs

Yes

No

No

Equal costs

Time

O(b^d)

≈ O(b^d)

O(b^m)

O(b^l)

O(b^d)

Space

O(b^d)

≈ O(b^d)

O(bm)

O(bl)

O(bd)

* if b is finite. b branching factor, d depth of the shallowest goal, m maximum depth, l depth limit.

4. Common Mistakes

Wrong idea

Correct idea

DFS uses a queue.

DFS uses a LIFO stack.

DFS finds the shortest path.

DFS returns the first goal it reaches.

IDDFS wastes a lot of time.

Only about 11% extra for b = 10.

Cutoff means no solution.

Cutoff means the limit was too small.

Word Bank

Word

Simple meaning

Example

LIFO stack

Last in, first out

Stack of plates

Backtracking

Going back to the last choice after a dead end

Mailsi -> back to Vehari

Depth limit

Maximum depth DFS may go

l = 2

Cutoff

Stopped by the limit

Limit 2 for Lahore

Iterative deepening

Repeat DLS with growing limits

0, 1, 2, 3 ...

Self-Check Questions

  1. How is DFS different from BFS? (see §1.1)
  2. Trace DFS from Burewala to Sahiwal. (see §2.1)
  3. Why is DFS not optimal? (see §2.2)
  4. What does 'cutoff' mean? (see §3.1)
  5. Why does IDDFS not waste much time? (see §3.2)
  6. Fill the comparison table from memory. (see §3.3)

Reading: Russell & Norvig (4e) 3.4.3-3.4.5; Luger (6e) 3.2.3.

← All CS-511 material