# A* Path Finding Algorithm - PowerPoint PPT Presentation

View by Category
Title:

## A* Path Finding Algorithm

Description:

### An explanation of the A* path finding algorithm – PowerPoint PPT presentation

Number of Views:2458
Slides: 46
Provided by: dnatapov
Tags:
Transcript and Presenter's Notes

Title: A* Path Finding Algorithm

1
A Path Finding Algorithm
• Presented by Daniel Natapov

2
Problem Definition
• Find the shortest (weighted) path from a start
node to a goal node in a graph (or grid).
• What if we are informed with heuristics? Can we

3
Application
• Games NPC movement. Needs to be smart and fast.
• Games use grids to describe the environment.
These slides do too.
• Many other applications
• Network routing
• Image processing
• A.I. Path finding
• ...

4
Grid Graph
• Grid allows movement between adjacent cells in 4
or 8 possible directions.
• Each direction may have a different cost.

5
Example Get from S to T

S

T

6
Example Edge Weights
• In a game, edge weights depend on various
factors, ie travel on road vs. grass.
• For simplicity lets say all horizontal and
vertical costs are the same.
• Also assume no diagonal paths.

7
WWDD? What Would Dijkstras Do?

S

T

S
Found it! (finally)
8
WWDD?
• Dijkstras algorithm guarantees shortest path.
• But searches a lot of unneeded area.
• We know where the destination node is, (just not
how to get there).
• We can try to direct the search with greedy
Best-First-Search.

9
Best-First-Search
• Similar to Dijkstras, but is informed.
• Has some estimate of how far from the goal each
• This estimate is a heuristic.
• It prioritizes vertices which it believes to be
closets to the goal, as opposed to vertices
closest to the start.

10
Best-First-Search example

S

T

S
11
How to break it Obstacles!

S

T

S
Found it! (couldve taken a better route)
12
Best-First-Search
• Best-First-Search works faster than Dijkstras.
• But does not guarantee an optimal-path.
• We want some combination of Dijkstras and
Best-First-Search.
• Enter A!

13
A algorithm
• Prioritizes its search based on
• The distance traveled (Dijkstras)
• The distance remaining (Best-First-Search)
• g(n) Distance traveled from the start to a
cell.
• h(n) Estimated distance from a cell to the
target.
• Value of a cell is f(n) g(n) h(n).
• The algorithm prioritizes cells whose f(n) is
lowest.

14
Whats all this talk about estimates?
• An estimate of the distance between a cell and a
target is a heuristic.
• May be able to estimate distance between two
cells.
• Choosing a good heuristic is important, and can
be difficult.
• In our simplified case it is easy the Manhattan
Distance
• h(n) cell.x goal.x cell.y goal.y

15
Manhattan Distance
• Good for our case. Actual distance can never be
less (more on this later).
• Lets go through a complete example.

16
A example

S T

17
A example

g 1 h 6 f 7
g 1 h 6 f 7 S g 1 h 4 f 5 T
g 1 h 6 f 7

18
A example

g 1 h 6 f 7
g 1 h 6 f 7 S g 1 h 4 f 5 T
g 1 h 6 f 7

19
A example

g 1 h 6 f 7 g 2 h 5 f 7
g 1 h 6 f 7 S g 1 h 4 f 5 T
g 1 h 6 f 7 g 2 h 5 f 7

g 2 h 3 f 5
20
A example

g 1 h 6 f 7 g 2 h 5 f 7
g 1 h 6 f 7 S g 1 h 4 f 5 T
g 1 h 6 f 7 g 2 h 5 f 7

21
A example

g 1 h 6 f 7 g 2 h 5 f 7
g 1 h 6 f 7 S g 1 h 4 f 5 T
g 1 h 6 f 7 g 2 h 5 f 7
g 3 h 6 f 9
g 3 h 6 f 9
22
A example

g 1 h 6 f 7 g 2 h 5 f 7
g 1 h 6 f 7 S g 1 h 4 f 5 T
g 1 h 6 f 7 g 2 h 5 f 7
g 3 h 6 f 9
23
A example

