COSC 461/561:
Introduction to Artificial
Intelligence
Lecture 3: Uninformed Search
Announcements
Today’s Agenda
● Solving Problems by Searching (Chapter 3)
○ Uninformed Search
■ State Space Graph
■ Search Tree
○ Search Strategies
■ BFS
■ DFS
● Coding Exercise: Maze
Search
Finding a solution to a problem.
Involves an agent that is given an initial state and a goal state, and it returns a
solution of how to get from the former to the latter.
Successor function:
Takes a state and returns all states that can
be reached from it by one legal action.
Search
In a search process, data is stored in a node, a data structure that contains the
following data:
●
●
●
●
A state
Its parent node, through which the current node was generated
The action that was applied to the state of the parent to get to the current
node
The path cost from the initial state to this node
Frontier: the mechanism that manages the nodes. The frontier starts by
containing an initial state and an empty set of explored items.
• state space
Ypsilanti to Traverse City
• cities
3.5
• successor function
4
• roads connect adjacent cities with
cost = travel time
3.2
3
• start state
Traverse City
0.2
• Ypsilanti
0.8
2.2
• goal test
1.5
1
Ypsilanti
• state ==Traverse City
• state space
Touring Michigan
• cities (visited boolean array)
3.5
• successor function
4
• roads connect adjacent cities with
cost = travel time
3.2
3
• start state
Traverse City
0.2
0.8
• Ypsilanti
2.2
• goal test
1.5
1
Ypsilanti
• visited is all true and
city == Ypsilanti
State Space Graphs
Represent transitions between states as a
graph
● Nodes are states
● Arcs represent successors (action
results)
● Goals are a set of nodes
● Each state is represented once
Search Trees
Represent sequences of actions on paths to root
● Nodes are states
● Children represent successors (action
results)
● States can be repeated
Graphs vs. Trees
State Space Graph
G
a
c
b
e
d
S
f
h
p
q
r
Search Tree
Each node in a
search tree
represents an entire
path from the root.
Trees get very large,
so we construct on
demand and as little
as possible.
S
e
d
b
c
a
a
e
h
p
q
q
c
a
p
h
r
p
f
q
G
q
r
q
f
c
a
G
Graphs vs. Trees
The tree is infinitely big because the
graph has a cycle.
S
a
a
b
G
S
a
G
b
G a
G
b
G
b
…
…
Tree Search
Tree search is a search algorithm that maintains a frontier (or fringe) of nodes to
consider exploring.
• Nodes on the frontier are expanded to find new nodes
unexplored nodes
expanded
nodes
fringe nodes
General Tree Search
function TREE-SEARCH(problem, strategy) returns solution or failure
initialize tree using initial state of problem
loop do
if no candidates for expansion then return failure
choose a leaf node for expansion according to strategy
if node contains goal state then return corresponding solution
else expand node and add resulting nodes to search tree
end
Random Exploration
S
G
a
c
b
e
d
f
S
q
b
c
a
a
e
h
r
q
q
c
r
r
p q
f
f
q
G
a
Expanded
p
h
e
p
h
p
d
Frontier
S
c
a
q
G
Random Exploration
S
G
a
d
c
b
e
d
f
S
q
c
a
a
h
r
q
q
c
r
r
p q
f
f
q
G
a
Expanded
Frontier
S
Expand
S
p
h
e
p
h
p
b
e
d, e, p
c
a
q
G
Random Exploration
S
G
a
d
c
b
e
d
f
S
q
c
a
a
h
r
q
q
c
r
r
p q
f
f
q
G
a
Expanded
Frontier
S, e
Expand
e
p
h
e
p
h
p
b
e
d, p, h, r
c
a
q
G
Random Exploration
S
G
a
d
c
b
e
d
f
S
q
c
a
a
h
r
q
q
c
r
r
p q
f
f
q
G
a
Frontier
Expanded
S, e, p
Expand
p
p
h
e
p
h
p
b
e
d, h, r, q
c
a
q
G
Random Exploration
S
G
a
d
c
b
e
d
f
S
q
c
a
a
h
r
q
q
c
r
r
p q
f
f
q
G
a
Expanded
Frontier
S, e, p, r
Expand
r
p
h
e
p
h
p
b
e
d, h, q, f
c
a
q
G
Random Exploration
S
G
a
d
c
b
e
d
f
S
q
c
a
a
h
r
q
q
c
p
h
r
r
p q
f
f
q
e
p
h
p
b
e
G
c
a
a
Expanded
S, e, p, r, f
Frontier
Expand
f
d, h, q, c, G
q
G
Random Exploration
S
G
a
d
c
b
e
d
f
S
q
c
a
a
h
r
q
q
c
r
r
p q
f
f
q
G
a
Expanded
S, e, p, r, f
Frontier
“Expand”
G
p
h
e
p
h
p
b
e
d, h, q, c
c
a
q
G
G
a
Depth-First Search
c
b
e
d
● Strategy
f
S
h
p
○
Expand deepest (most recently
added) node first
S
● Implementation
○
○
Frontier is a LIFO stack
Usually break ties alphabetically
r
q
d
b
c
a
a
e
h
r
r
p q
f
f
q
e
h
p
q
q
c
a
p
G
c
a
q
G
Properties of DFS
● Complete?
○
m could be infinite, so only if no cycles
● Optimal?
○
○
…
1 node
b nodes
b2 nodes
No.
● Time Complexity
b
m tiers
O(bm) if m is finite
● Space Complexity
○
O(bm)
bm nodes
G
a
Breadth-First Search
c
b
e
d
● Strategy
f
S
h
p
○
Expand shallowest (oldest) node
first
S
● Implementation
○
○
Frontier is a FIFO queue
Usually break ties alphabetically
r
q
d
b
c
a
a
e
h
r
r
p q
f
f
q
e
h
p
q
q
c
a
p
G
c
a
q
G
Properties of BFS
s = depth of shallowest solution
• Complete?
• Yes (s must be finite for a solution to
exist)
…
• Optimal?
1 node
b nodes
b2 nodes
• Only if costs are all 1
• Time Complexity
b
s tiers
• O(bs)
• Space Complexity
• O(bs) (roughly all nodes in tier s)
bm nodes
Handout Exercise
For (2) and (3):
Do not add a state if it is already:
● In the frontier
● In the explored set
Test whether a node is a goal when it is removed from the
frontier.
In your written frontier, place the next node to be removed
on the left.
Actions Have Costs
3.5
How to find the lowest cost
path?
4
3.2
3
Traverse City
0.2
0.8
2.2
1.5
1
Ypsilanti
Uniform Cost Search
2
1
a
b
1
G
c
8
3
2
2
3
• Strategy
d
e
9
S
1
1
p
• Expand cheapest (least cost) node first
r
1
S
• Implementation
d
• Frontier is a priority queue
• Usually break ties alphabetically
1
h
q
15
f
2
8
e
b
c
a
a h
p
q
h
r
r
p q
f
f
q
e
q
c
a
p
G
c
a
q
G
General Graph Search
function GRAPH-SEARCH(problem, strategy) returns solution or failure
closed ← empty set
initialize fringe using initial state of problem
loop do
if fringe empty then return failure
node ← remove node from fringe
if node is goal state then return corresponding solution
if node not in closed then
expand node and add resulting nodes to fringe
add node to closed
end
Coding Exercise: Maze
Get maze.zip on Canvas under Week 2.
Unzip and open in your code editor.
Coding Exercise: Maze
After you finish the TODOs and tried sample files:
Create maze files that would cause DFS to find a longer solution.
Submit on Canvas under assignment “DFS Maze Trap”.