the a* search algorithm - duke universitydb.cs.duke.edu/courses/fall11/cps149s/notes/a_star.pdf ·...

21
The A* Search Algorithm Siyang Chen

Upload: nguyendung

Post on 17-Apr-2018

216 views

Category:

Documents


2 download

TRANSCRIPT

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.)