Artificial Intelligence and Machine Learning · Lecture 03 of 16

Search algorithms · Part 1

Problem formulation and uninformed search

Model a search problem, then compare exploration rules that do not use an estimate of the remaining distance.

01States, paths, costs and the frontier
02Breadth-first and depth-first search
03Iterative deepening and lowest-cost-first
Search · Uninformed strategies
Search · Model the problem

Turn the task into a search problem

A robot must deliver a parcel. Its state describes the situation; search finds a sequence of actions.

01State + initial stateState = (robot location, parcel status)
02 Actions + transitionsEach legal action creates a new state.
A robot delivers a parcel in three actions Initially the robot and parcel are at the depot. Pick up costs one unit and changes the parcel status to carried. Move costs one unit and changes the robot location to the customer. Deliver costs one unit and changes the parcel status to delivered. Each panel shows the resulting state. INITIAL STATE (depot, at depot) Start here Pick up+1 PARCEL PICKED UP (depot, carried) New state after one action Move+1 AT THE CUSTOMER (customer, carried) New state after one action Deliver+1 GOAL SATISFIED (customer, delivered) Parcel status = delivered

03 Goal test

Is the parcel delivered?
Several robot locations may satisfy the same goal.

04 Step cost 1 unit / action · 3 in this route

A real robot might minimize energy; distance is a proxy only under suitable assumptions.

A solution is a legal action sequence from the initial state to any state satisfying the goal test.

Search · Model the problem

Same location does not mean the same state

Two robots stand at the customer’s door. Can both take the “deliver” action?

State A

At the door · empty-handed

Parcel is still at the depot.

Deliver is not a legal action.

State B

At the door · carrying parcel

Parcel is ready to hand over.

Deliver is a legal action.

Same encoded state ⇒ same possible futuresLegal actions, their outcomes and costs, and the goal test must be determined by the state.

The state must distinguish situations with different legal actions or outcomes.

Search · 01 · Transfer the model

The state depends on the task

Apply the robot’s lesson elsewhere: keep the information that changes what can happen next.

Road navigation

Current intersection.

For a simple map, it determines which roads are available.
Objective: minimum travel time.

Traveling salesperson

Current city and visited-city set.

We must know which cities remain.
Objective: minimum total fare.

A key-and-door puzzle

Position and keys held.

Being at the door is not enough to open it.
Goal: complete the game’s tasks.

A state is a task-specific description, not just a location.

01 · Model the problem

Directed edges encode legal moves

G is the graph: the pair G = (V, E).
V is its set of vertices (states).
E is its set of directed edges (legal moves).

V = {A, B, C, D}
E ⊆ V × V
  • A, B, C and D are example state labels.
  • An edge (A, B) permits A → B.
  • B → A needs its own edge.
  • V × V means all ordered pairs of vertices.
    E ⊆ V × V means the legal edges are a subset of those pairs.
Search · Model the problem

The whole graph need not exist in memory

A successor function generates legal transitions only when a state is expanded.

Input

Current state

(depot, at depot)

Generate

Successor function

Check action preconditions.
Compute the resulting states and costs.

Output

Transitions

Pick up → (depot, carried), cost 1.
Move → (customer, at depot), cost 1.

successors(current state) → (action, next state, step cost)Explicit graph: store the map in advance. Implicit graph: compute each needed neighborhood.

Generating a graph on demand saves upfront storage; it does not remove the growth in possible paths.

01 · Model the problem

A solution path reaches a goal; its cost measures quality

Start at A. Reach D.

g(p) = ∑ edge costs along p
p is a path: an ordered route through states.
g(p) is its total cost from the start.
∑ means add the cost of every edge on that route.
Two valid paths

A → B → D   1 + 5 = 6
A → C → D   10 + 8 = 18

Equal edge counts can hide very different costs.

Search · Read the mathematics

Write a legal route and add its edge costs

The same A → B → D example, now written as a mathematical object.

A path is an ordered sequence

p = ⟨v₁, …, vₖ⟩

p is the path; k is its number of vertices.
vᵢ is the vertex at position i, from 1 to k.
⟨…⟩ means an ordered sequence.

