Honors Track: Competitive Programming Problem Solving Finding your way with A*-algorithm Patrick Shaw

Embed Size (px)

DESCRIPTION

Getting home

Citation preview

Honors Track: Competitive Programming & Problem Solving Finding your way with A*-algorithm Patrick Shaw intro Topics: Dijkstra (revisited) Greedy Best-First-Search A* Heuristics Ties Data structures Implementation Speed Variations Visual examples Getting home Dijkstras algorithm Add up distance from starting point Expands evenly into every direction Pros: Guarantees shortest path Cons: Slow Getting home with Dijkstra Start End Finally home! 16 steps! End has finally been found Greedy Best-First-Search Similar to Dijkstras algorithm Estimates distance to end (heuristic) Selects vertex closest to goal Pros: Fast Cons: Does not guarantee shortest path Greedy best-first-search Going home again Start End A lot faster! but might not be the shortest route A* algorithm Best of both worlds: A* Fast & shortest path Dijkstra for shortest path + heuristic to guide itself F(n) = g(n) + h(n) f(n) = total cost g(n) = exact distance from start to current vertex 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) Dijkstras algorithm Heuristics Heuristic function h(n): estimation of minimum cost to goal h(n) = 0 f(n) = g(n)+0 f(n) = g(n) Dijkstras 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) Dijkstras 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) Dijkstras 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) Dijkstras 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) Dijkstras 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) Dijkstras 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 How do we fix this? h(n) = 4 h(n) = 3 ties How do we fix this? Method 1: Scale the heuristic h(n) = 4 h(n) = 3 ties How do we fix this? Method 1: Scale the heuristic Example: f(n) = 4+3 = 7 f(n) = 3+4 = h(n) = 4 h(n) = 3 ties How do we fix this? Method 1: Scale the heuristic Example: f(n) = 4+3 = 7 4+3*1.01 = 7.03 f(n) = 3+4 = 7 3+4*1.01 = h(n) = 4 h(n) = 3 ties How do we fix this? Method 1: Scale the heuristic 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: Newest first h(n) = 4 h(n) = 3 Data Structure Linked lists: Sorted array: Binary heap: insertionFinding best element Removing best element Increase priority O(1)O(n)O(1)O(n) insertionFinding best element Removing best element Increase priority O(n)O(1) O(log(n)) insertionFinding best element Removing best element Increase priority O(log(n))O(1)O(log(n)) 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: DijkstraA* O(|E|+|V|log|V|) Some others Beam Search IDA* Dynamic weighting examples Dijkstra best first search A* IDA Source:n.htmln.html