A* search is an informed search algorithm that finds the shortest path between a starting node and a goal node. It uses a heuristic function to estimate the distance to the goal for each node, guiding the search towards the most promising nodes first. A* is optimal if the heuristic is admissible (never overestimates the actual cost to reach the goal). It is also complete and optimally efficient. The algorithm maintains two lists, an open list of nodes to explore and a closed list of explored nodes. It iteratively removes the node with the lowest estimated total cost from the open list and expands it until the goal is found.
6. A student’s mental model of Kingston...
Domino's Pizza
350
Grizzly Grill 500 TD
50
Pizza Pizza
60
140 A&P 700
100
Hospital
Starbucks
900
400 750
1000
Goodwin Hall
100
400
Gordon Hall
Biosciences
6
7. Describe the Problem as a State Space
Gordon Hall
Goodwin Domino's
Hall Pizza
Gordon Hall Biociences Hospital Starbucks Gordon Hall Grizzly Grill
Goodwin Goodwin Goodwin
Hospital TD Canada A&P Pizza PIzza TD Canada
Hall Hall Hall
Starbucks Grizzly Grill Starbucks Grizzly Grill
7
8. Uninformed Search
Breadth-First Search will find the path through
the search tree to the shallowest solution.
Gordon Hall
Domino's Pizza Goodwin Hall
Grizzly Grill Biociences Starbucks Hospital
TD Pizza TD
Hospital A&P
Canada PIzza Canada
1,75 km 1,55 km
TD Grizzly Grizzly
Canada Grill Grill
8
9. Breadth-First Search will need to keep
O(bd+1 ) nodes in memory!
b is the branching factor
d is the depth of the solution
b=10
Depth Nodes Time Memory
2 1100 1/10 s 1 MB
4 111,100 11 s 106 MB
6 10^7 19 m 10 GB
8 10^9 31 h 1 TB
10 10^11 129 d 101 TB
12 10^13 35 y 10 PB
14 10^15 3,523 y 1 EB
9
10. Uninformed Search
Depth-First Search will always expand
the deepest unexplored node.
Gordon Hall
Domino's Pizza Goodwin Hall
Grizzly Grill Biociences Starbucks Hospital
TD Pizza TD
Hospital A&P
Canada PIzza Canada
TD Grizzly Grizzly
Canada Grill Grill
TD TD
Canada Canada
10
11. Uninformed Search
Depth-First Breadth-First
Search Search
Space complexity O(b · m) O(bd+1 )
Time complexity O(bm ) O(bd+1 )
Complete? No Yes
11
12. Can you compute
a solution?
Yes.
Can you compute
a best solution?
Yes. With enough
time and memory
Can you compute
a best solution
more efficiently?
This rest of this
presentation.
12
14. For each node, measure the distance from that node
to the goal “as the bird flies”.
Domino's Pizza
350
Grizzly Grill 500 TD
50
Pizza Pizza
60
140 A&P 700
100
Starbucks Hospital
900
400 750
1000
Goodwin Hall
100
400
Gordon Hall
Biosciences
14
15. Greedy Best-First
Search
Domino's Pizza
350
Grizzly Grill 500 TD
50
Always to expand the node Pizza Pizza
140
60
A&P 700
that is closest to the goal. 100
Starbucks Hospital
900
400 750
1000
Goodwin Hall
100
400
Gordon Hall
Biosciences
15
16. Greedy Best-First
Search
Gordon Hall
900
Goodwin Domino's
Hall Pizza
780 1000
Gordon Hall Biociences Hospital Starbucks Gordon Hall Grizzly Grill
900 820 300
Goodwin Goodwin Goodwin
Hospital TD Canada A&P Pizza PIzza TD Canada
Hall Hall Hall
780 0
Starbucks Grizzly Grill Starbucks Grizzly Grill
16
17. Domino's Pizza
350
Grizzly Grill 500 TD
50
Pizza Pizza
60
140 A&P 700
100
Starbucks Hospital
900
400 750
1000
100
Goodwin Hall 1,55km!
Not the best solution
400
Gordon Hall
Biosciences
but much faster with
less memory consumption!
17
18. Problems
• follows single path all the way to goal
• will back up when hitting dead end
• not better than Depth-First Search
18
19. A* Search
Define an evaluation function as:
f(n) = g(n) + h(n)
g(n) = cost(start, n)
h(n) = distance(n, end)
Thus f(n) describes the estimated cost
of the cheapest solution through n.
Expand the node with the smallest f(n)
19
20. Input:
1 OPEN = {n0}
2 CLOSED = {} G, n0, t
3 while (OPEN is not empty)
4 n = pop(OPEN)
5
6
if (n = t)
terminate
Output:
7 else Best path from n0 to t
8 M = Expand(n)
9 for each m in M do
10 if (exists m’ in OPEN with m’ = m)
11 if (f(m’) smaller or equal f(n) + f(m))
12 continue
13 else if (exists m’ in CLOSED with m’ = m)
14 if (f(m’) smaller or equal f(n) + f(m))
15 continue
16 else
17 OPEN = OPEN {m}
18 CLOSED = CLOSED {m}
19 parent (m) = n and OPEN = OPEN + {m}
20 end for
21 CLOSED = CLOSED + {n}
22 end if
23 end while
20
22. Properties of A*
A* is optimal if h(n) is consistent
h(n)is consistent, if it never over-estimates the true cost
and it fullfils the triangle inequality.
f(n) = g(n) + h(n)
with g(n) is the true cost from start to n.
Hence f(n) never over-estimates
the true cost of a solution through n.
22
23. Properties of A*
Suppose an sub-optimal goal node n’ appears on the fringe.
Let the cost of the optimal solution be C*.
Since n’ is sub-optimal and h(n’) = 0
f(n’) = g(n’) + h(n’) = g(n’) + 0 = g(n’) > C* (a)
For a fringe node n on the optimal path
f(n) = g(n) + h(n) ≤C* (b)
Combining (a) and (b) shows f(n) ≤ C* < f(g’)
So n’ will not be expanded and A* will return an optimal
solution.
23
24. Properties of A*
A* is complete
The f-costs along any path are non-decreasing.
Since A* always expands the the fringe node with lowest f-cost,
we will eventually find a path where the f-cost equals the
cost of an optimal solution C*, if such a path exists.
24
25. Properties of A*
A* is optimally efficient
For any heuristic function h(n),
no other algorithm can expand fewer nodes with f(n) < C*
without running the risk of missing the optimal solution.
25
26. A* can be used for many other problems:
8-Puzzle Rubik’s Cube
26
27. Start State Goal State
average solution cost: 22 steps
average branching factor: 3
complete state space: 322 ≈ 3.1 x 1010
distinct state space: 181,440
27
28. Good heuristics to use with
A* and the 8-Puzzle:
h1 : number of misplaced tiles
h2: sum of manhattan distances from
current positions to goal positions
Depth Cost h1 Cost h2
4 13 12
22 18,094 1,219
28
29. There are much harder
Problems
15-Puzzle:
1013 distinct states
Rubik’s Cube:
8! x 37 x 12! x 210 ≈ 4.3 x 1019 states
branching factor: 13
8! ways to arrange corner cubes
seven indenpendent, eighth dependent on previous choices
12! ways to arrange edges
eleven can be flipped independently,
twelfth depending on previous choices
29
31. Divide into Subproblems!
* 2 4 1 2
* * 3 4 *
* 3 1 * * *
Start Goal
Cost of C* for this subproblem forms lower
bound on the cost of complete problem
31
32. Calculate costs for every possible
subproblem instance
Store these in a database
Use the recorded values as h(n)
Can solve 15-puzzles in a few
milliseconds!
32