'S'
→ 'Sa'
→ 'Sab'
→ 'SabG'
path[-1] gives 'b'path[-1] gives 'G'['A'] -> []'A'[]A ≠ B[] -> ['AS', 'AT', 'AZ']
A -> ['AS', 'AT', 'AZ']AS -> ['AT', 'AZ', 'ASA', 'ASF',
'ASO', 'ASR']AT -> ['AZ', 'ASA', 'ASF', 'ASO',
'ASR', 'ATA', 'ATL']AZ -> ['ASA', 'ASF', 'ASO', 'ASR',
'ATA', 'ATL', 'AZA', 'AZO']explored = {'A', 'S'}
A ∈ explored → skip
['AS','AT','AZ']stores path strings{'A'}stores states onlyexplored = set()state in exploredexplored.add(state)
S -> ['Sa', 'Sd', 'SG']
explored: ['S']
Sa -> ['Sd', 'SG', 'Sab']
explored: ['S', 'a']
Sd -> ['SG', 'Sab', 'Sdb', 'Sde']
explored: ['S', 'a', 'd']
solution: SG
| Depth | Nodes | Time | Memory |
|---|---|---|---|
| 2 | 110 | 0.11 milliseconds | 110 kilobytes |
| 4 | 11,110 | 11 milliseconds | 11 megabytes |
| 6 | 1.11 × 106 | 1.1 seconds | 1.1 gigabytes |
| 8 | 1.11 × 108 | 1.9 minutes | 111 gigabytes |
| 10 | 1.11 × 1010 | 3.1 hours | 11 terabytes |
| 12 | 1.11 × 1012 | 12.9 days | 1.1 petabytes |
| 14 | 1.11 × 1014 | 3.5 years | 111 petabytes |
| 16 | 1.11 × 1016 | 352 years | 11 exabytes |

frontier is a queue (first in first out data structure)

frontier is a stack (last in first out data structure)
A -> ['AS', 'AT', 'AZ']
explored: ['A']AZ -> [..., 'AZA', 'AZO']
explored: ['A', 'Z']AZO -> [..., 'AZOS', 'AZOZ']
explored: ['A', 'O', 'Z']AZOS -> [..., 'AZOSR']
explored: ['A', 'O', 'S', 'Z']
S -> ['Sa', 'Sd', 'SG']
explored: ['S']
solution: SG
When a solution is shallow
When a solution lies deep along an early branch
| TSA | GSA | |
|---|---|---|
| Memory | Frontier | Frontier + explored set |
| Repeated state | Expand it again | Skip it |
| Trade-off | Less memory | Fewer repeated expansions |

frontier is a priority queue (smallest total path cost first)
A @ 0 -> [(75, 'AZ'), (118, 'AT'), (140, 'AS')]
explored: ['A']AZ @ 75 -> [(118, 'AT'), (140, 'AS'), ...]
explored: ['A', 'Z']AT @ 118 -> [(140, 'AS'), (146, 'AZO'), ...]
explored: ['A', 'T', 'Z']AS @ 140 -> [(146, 'AZO'), (150, 'AZA'), ...]
explored: ['A', 'S', 'T', 'Z']AZO @ 146 -> [(150, 'AZA'), (217, 'AZOZ'), ...]
explored: ['A', 'O', 'S', 'T', 'Z']
S @ 0 -> [(2, 'Sd'), (3, 'Sa'), (10, 'SG')]
explored: ['S']Sd @ 2 -> [(3, 'Sa'), (3, 'Sdb'), (6, 'Sde'), (10, 'SG')]
explored: ['S', 'd']Sa @ 3 -> [(3, 'Sdb'), (6, 'Sde'), (8, 'Sab'), (10, 'SG')]
explored: ['S', 'a', 'd']Sdb @ 3 -> [(4, 'Sdbe'), (5, 'Sdbc'), (6, 'Sde'), (8, 'Sab'), (10, 'SG')]
explored: ['S', 'a', 'b', 'd']Sdbe @ 4 -> [(5, 'Sdbc'), (6, 'Sde'), (7, 'SdbeG'), (8, 'Sab'), (10, 'SG')]
explored: ['S', 'a', 'b', 'd', 'e']Sdbc @ 5 -> [(6, 'Sde'), (7, 'SdbeG'), (8, 'Sab'), (9, 'SdbcG'), (10, 'SG')]
explored: ['S', 'a', 'b', 'c', 'd', 'e']| Strategy | Frontier | Next path | Optimal? |
|---|---|---|---|
| BFS | FIFO queue | Shallowest | Only with equal step costs |
| DFS | LIFO stack | Deepest | No |
| UCS | Priority queue | Cheapest | Yes |
| Path | Priority |
|---|---|
AS |
h(S) = 253 |
AT |
h(T) = 329 |
AZ |
h(Z) = 374 |

