Kuliah 3 : Problem Solving & Search
1
Outline
Problem-Solving Agent (Goal Based)
Problem Formulation
Example Problems
Basic Search Algorithms
2
Problem-Solving Agents
Problem-Solving Agent is a Goal-Based
Agent that decides what to do by finding
sequences of actions that lead to
desirable states.
3
Problem-Solving Agent
sensors
?
environment
agent
actuators
4
Problem-Solving Agent
sensors
?
environment
agent
actuators
• Formulate Goal
• Formulate Problem
•States
•Actions
• Find Solution
5
Selecting a State Space
Real world is absurdly complex
State Space must be abstracted for
Problem Solving : mendefinisikan
masalah sebagai ruang keadaan.
Ruang keadaan / state space = ruang
yang berisi semua keadaan yang
mungkin.
Untuk merepresentasikan state space
ada beberapa cara : graph, tree.
6
Selecting a State Space
State space terdiri dari :
State = Set of Real States.
Action = Complex combination of real actions
= suatu tindakan yang dapat mengubah
suatu state (current state) menjadi state
lain (next state)
Action dilakukan berdasarkan aturan permainan.
Solution =
Set of real paths that are solutions in the real world.
7
Problem solving
Mendeskripsikan permasalahan yang baik
maka kita harus :
1.
2.
3.
4.
Mendefinisikan ruang keadaan (state space)
Menetapkan keadaan awal (initial/state
space)
Menetapkan keadaan tujuan (goal state)
Menetapkan kumpulan aturan untuk
menentukan aksi yang dilakukan (action)
8
Problem Formulation
A Problem is defined by four items:
1. Initial state : kondisi awal permasalahan
2. Actions or Successor Function
S(x) = set of action – state pairs
an abstract representation of the possible actions
3. Goal Test
usually a condition, sometimes the description
of a state)
4. Path Cost (additive)
e.g., sum of distances, number of actions executed,
etc.
9
Example : Teko Air
2 buah teko air:
Teko A (berkapasitas 4 galon air)
Teko B (berkapasitas 3 galon air)
Tidak ada tanda yang menunjukkan batas ukuran
pada kedua teko tersebut.
Ada sebuah pompa air yang akan digunakan untuk
mengisikan air pada kedua teko tersebut.
Bagaimanakah kita dapat mengisi tepat 2 galon air
ke dalam teko A?
10
Example: Teko Air
State : jumlah air pada masing-masing teko
Representasi : (X,Y) di mana 0X4, 0Y3
X= kapasitas air pada Teko A
Y= kapasitas air pada Teko B
Initial state : kondisi masing-masing teko kosong
X=0, Y=0 sehingga state : (0,0)
Goal: mengisi tepat 2 galon air ke dalam teko A
state : (2,Y)
Actions : isi air, pindah isi air, kosongkan teko
11
Aksi (sekumpulan aturan)
1.
2.
3.
Isi teko A/B sampai penuh
Kosongkan teko A/B (buang air ke
tanah)
Tuang air dari teko A ke B, tuang dari
dari teko B ke A
12
Example: Measuring problem
9l
3l
5l
Problem:
Using these three buckets, measure 7 liters of
Water in 9-liter bucket!
13
Example: Measuring problem
Measure 7 liters of water using a 3-liter, a 5liter, and a 9-liter buckets.
Formulate goal: Have 7 liters of water in 9liter bucket
Formulate problem:
States: Amount of water in the buckets
Actions: Fill bucket from source, empty
bucket
Find solution: Sequence of operators that
bring you from current state to the goal state
14
Example: Measuring problem
(one possible) Solution :
a
b
c
0
0
0
start
3
0
0
0
0
3
3
0
3
0
0
6
3
0
6
0
3
6
3
3
6
1
5
6
0
5
7
goal
9l
3l
a
5l
b
c
15
Example: Measuring problem
• Another Solution :
a
0
0
3
3
3
3
b
0
5
2
0
5
0
c
0
0
0
2
2
7
start
9l
3l
a
5l
b
c
goal
Which solution do we prefer ?
16
Example: Route finding
Problem:
Find path from Arad to Bucharest !
17
Example: Route finding
States
Actions
Start
Solution
Goal
18
Example: Route finding
On holiday in Romania; currently in Arad.
Flight leaves tomorrow from Bucharest
Formulate goal: Be in Bucharest
Formulate problem:
States: Various cities
Actions: Drive between cities
Find solution: Sequence of cities, e.g.,
Arad, Sibiu, Fagaras, Bucharest
19
Example: Vacuum World
State Space Graph
States ?
Actions?
Goal Test ?
Path Cost ?
• Dirt and robot location
• Left, Right, Suck
• No dirt at all locations
• 1 per action
*) ignore dirt amount
20
Example: The 8-Puzzle
States ?
Actions ?
Goal Test ?
Path Cost ?
8
2
3
4
5
1
•
•
•
•
Locations of tiles
Move blank left, right, up, down
Goal state (given)
1 per move
1
2
3
7
4
5
6
6
7
8
Initial state
Goal state
21
Example: The 8-Puzzle
8
2
3
4
7
5
1
6
8
2
3
4
5
1
8
7
6
2
8
2
3
4
7
3
4
7
5
1
6
5
1
6
22
Example: The 8-Puzzle
Size of the state space = 181,440
15-puzzle .65 x 1012
0.18 sec
6 days
24-puzzle .5 x 1025
12 billion years
10 million states/sec
23
Example: Robotic Assembly
real-valued coordinates of robot joint
States ?
angles parts of the object to be assembled
Actions ? continuous motions of robot joints
Goal Test ? complete assembly
Path Cost ? time to execute
24
Example: Robotic Assembly
25
Example: Robotic Assembly
26
Example: The 8-Queens
Place 8 queens in a chessboard so that no two
queens are in the same row, column, or
diagonal.
States ?
Actions ?
Goal Test ?
Path Cost ?
27
Example: The 8-Queens
Formulation 1 :
States ?
Any arrangement of 0 to 8 queens on the board.
Actions ?
Add a queen in any square.
Goal Test ?
8 queens on the board, none attacked.
Path Cost ?
1 per move.
648 states with 8 queens
28
Example: The 8-Queens
Formulation 2 :
States ?
Any arrangement of k = 0 to 8 queens in the k
leftmost columns with none attacked.
Actions ?
Goal Test ?
Path Cost ?
Add a queen to any square in the leftmost empty
column such that it is not attacked by any other
queen.
8 queens on the board, none attacked.
1 per move.
2,067 states
29
Exercise: River Crossing
(*)
30
Exercise: Jugs
31
Exercise: Tower of Hanoi
(*)
32
Answer: River Crossing
A directed graph in state space
33
Answer : River Crossing
State space S : all valid configurations
• Initial states I={(CSDF,)}
. Where’s the boat?
• Goal states G={(,CSDF)}
• Actions : states reachable in one step from S
. Succs ((CSDF,)) = {(CD, SF)}
. Succs ((CDF,S)) = {(CD,FS), (D,CFS), (C, DFS)}
• Path Cost = 1 for all transitions. (weighted later)
• The search problem: find a path from a state in I to a
state in G.
(*)
34
Answer : Tower of Hanoi
(*)
35
Other Example Problems
Travelling Salesperson Problem (TSP) :
Each city must be visited exactly once – find the
shortest tour
VLSI (Very Large Scale Integration) Layout
Given schematic diagram comprising components
(chips, resistors, capacitors, etc) and
interconnections (wires), find optimal way to place
components on a printed circuit board, under the
constraint that only a small number of wire layers are
available (and wires on a given layer cannot cross !)
36
Problem solving by
Searching
37
Searching as a Problem Solving
Technique
Problem solving by Searching
Searching is the process of looking for the solution
of a problem through a set of possibilities (state
space).
Search conditions include :
Current state - where one is;
Goal state - the solution reached; check whether
it has been reached;
Cost of obtaining the solution.
The Solution is a path from the current state to the
goal state.
38
Searching as a Problem Solving
Technique
Process of Searching
Searching proceeds as follows :
Check the current state;
Execute allowable actions to move to the next
state;
Check if the new state is the solution state; if it is
not, then the new state becomes the current state
and the process is repeated until a solution is
found or the state space is exhausted.
39
Search of State Space
40
Search of State Space
41
Search of State Space
42
Search of State Space
43
Search of State Space
44
Search of State Space
Explore the Search Tree to find solution(s). 45
Assumptions in Basic Search
The
The
The
The
environment is Static
environment is Discretizable
environment is Observable
actions are Deterministic
46
Tree Search Algorithms
Basic idea:
offline, simulated exploration of state space by
generating successors of already-explored states
(a.k.a.~expanding states)
47
Tree Search Example
48
Tree Search Example
49
Tree Search Example
50
Implementation: States vs. Nodes
A state is a (representation of) a physical
configuration
A node is a data structure constituting part of
a search tree includes state, parent node,
action, path cost g(x), depth
51
The Expand function creates new nodes,
filling in the various fields and using the
SuccessorFn of the problem to create the
corresponding states.
52
Implementation :
General Tree Search
53
Fringe
Set of search nodes that have not been
expanded yet.
Implemented as a queue FRINGE.
INSERT(node,FRINGE)
REMOVE(FRINGE)
The ordering of the nodes in FRINGE
defines the search strategy.
54
Search Strategies
A search strategy is defined by picking the
order of node expansion
Strategies are evaluated along the following
dimensions:
Completeness: does it always find a solution if one
exists?
Time complexity: number of nodes generated
Space complexity: maximum number of nodes in
memory
Optimality: does it always find a least-cost
solution?
55
Search Strategies
Time and space complexity are measured in
terms of
b: maximum branching factor of the search tree
d: depth of the least-cost solution
m: maximum depth of the state space (may be ∞)
56
Blind vs. Heuristic Strategies
Blind (or uninformed) strategies do not
exploit any of the information contained in a
state.
Heuristic (or informed) strategies exploits
such information to assess that one node is
“more promising” than another.
57
Uninformed search strategies
Uninformed search strategies use only the
information available in the problem
definition.
Breadth-First Search
Uniform-Cost Search
Depth-First Search
Depth-Limited Search
Iterative Deepening Search
58
Breadth-First Search
Expand shallowest unexpanded node.
Implementation:
Fringe is a FIFO queue, i.e., new
successors go at end.
59
Breadth-First Search
Fringe = {A}
D adalah
Node Goal
60
Breadth-First Search
Fringe = {B,C}
D adalah
Node Goal
61
Breadth-First Search
Fringe = {C,D,E}
D adalah
Node Goal
62
Breadth-First Search
Fringe = {D,E,F,G}
D adalah
Node Goal
stop
63
8-Puzzle Problem
64
65
Properties of Breadth-First Search
Complete ? Yes (if b is finite)
Time ? 1+b+b2+b3+… +bd + b(bd-1) =
O(bd+1)
Space ? O(bd+1) (keeps every node in
memory)
Optimal ? Yes (if cost = 1 per step)
Space is the bigger problem (more than time)
66
Uniform-Cost Search
Expand least-cost unexpanded node
Implementation:
Fringe = queue ordered by path cost.
Equivalent to breadth-first if step costs all
equal.
67
Uniform-Cost Search
Fringe = {S|0}
Fringe = {A|1, B|5, C|15}
Fringe = {B|5, G|11, C|15}
Fringe = {G|10, G|11, C|15}
68
0
69
75
X
140
118
70
146
X
X
140
118
71
146
X
X
140
X
229
72
Properties of Uniform-Cost Search
Complete? Yes, if step cost ≥ ε.
Time? # of nodes with g ≤ cost of optimal
solution, O(bceiling(C*/ ε)) where C* is the cost of
the optimal solution.
Space? # of nodes with g ≤ cost of optimal
solution, O(bceiling(C*/ ε)).
Optimal? Yes – nodes expanded in increasing
order of g(n).
73
Depth-First Search
Expand deepest unexpanded node.
Implementation:
Fringe = LIFO queue, i.e., put successors
at front.
74
Depth-First Search
Fringe = {A}
M adalah
Node Goal
75
Depth-First Search
Fringe = {B,C}
M adalah
Node Goal
76
Depth-First Search
Fringe = {D,E,C}
M adalah
Node Goal
77
Depth-First Search
Fringe = {H,I,E,C}
M adalah
Node Goal
78
Depth-First Search
Fringe = {I,E,C}
M adalah
Node Goal
79
Depth-First Search
Fringe = {E,C}
M adalah
Node Goal
80
Depth-First Search
M adalah
Node Goal
Fringe = {J,K,C}
81
Depth-First Search
M adalah
Node Goal
Fringe = {K,C}
82
Depth-First Search
M adalah
Node Goal
Fringe = {C}
83
Depth-First Search
M adalah
Node Goal
Fringe = {F,G}
84
Depth-First Search
M adalah
Node Goal
Fringe = {L,M,G}
85
Depth-First Search
M adalah
Node Goal
Fringe = {M,G}
86
Properties of Depth-First Search
Complete? No: fails in infinite-depth spaces,
spaces with loops.
Modify to avoid repeated states along path
complete in finite spaces.
Time? O(bm): terrible if m is much larger than
d but if solutions are dense, may be much
faster than breadth-first.
Space? O(bm), i.e., linear space!
Optimal? No
87
Depth-Limited Search
= depth-first search with depth limit l,
i.e., nodes at depth l have no successors
Recursive implementation:
88
Depth-First Search of the
8-puzzle with a depth bound of 5
89
Iterative Deepening Search
90
Iterative Deepening Search l =0
91
Iterative Deepening Search l =1
92
Iterative Deepening Search l =2
93
Iterative Deepening Search l =3
94
Number of nodes generated in a depth-limited search to depth d
with branching factor b:
NDLS = b0 + b1 + b2 + … + bd-2 + bd-1 + bd
Number of nodes generated in an iterative deepening search to
depth d with branching factor b:
NIDS = (d+1)b0 + d b^1 + (d-1)b^2 + … + 3bd-2 +2bd-1 + 1bd
For b = 10, d = 5,
NDLS = 1 + 10 + 100 + 1,000 + 10,000 + 100,000 = 111,111
NIDS = 6 + 50 + 400 + 3,000 + 20,000 + 100,000 = 123,456
Overhead = (123,456 - 111,111)/111,111 = 11%
95
Properties of Iterative Deepening
Search
Complete? Yes
Time? (d+1)b0 + d b1 + (d-1)b2 + … + bd = O(bd)
Space? O(bd)
Optimal? Yes, if step cost = 1
96
Summary of Algorithms
97
Repeated States
Failure to detect repeated states can turn a
linear problem into an exponential one !
98
Repeated States
Do not return to the state you just came
from.
Do not create paths with cycles in them.
Do not generate any state that was ever
generated before.
99
Example Problems that could be
represented by Graphs
100
Graph Search as Tree Search
101
Breadth First Search
102
Breadth First Search
103
Breadth First Search
104
Breadth First Search
105
Breadth First Search
106
Breadth First Search
107
Breadth First Search
108
Breadth First Search
109
Breadth First Search
110
Depth First Search
111
Depth First Search
112
Depth First Search
113
Depth First Search
114
Depth First Search
115
Depth First Search
116
Uniform Cost Search
117
Uniform Cost Search
118
Uniform Cost Search
119
Uniform Cost Search
120
Uniform Cost Search
121
Uniform Cost Search
122
Uniform Cost Search
123
Uniform Cost Search
124
Why not stop on the first goal
125
Exercise
4
3 A
5
S
4
D 2E
4
B
TREE (*)
BFS (*)
DFS (*)
C
5
G
4
F
3
Implicit network of towns
126
Exercise (TREE)
3
S
Problem space
A
4
C
B
5
2
D
E
B
4
5
D
4
4
Associated
loop-free
search tree
A
2
4
5
B
3
G
4
C
3
4
D
4
F
G
F
S
3
5
5
E
C
4
5
A
2
E
4
F
3
G
4
C
4
B
5
E
F
D
4
A
2
B
5
4
C
E
4
F
G
3
4
G3
127
BFS
DFS
S
S
D
A
B
C
E
D
D
A
E
B
F B F
G C G
C
A
E
B
E
F
B
F
A C G
C
E
D
F
G
G
128
© Copyright 2026 Paperzz