Upload
others
View
10
Download
0
Embed Size (px)
Citation preview
1
CSCI 104Dijkstra’s algorithm and A*
Mark Redekopp
David Kempe
Sandra Batista
2
SINGLE-SOURCE SHORTEST PATH (SSSP)
Dijkstra's Algorithm
3
SSSP• Assign to each edge a positive
weight
– Could be physical distance, cost of using the link, etc.
• Find the shortest path from a source node, 'a' to all other nodes
a
b
d
c
h
ef
g
13
4
3
1
27
5
6
14
4
(c,13),(e,4)a
b
c
d
e
f
g
h
(c,5),(h,6)
(a,13),(b,5),(d,2),(e,8),(g,7)
(c,2),(f,1)
(a,4),(c,8),(f,3)
(d,1),(e,3),(g,4)
(c,7),(f,4),(h,14)
(b,6),(g,14)
Lis
t of V
ert
ices
Adja
cency L
ists
8
Edge weights
4
SSSP• What is the shortest distance from 'a' to
all other vertices?
• Distance is defined to be sum of weights on path between source and node.
• How would you compute these distances?
a
b
d
c
h
ef
g
13
4
3
1
27
5
6
14
4
(c,13),(e,4)a
b
c
d
e
f
g
h
(c,5),(h,6)
(a,13),(b,5),(d,2),(e,8),(g,7)
(c,2),(f,1)
(a,4),(c,8),(f,3)
(d,1),(e,3),(g,4)
(c,7),(f,4),(h,14)
(b,6),(g,14)
Lis
t of V
ert
ices
Adja
cency L
ists
8
a
b
c
d
e
f
g
h
Lis
t of V
ert
ices
0
Vert Dist
5
Dijkstra's Algorithm• On each iteration, Dijkstra's
algorithm selects vertex with minimum distance to source vertex
• BFS uses queue to maintain vertices in order discovered
• Dijkstra’s uses a priority queue to maintain vertices in shortest distance to source
– To demonstrate, we’ll use table of all vertices with their current known distances to source
a
b
d
c
h
ef
g
13
4
3
1
27
5
6
14
4
8
a
b
c
d
e
f
g
h
Lis
t of V
ert
ices
0
inf
inf
inf
inf
inf
inf
inf
Vert Dist
6
Dijkstra's Algorithm1. SSSP(G, s)
2. PQ = empty PQ
3. s.dist = 0; s.pred = NULL
4. PQ.insert(s)
5. For all v in vertices
6. if v != s then v.dist = inf; PQ.insert(v)
7. while PQ is not empty
8. v = min(); PQ.remove_min()
9. for u in neighbors(v)
10. w = weight(v,u)
11. if(v.dist + w < u.dist)
12. u.pred = v
13. u.dist = v.dist + w;
14. PQ.decreaseKey(u, u.dist)
a
b
d
c
h
ef
g
13
4
3
1
27
5
6
14
4
8
a
b
c
d
e
f
g
h
Lis
t of V
ert
ices
0
inf
inf
inf
inf
inf
inf
inf
Vert Dist
7
Dijkstra's Algorithm1. SSSP(G, s)
2. PQ = empty PQ
3. s.dist = 0; s.pred = NULL
4. PQ.insert(s)
5. For all v in vertices
6. if v != s then v.dist = inf; PQ.insert(v)
7. while PQ is not empty
8. v = min(); PQ.remove_min()
9. for u in neighbors(v)
10. w = weight(v,u)
11. if(v.dist + w < u.dist)
12. u.pred = v
13. u.dist = v.dist + w;
14. PQ.decreaseKey(u, u.dist)
a
b
d
c
h
ef
g
13
4
3
1
27
5
6
14
4
8
a
b
c
d
e
f
g
hLis
t of V
ert
ices
0
inf
inf
inf
inf
inf
inf
inf
Vert Dist
v=a13
4
8
Dijkstra's Algorithm1. SSSP(G, s)
2. PQ = empty PQ
3. s.dist = 0; s.pred = NULL
4. PQ.insert(s)
5. For all v in vertices
6. if v != s then v.dist = inf; PQ.insert(v)
7. while PQ is not empty
8. v = min(); PQ.remove_min()
9. for u in neighbors(v)
10. w = weight(v,u)
11. if(v.dist + w < u.dist)
12. u.pred = v
13. u.dist = v.dist + w;
14. PQ.decreaseKey(u, u.dist)
a
b
d
c
h
ef
g
13
4
3
1
27
5
6
14
4
8
a
b
c
d
e
f
g
hLis
t of V
ert
ices
0
inf
13
inf
4
inf
inf
inf
Vert Dist
v=e12
7
9
Dijkstra's Algorithm1. SSSP(G, s)
2. PQ = empty PQ
3. s.dist = 0; s.pred = NULL
4. PQ.insert(s)
5. For all v in vertices
6. if v != s then v.dist = inf; PQ.insert(v)
7. while PQ is not empty
8. v = min(); PQ.remove_min()
9. for u in neighbors(v)
10. w = weight(v,u)
11. if(v.dist + w < u.dist)
12. u.pred = v
13. u.dist = v.dist + w;
14. PQ.decreaseKey(u, u.dist)
a
b
d
c
h
ef
g
13
4
3
1
27
5
6
14
4
8
a
b
c
d
e
f
g
hLis
t of V
ert
ices
0
inf
12
inf
4
7
inf
inf
Vert Dist
v=f8
11
10
Dijkstra's Algorithm1. SSSP(G, s)
2. PQ = empty PQ
3. s.dist = 0; s.pred = NULL
4. PQ.insert(s)
5. For all v in vertices
6. if v != s then v.dist = inf; PQ.insert(v)
7. while PQ is not empty
8. v = min(); PQ.remove_min()
9. for u in neighbors(v)
10. w = weight(v,u)
11. if(v.dist + w < u.dist)
12. u.pred = v
13. u.dist = v.dist + w;
14. PQ.decreaseKey(u, u.dist)
a
b
d
c
h
ef
g
13
4
3
1
27
5
6
14
4
8
a
b
c
d
e
f
g
hLis
t of V
ert
ices
0
inf
12
8
4
7
11
inf
Vert Dist
v=d10
11
Dijkstra's Algorithm1. SSSP(G, s)
2. PQ = empty PQ
3. s.dist = 0; s.pred = NULL
4. PQ.insert(s)
5. For all v in vertices
6. if v != s then v.dist = inf; PQ.insert(v)
7. while PQ is not empty
8. v = min(); PQ.remove_min()
9. for u in neighbors(v)
10. w = weight(v,u)
11. if(v.dist + w < u.dist)
12. u.pred = v
13. u.dist = v.dist + w;
14. PQ.decreaseKey(u, u.dist)
a
b
d
c
h
ef
g
13
4
3
1
27
5
6
14
4
8
a
b
c
d
e
f
g
hLis
t of V
ert
ices
0
inf
10
8
4
7
11
inf
Vert Dist
v=c15
12
Dijkstra's Algorithm1. SSSP(G, s)
2. PQ = empty PQ
3. s.dist = 0; s.pred = NULL
4. PQ.insert(s)
5. For all v in vertices
6. if v != s then v.dist = inf; PQ.insert(v)
7. while PQ is not empty
8. v = min(); PQ.remove_min()
9. for u in neighbors(v)
10. w = weight(v,u)
11. if(v.dist + w < u.dist)
12. u.pred = v
13. u.dist = v.dist + w;
14. PQ.decreaseKey(u, u.dist)
a
b
d
c
h
ef
g
13
4
3
1
27
5
6
14
4
8
a
b
c
d
e
f
g
hLis
t of V
ert
ices
0
15
10
8
4
7
11
inf
Vert Dist
v=g
25
13
Dijkstra's Algorithm1. SSSP(G, s)
2. PQ = empty PQ
3. s.dist = 0; s.pred = NULL
4. PQ.insert(s)
5. For all v in vertices
6. if v != s then v.dist = inf; PQ.insert(v)
7. while PQ is not empty
8. v = min(); PQ.remove_min()
9. for u in neighbors(v)
10. w = weight(v,u)
11. if(v.dist + w < u.dist)
12. u.pred = v
13. u.dist = v.dist + w;
14. PQ.decreaseKey(u, u.dist)
a
b
d
c
h
ef
g
13
4
3
1
27
5
6
14
4
8
a
b
c
d
e
f
g
hLis
t of V
ert
ices
0
15
10
8
4
7
11
25
Vert Dist
v=b
21
14
Dijkstra's Algorithm1. SSSP(G, s)
2. PQ = empty PQ
3. s.dist = 0; s.pred = NULL
4. PQ.insert(s)
5. For all v in vertices
6. if v != s then v.dist = inf; PQ.insert(v)
7. while PQ is not empty
8. v = min(); PQ.remove_min()
9. for u in neighbors(v)
10. w = weight(v,u)
11. if(v.dist + w < u.dist)
12. u.pred = v
13. u.dist = v.dist + w;
14. PQ.decreaseKey(u, u.dist)
a
b
d
c
h
ef
g
13
4
3
1
27
5
6
14
4
8
a
b
c
d
e
f
g
hLis
t of V
ert
ices
0
15
10
8
4
7
11
21
Vert Dist
v=h
15
Analysis• What is the loop invariant?
– The vertex v removed from PQ is guaranteed to be the vertex with shortest path to source of all vertices in PQ and its distance is set to its shortest distance to the source.
– All vertices whose distances and predecessors are set have shortest known distance thus far to source.
• Proof sketch by induction
– First node from PQ is source itself with distance 0 to itself.
– Decrease the distance to its neighbors; its neighbor with the shortest distance will be at front of PQ
– No shorter path from source to vertex at front of PQ; any other path would use some edge from the start having greater distance
a
b
d
c
h
ef
g
13
4
3
1
27
5
6
14
4
8
16
ALGORITHM HIGHLIGHT
A* Search Algorithm
17
Search Methods
• Many systems require searching for goal states
– Path Planning
• Mapquest/Google Maps
• Games
– Optimization Problems
• Find the optimal solution to a problem with many constraints
18
Search Applied to 8-Tile Game• 8-Tile Puzzle
– 3x3 grid with one blank space
– With a series of moves, get the tiles in sequential order
– Goal state:
1 8
7 6 4
5 3 2
Original: No order
1 2 3
4 5 6
7 8
Goal State
19
Search Methods
• Brute-Force Search: Search all possibilities until you find it!
• Heuristic Search: A heuristic is a “rule of thumb”.
– Heuristics are not perfect; they are quick computations to give an approximate measure.
20
Brute Force Search
• Brute Force Search Tree
– Generate all possible moves
– Explore each move despite its proximity to the goal node
1 24 8 37 6 5
1 24 8 37 6 5
1 24 8 37 6 5
1 8 24 37 6 5
1 24 8 37 6 5
1 24 8 37 6 5
1 2 34 8 57 6
1 2 34 87 6 5
1 2 34 87 6 5
1 2 34 8 57 6
1 2 34 57 8 6
W S
W S E
21
Heuristics
• Heuristics are “scores” of how close a state is to the goal (usually, lower = better)
• Heuristics must be easy to compute from current state
• Heuristics for 8-tile puzzle– # of tiles out of place
– Total x-, y- distance of each tile from its correct location (Manhattan distance)
1 8 3
4 5 6
2 7
1 8 3
4 5 6
2 7
# of Tiles out of Place = 3
Total x-/y- distance = 6
22
Heuristic Search
• Heuristic Search Tree
– Use total x-/y-distance (Manhattan distance) heuristic
– Explore the lowest scored states
1 24 8 37 6 5
1 24 8 37 6 5
1 24 8 37 6 5
1 2 34 8 57 6
1 2 34 5 67 8
1 2 34 87 6 5
1 2 34 87 6 5
1 2 34 8 57 6
1 2 34 57 8 6
1 2 34 57 8 6
H=6
H=7 H=5
H=6 H=6 H=4
H=3
H=2
H=1
Goal
1 2 34 87 6 5
H=5
23
Caution About Heuristics
• Heuristics may be wrong
• Sometimes pursuing lowest heuristic score leads to a less-than optimal solution or even no solution
• Solution
– Take # of moves from start (depth) into account
H=2
Start
H=1
H=1
H=1
H=1
H=1
H=1
…
Goal
H=1
24
A-star Algorithm• Use a new metric to decide which state to
explore/expand
• Define– h = heuristic score (Manhattan distance)
– g = number of moves from start to current state
– f = g + h
• As we explore states and their successors, assign each state its f-score and always explore the state with lowest f-score
• Heuristics should always underestimate the distance to the goal– If so, A* guarantees optimal solutions
g=1,h=2
f=3
Start
g=1,h=1
f=2
g=2,h=1
f=3
g=3,h=1
f=4Goal
g=2,h=1
f=3
25
A-Star Algorithm• Maintain 2 lists
– Open list = Nodes to be explored (chosen from)
– Closed list = Nodes already explored (already chosen)
• Pseudocode
open_list.push(Start State)
while(open_list is not empty)
1. s ← remove min. f-value state from open_list
(if tie in f-values, select one w/ larger g-value)
2. Add s to closed list
3a. if s = goal node then trace path back to start; STOP!
3b. For each neighbor, v, of s, compute f-value
if v on closed_list, skip v.
if v not on open_list, add v to open list
if v on open_list, update f-value if current value is smaller
g=1,h=2
f=3
Start
g=1,h=1
f=2
g=2,h=1
f=3
g=3,h=1
f=4Goal
g=2,h=1
f=3
26
Path-Planning w/ A* Algorithm• Find optimal path from S to G using A*
– Use heuristic of Manhattan (x-/y-) distance
S
G
open_list.push(Start State)
while(open_list is not empty)
1. s ← remove min. f-value state from open_list
(if tie in f-values, select one w/ larger g-value)
2. Add s to closed list
3a. if s = goal node then trace path back to start;
STOP!
3b. For each neighbor, v, of s, compute f-value
if v on closed_list, skip v.
if v not on open_list, add v to open list
if v on open_list, update f-value if current
value is smaller
Closed List
Open List
27
Path-Planning w/ A* Algorithm• Find optimal path from S to G using A*
– Use heuristic of Manhattan (x-/y-) distance
S
G
Closed List
Open Listg=0,
h=6,
f=6
open_list.push(Start State)
while(open_list is not empty)
1. s ← remove min. f-value state from open_list
(if tie in f-values, select one w/ larger g-value)
2. Add s to closed list
3a. if s = goal node then trace path back to start;
STOP!
3b. For each neighbor, v, of s, compute f-value
if v on closed_list, skip v.
if v not on open_list, add v to open list
if v on open_list, update f-value if current
value is smaller
28
Path-Planning w/ A* Algorithm• Find optimal path from S to G using A*
– Use heuristic of Manhattan (x-/y-) distance
g=1,
h=7,
f=8
S
G
g=1,
h=5,
f=6
g=1,
h=5,
f=6
g=1,
h=7,
f=8
Closed List
Open List
open_list.push(Start State)
while(open_list is not empty)
1. s ← remove min. f-value state from open_list
(if tie in f-values, select one w/ larger g-value)
2. Add s to closed list
3a. if s = goal node then trace path back to start;
STOP!
3b. For each neighbor, v, of s, compute f-value
if v on closed_list, skip v.
if v not on open_list, add v to open list
if v on open_list, update f-value if current
value is smaller
29
Path-Planning w/ A* Algorithm• Find optimal path from S to G using A*
– Use heuristic of Manhattan (x-/y-) distance
g=1,
h=7,
f=8
S
G
g=1,
h=5,
f=6
g=1,
h=5,
f=6
g=1,
h=7,
f=8
Closed List
Open List
g=2,
h=6,
f=8
g=2,
h=4,
f=6
open_list.push(Start State)
while(open_list is not empty)
1. s ← remove min. f-value state from open_list
(if tie in f-values, select one w/ larger g-value)
2. Add s to closed list
3a. if s = goal node then trace path back to start;
STOP!
3b. For each neighbor, v, of s, compute f-value
if v on closed_list, skip v.
if v not on open_list, add v to open list
if v on open_list, update f-value if current
value is smaller
30
Path-Planning w/ A* Algorithm• Find optimal path from S to G using A*
– Use heuristic of Manhattan (x-/y-) distance
g=1,
h=7,
f=8
S
G
g=1,
h=5,
f=6
g=1,
h=5,
f=6
g=1,
h=7,
f=8
Closed List
Open List
g=2,
h=6,
f=8
g=2,
h=4,
f=6
open_list.push(Start State)
while(open_list is not empty)
1. s ← remove min. f-value state from open_list
(if tie in f-values, select one w/ larger g-value)
2. Add s to closed list
3a. if s = goal node then trace path back to start;
STOP!
3b. For each neighbor, v, of s, compute f-value
if v on closed_list, skip v.
if v not on open_list, add v to open list
if v on open_list, update f-value if current
value is smaller
31
Path-Planning w/ A* Algorithm• Find optimal path from S to G using A*
– Use heuristic of Manhattan (x-/y-) distance
g=1,
h=7,
f=8
S
G
g=1,
h=5,
f=6
g=1,
h=5,
f=6
g=1,
h=7,
f=8
Closed List
Open List
g=2,
h=6,
f=8
g=2,
h=4,
f=6
g=1,
h=5,
f=6
g=2,
h=6,
f=8
open_list.push(Start State)
while(open_list is not empty)
1. s ← remove min. f-value state from open_list
(if tie in f-values, select one w/ larger g-value)
2. Add s to closed list
3a. if s = goal node then trace path back to start;
STOP!
3b. For each neighbor, v, of s, compute f-value
if v on closed_list, skip v.
if v not on open_list, add v to open list
if v on open_list, update f-value if current
value is smaller
32
Path-Planning w/ A* Algorithm• Find optimal path from S to G using A*
– Use heuristic of Manhattan (x-/y-) distance
g=1,
h=7,
f=8
S
G
g=1,
h=5,
f=6
g=1,
h=5,
f=6
g=1,
h=7,
f=8
Closed List
Open List
g=2,
h=6,
f=8
g=2,
h=4,
f=6
g=2,
h=6,
f=8
g=3,
h=7,
f=10
g=3,
h=5,
f=8
open_list.push(Start State)
while(open_list is not empty)
1. s ← remove min. f-value state from open_list
(if tie in f-values, select one w/ larger g-value)
2. Add s to closed list
3a. if s = goal node then trace path back to start;
STOP!
3b. For each neighbor, v, of s, compute f-value
if v on closed_list, skip v.
if v not on open_list, add v to open list
if v on open_list, update f-value if current
value is smaller
33
Path-Planning w/ A* Algorithm• Find optimal path from S to G using A*
– Use heuristic of Manhattan (x-/y-) distance
g=1,
h=7,
f=8
S
G
g=1,
h=5,
f=6
g=1,
h=5,
f=6
g=1,
h=7,
f=8
Closed List
Open List
g=2,
h=6,
f=8
g=2,
h=4,
f=6
g=2,
h=6,
f=8
g=3,
h=7,
f=10
g=3,
h=5,
f=8
g=2,
h=8,
f=10
g=2,
h=8,
f=10
g=2,
h=8,
f=10
g=3,
h=7,
f=10
g=3,
h=5,
f=8
g=4,
h=6,
f=10
g=4,
h=4,
f=8
open_list.push(Start State)
while(open_list is not empty)
1. s ← remove min. f-value state from open_list
(if tie in f-values, select one w/ larger g-value)
2. Add s to closed list
3a. if s = goal node then trace path back to start;
STOP!
3b. For each neighbor, v, of s, compute f-value
if v on closed_list, skip v.
if v not on open_list, add v to open list
if v on open_list, update f-value if current
value is smaller
34
Path-Planning w/ A* Algorithm• Find optimal path from S to G using A*
– Use heuristic of Manhattan (x-/y-) distance
g=1,
h=7,
f=8
S
G
g=1,
h=5,
f=6
g=1,
h=5,
f=6
g=1,
h=7,
f=8
Closed List
Open List
g=2,
h=6,
f=8
g=2,
h=4,
f=6
g=2,
h=6,
f=8
g=3,
h=7,
f=10
g=3,
h=5,
f=8
g=2,
h=8,
f=10
g=2,
h=8,
f=10
g=2,
h=8,
f=10
g=3,
h=7,
f=10
g=3,
h=5,
f=8
g=4,
h=6,
f=10
g=4,
h=4,
f=8
g=5,
h=5,
f=10
g=5,
h=5,
f=10
g=5,
h=3,
f=8
g=1,
h=7,
f=8
g=1,
h=7,
f=8
g=2,
h=6,
f=8
g=1,
h=7,
f=8
g=1,
h=7,
f=8
g=2,
h=6,
f=8
open_list.push(Start State)
while(open_list is not empty)
1. s ← remove min. f-value state from open_list
(if tie in f-values, select one w/ larger g-value)
2. Add s to closed list
3a. if s = goal node then trace path back to start;
STOP!
3b. For each neighbor, v, of s, compute f-value
if v on closed_list, skip v.
if v not on open_list, add v to open list
if v on open_list, update f-value if current
value is smaller
35
Path-Planning w/ A* Algorithm• Find optimal path from S to G using A*
– Use heuristic of Manhattan (x-/y-) distance
g=1,
h=7,
f=8
S
G
g=1,
h=5,
f=6
g=1,
h=5,
f=6
g=1,
h=7,
f=8
Closed List
Open List
g=2,
h=6,
f=8
g=2,
h=4,
f=6
g=2,
h=6,
f=8
g=3,
h=7,
f=10
g=3,
h=5,
f=8
g=2,
h=8,
f=10
g=2,
h=8,
f=10
g=2,
h=8,
f=10
g=3,
h=7,
f=10
g=3,
h=5,
f=8
g=4,
h=6,
f=10
g=4,
h=4,
f=8
g=5,
h=5,
f=10
g=5,
h=5,
f=10
g=5,
h=3,
f=8
g=6,
h=4,
f=10
g=6,
h=2,
f=8
g=1,
h=7,
f=8
g=1,
h=7,
f=8
g=2,
h=6,
f=8
open_list.push(Start State)
while(open_list is not empty)
1. s ← remove min. f-value state from open_list
(if tie in f-values, select one w/ larger g-value)
2. Add s to closed list
3a. if s = goal node then trace path back to start;
STOP!
3b. For each neighbor, v, of s, compute f-value
if v on closed_list, skip v.
if v not on open_list, add v to open list
if v on open_list, update f-value if current
value is smaller
36
Path-Planning w/ A* Algorithm• Find optimal path from S to G using A*
– Use heuristic of Manhattan (x-/y-) distance
g=1,
h=7,
f=8
S
G
g=1,
h=5,
f=6
g=1,
h=5,
f=6
g=1,
h=7,
f=8
Closed List
Open List
g=2,
h=6,
f=8
g=2,
h=4,
f=6
g=2,
h=6,
f=8
g=3,
h=7,
f=10
g=3,
h=5,
f=8
g=2,
h=8,
f=10
g=2,
h=8,
f=10
g=2,
h=8,
f=10
g=3,
h=7,
f=10
g=3,
h=5,
f=8
g=4,
h=6,
f=10
g=4,
h=4,
f=8
g=5,
h=5,
f=10
g=5,
h=5,
f=10
g=5,
h=3,
f=8
g=6,
h=4,
f=10
g=6,
h=2,
f=8
g=7,
h=3,
f=10
g=7,
h=1,
f=8
g=1,
h=7,
f=8
g=1,
h=7,
f=8
g=2,
h=6,
f=8
open_list.push(Start State)
while(open_list is not empty)
1. s ← remove min. f-value state from open_list
(if tie in f-values, select one w/ larger g-value)
2. Add s to closed list
3a. if s = goal node then trace path back to start;
STOP!
3b. For each neighbor, v, of s, compute f-value
if v on closed_list, skip v.
if v not on open_list, add v to open list
if v on open_list, update f-value if current
value is smaller
37
Path-Planning w/ A* Algorithm• Find optimal path from S to G using A*
– Use heuristic of Manhattan (x-/y-) distance
g=1,
h=7,
f=8
S
G
g=1,
h=5,
f=6
g=1,
h=5,
f=6
g=1,
h=7,
f=8
Closed List
Open List
g=2,
h=6,
f=8
g=2,
h=4,
f=6
g=2,
h=6,
f=8
g=3,
h=7,
f=10
g=3,
h=5,
f=8
g=2,
h=8,
f=10
g=2,
h=8,
f=10
g=2,
h=8,
f=10
g=3,
h=7,
f=10
g=3,
h=5,
f=8
g=4,
h=6,
f=10
g=4,
h=4,
f=8
g=5,
h=5,
f=10
g=5,
h=5,
f=10
g=5,
h=3,
f=8
g=6,
h=4,
f=10
g=6,
h=2,
f=8
g=7,
h=3,
f=10
g=7,
h=1,
f=8
g=8,
h=2,
f=10
g=8,
h=0,
f=8
g=1,
h=7,
f=8
g=1,
h=7,
f=8
g=2,
h=6,
f=8
open_list.push(Start State)
while(open_list is not empty)
1. s ← remove min. f-value state from open_list
(if tie in f-values, select one w/ larger g-value)
2. Add s to closed list
3a. if s = goal node then trace path back to start;
STOP!
3b. For each neighbor, v, of s, compute f-value
if v on closed_list, skip v.
if v not on open_list, add v to open list
if v on open_list, update f-value if current
value is smaller
38
Path-Planning w/ A* Algorithm• Find optimal path from S to G using A*
– Use heuristic of Manhattan (x-/y-) distance
g=1,
h=7,
f=8
S
G
g=1,
h=5,
f=6
g=1,
h=5,
f=6
g=1,
h=7,
f=8
Closed List
Open List
g=2,
h=6,
f=8
g=2,
h=4,
f=6
g=2,
h=6,
f=8
g=3,
h=7,
f=10
g=3,
h=5,
f=8
g=2,
h=8,
f=10
g=2,
h=8,
f=10
g=2,
h=8,
f=10
g=3,
h=7,
f=10
g=3,
h=5,
f=8
g=4,
h=6,
f=10
g=4,
h=4,
f=8
g=5,
h=5,
f=10
g=5,
h=5,
f=10
g=5,
h=3,
f=8
g=6,
h=4,
f=10
g=6,
h=2,
f=8
g=7,
h=3,
f=10
g=7,
h=1,
f=8
g=8,
h=2,
f=10
g=8,
h=0,
f=8
g=1,
h=7,
f=8
g=1,
h=7,
f=8
g=2,
h=6,
f=8
open_list.push(Start State)
while(open_list is not empty)
1. s ← remove min. f-value state from open_list
(if tie in f-values, select one w/ larger g-value)
2. Add s to closed list
3a. if s = goal node then trace path back to start;
STOP!
3b. For each neighbor, v, of s, compute f-value
if v on closed_list, skip v.
if v not on open_list, add v to open list
if v on open_list, update f-value if current
value is smaller
39
Path-Planning w/ A* Algorithm• Find optimal path from S to G using A*
– Use heuristic of Manhattan (x-/y-) distance
g=1,
h=7,
f=8
S
G
g=1,
h=5,
f=6
g=1,
h=5,
f=6
g=1,
h=7,
f=8
Closed List
Open List
g=2,
h=6,
f=8
g=2,
h=4,
f=6
g=2,
h=6,
f=8
g=3,
h=7,
f=10
g=3,
h=5,
f=8
g=2,
h=8,
f=10
g=2,
h=8,
f=10
g=2,
h=8,
f=10
g=3,
h=7,
f=10
g=3,
h=5,
f=8
g=4,
h=6,
f=10
g=4,
h=4,
f=8
g=5,
h=5,
f=10
g=5,
h=5,
f=10
g=5,
h=3,
f=8
g=6,
h=4,
f=10
g=6,
h=2,
f=8
g=7,
h=3,
f=10
g=7,
h=1,
f=8
g=8,
h=2,
f=10
g=8,
h=0,
f=8
g=1,
h=7,
f=8
g=1,
h=7,
f=8
g=2,
h=6,
f=8
open_list.push(Start State)
while(open_list is not empty)
1. s ← remove min. f-value state from open_list
(if tie in f-values, select one w/ larger g-value)
2. Add s to closed list
3a. if s = goal node then trace path back to start;
STOP!
3b. For each neighbor, v, of s, compute f-value
if v on closed_list, skip v.
if v not on open_list, add v to open list
if v on open_list, update f-value if current
value is smaller
40
A* and BFS• BFS explores all nodes at a shorter distance
from the start (i.e. g value)
g=1,
h=7,
f=8
S
G
g=1,
h=5,
f=6
g=1,
h=5,
f=6
g=1,
h=7,
f=8
Closed List
Open List
41
A* and BFS• BFS explores all nodes at a shorter distance
from the start (i.e. g value)
g=1,
h=7,
f=8
S
G
g=1,
h=5,
f=6
g=1,
h=5,
f=6
g=1,
h=7,
f=8
Closed List
Open List
g=2,
h=8,
f=10
g=2,
h=8,
f=10
g=2,
h=8,
f=10
g=2,
h=6,
f=8
g=2,
h=6,
f=8
g=2,
h=4,
f=6
42
A* and BFS• BFS is A* using just the g value to choose
which item to select and expand
g=1,
h=7,
f=8
S
G
g=1,
h=5,
f=6
g=1,
h=5,
f=6
g=1,
h=7,
f=8
Closed List
Open List
g=2,
h=8,
f=10
g=2,
h=8,
f=10
g=2,
h=8,
f=10
g=2,
h=6,
f=8
g=2,
h=6,
f=8
g=2,
h=4,
f=6
43
A* Analysis
• What data structure should we use for the open-list?
• What data structure should we use for the closed-list?
• A* is essentially modification of Dijkstra’s with heuristic used for priorities in PQ
• Run time is similar to Dijkstra's algorithm…– Each node added/removed once from the open-list so that incurs N*O(remove-cost)
– Visiting each neighbor requires O(E) operations and performing an insert or decrease operation is E*max(O(insert), O(decrease))
– E = Number of potential successors and this depends on the problem and the possible solution space
– For the tile puzzle game, how many potential boards are there? (This is why there was a sad face next to the brute force approach…)