A @ 366 →
[(253, 'AS'), (329, 'AT'), (374, 'AZ')]
explored: ['A']AS @ 253 →
[(176, 'ASF'), (193, 'ASR'), ...]
explored: ['A', 'S']ASF @ 176 →
[(0, 'ASFB'), (193, 'ASR'), ...]
explored: ['A', 'F', 'S']ASFB @ 0 → goal B
solution: ASFBStart: S · Goal: G
Which states are expanded?
Which path is returned? Is it the cheapest?
Initial frontier: [(7, 'S')]
Exploring: S at h = 7
Frontier:
[(0, 'SG'), (5, 'Sd'), (9, 'Sa')]Solution path: SGSab: g=2
Sad: g=4
Sae: g=9
Sab → Sabc
c is a dead end
Sad → SadG
Solution cost: 6
Sab: h(b)=6
Sad: h(d)=2
Sae: h(e)=1
Sae is selected first
h(e)=1 is the smallest estimate
Sad and Saed tie at h=2
Path-string order selects Sad



Sab: 2 + 6 = 8
Sad: 4 + 2 = 6
Sae: 9 + 1 = 10
Its estimated total cost is smallestSadG: 6 + 0 = 6
Expanded: S → a → d
Returned path: SadG, cost 6
| Path | g | h | f = g + h |
|---|---|---|---|
AS |
140 | 253 | 393 |
AT |
118 | 329 | 447 |
AZ |
75 | 374 | 449 |
| Expanded path | f | Two lowest frontier priorities |
|---|---|---|
| A | 366 | AS: 393 · AT: 447 |
| AS | 393 | ASR: 413 · ASF: 415 |
| ASR | 413 | ASF: 415 · ASRP: 417 |
| ASF | 415 | ASRP: 417 · AT: 447 |
| ASRP | 417 | ASRPB: 418 · AT: 447 |
Start: S · Goal: G
Use f(n)=g(n)+h(n)
Predict the expansion order
and returned path
Break ties by path-string order
Initial frontier: [(7, 'S')]S @ 7 -> [(7, 'Sd'),
(10, 'SG'), (12, 'Sa')]Sd @ 7 -> [(7, 'Sdb'), (9, 'Sde'),
(10, 'SG'), (12, 'Sa')]Sdb @ 7 -> [(7, 'Sdbc'), (7, 'Sdbe'), (9, 'Sde'),
(10, 'SG'), (12, 'Sa')]Sdbc @ 7 -> [(7, 'Sdbe'), (9, 'SdbcG'), (9, 'Sde'),
(10, 'SG'), (12, 'Sa')]Sdbe @ 7 -> [(7, 'SdbeG'), (9, 'SdbcG'), (9, 'Sde'),
(10, 'SG'), (12, 'Sa')]Solution path: (7, 'SdbeG')Initial frontier: [(7, 'S')]
S @ 7 -> [(5, 'SG'), (7, 'Sa')]Solution path: (5, 'SG')0 ≤ h(n) ≤ h*(n)
h(n) may underestimate, but must not overestimate
h(goal)=0
h(a) = 6 > h*(a) = 3
h(S) = 7 > h*(S) = 4
What values are allowed for h(a)?
What values are allowed for h(S)?
h*(a)=3
Therefore 0 ≤ h(a) ≤ 3
h*(S)=min(5, 1+3)=4
Therefore 0 ≤ h(S) ≤ 4
Let
Claim
Let n be an ancestor of A
Let B and n both be on the frontier
Claim: n exits the frontier before B
f(n) ≤ f(A)
f(A) < f(B)
All ancestors of A exit the frontier before B
A exits the frontier before B
A*-TSA is optimal with admissible h
h*(G) = 0, h*(c) = 3
h*(a) = 4, h*(b) = 5
h*(S) = 5
0 ≤ 0 · 1 ≤ 3 · 4 ≤ 4
1 ≤ 5 · 2 ≤ 5
Yes, this heuristic is admissible
Initial frontier: [(2, 'S')]S @ 2 -> [(2, 'Sb'), (5, 'Sa')]Sb @ 2 -> [(4, 'Sbc'), (5, 'Sa')]Sbc @ 4 -> [(5, 'Sa'), (6, 'SbcG')]Sa @ 5 -> [(3, 'Sac'), (6, 'SbcG')]Sac @ 3 -> [(5, 'SacG'), (6, 'SbcG')]Solution path: (5, 'SacG')Initial frontier: [(2, 'S')]
Explored: ∅S @ 2 -> [(2, 'Sb'), (5, 'Sa')]
Explored: {S}Sb @ 2 -> [(4, 'Sbc'), (5, 'Sa')]
Explored: {S, b}Sbc @ 4 -> [(5, 'Sa'), (6, 'SbcG')]
Explored: {S, b, c}Sa @ 5 -> [(3, 'Sac'), (6, 'SbcG')]
Explored: {S, b, c, a}Skip Sac: c already exploredSolution path: (6, 'SbcG')Definition
h(a) − h(c) ≤ cost(a,c)
Equivalently
h(a) ≤ cost(a,c) + h(c)
Consequence of consistency
h(u) ≤ cost(u, v) + h(v)
S → a: 2 ≤ 1 + 4
S → b: 2 ≤ 1 + 1
a → c: 4 > 1 + 1
b → c: 1 ≤ 2 + 1
c → G: 1 ≤ 3 + 0
a → c: 2 ≤ 1 + 1
S → a: 2 ≤ 1 + 2
a → c: 4 ≤ 1 + 3
c → G: 3 ≤ 3 + 0
Other edges remain consistent
Both heuristics remain admissible
Returns a cheapest solution when h is admissible
Returns a cheapest solution when h is consistent
Yes, if one exists, with finite branching
and every step cost ≥ ε > 0
Can still grow exponentially
Find a sequence of actions
Compare path costs and depths
Use heuristics and a frontier
Find an assignment to variables
Example: assign a position to each queen
Local search can be faster for some problems
Eight queens on the board, one per column
Move one queen to another square
in the same column
Every such move gives a neighbour
Count pairs of queens that attack each other
Same row or diagonal
Direct: Q6 and Q8
Indirect: Q2 and Q8, with Q6 between
Count each pair once
Q1 with Q2, Q3, Q5+3
Q2 with Q3, Q4, Q6, Q8+4
Q3 with Q5, Q7+2
Q4 with Q5, Q6, Q7+3
Q5 with Q6, Q7+2
Q6 with Q7, Q8+2
Q7 with Q8+1
3 + 4 + 2 + 3 + 2 + 2 + 1 = 17
One queen
7 other rows
Eight queens
8 × 7 = 56 neighbours
Each number is the cost of the whole board
after moving that column’s queen
Example: move Q2 to the top row
The resulting board costs 12
The current board costs 17
Lowest of all 56 costs: 12
Several moves tie; choose one at random
Steepest descent for a cost we want to minimise
Accept only strictly lower costs
Current cost: 2
Lowest neighbour cost: 2
No strictly better move
Stop, but not solved
An equal-cost move would be a sideways move
Five seeds × 10,000 independent attempts
Solved
14.07%Mean moves if solved
4.08Mean moves if stopped
3.067,035 solutions in 50,000 attempts
Success across the five seeds: 13.23%–14.42%
Close to AIMA’s approximate 14% success and 4 / 3 moves
One strict attempt, without sideways moves · Accepted moves only
Attempt 1
Stopped: cost 1
Attempt 2
Stopped: cost 3
Attempt 3
Solved: cost 0
Repeat until solved, or a chosen attempt limit is reached
The 14% success rate describes one attempt
Start at S, goal G
Frontier: Add alphabetically
1. Exploration Order
2. Solution path
Uppercase strings, no spaces
Example: SAG
Submit on EdExplored
SABCDEFJH
Solution
SAEG
Start at S, goal G
Frontier: Add alphabetically
1. Exploration Order
2. Solution path
Uppercase strings, no spaces
Example: SAG
Submit on EdExplored
SCJFIH
Solution
SCFIHG
Start at S, goal G
Frontier: Add alphabetically
1. Exploration Order
2. Solution path
Uppercase strings, no spaces
Example: SAG
Submit on EdExplored
SBADECFJHI
Solution
SBEHG
Cost: 14
Submit on Ed
Start at S, goal G
Frontier: Add alphabetically
1. Exploration Order
2. Solution path
3. Admissible?
4. Consistent?
Uppercase strings, no spaces
Example: SAG
Explored: SABEH
Solution: SBEHG
Cost: 14
Admissible: Yes
Consistent: No
Minimise attacking pairs
No sideways moves
1. Sequence of moves
2. What is the cost after the last move?
Uppercase cells, separated by one space
Example: B4 E2
Submit on Ed1. A4 D2
2. 1
A factored state records a value for each variable
A solution assigns every variable a value that satisfies all constraints
Use constraints to rule out combinations before exploring them
Variables
Domains contains the allowed values of
ConstraintsAllowable combinations of values
A state is a partial or complete assignment
Consistent: no constraint is violated
Complete: every variable has a value
A solution is both consistent and complete
Variables
Domains
ConstraintsNeighbours have different colours