Each vᵢ is in V; each consecutive pair (vᵢ, vᵢ₊₁) is in E, for i = 1, …, k − 1.

vₛ is the start vertex; vG is the goal vertex.
A solution has v₁ = vₛ and vₖ = vG.

A weighted graph adds prices

G = (V, E, ωE)
ωE : E → ℝ

G is the graph; V its vertices; E its edges.
ωE (omega) maps each edge to its cost.
ℝ is the set of real numbers.
Example: ωE(A, B) = 1 prices A → B at 1.

g(p) = k − 1∑i = 1 ωE(vᵢ, vᵢ₊₁)

g(p) is the total cost of path p.
∑ adds the edge costs for i = 1 to k − 1.

A path with k vertices has k − 1 edges. For p = ⟨A, B, D⟩: k = 3 and g(p) = 1 + 5 = 6. The start-only path has cost 0.

Search · Read the mathematics

A graph without directed cycles need not be a tree

DAG = directed acyclic graph. A rooted tree is a more restricted case.

A diamond DAG: A to B and C; B and C to D. D has two parents, so this is not a rooted tree.ABCD

Two routes to D; no directed route loops back.

No directed cycle

For a path ⟨v₁, …, vₖ⟩, k counts vertices; v₁ is the first and vₖ the last.
No legal path with k ≥ 2 has v₁ = vₖ: it cannot return to its start.

A rooted tree also requires

The underlying undirected graph is connected and has no cycle.
The vertex r is the root: it reaches every vertex along the arrows.

A DAG may merge branches. In a rooted tree, there is exactly one directed path from the root to each vertex.

01 · Model the problem

A rooted tree makes the choices visible

One root. One path from the root to every node.

  • Depth = number of edges from the root.
  • A leaf has no children.
  • Every non-root node has one parent.
A → B → F → P → S
4 edges · goal S at depth 4
01 · Model the problem

A search tree can repeat a graph state

  • The state graph describes the environment.
  • The search tree describes possible paths.
  • Different paths may end in the same state.
  • A cycle can create infinitely many paths.

Next: distinguish a loop on one route from a state reached by two routes.

Search · 01 · Avoid repeated work

A loop and a duplicate are different problems

The same state can reappear on one route, or on two different routes.

Cycle

Check this path

A → B → A

A already occurs on this route.
Reject this cyclic extension.

Duplicate

Compare different paths

A → B → X
A → C → X

Both reach X, but neither route loops.

Cycle checking prevents loops. Reusing work across routes needs extra memory and a strategy-specific rule.

Search · 02 · Organize unfinished routes

The frontier is the waiting list of routes

After exploring A, suppose B, C and D are available next. Keep all three possibilities.

⟨A,B⟩   ·   ⟨A,C⟩   ·   ⟨A,D⟩Three candidate paths waiting for attention

Keep the alternatives

Choosing one route now must not lose the others.

A path, not just a place

⟨A,B⟩ records how we reached B, so we can return a complete route later.

The frontier contains unfinished candidates. It is not the list of states already explored.

Search · 02 · Explain the algorithm in words

Search in five steps

Use the waiting list to explore one candidate at a time.

StepWhat to do
1 · InitializePut only the start path ⟨A⟩ in the frontier.
2 · SelectChoose and remove one waiting path.
3 · TestIf its endpoint is a goal, return this path as the solution.
4 · ExpandOtherwise, extend the path to each legal successor.
Add these extended paths to the frontier.
5 · RepeatRepeat steps 2–4. If the frontier is empty, return failure.

Step 2 defines the search strategy: choosing a different waiting path changes the order of exploration.

Search · 02 · Trace one iteration

Now execute one iteration by hand

Use the course tree. Suppose the oldest waiting path is selected next; B has children E, F and G.

OperationFrontier · next first
Before selection⟨A,B⟩ · ⟨A,C⟩ · ⟨A,D⟩
Select ⟨A,B⟩⟨A,C⟩ · ⟨A,D⟩
Test B: not a goal⟨A,C⟩ · ⟨A,D⟩
Expand B; add its extensions⟨A,C⟩ · ⟨A,D⟩ · ⟨A,B,E⟩ · ⟨A,B,F⟩ · ⟨A,B,G⟩