g 1 h 6 f 7 g 2 h 5 f 7
g 1 h 6 f 7 S g 1 h 4 f 5 T
g 2 h 7 f 9 g 1 h 6 f 7 g 2 h 5 f 7
g 2 h 7 f 9 g 3 h 6 f 9
24
A example
g 3 h 6 f 9
g 1 h 6 f 7 g 2 h 5 f 7
g 1 h 6 f 7 S g 1 h 4 f 5 T
g 2 h 7 f 9 g 1 h 6 f 7 g 2 h 5 f 7
g 2 h 7 f 9 g 3 h 6 f 9
g2 h7 f9
g3 h8 f11
g2 h7 f9
g3 h8 f11
g2 h7 f9
g3 h8 f11
g3 h8 f11
25
A example
g 3 h 8 f 11 g 2 h 7 f 9 g 3 h 6 f 9
g 3 h 8 f 11 g 2 h 7 f 9 g 1 h 6 f 7 g 2 h 5 f 7
g 2 h 7 f 9 g 1 h 6 f 7 S g 1 h 4 f 5 T
g 3 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g 3 h 8 f 11 g 2 h 7 f 9 g 1 h 6 f 7 g 2 h 5 f 7 g 6 h 3 f 9 g 7 h 2 f 9 g 8 h 1 f 9
g 3 h 8 f 11 g 2 h 7 f 9 g 3 h 6 f 9 g 4 h 5 f 9 g 5 h 4 f 9 g 6 h 3 f 9
30
(No Transcript)
31
• Depending heuristic, A can be admissible.
• This guarantees an optimal solution, despite
using an estimate.
• For A to be admissible and guarantee an optimal
solution we need
• ?n, h(n) h(n)
• h(n) is the actual distance.
• If the heuristic overestimates the actual
distance, an optimal solution is not guaranteed.

32
• For admissibility we also need monotonicity.
• Satisfy triangle inequality h(n1) c(n1 ? n2)
h(n2)

n1
h(n1)
c(n1-gtn2)
goal
n2
h(n2)
33
Fiddling with the heuristic
• Use the heuristic to balance speed vs. accuracy.
• If h(n) 0, then f(n) g(n).
• In other words, A becomes Dijkstras.
• If h(n) gtgt g(n), g(n) can be ignored.
• f(n) h(n). A becomes Best-First-Search.
• In general
• The bigger g(n) is, the more it expands, which
makes it slower.
• The bigger h(n) is, the more direct the search
is, but better paths could be missed.

34
Fiddling with the heuristic 2
• If h(n) h(n), then A will find the optimal
solution, and not expand anything unnecessary.
• Straight to the target.
• Only possible with good heuristic and no
obstacles.
• Can fiddle with the heuristic and set it
depending on the need.
• Sometimes okay to get an approximate solution at
the cost of a speed-up.

35
Speed-Accuracy see-saw
g(n)
h(n)
Speed
Accuracy
36
Formal Definition
• preCond Input a grid/graph G with positive edge
weights, a source node s, a target node t.
• Also given a admissible heuristic for estimating
distances.
• postCond Finds a shortest weighted path from s
to t.
• Loop Invariant So far, the nodes have been
handled in order of f(n), where f(n) g(n)
h(n).

37
Formal Definition Contd
• Step Handle the found (not handled) node with
min f(n).
• Store the parent for each cell the cell through
which the shortest path from s came.
• Exit Stop when t has been found.
• Obtaining the post condition LI Exit Code gt
PostCond.
• Proving the path we traced back is shortest
• Prove there is a path of this length We have
one.
• Prove there is no shorter path ...

38
Proof that there is no better path
COMPLICATED SLIDE. PAY ATTENTION!
• We know our heuristic is admissible, h(n)
h(n).
• By LI, our path handles cells in order of the
minimum of f(n) g(n) h(n).
• All unhandled paths have a larger f(n) than ours.
• We found t. So h(n)0. f(n) becomes our actual
-g(n). In other words, our actual cost is lower
than the actualestimated of any other found
node.
• The estimated cost is always less than the actual
cost. Meaning our actual cost is less than any
other actual cost.

39
Confused?
f(n)
f(any) g(any) h(any)
g(any) actual(any)
Our actual cost
Any other found estimate
Any other actual
40
What it all means
• If heuristic is admissible, A returns the
shortest path.
• It will find it by (likely) expanding and
searching less cells than Dijkstras.
• But if the condition that h(n) h(n) is
violated, we can no longer ensure optimality.
• Should it always be optimal?

41
Running Time
• Well....
• Dijkstras O(E V log V )
• V number of vertices, E number of edges.
• Obviously it is possible for A to search every
edge as well, so we have no savings in the worst
case.
• Lets focus on the nodes instead
• Dijkstras O(V2)

42
Running time Dijkstras
L
S
T
Area of circle is O(L2)
43
Running time A
H
L
S
T
Area of half ellipse O(LH)
44
Running time A
T
S
H
L/n
In total O((L/n)Hn) O(LH)
45
All Done!
• Thank you. Questions?
• The algorithm was first described in 1968 by
Peter Hart, Nils Nilsson, and Bertram Raphael
• References Resources
• http//theory.stanford.edu/amitp/GameProgramming/
(Great source)
• http//www.policyalmanac.org/games/aStarTutorial.h
tm
• http//en.wikipedia.org/wiki/A_search_algorithm
• http//www.cse.yorku.ca/course_archive/2008-09/W/3
402/slides/Week3.pdf