Nodes represent variables
Edges represent constraints
Tasmania is independent
of the mainland variables
Q
NSW
V
NT
WA
SA
T

StateThe values assigned so far
Initial StateEmpty assignment
SuccessorAssign a value to an unassigned variable
Goal TestComplete and consistent
All solutions are at depth 7


…




…





…






…
Try DFS


…
…

…



Early mistake …


…
…

Choose one unassigned variable at each levelBranch over its legal values
Try only values consistent with earlier assignments
If no legal value remains, backtrack
Depth-first search with these two ideas is backtracking search





. . .



Q
NSW
V
NT
WA
SA
T
NSW
WA
NT
NT
Q
Q
Q
SA
SA
SA
SA
Q
NT
Q
Q
SA
SA
SA
SA
Q
NT
Q
WA
NT
Q
Q
SA
SA
SA
V
V
T
Can we detect inevitable failure earlier?
Forward checking: remove conflicting values from neighbours
Constraint propagation: repeat these deductions with AC-3

| WA | NT | Q | NSW | V | SA | T |




Detect failure earlier
Q
NSW
V
NT
WA
SA
T
NSW
NSW
WA
NT
Q
NT
Q
Q
NT
Q
WA
NT
Q
SA
V
T

| WA | NT | Q | NSW | V | SA | T |



