0% found this document useful (0 votes)
7 views46 pages

Ai Lect 03

This lecture covers Uniform Cost Search (UCS) and A* algorithms, highlighting their completeness and optimality in finding solutions with varying action costs. It explains the differences between UCS and other search methods like BFS and greedy search, emphasizing the importance of heuristics in improving search efficiency. The A* algorithm combines path cost and heuristic estimates to ensure the shortest path is found, provided the heuristic is admissible.

Uploaded by

Như Lê
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views46 pages

Ai Lect 03

This lecture covers Uniform Cost Search (UCS) and A* algorithms, highlighting their completeness and optimality in finding solutions with varying action costs. It explains the differences between UCS and other search methods like BFS and greedy search, emphasizing the importance of heuristics in improving search efficiency. The A* algorithm combines path cost and heuristic estimates to ensure the shortest path is found, provided the heuristic is admissible.

Uploaded by

Như Lê
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

Lecture 3: Uniform

Cost Search and A*

Mark Hasegawa-Johnson, January 2021


With some slides by Svetlana Lazebnik, 9/2016
Distributed under CC-BY 3.0
Title image: By Harrison Weir - From reuseableart.com,
Public Domain,
https://commons.wikimedia.org/w/index.php?curid=47
879234
Review: DFS and BFS
• Breadth-first search
• Frontier is a queue: expand the shallowest node
• Complete: always finds a solution, if one exists
• Optimal (finds the best solution) if all actions have the same cost.
• Time complexity: 𝑂{𝑏 ! }
• Space complexity: 𝑂{𝑏 ! }.
• Depth-first search – utility depends on relationship between m and d
• Frontier is a stack: expand the deepest node
• Not complete (might never find a solution, if m is infinite)
• Not optimal (returned solution is rarely the best one)
• Time complexity: 𝑂{𝑏 " }
• Space complexity: 𝑂{𝑏𝑚}.
Outline of today’s lecture
1. Uniform Cost Search (UCS): like BFS, but for actions that have different
costs
• Complete: always finds a solution, if one exists
• Optimal: finds the best solution
• Time complexity = # nodes that have cost < goal
• Space complexity = # nodes that have cost < goal
2. Heuristics, e.g., Manhattan distance
3. Greedy Best-first search
4. A*: Like UCS but adds an estimate of the remaining path length
• Complete: always finds a solution, if one exists
• Optimal: finds the best solution
• Time complexity = # nodes that have cost+heuristic < goal
• Space complexity = # nodes that have cost+heuristic < goal
An example for which BFS is not optimal: Romania
Arad

Timisoara Sibiu Zerind

Lugoj RV Fagaras Oradea

Mehadia C P Bucharest

BFS returns this path,


because it requires
only 3 actions.
Cost = 450 km
An example for which BFS is not optimal: Romania
Arad

Timisoara Sibiu Zerind

Lugoj RV Fagaras Oradea

Mehadia C P Bucharest

Bucharest

It would have been


better to find this
path!
Cost = 418 km
The solution: Uniform Cost Search
• Breadth-first search (BFS): Next node expanded is the one with the
fewest required actions
• Frontier is a queue
• First node into the queue is the first one expanded (FIFO)
• Uniform cost search (UCS): Next node expanded is the one with the
lowest accumulated path cost
• Frontier is a priority queue
• Lowest-cost node is the first one expanded
Example of UCS: Romania
Arad

Arad:0
Example of UCS: Romania
Arad

Timisoara Sibiu Zerind

Zerind:75, Timisoara:118, Sibiu:140


Example of UCS: Romania
Arad

Timisoara Sibiu Zerind

Oradea

Timisoara:118, Sibiu:140, Oradea:146


Example of UCS: Romania
Arad

Timisoara Sibiu Zerind

Lugoj Oradea

Sibiu:140, Oradea:146, Lugoj:239


Example of UCS: Romania
Arad

Timisoara Sibiu Zerind

Lugoj RV Fagaras Oradea

Oradea:146, Rimnicu Vilcea:220, Lugoj:239, Fagaras:239


Example of UCS: Romania
Arad

Timisoara Sibiu Zerind

Lugoj RV Fagaras Oradea

Rimnicu Vilcea:220, Lugoj:239, Fagaras:239


Example of UCS: Romania
Arad

Timisoara Sibiu Zerind

Lugoj RV Fagaras Oradea

C P

Lugoj:239, Fagaras:239, Pitesti:317, Craiova:366


Example of UCS: Romania
Arad

Timisoara Sibiu Zerind

Lugoj RV Fagaras Oradea

Mehadia C P

Fagaras:239, Mehadia:309, Pitesti:317, Craiova:366


Example of UCS: Romania
Arad

Timisoara Sibiu Zerind

Lugoj RV Fagaras Oradea

Mehadia C P Bucharest

Mehadia:309, Pitesti:317, Craiova:366, Bucharest:450


Example of UCS: Romania
Arad

Timisoara Sibiu Zerind

Lugoj RV Fagaras Oradea

Mehadia C P Bucharest

Dobreta

Pitesti:317, Craiova:366, Dobreta:384, Bucharest:450


Example of UCS: Romania
Arad

Timisoara Sibiu Zerind

Lugoj RV Fagaras Oradea

Mehadia C P

Dobreta Bucharest

Craiova:366, Dobreta:384, Bucharest:418


Example of UCS: Romania
Arad

Timisoara Sibiu Zerind

Lugoj RV Fagaras Oradea

Mehadia C P

Dobreta Bucharest

Dobreta:384, Bucharest:418
Example of UCS: Romania
Arad

Timisoara Sibiu Zerind

Lugoj RV Fagaras Oradea

Mehadia C P

Dobreta Bucharest

Bucharest:418
GOAL!!!! GOL!!!!!

Image by Rick Dikeman, GFDL 1996, https://commons.wikimedia.org/wiki/File:Football_iu_1996.jpg


Outline of today’s lecture
1. Uniform Cost Search (UCS): like BFS, but for actions that have different
costs
• Complete: always finds a solution, if one exists
• Optimal: finds the best solution
• Time complexity = # nodes that have cost < goal
• Space complexity = # nodes that have cost < goal
2. Heuristics, e.g., Manhattan distance
3. Greedy Best-first search
4. A*: Like UCS but adds an estimate of the remaining path length
• Complete: always finds a solution, if one exists
• Optimal: finds the best solution
• Time complexity = # nodes that have cost+heuristic < goal
• Space complexity = # nodes that have cost+heuristic < goal
Heuristics main idea
Instead of choosing the node with the smallest total cost so far (UCS),

why not choose the node that’s CLOSEST TO GOAL,


and expand that one first?
Why not choose the node CLOSEST TO GOAL?
• Answer: because we don’t know Start state
which node that is!!

• Example: which of these two is


closest to goal?

Goal state
We don’t know which state is closest to goal
• Finding the shortest path is the Start state
whole point of the search
• If we already knew which state
was closest to goal, there would
be no reason to do the search
• Figuring out which one is closest,
in general, is a complexity 𝑂 𝑏!
problem.
Goal state
Search heuristics: estimates of distance-to-goal
• Often, even if we don’t know the
distance to the goal, we can Start state
estimate it.
• This estimate is called a
heuristic.
• A heuristic is useful if:
1. Accurate: ℎ(𝑛) ≈ 𝑑(𝑛), where
ℎ(𝑛) is the heuristic estimate,
and 𝑑(𝑛) is the true distance to
the goal
2. Cheap: It can be computed in Goal state
complexity less than 𝑂 𝑏 !
Example heuristic: Manhattan distance
If there were no walls, this would
If there were no walls in the maze, be the path to goal: straight down,
Start state then straight right.
then the number of steps from
position (𝑥" , 𝑦" ) to the goal
position (𝑥# , 𝑦# ) would be 𝑦#

ℎ(𝑛) = |𝑥" − 𝑥# | + |𝑦" − 𝑦# |

𝑦$ Goal state

𝑥
𝑥# 𝑥$
Outline of today’s lecture
1. Uniform Cost Search (UCS): like BFS, but for actions that have different
costs
• Complete: always finds a solution, if one exists
• Optimal: finds the best solution
• Time complexity = # nodes that have cost < goal
• Space complexity = # nodes that have cost < goal
2. Heuristics, e.g., Manhattan distance
3. Greedy Best-first search
4. A*: Like UCS but adds an estimate of the remaining path length
• Complete: always finds a solution, if one exists
• Optimal: finds the best solution
• Time complexity = # nodes that have cost+heuristic < goal
• Space complexity = # nodes that have cost+heuristic < goal
Greedy Best-First Search
Instead of choosing the node with the smallest total cost so far (UCS),

why not choose the node whose


HEURISTIC ESTIMATE
indicates that it might be
CLOSEST TO GOAL?
Greedy Search Example
According to the Manhattan Start state
distance heuristic, these two
nodes are equally far from the
goal, so we have to choose one at
random.

Goal state
Greedy Search Example
Start state
If our random choice goes badly,
we might end up very far from the
goal.

= states in the explored set

= states on the frontier


Goal state
The problem with Greedy Search
Start state
Having gone down a bad path, it’s
very hard to recover, because
now, the frontier node closest to
goal (according to the Manhattan
distance heuristic) is this one:

Goal state
The problem with Greedy Search
Start state
That’s not a useful path…

Goal state
The problem with Greedy Search
Start state
Neither is that one…

Goal state
What went wrong?
Outline of today’s lecture
1. Uniform Cost Search (UCS): like BFS, but for actions that have different
costs
• Complete: always finds a solution, if one exists
• Optimal: finds the best solution
• Time complexity = # nodes that have cost < goal
• Space complexity = # nodes that have cost < goal
2. Heuristics, e.g., Manhattan distance
3. Greedy Best-first search
4. A*: Like UCS but adds an estimate of the remaining path length
• Complete: always finds a solution, if one exists
• Optimal: finds the best solution
• Time complexity = # nodes that have cost+heuristic < goal
• Space complexity = # nodes that have cost+heuristic < goal
The problem with Greedy Search
Among nodes on the frontier, this Start state
one seems closest to goal (smallest
ℎ(𝑛), where ℎ(𝑛) ≤ 𝑑(𝑛)).

But it’s also farthest from the start.


Let’s say 𝑔(𝑛) = total path cost so far.

So the total distance from start to


goal, going through node 𝑛, is
Goal state

𝑐(𝑛) = 𝑔 𝑛 + 𝑑 𝑛 ≥ 𝑔 𝑛 + ℎ(𝑛)
The problem with Greedy Search
Of these three nodes, this one has 15
the smallest 𝑔 𝑛 + ℎ 𝑛

(𝑔(𝑛) + ℎ(𝑛) = 4 + 28 = 32)

So if we want to find the lowest- 13


cost path, then it would be better
to try that node, instead of this
one, which has 2

𝑔 𝑛 + ℎ 𝑛 = 21 + 14 = 35 12
A* notation
• 𝑐(𝑛) = cost of the total path Start state
(START,…,n,…,GOAL).
• 𝑑(𝑛) = distance of the remaining
partial path (n,…,GOAL).
• 𝑔(𝑛) = gone-already on the path
so far, (START,…,n).

• ℎ(𝑛) = heuristic, ℎ(𝑛) ≤ 𝑑(𝑛).


Goal state

𝑐(𝑛) = 𝑔 𝑛 + 𝑑 𝑛 ≥ 𝑔 𝑛 + ℎ(𝑛)
A* Search
• Idea: avoid expanding paths that are already expensive
• The evaluation function f(n) is the estimated total cost of the
path through node n to the goal:

f(n) = g(n) + h(n)


g(n): cost so far to reach n (path cost)
h(n): estimated cost from n to goal (heuristic)

• This is called A* search if and only if the heuristic, h(n), is


admissible. The word “admissible” just means that ℎ(𝑛) ≤
𝑑(𝑛), and therefore, 𝑓(𝑛) ≤ 𝑐(𝑛).
𝑐 𝑚
m
Admissible heuristic S G
n
𝑔 𝑛 ≥ℎ 𝑛
• Suppose we’ve found one path to 𝐺; the path goes through node 𝑚. Since
we’ve calculated the whole path, we know its total path cost to be 𝑐 𝑚 .
• For every other node, 𝑛, we don’t know 𝑐(𝑛), but we know 𝑓(𝑛) = 𝑔(𝑛) +
ℎ(𝑛), and we know that
𝑐 𝑛 ≥𝑓 𝑛
• Therefore we know that
𝐼𝐹 𝑓 𝑛 ≥ 𝑐 𝑚
𝑇𝐻𝐸𝑁 𝑐(𝑛) ≥ 𝑐(𝑚)
• So if 𝑓 𝑛 ≥ 𝑐 𝑚 for every node n that’s still in the frontier, then we know
that m is the best path.
𝑐 𝑚
m
A* Search S G
n
𝑔 𝑛 ≥ℎ 𝑛
Definition: A* SEARCH
• If ℎ 𝑛 is admissible (𝑑(𝑛) ≥ ℎ 𝑛 ), and
• if the frontier is a priority queue sorted according to 𝑔 𝑛 + ℎ(𝑛),
then
• the FIRST path to goal uncovered by the tree search, path 𝑚, is
guaranteed to be the SHORTEST path to goal
(ℎ 𝑛 + 𝑔 𝑛 ≥ 𝑐(𝑚) for every node 𝑛 that is not on path 𝑚)
Example of A*: Romania Suppose we don’t know the distance
from Sibiu to Bucharest on highways,
but we DO know the distance “as the
crow flies.”
ℎ(𝑛) = Euclidean distance (as the
crow flies)

• Sibiu: h(n) = 260km


• Timisoara: h(n) = 410km
• Zerind: h(n) = 422km
Romania using UCS
Arad

Timisoara Sibiu Zerind

Pick this one first?

Zerind:75, Timisoara:118, Sibiu:140


Romania using A*
Arad

Timisoara Sibiu Zerind

No, pick this one first!!!

Sibiu:140+260=400, Zerind:75+422=495, Timisoara:118+410=528


BFS vs. A* Search Example
The heuristic h(n)=Euclidean distance favors nodes on the main diagonal.
Those nodes all have the same g(n)+h(n), so A* evaluates them first.

CC-BY 3.0 by Subh83, 2011, https://en.wikipedia.org/wiki/File:Astar_progress_animation.gif


Outline of today’s lecture
1. Uniform Cost Search (UCS): like BFS, but for actions that have different
costs
• Complete: always finds a solution, if one exists
• Optimal: finds the best solution
• Time complexity = # nodes that have cost < goal
• Space complexity = # nodes that have cost < goal
2. Heuristics, e.g., Manhattan distance
3. Greedy Best-first search
4. A*: Like UCS but adds a lower bound of the remaining path length
• Complete: always finds a solution, if one exists
• Optimal: finds the best solution
• Time complexity = # nodes that have cost+heuristic < goal
• Space complexity = # nodes that have cost+heuristic < goal

You might also like