The A* Algorithm
Héctor Muñoz-Avila
The Search Problem
Starting from a node n find the shortest path to a goal node g
10
15
20
?
20
15
5
18
25
33
Shortest Path
Consider the following weighted undirected graph:
5
2
7
10
18
22
66
20
10
20
16
4
6
8
24
20
6
9
We want:
A path 5 🡪 v1 🡪 v2 🡪 … 🡪 20
Such that g(20) = cost(5🡪v1) + cost(v1🡪 v2) + … + cost ( 🡪20) is minimum
Djikstra Algorithm
Greedy algorithm: from the candidate nodes select the one that has a path with minimum cost from the starting node
10
15
20
?
20
15
5
18
25
33
Djikstra Algorithm
T
Given a Graph G = (V,E) and T a subset of V, the fringe of T, is defined as:
Fringe(T) = { (w,x) in E : w ∈ T and x ∈V − T}
Djikstra’s algorithm pick the edge v in Fringe(T) that has minimum distance to the starting node
g(v) is minimum
Example
5
2
7
10
18
22
66
20
10
20
16
4
6
8
24
20
6
9
Properties
Dijkstra’s is a greedy algorithm
Why Dijkstra’s Algorithm works?
T
V T
The path from u to every node in T is the minimum path
w
u
r
s
v
6
20
20
x
7
15
Example
What does Djikstra’s algorithm will do? (minimizing g(n))
Problem: Visit too many nodes, some clearly out of the question
Complexity
Actually it is bad for very large graphs!
Branching factor: b
….
….
# nodes = b(# levels)
Another Example: think of the search space in chess
Better Solution: Make a ‘hunch”!
h(n) = “estimate of the cost of the cheapest path from the starting node to the goal node”
Lets Try A Heuristic
Heuristic: minimize h(n) = “Euclidean distance to destination”
Problem: not optimal (through Rimmici Viicea and Pitesti is shorter)
The A* Search
“actual cost”
“estimated cost”
A*
10
15
20
20
15
5
18
25
33
n
g(n)
h(n)
Example
A*: minimize f(n) = g(n) + h(n)
Properties of A*
h(n) ≤ c(n,n’) + h(n’)
n
n’
d
h(n)
c(n,n’)
h(n’)
Admissible Heuristics
In the road map domain,
h(n) = “Euclidean distance to destination”
is admissible as normally cities are not connected by roads that make straight lines
How to Create Admissible Heuristics
http://www.permadi.com/java/puzzle8/
Example: Admissible Heuristics in 8-Puzzle Game
Using A* in Planning
A
C
B
A
B
C
A
C
B
C
B
A
B
A
C
B
A
C
B
C
A
C
A
B
A
C
B
B
C
A
A
B
C
A
B
C
A
B
C
h(n) = “# of goals remaining to be satisfied” g(n) = “# of steps so far”
A* in Games