| WA | NT | Q | NSW | V | SA | T |
| WA | NT | Q | NSW | V | SA | T |

Why don’t I have to add this one ?
Update the domain
of
has four neighbours
including
Q
NSW
V
NT
WA
SA
T
NSW
WA
NT
NT
WA
NT
NT
Q
SA
V
T
Variable Ordering
Minimum Remaining Values (MRV)
Choose the variable with the fewest legal values
Most constrained variable
Fail-first heuristic
Break MRV Ties
Degree Heuristic
Most constraints on unassigned variables
Value Ordering
Least Constraining Value (LCV)
Rules out the fewest values in unassigned neighbours
Minimum Remaining Values
Choose the unassigned variable with the fewest legal values
Most constrained variable · Fail-first heuristic



Using Forward Checking

Degree Heuristic
Among MRV ties, choose the variable with the most
constraints on unassigned variables

using forward checking

Least Constraining Value
Try the value that removes the fewest options
from unassigned neighbours



Q
NSW
V
NT
WA
SA
T
SA
NSW
WA
NT
Q
V
T

Our next state depends on our move and their response


Chance: Later

Here: Two Players · Alternating Turns · Deterministic · Fully Observable
Zero-Sum: What Helps MAX Hurts MIN
Game Tree: Nodes Are States; Edges Are Moves
Players
X is MAX
O is MIN
Utility for X
Win: +1
Draw: 0
Loss: −1
State Includes
the Player to Move

MAX Chooses Left: 3 Is Larger Than 2
def minimax(tree, maximizing=True):
if not isinstance(tree, list): return tree
values = [minimax(child, not maximizing) for child in tree]
return max(values) if maximizing else min(values)Base Case
A leaf returns its utility
Recursive Step
Visit every child and switch the player
Back Up
MAX returns max; MIN returns min
One Recursive Call per State in the Game Tree
MIN Moves First: What Is the Root Value? Which Move?
Value 1 · MIN Chooses the Middle Branch
Find the Values of x That Make Left Strictly Better
Use x on the left
No spaces
Example: x<3
Submit on EdLeft = min(max(x, −8), 11) · Right = 8
x > 8: Left Wins · x = 8: Tie
An Optimal Strategy Against Optimal Opposition


