Uninformed and Informed Search
Uninformed and Informed Search
Section A
UNINFORMED AND INFORMED SEARCH
LECTURE 1
Outline
Motivation
Technical Solution
Uninformed Search
Depth-First Search
Breadth-First Search
Informed Search
Best-First Search
Hill Climbing
A*
Illustration by a Larger Example
Extensions
Summary
Motivation
One of the major goals of AI is to help humans in solving complex tasks
How can I fill my container with pallets?
Which is the shortest way from Milan to Innsbruck?
Which is the fastest way from Milan to Innsbruck?
How can I optimize the load of my freight to maximize my revenue?
How can I solve my Sudoku game?
What is the sequence of actions I should apply to win a game?
Sometimes finding a solution is not enough, you want the optimal solution
according to some “cost” criteria
All the example presented above involve looking for a plan
A plan that can be defined as the set of operations to be performed of an
initial state, to reach a final state that is considered the goal state
Thus we need efficient techniques to search for paths, or sequences of
actions, that can enable us to reach the goal state, i.e. to find a plan
Such techniques are commonly called Search Methods
Examples of Problems: Towers of
Hanoi
3 pegs A, B, C
1
3 discs represented as natural 2
numbers (1, 2, 3) which 3
correspond to the size of the A B C
discs Operators:
The three discs can be Move disk to peg
arbitrarily distributed over the
three pegs, such that the
following constraint holds: Applying: Move 1 to C (1 → C)
to the initial state ((123)()())
di is on top of dj → di < dj
a new state is reached
Initial status: ((123)()())
((23)()(1))
Goal status: (()()(123))
Cycles may appear in the
solution!
Examples of Problems:
Blocksworld
E
D C B
E A B C A D
Initial State Goal State
connected disconnected
A graph is connected if
every pair of nodes is
connected by a path
tree
A connected graph with no
loop is called tree
A weighted graph, is a
graph for which a value is weighted
1
associated to each arc 1
4 5
6
2 1
2 1
Example: Towers of Hanoi*
These nodes
are equals
1
2
3
A B C
2 2
3 1 3 1
A B C A B C
… 2
3 1 2 3 2 1 3 1
A B C A B C A B C
…
1 1 1 1
3 2 3 2 3 2 3 2
A B C A B C A B C A B C
… … … … …
1 …
2 3
A B C
… …
* A partial tree search space representation
Example: Towers of Hanoi*
Informed methods
Use problem specific information to guide search
in promising directions
UNINFORMED SEARCH
Brute force approach to explore search
space
Uninformed Search
A class of general purpose algorithms that operates in a brute force way
The search space is explored without leveraging on any information on the
problem
Also called blind search, or naïve search
Since the methods are generic they are intrinsically inefficient
Prominent methods:
Depth-First Search
Breadth-First Search
Uniform-Cost Search
Depth-First Search
Depth-First Search (DFS) begins at the root node and exhaustively search each
branch to it maximum depth till a solution is found
The successor node is selected going in depth using from right to left (w.r.t. graph
representing the search space)
If greatest depth is reach with not solution, we backtrack till we find an unexplored
branch to follow
The algorithm can be implemented with a Last In First Out (LIFO) stack or recursion
Depth-First Search: Algorithm
List open, closed, successors={};
Node root_node, current_node;
insert-first(root_node,open)
while not-empty(open);
current_node= remove-first(open);
insert-first (current_node,closed);
if (goal(current_node)) return current_node;
else
successors=successorsOf(current_node);
for(x in successors)
if(not-in(x,closed)) insert-first(x,open);
endIf
endWhile
A B
2
3
S B F F A C D
5
4
6
open={S} closed ={}
A B
S B F F A C D
S A F D
d=1
Space Complexity
how many nodes can be in the
queue (worst-case)? d=2
at each depth l < d we have b-1 d=3
nodes
at depth m we have b nodes m=d=4
total = (d-1)*(b-1) + b = O(bm)
Breadth-First Search
Breadth-First Search (BFS) begins at the root
node and explore level-wise al the branches
BFS is complete
If there is a solution, BFS will found it
BFS is optimal
The solution found is guaranteed to be the shortest
path possible
while not-empty(open);
current_node=remove-first(open);
insert-last(current_node,closed);
if (goal(current_node)) return current_node;
else
successors=successorsOf(current_node);
for(x in successors)
if(not-in(x,closed)) insert-last(x,open);
endIf
endWhile
2
A B
3
4 5
S B F F A C D
A B
S B F F A C D
S A F D
Space Complexity
how many nodes can be in the queue (worst-case)?
assume (worst case) that there
1 is 1 goal leaf at the d=0
RHS
d=1
so BFS will store
2
all nodes 3
d=2
=1 +4 b + b2+ .........
5
+ bm
6 7
= O (bm) d=3
8 9 10 11 12 13 14 G 15 d=4
Further Uninformed Search
Strategies
Depth-limited search (DLS): Impose a cut-off (e.g. n
for searching a path of length n-1), expand nodes with
max. depth first until cut-off depth is reached (LIFO
strategy, since it is a variation of depth-first search).
Bidirectional search (BIDI): forward search from initial
state & backward search from goal state, stop when
the two searches meet. Average effort O(bd/2) if
testing whether the search fronts intersect has
constant effort
In AI, the problem graph is typically not known. If the
graph is known, to find all optimal paths in a graph
with labelled arcs, standard graph algorithms can be
used
INFORMED SEARCH
Using knowledge on the search space to
reduce search costs
Informed Search
Blind search methods take O(bm) in the worst case
A*
Hill Climbing
Cost and Cost Estimation
f(n)=g(n)+h(n)
g(n) the cost (so far) to reach the node n
h(n) estimated cost to get from the node to the
goal
f(n) estimated total cost of path through n to
goal
Informed Search: Best-First Search
Special case of breadth-first search
Uses h(n) = heuristic function as its evaluation function
Ignores cost so far to get to that node (g(n))
Expand the node that appears closest to goal
Special cases:
uniform cost search: f(n) = g(n) = path to n
A* search
31
Best-First Search: Algorithm
List open, closed, successors={};
Node root_node, current_node;
insert-last(root_node,open)
h=1 h=1
2
A B
h=2
3 h=2
h=2 h=2 h=2 h=2 h=2
S B F F A C D
In this case we estimate the cost as the distance from the root node (in term of nodes)
33
Best-First Search: Example
S
S A F D
34
A*
Derived from Best-First Search
Uses both g(n) and h(n)
A* is optimal
A* is complete
A* : Algorithm
List open, closed, successors={};
Node root_node, current_node, goal;
insert-back(root_node,open)
returns the list of direct
while not-empty(open); descendants of the
current_node=remove-front(open); current node in shortest
insert-back(current_node,closed); total estimation order
if (current_node==goal) return current_node;
else
successors=totalEstOrderedSuccessorsOf(current_node);
for(x in successors)
if(not-in(x,closed)) insert-back(x,open);
endIf
endWhile
In this case we estimate the cost as the distance from the root node (in term of nodes)
37
A* : Example
S
h=1, w=2, g=2 h=1, w=1, g=1
A B
h=2, h=2, w=7, g=9 h=2, w=4, g=5
w=2, h=2,
h=2 h=2, w=4, g=5
g=4 w=1
w=3
g=3 h=2,
S B F F A g=4 C w=1 D
g=2
S A F D
38
Hill Climbing
Special case of depth-first search
Uses h(n) = heuristic function as its evaluation
function
Ignores cost so far to get to that node (g(n))
Expand the node that appears closest to goal
current_node=root_node
while (current_node!=null)
if (goal(current_node)) return current_node;
else
successors=successorsOf(current_node);
nextEval = -∞; nextNode=null;
for(x in successors)
if(eval(x)> nextEval)
nexEval=eval(x);
nextNode=x;
current_node=nextNode,
endIf
endWhile
N.B.= this version is not using backtracking
40
Hill Climbing: Example
1 S
h=1 h=1
A B
h=2
2
h=2
h=3 h=2 h=2 h=2 h=2
S B F F A C D
In this case we estimate the cost as the distance from the root node (in term of nodes)
41
Hill Climbing: Example
S
h=1 h=1
A B
h=2
h=2
h=3 h=2 h=2 h=2 h=2
S B F F A C D
h=1
S A F D
42
Informed Search Algorithm
Comparison
Algorithm Time Space Optimal Complete Derivative
Best First O(bm) O(bm) No Yes BFS
Search
Hill Climbing O() O(b) No No
A* O(2N) O(bd) Yes Yes Best First
Search
b, branching factor
d, tree depth of the solution
m, maximum tree depth
43
ILLUSTRATION BY A LARGER
EXAMPLE
Route Search
Start point:
Milan
End point:
Innsbruck
Search space:
Cities
Nodes: Cities
Arcs: Roads
Let’s find a
possible route!
Graph Representation
We start from the
Feldkirck root node, and
Landeck Innsbruck (Goal) pick the leaves
Piacenza
Depth-First Search
Milan
Innsbruck Innsbruck
Innsbruck Innsbruck
N.B.: by building the tree, we are exploring the search space!
Breadth-First search
Milan
1 4
2
3
Innsbruck Innsbruck
Merano Merano
Innsbruck Innsbruck According to Google Maps:
358 km – 5 hours 18 mins
Landeck Landeck
Innsbruck Innsbruck
N.B.: by building the tree, we are exploring the search space!
Depth-First Search vs Breadth-
First search
Distance
DFS: 464 km
BFS: 358 km
Q1: Can we use an algorithm to optimize according to distance?
Time
DFS: 4 hours 37 mins
BFS: 5 hours 18 mins
Q2: Can we use an algorithm to optimize according to time?
Search space:
DFS: 5 expansions
BFS: 26 expansions
Not very relevant… depends a lot on how you pick the order of node expansion, never the
less BFS is usually more expensive
100
90
180 140 Merano
25
Bolzano
135
Chiavenna
55
40 Sondrio
Lugano 60 Trento
42
20
25 Lecco
Como 90
50 Bergamo 80
45
45
55
Milan (Root) 60
Brescia Verona
60 70
Piacenza
Best-First search
Milan
H=110 H=92
Brescia 11 H=130 Brescia 8 H=100 Chiavenna 10 Sondrio 7 Lugano 5 H=65 Lecco 6 H=70
H=190 H=220 H=160 H=190 H=150 H=227 H=105 H=245 H=130 H=142
H=250
Verona 16 Trento 18 Verona 15 Trento 17 Landeck 22 Lugano 14 Merano 19 Chi. 9 Feld. 21 Chi. 12 Sondrio 13
H=270 H=275 H=240 H=245
Trento Bolzano Trento 20 Bolzano 21 H=313
29
Bolzano Bolzano Innsbruck
Innsbruck Innsbruck
Merano Merano
Innsbruck Innsbruck According to Google Maps:
358 km – 5 hours 18 mins
Landeck
Landeck And this is really the shortest way!
Innsbruck Innsbruck
N.B.: by building the tree, we are exploring the search space!
EXTENSIONS
Variants to presented algorithms
Combine Depth First Search and Breadth First Search, by
performing Depth Limited Search with increased depths until a goal
is found
Enrich Hill Climbing with random restart to hinder the local
maximum and foothill problems
Stochastic Beam Search: select w nodes randomly; nodes with
higher values have a higher probability of selection
Genetic Algorithms: generate nodes like in stochastic beam search,
but from two parents rather than from one
SUMMARY
Summary
Uninformed Search
If the branching factor is small, BFS is the best
solution
If the tree is depth IDS is a good choice
Informed Search
Heuristic function selection determines the efficiency
of the algorithm
If actual cost is very expensive to be computed, then
Best First Search is a good solution
Hill climbing tends to stack in local optimal solutions