B was selected and expanded. E, F and G are discovered, but still waiting.

01 · Model the problem

Four questions to ask of any search strategy

Completeness

Will it find a solution whenever one exists?

Optimality

Will it return a minimum-cost solution?

Time

How much search must it perform?

Memory

How many search records must it retain?

Every guarantee depends on assumptions about the graph, costs and implementation.

02 · Uninformed search

Uninformed Search: Search without an estimate of the goal

Choose systematically, using arrival order,
depth or cost already paid.

BFSDFSIDSUCS (Lowest-cost-first)
Search · 03 · Choose the oldest path

Breadth-first: serve the oldest waiting path

Breadth-first search (BFS) uses a first-in, first-out queue—like a checkout line.

First in → first out

Queue rule

Remove a path from the front.
Add its extensions to the back.

Course tree

After expanding A

Front → ⟨A,B⟩ · ⟨A,C⟩ · ⟨A,D⟩

Select B; its children join behind C and D.

In the lab, watch the queue: new deeper paths wait behind the shallower paths already there.

Interactive lab · Uninformed

BFS · watch the queue expand

Predict: after A, B, C and D, which node is selected? Try goal B as well as S.
Unit step costs: the returned cost equals the number of edges.

Search · Uninformed strategies

Why can BFS promise the fewest steps?

The lab completed each shallow layer before moving deeper. Queue order explains why.

First

Depth 0

Select the start state.

Then

Depth 1

Select all one-edge paths before the two-edge paths they generate.

Therefore

Depth d

d is the number of edges from the start.
The first selected goal cannot have a shallower goal still waiting.

Completeness & fewest edges

With finite branching, BFS eventually reaches a finite-depth goal; no shallower goal was skipped.

What it does not prove

Fewest edges does not necessarily mean lowest total cost.

Fewest steps means minimum cost only when each step has the same positive cost.

Search · 03 · Choose the newest path

Depth-first: serve the newest waiting path

Depth-first search (DFS) uses a last-in, first-out stack—like a pile of books.

Last in → first out

Stack rule

Remove the path on top.
Put its extensions on top.

Same course tree

After expanding A

Push B, then C, then D.

Top → ⟨A,D⟩ · ⟨A,C⟩ · ⟨A,B⟩
D is selected next.

In the lab, set the goal to B. Does a one-edge-away goal necessarily get selected quickly?

Interactive lab · Uninformed

DFS · follow the stack

Predict the first four selections. Then set goal B and compare with BFS.
Unit step costs: the returned cost equals the number of edges.

Search · Uninformed strategies

Backtracking means choosing a waiting alternative

Use the course tree: push A’s children in the order B, C, D. The top is shown first.

SelectedStack after expansion · next firstReason
AD · C · BD was pushed last.
DC · BD is a leaf; it adds nothing.
CJ · I · H · BC’s new children go above the old B.
JI · H · BAnother leaf: pop the next alternative.

The stack remembers where to try next. A recursive DFS uses the function-call stack for the same purpose.

02 · Uninformed search

What did changing the frontier rule change?

BFS · layers
DFS · branches

BFS: finish shallow layers. DFS: follow one branch; no shortest-path guarantee, and an infinite branch can postpone a goal forever.

Search · 04 · Explain the memory trade-off

A little branching creates a very wide tree

In a full tree, b, the branching factor, is the number of children per non-leaf. d is depth: the number of edges from the root.

Depths zero through four contain 1, 3, 9, 27 and 81 nodes; depth ten contains 59049011329327481At depth d: bᵈ nodesb = 3, d = 1059,049 nodes in one levelEach level triples the count.

bᵈ means b multiplied by itself d times. Each added depth level multiplies the node count by b.

Search · 04 · Count storage fairly

Store a parent link, reconstruct the route

A, B and G label states. Each record stores g (its total path cost from the start), and a link to its parent record.

Start

Record A

State: A
Parent: none
g = 0

After cost 2

Record B

State: B
Parent: record A
g = 2

After cost 1

Record G

State: G
Parent: record B
g = 3

