![Page 1: The A* Search Algorithm - Duke Universitydb.cs.duke.edu/courses/fall11/cps149s/notes/a_star.pdf · Introduction A* (pronounced ‘A-star’) is a search algorithm that finds the](https://reader031.vdocument.in/reader031/viewer/2022022007/5ad5b8197f8b9a48398de9fe/html5/thumbnails/1.jpg)
The A* Search Algorithm
Siyang Chen
![Page 2: The A* Search Algorithm - Duke Universitydb.cs.duke.edu/courses/fall11/cps149s/notes/a_star.pdf · Introduction A* (pronounced ‘A-star’) is a search algorithm that finds the](https://reader031.vdocument.in/reader031/viewer/2022022007/5ad5b8197f8b9a48398de9fe/html5/thumbnails/2.jpg)
Introduction
A* (pronounced ‘A-star’) is a search algorithm that finds theshortest path between some nodes S and T in a graph.
![Page 3: The A* Search Algorithm - Duke Universitydb.cs.duke.edu/courses/fall11/cps149s/notes/a_star.pdf · Introduction A* (pronounced ‘A-star’) is a search algorithm that finds the](https://reader031.vdocument.in/reader031/viewer/2022022007/5ad5b8197f8b9a48398de9fe/html5/thumbnails/3.jpg)
Heuristic Functions
I Suppose we want to get to node T , and we are currently atnode v . Informally, a heuristic function h(v) is a function that‘estimates’ how v is away from T .
I Example: Suppose I am driving from Durham to Raleigh. Aheuristic function would tell me approximately how muchlonger I have to drive.
![Page 4: The A* Search Algorithm - Duke Universitydb.cs.duke.edu/courses/fall11/cps149s/notes/a_star.pdf · Introduction A* (pronounced ‘A-star’) is a search algorithm that finds the](https://reader031.vdocument.in/reader031/viewer/2022022007/5ad5b8197f8b9a48398de9fe/html5/thumbnails/4.jpg)
Heuristic Functions
I Suppose we want to get to node T , and we are currently atnode v . Informally, a heuristic function h(v) is a function that‘estimates’ how v is away from T .
I Example: Suppose I am driving from Durham to Raleigh. Aheuristic function would tell me approximately how muchlonger I have to drive.
![Page 5: The A* Search Algorithm - Duke Universitydb.cs.duke.edu/courses/fall11/cps149s/notes/a_star.pdf · Introduction A* (pronounced ‘A-star’) is a search algorithm that finds the](https://reader031.vdocument.in/reader031/viewer/2022022007/5ad5b8197f8b9a48398de9fe/html5/thumbnails/5.jpg)
Admissible Heuristics
I A heuristic function is admissible if it never overestimates thedistance to the goal.
I Example: h(v) = 0 is an admissible heuristic.
I Less trivial example: If our nodes are points on the plane,then the straight-line distanceh(v) =
√(vx − Tx)2 + (vy − Ty )2 is an admissible heuristic.
![Page 6: The A* Search Algorithm - Duke Universitydb.cs.duke.edu/courses/fall11/cps149s/notes/a_star.pdf · Introduction A* (pronounced ‘A-star’) is a search algorithm that finds the](https://reader031.vdocument.in/reader031/viewer/2022022007/5ad5b8197f8b9a48398de9fe/html5/thumbnails/6.jpg)
Admissible Heuristics
I A heuristic function is admissible if it never overestimates thedistance to the goal.
I Example: h(v) = 0 is an admissible heuristic.
I Less trivial example: If our nodes are points on the plane,then the straight-line distanceh(v) =
√(vx − Tx)2 + (vy − Ty )2 is an admissible heuristic.
![Page 7: The A* Search Algorithm - Duke Universitydb.cs.duke.edu/courses/fall11/cps149s/notes/a_star.pdf · Introduction A* (pronounced ‘A-star’) is a search algorithm that finds the](https://reader031.vdocument.in/reader031/viewer/2022022007/5ad5b8197f8b9a48398de9fe/html5/thumbnails/7.jpg)
Admissible Heuristics
I A heuristic function is admissible if it never overestimates thedistance to the goal.
I Example: h(v) = 0 is an admissible heuristic.
I Less trivial example: If our nodes are points on the plane,then the straight-line distanceh(v) =
√(vx − Tx)2 + (vy − Ty )2 is an admissible heuristic.
![Page 8: The A* Search Algorithm - Duke Universitydb.cs.duke.edu/courses/fall11/cps149s/notes/a_star.pdf · Introduction A* (pronounced ‘A-star’) is a search algorithm that finds the](https://reader031.vdocument.in/reader031/viewer/2022022007/5ad5b8197f8b9a48398de9fe/html5/thumbnails/8.jpg)
Consistent Heuristics
I Suppose two nodes u and v are connected by an edge. Aheuristic function h is consistent or monotone if it satisfiesthe following:
h(u) ≤ e(u, v) + h(v)
where e(u, v) is the edge distance from u to v .
I Reasoning: If I want to reach T from u, then I can first gothrough v , then go to T from there. (This is very similar tothe triangle inequality.)
I Example: h(v) = 0 is a consistent heuristic.
I Less trivial example, again: If our nodes are points on theplane, h(v) =
√(vx − Tx)2 + (vy − Ty )2 is a consistent
heuristic.
I All consistent heuristics are admissible. (Proof left to thereader.)
![Page 9: The A* Search Algorithm - Duke Universitydb.cs.duke.edu/courses/fall11/cps149s/notes/a_star.pdf · Introduction A* (pronounced ‘A-star’) is a search algorithm that finds the](https://reader031.vdocument.in/reader031/viewer/2022022007/5ad5b8197f8b9a48398de9fe/html5/thumbnails/9.jpg)
Consistent Heuristics
I Suppose two nodes u and v are connected by an edge. Aheuristic function h is consistent or monotone if it satisfiesthe following:
h(u) ≤ e(u, v) + h(v)
where e(u, v) is the edge distance from u to v .
I Reasoning: If I want to reach T from u, then I can first gothrough v , then go to T from there. (This is very similar tothe triangle inequality.)
I Example: h(v) = 0 is a consistent heuristic.
I Less trivial example, again: If our nodes are points on theplane, h(v) =
√(vx − Tx)2 + (vy − Ty )2 is a consistent
heuristic.
I All consistent heuristics are admissible. (Proof left to thereader.)
![Page 10: The A* Search Algorithm - Duke Universitydb.cs.duke.edu/courses/fall11/cps149s/notes/a_star.pdf · Introduction A* (pronounced ‘A-star’) is a search algorithm that finds the](https://reader031.vdocument.in/reader031/viewer/2022022007/5ad5b8197f8b9a48398de9fe/html5/thumbnails/10.jpg)
Consistent Heuristics
I Suppose two nodes u and v are connected by an edge. Aheuristic function h is consistent or monotone if it satisfiesthe following:
h(u) ≤ e(u, v) + h(v)
where e(u, v) is the edge distance from u to v .
I Reasoning: If I want to reach T from u, then I can first gothrough v , then go to T from there. (This is very similar tothe triangle inequality.)
I Example: h(v) = 0 is a consistent heuristic.
I Less trivial example, again: If our nodes are points on theplane, h(v) =
√(vx − Tx)2 + (vy − Ty )2 is a consistent
heuristic.
I All consistent heuristics are admissible. (Proof left to thereader.)
![Page 11: The A* Search Algorithm - Duke Universitydb.cs.duke.edu/courses/fall11/cps149s/notes/a_star.pdf · Introduction A* (pronounced ‘A-star’) is a search algorithm that finds the](https://reader031.vdocument.in/reader031/viewer/2022022007/5ad5b8197f8b9a48398de9fe/html5/thumbnails/11.jpg)
Consistent Heuristics
I Suppose two nodes u and v are connected by an edge. Aheuristic function h is consistent or monotone if it satisfiesthe following:
h(u) ≤ e(u, v) + h(v)
where e(u, v) is the edge distance from u to v .
I Reasoning: If I want to reach T from u, then I can first gothrough v , then go to T from there. (This is very similar tothe triangle inequality.)
I Example: h(v) = 0 is a consistent heuristic.
I Less trivial example, again: If our nodes are points on theplane, h(v) =
√(vx − Tx)2 + (vy − Ty )2 is a consistent
heuristic.
I All consistent heuristics are admissible. (Proof left to thereader.)
![Page 12: The A* Search Algorithm - Duke Universitydb.cs.duke.edu/courses/fall11/cps149s/notes/a_star.pdf · Introduction A* (pronounced ‘A-star’) is a search algorithm that finds the](https://reader031.vdocument.in/reader031/viewer/2022022007/5ad5b8197f8b9a48398de9fe/html5/thumbnails/12.jpg)
Consistent Heuristics
I Suppose two nodes u and v are connected by an edge. Aheuristic function h is consistent or monotone if it satisfiesthe following:
h(u) ≤ e(u, v) + h(v)
where e(u, v) is the edge distance from u to v .
I Reasoning: If I want to reach T from u, then I can first gothrough v , then go to T from there. (This is very similar tothe triangle inequality.)
I Example: h(v) = 0 is a consistent heuristic.
I Less trivial example, again: If our nodes are points on theplane, h(v) =
√(vx − Tx)2 + (vy − Ty )2 is a consistent
heuristic.
I All consistent heuristics are admissible. (Proof left to thereader.)
![Page 13: The A* Search Algorithm - Duke Universitydb.cs.duke.edu/courses/fall11/cps149s/notes/a_star.pdf · Introduction A* (pronounced ‘A-star’) is a search algorithm that finds the](https://reader031.vdocument.in/reader031/viewer/2022022007/5ad5b8197f8b9a48398de9fe/html5/thumbnails/13.jpg)
Description of A*
We are now ready to define the A* algorithm. Suppose we aregiven the following inputs:
I A graph G = (V ,E ), with nonnegative edge distances e(u, v)
I A start node S and an end node T
I An admissible heuristic h
Let d(v) store the best path distance from S to v that we haveseen so far. Then we can think of d(v) + h(v) as the estimate ofthe distance from S to v , then from v to T . Let Q be a queue ofnodes, sorted by d(v) + h(v).
![Page 14: The A* Search Algorithm - Duke Universitydb.cs.duke.edu/courses/fall11/cps149s/notes/a_star.pdf · Introduction A* (pronounced ‘A-star’) is a search algorithm that finds the](https://reader031.vdocument.in/reader031/viewer/2022022007/5ad5b8197f8b9a48398de9fe/html5/thumbnails/14.jpg)
Pseudocode for A*
d(v)←
{∞ if v 6= S
0 if v = S
Q := the set of nodes in V , sorted by d(v) + h(v)while Q not empty do
v ← Q.pop()for all neighbours u of v do
if d(v) + e(v , u) ≤ d(u) thend(u)← d(v) + e(v , u)
end ifend for
end while
![Page 15: The A* Search Algorithm - Duke Universitydb.cs.duke.edu/courses/fall11/cps149s/notes/a_star.pdf · Introduction A* (pronounced ‘A-star’) is a search algorithm that finds the](https://reader031.vdocument.in/reader031/viewer/2022022007/5ad5b8197f8b9a48398de9fe/html5/thumbnails/15.jpg)
Comparison to Dijkstra’s Algorithm
Observation: A* is very similar to Dijkstra’s algorithm:
d(v)←
{∞ if v 6= S
0 if v = S
Q := the set of nodes in V , sorted by d(v)while Q not empty do
v ← Q.pop()for all neighbours u of v do
if d(v) + e(v , u) ≤ d(u) thend(u)← d(v) + e(v , u)
end ifend for
end while
In fact, Dijkstra’s algorithm is a special case of A*, when we seth(v) = 0 for all v .
![Page 16: The A* Search Algorithm - Duke Universitydb.cs.duke.edu/courses/fall11/cps149s/notes/a_star.pdf · Introduction A* (pronounced ‘A-star’) is a search algorithm that finds the](https://reader031.vdocument.in/reader031/viewer/2022022007/5ad5b8197f8b9a48398de9fe/html5/thumbnails/16.jpg)
Comparison to Dijkstra’s Algorithm
Observation: A* is very similar to Dijkstra’s algorithm:
d(v)←
{∞ if v 6= S
0 if v = S
Q := the set of nodes in V , sorted by d(v)while Q not empty do
v ← Q.pop()for all neighbours u of v do
if d(v) + e(v , u) ≤ d(u) thend(u)← d(v) + e(v , u)
end ifend for
end while
In fact, Dijkstra’s algorithm is a special case of A*, when we seth(v) = 0 for all v .
![Page 17: The A* Search Algorithm - Duke Universitydb.cs.duke.edu/courses/fall11/cps149s/notes/a_star.pdf · Introduction A* (pronounced ‘A-star’) is a search algorithm that finds the](https://reader031.vdocument.in/reader031/viewer/2022022007/5ad5b8197f8b9a48398de9fe/html5/thumbnails/17.jpg)
Performance
How good is A*?
I If we use an admissible heuristic, then A* returns the optimalpath distance. Furthermore, any other algorithm using thesame heuristic will expand at least as many nodes as A*.
I In practice, if we have a consistent heuristic, then A* can bemuch faster than Dijkstra’s algorithm.
I Example: Consider cities (points on the plane), with roads(edges) connecting them. Then the straight-line distance is aconsistent heuristic.
(Proofs may be found in most introductory textbooks on artificialintelligence.)
![Page 18: The A* Search Algorithm - Duke Universitydb.cs.duke.edu/courses/fall11/cps149s/notes/a_star.pdf · Introduction A* (pronounced ‘A-star’) is a search algorithm that finds the](https://reader031.vdocument.in/reader031/viewer/2022022007/5ad5b8197f8b9a48398de9fe/html5/thumbnails/18.jpg)
Performance
How good is A*?
I If we use an admissible heuristic, then A* returns the optimalpath distance. Furthermore, any other algorithm using thesame heuristic will expand at least as many nodes as A*.
I In practice, if we have a consistent heuristic, then A* can bemuch faster than Dijkstra’s algorithm.
I Example: Consider cities (points on the plane), with roads(edges) connecting them. Then the straight-line distance is aconsistent heuristic.
(Proofs may be found in most introductory textbooks on artificialintelligence.)
![Page 19: The A* Search Algorithm - Duke Universitydb.cs.duke.edu/courses/fall11/cps149s/notes/a_star.pdf · Introduction A* (pronounced ‘A-star’) is a search algorithm that finds the](https://reader031.vdocument.in/reader031/viewer/2022022007/5ad5b8197f8b9a48398de9fe/html5/thumbnails/19.jpg)
Performance
How good is A*?
I If we use an admissible heuristic, then A* returns the optimalpath distance. Furthermore, any other algorithm using thesame heuristic will expand at least as many nodes as A*.
I In practice, if we have a consistent heuristic, then A* can bemuch faster than Dijkstra’s algorithm.
I Example: Consider cities (points on the plane), with roads(edges) connecting them. Then the straight-line distance is aconsistent heuristic.
(Proofs may be found in most introductory textbooks on artificialintelligence.)
![Page 20: The A* Search Algorithm - Duke Universitydb.cs.duke.edu/courses/fall11/cps149s/notes/a_star.pdf · Introduction A* (pronounced ‘A-star’) is a search algorithm that finds the](https://reader031.vdocument.in/reader031/viewer/2022022007/5ad5b8197f8b9a48398de9fe/html5/thumbnails/20.jpg)
Performance
How good is A*?
I If we use an admissible heuristic, then A* returns the optimalpath distance. Furthermore, any other algorithm using thesame heuristic will expand at least as many nodes as A*.
I In practice, if we have a consistent heuristic, then A* can bemuch faster than Dijkstra’s algorithm.
I Example: Consider cities (points on the plane), with roads(edges) connecting them. Then the straight-line distance is aconsistent heuristic.
(Proofs may be found in most introductory textbooks on artificialintelligence.)
![Page 21: The A* Search Algorithm - Duke Universitydb.cs.duke.edu/courses/fall11/cps149s/notes/a_star.pdf · Introduction A* (pronounced ‘A-star’) is a search algorithm that finds the](https://reader031.vdocument.in/reader031/viewer/2022022007/5ad5b8197f8b9a48398de9fe/html5/thumbnails/21.jpg)
Performance
How good is A*?
I If we use an admissible heuristic, then A* returns the optimalpath distance. Furthermore, any other algorithm using thesame heuristic will expand at least as many nodes as A*.
I In practice, if we have a consistent heuristic, then A* can bemuch faster than Dijkstra’s algorithm.
I Example: Consider cities (points on the plane), with roads(edges) connecting them. Then the straight-line distance is aconsistent heuristic.
(Proofs may be found in most introductory textbooks on artificialintelligence.)