Honors Track Competitive Programming Problem Solving Finding your
Honors Track: Competitive Programming & Problem Solving Finding your way with A*-algorithm Patrick Shaw
intro Topics: n Dijkstra (revisited) n Greedy Best-First-Search n A* p Heuristics p Ties p Data structures p Implementation p Speed n Variations n Visual examples
Getting home
Dijkstra’s algorithm n Add up distance from starting point n Expands evenly into every direction n Pros: p Guarantees shortest path n Cons: p Slow
Getting home with Dijkstra End Start
Finally home! End has finally been found 16 steps!
Greedy Best-First-Search n Similar to Dijkstra’s algorithm n Estimates distance to end (heuristic) n Selects vertex closest to goal n Pros: p Fast n Cons: p Does not guarantee shortest path
Greedy best-first-search
Going home again End Start
End A lot faster! …but might not be the shortest route
A* algorithm n Best of both worlds: A* p Fast & shortest path p Dijkstra for shortest path + heuristic to guide itself n F(n) = g(n) + h(n) p f(n) = total cost p g(n) = exact distance from start to current vertex p h(n) = estimated distance of current vertex to end
Heuristics Heuristic function h(n): estimation of minimum cost to goal § h(n) = 0 f(n) = g(n)+0 f(n) = g(n) ? ?
Heuristics Heuristic function h(n): estimation of minimum cost to goal § h(n) = 0 f(n) = g(n)+0 f(n) = g(n) Dijkstra’s algorithm
Heuristics Heuristic function h(n): estimation of minimum cost to goal § h(n) = 0 f(n) = g(n)+0 f(n) = g(n) Dijkstra’s algorithm § h(n) ≤ distance to goal : admissible heuristic
Heuristics Heuristic function h(n): estimation of minimum cost to goal § h(n) = 0 f(n) = g(n)+0 f(n) = g(n) Dijkstra’s algorithm § h(n) ≤ distance to goal : admissible heuristic § h(n) = distance to goal
Heuristics Heuristic function h(n): estimation of minimum cost to goal § h(n) = 0 f(n) = g(n)+0 f(n) = g(n) Dijkstra’s algorithm § h(n) ≤ distance to goal : admissible heuristic § h(n) = distance to goal § h(n) ≥ distance to goal
Heuristics Heuristic function h(n): estimation of minimum cost to goal § h(n) = 0 f(n) = g(n)+0 f(n) = g(n) Dijkstra’s algorithm § h(n) ≤ distance to goal : admissible heuristic § h(n) = distance to goal § h(n) ≥ distance to goal § h(n) = very large such that g(n) is irrelevant ? ?
Heuristics Heuristic function h(n): estimation of minimum cost to goal § h(n) = 0 f(n) = g(n)+0 f(n) = g(n) Dijkstra’s algorithm § h(n) ≤ distance to goal : admissible heuristic § h(n) = distance to goal § h(n) ≥ distance to goal § h(n) = very large such that g(n) is irrelevant greedy best-first-search
Heuristics Heuristic function h(n): estimation of minimum cost to goal § h(n) = 0 f(n) = g(n)+0 f(n) = g(n) Dijkstra’s algorithm § h(n) ≤ distance to goal : admissible heuristic § h(n) = distance to goal § h(n) ≥ distance to goal § h(n) = very large such that g(n) is irrelevant greedy best-first-search § h(n) ≤ distance to next node + h(next node): consistent heuristic
ties p How do we fix this? 3 h(n) = 4 4 h(n) = 3 4 3
ties p How do we fix this? 3 Method 1: p Scale the heuristic h(n) = 4 4 h(n) = 3 4 3
ties p How do we fix this? 3 Method 1: p Scale the heuristic Example: f(n) = 4+3 = 7 f(n) = 3+4 = 7 h(n) = 4 4 h(n) = 3 4 3
ties p How do we fix this? 3 Method 1: p Scale the heuristic h(n) = 4 4 h(n) = 3 4 Example: f(n) = 4+3 = 7 4+3*1. 01 = 7. 03 f(n) = 3+4 = 7 3+4*1. 01 = 7. 04 3
ties p How do we fix this? 3 Method 1: p Scale the heuristic h(n) = 4 h(n) = 3 4 Example: f(n) = 4+3 = 7 4+3*1. 01 = 7. 03 f(n) = 3+4 = 7 3+4*1. 01 = 7. 04 Method 2: p Newest first 4 3
Data Structure Linked lists: insertion Finding best element Removing best element Increase priority O(1) O(n) insertion Finding best element Removing best element Increase priority O(n) O(1) O(log(n)) insertion Finding best element Removing best element Increase priority O(log(n)) O(1) O(log(n)) Sorted array: Binary heap:
A* implementation OPEN = priority queue containing initial node CLOSED = empty set while lowest rank in OPEN is not the GOAL: current = lowest rank from OPEN remove lowest rank from OPEN add current to CLOSED for all neighbours of current: cost = g(current) + movementcost(current, neighbour) if neighbour in OPEN and cost less than g(neighbour): remove neighbour from OPEN, because new path is better if neighbour in CLOSED and cost less than g(neighbour): ⁽²⁾ remove neighbour from CLOSED if neighbour not in OPEN and neighbour not in CLOSED: set g(neighbour) to cost add neighbour to OPEN set priority queue rank to g(neighbour) + h(neighbour) set neighbour's parent to current reconstruct reverse path from goal to start by following parent pointers
But is it any faster? Running time: Dijkstra A* O(|E|+|V|log|V|)
Some others n Beam Search n IDA* n Dynamic weighting
examples § Dijkstra § best first search § A* § IDA p http: //qiao. github. io/Path. Finding. js/visual/ p Source: http: //theory. stanford. edu/~amitp/Game. Programming/AStar. Compariso n. html
- Slides: 37