Trace parents: G → B → A   ·   Reverse: A → B → GAlso retain the incoming action if the solution must specify actions, not just visited states.

One extension needs one new record, not a copied route. The memory comparisons use this representation.

Search · Uninformed strategies

A wide layer versus a deep branch

The main memory difference is how many unfinished alternatives stay live.

Exponential growth

BFS · keep a wide frontier

Keep the waiting paths across a wide layer.

The queue may span adjacent levels; storage grows exponentially with depth.

Linear in depth for fixed b

DFS · keep a branch and siblings

b is the maximum number of children per node; m is the maximum depth in edges.
Keep the branch and its waiting siblings.

O(bm) is an upper bound on record-count growth, proportional to b × m, ignoring constant factors.

Can we keep DFS-sized storage and still find a shallowest goal? This motivates iterative deepening.

02 · Uninformed search

Iterative deepening (IDS): keep the stack, limit the depth

Limit 0Limit 1Limit 2…
  • Run depth-limited DFS from the root, using a stack or recursion.
  • If a deeper branch was cut off, increase the limit.
  • Restart the search; stop at the first goal.
Shallowest solution, small frontier
  • Complete for finite branching and a finite-depth goal.
  • Fewest edges; cost-optimal for equal positive edge costs.
  • b is the maximum children per node; d is the shallowest goal depth in edges.
  • O(bd) bounds record-count growth by a constant times b × d; the price is repeated work.
Search · Uninformed strategies

A depth cutoff is not proof of failure

Each depth-limited pass needs to distinguish three outcomes.

Success

Found

A selected state is a goal.

Return the solution.

More may exist

Cutoff

Some legal continuation was stopped by the current depth limit.

Try a larger limit.

Nothing left

Exhausted

No goal and no deeper continuation remains under the search rules.

Stop: no solution.

Limit 0 → limit 1 → limit 2 → …Restart from the root each time; a cutoff says “not deep enough yet.”

Do not carry a simple “visited forever” set across passes: shallow visits must not block deeper exploration.

Interactive lab · Uninformed

IDS · replay the search at each depth

Watch the limit change from 0 to 4. Back restores the previous depth limit.
Unit step costs: the returned cost equals the number of edges.

02 · Uninformed search

Repeated work can be a reasonable price

We count node visits (search work), not edge costs. Assume every pass runs in full.

1 + 10 + 100 + 1000 = 1111

These are the nodes at depths 0, 1, 2 and 3. One full traversal visits each node once.

1·4 + 3·10 + 2·100 + 1000 = 1234

IDS restarts at limits 0, 1, 2 and 3: the root is visited 4 times, depth 1 3 times, depth 2 twice, and depth 3 once.

A complete tree · b = 10, d = 3

b = children per non-leaf node;
d = maximum depth in edges.

One pass through depth 3:
1111 node visits

Deepening through limits 0–3:
1234 node visits

123 extra visits: about 11% more work.

Most nodes are at the deepest level, which IDS visits only once. A long single chain is a less favorable case.

Search · 05 · Choose the cheapest path

UCS · When steps have different prices, select by total cost

Lowest-cost-first, also called uniform-cost search (UCS), orders the frontier by g, the total cost of a path from the start.

Change the selection rule

Priority queue

Always remove the path with the smallest g.

For an extension: new g = old g + edge cost.

Compare whole routes

Compare course-tree paths

A → C → H: 2 + 1 = 3
A → B → F: 1 + 6 = 7

The path to H has priority.

p ← arg minq ∈ F g(q)F is the frontier; q is a candidate path in F; g(q) is its total cost.
p is the selected path. ← assigns it; arg min chooses a path with the smallest g(q).

The smallest last edge is not enough: sum the whole path. Next, decide when a discovered goal is safe to accept.

Search · Uninformed strategies

Finding a goal is not yet enough to stop

Here A, B and G label states; G is the goal. g is total path cost from start A. Expanding A discovers G at cost 9 and B at cost 2.

A direct route to goal G costs 9; the two-edge route via B costs 2 plus 1219AstartBGgoal
Select by minimum gFrontier / result
A, g = 0B: 2 · G: 9
B, g = 2G: 3 replaces G: 9
G, g = 3Return A → B → G.