It Maximizes the Worst-Case Payoff
It Does Not Predict Every Opponent’s Actual Move
Left Guarantees 10 · Right Guarantees 9
If MIN Chooses Randomly: Right Averages 54.5
Finite Game Tree · Branching Factor b · Maximum Depth m
Search All Replies, Depth First
Illustrative Chess Estimate
b ≈ 35 · m ≈ 100
≈ 2.6 ×
We Cannot Search to Every Terminal Position


Depth Limit ℓ = 2 · MAX and MIN Still Alternate
Terminal: Exact Utility · Cutoff: Estimate the Position
Time: O() · Space: O(bℓ)
Estimated Root Value: 4 · Choose Right
The Guarantee Depends on the Estimates
Eval(s): Estimate the Position’s Value for MAX
Ideal: The True Minimax Value
Useful: Fast Enough to Evaluate Many Positions
A Larger Number Should Mean a Better Position for MAX



Which Functions Prefer the Left Position?
A.
B.
C.
D.
Left: f = 3, g = 7 · Right: f = 2, g = 3
B and C Prefer Left · A and D Prefer Right

Feature: MAX’s Material − MIN’s Material
Also Consider King Safety and Pawn Structure
Who Has the Advantage in Each Position?

(a) Material Count: Black Has More Material
(b) Tactical Check: White Can Capture the Queen
Material Alone Can Miss an Immediate Tactic
At the Cutoff, We Estimate a Non-Terminal State
A Deeper Cutoff Is Often Closer to the Real Outcome
Tradeoff: Better Features or More Search?


A Bad Consequence Lies Just Beyond the Cutoff
A Delaying Move Pushes It Out of Sight
The Delay May Make the Final Outcome Worse


Black to Move
Can a Delaying Tactic Avoid the Loss?
No · It Only Pushes the Loss Beyond a Shallow Horizon
Can We Compute the Exact Minimax Choice Without Visiting Every Node?

Yes · When a Branch Cannot Affect the Decision
Left Gives 3 · Right MIN Finds 2
The Other Right Leaves Cannot Change MAX’s Choice
Skip Branches That Cannot Change the Root Decision
Pruning Changes Work, Not the Answer
Root α = −∞ · Current β = +∞
Left MIN updates β to 6
Root updates α to 6
Middle MIN updates β to 2
β = 2 ≤ α = 6
β = 2 ≤ α = 6
Right MIN updates β to 8
Current v = 9 · Right MIN β = 8
v = 9 ≥ β = 8
Root α = 6 · Right value = 8
Final α = 8
Search Left to Right · Which Branch Is Pruned?
Pruned Branch: F · Left MIN Gives 8, Then Right MIN Finds 4 ≤ α
Search Left to Right · Which Branches Are Pruned?
Pruned Branches: F, J, M, N
Same Minimax Value: 3 · Different Amount of Search
Minimax: O()
Ideal Alpha–Beta Ordering: O()
Roughly Twice the Search Depth for the Same Node Budget
Example: b = 10 and m = 6 Gives 10⁶ Leaves vs About 10³
Worst Case Remains O()
MAX Chooses Left: 10 > 9
MAX Chooses Right: 54.5 > 10
A Chance Node Needs a Probability Model
Expected Value Is Only as Good as That Model
MAX Node
Take the Largest Child Value
CHANCE Node
Take the Probability-Weighted Average
Terminal Node
Return Utility

CHANCE: V(s) = Σ pᵢ V(sᵢ)
0.9(2) + 0.1(3) = 2.1
0.9(1) + 0.1(4) = 1.3
MAX Chooses Left: 2.1 > 1.3
Pacman First: WEST or EAST
After WEST and Both Ghosts, Pacman: EAST or SOUTH
Lower Ghost: NORTH or EAST · Upper Ghost: WEST only
Minimax: Worst Legal Ghost Move
Expectimax: Lower Ghost N/E with Probability 0.5 Each
Only Food Eaten: Win · Ghost Collision: Loss
Score: −1 per Pacman action · +10 food · +500 win · −500 loss
Collision on Food: Loss, No Food Points
1. Under minimax, what is Pacman’s optimal first action?
2. What value does minimax assign to the root?
3. Under expectimax, what is Pacman’s optimal first action?
4. What value does expectimax assign to the root?

Red chooses the first value · Green the second · Blue the third
Password: Dirk