For UCS, accept a goal when its cheapest waiting path is selected—not when the goal is first generated.

Interactive lab · Uninformed

Lowest-cost-first · compare cumulative costs

Predict the next selected path, then run to goal S. g is total path cost from the start; numbers on edges are individual step costs.

Search · 05 · Extend the idea to graphs

“Already seen” must not hide a cheaper path

A, B, X and G are state labels. A → X costs 8; A → B → X costs 2 + 1 = 3. g is total path cost from start A.

Tentative

First discovery

best_g[X] = 8
Parent of X = A

Improve

A better route arrives

new_g = 3 < best_g[X] = 8
Update the record and parent.

Propagate

Continue from X

If X → G costs 2,
the improved solution costs 5.

new_g < best_g[state] → keep the improvementstate is the route’s endpoint; new_g is the candidate route’s cost.
best_g[state] is the lowest cost discovered for that state; [state] selects its saved entry.
Replace or reinsert an improved record; skip obsolete entries when selected.

With nonnegative costs, UCS finalizes a state’s best g on selection, not on first discovery.

Search · Will the algorithm reach the goal?

Why a reachable goal can keep waiting

Uniform-cost search (UCS) selects the cheapest waiting path—even when a goal is already in the frontier.

A goal costing one competes with an infinite chain of free moves From start A, one edge reaches the goal at cost 1. An alternative edge costs 0 and reaches B, then zero-cost edges lead to C, D and endlessly many new states. UCS always chooses the next zero-cost path while the goal waits. edge cost 1The goal is reachable in one move.0000 ABCD…Goal New states forever—not a loop that duplicate checks can remove.

The goal keeps losing the comparison

After expandingWaiting paths · total cost
AA → B: 0   |   A → Goal: 1
BA → B → C: 0   |   A → Goal: 1
CA → B → C → D: 0   |   A → Goal: 1

Every selected free path creates another free path. The goal is never selected.

Finite graph: avoid repeating states

With nonnegative costs, keep a new route to a state only if it improves the best known cost. Zero-cost loops then stop adding copies.

Infinite space: make every step count

Require finitely many next moves per state and one fixed positive lower bound ε > 0 on every step cost; ε (epsilon) names that bound. For example, if each step costs at least 1, detours cost at least 1, 2, 3, …

With these safeguards, only finitely many candidates can come before the goal. UCS eventually selects it.

02 · Uninformed search

Choose a strategy for the final challenge

MethodSelect bySolution guaranteeFrontier memory
BFSOldest pathFewest edgesExponential in depth
DFSNewest pathAny solution, if it terminatesLinear in depth*
IDSDFS, increasing limitFewest edgesLinear in depth*
UCS (Lowest-cost-first)Minimum g · total path costLeast cost**Can be exponential

* For fixed branching and parent-linked records. ** Under the cost and termination assumptions discussed.

Interactive lab · Uninformed

Choose an exit: fewest steps or lowest cost?

Compare BFS and lowest-cost-first: predict the exit, then record steps and cost.
g = cost from the start. In the optional Part 2 modes: h = estimated remaining cost; f = g + h = estimated total cost.

Lecture 03 · Wrap-up

Before you leave this part

  • Represent a task using states, actions, a goal and costs.
  • Explain the difference between queue and stack exploration.
  • Choose an uninformed method using the costs and memory available.

Choose one idea and explain it in your own words. Use an example or an interactive experiment to support your explanation.

Sources & further reading

Continue exploring

Primary course resource

AIML 2025/26 · Lesson 01 — Search

Content sequence, six original interactive examples, course-tree topology, edge costs and heuristic values.

Textbook reference

Poole & Mackworth · Artificial Intelligence: Foundations of Computational Agents, 3rd ed. · Chapter 3

Search strategies, heuristic conditions and pruning.

Adaptation notes, complete slide notes and source mapping are included in the notes folder.
The interactive slides run locally; the gallery and PDF are static review copies.

Continue learning
AIML · Lecture 03 · Search algorithms · Part 1
Course home