Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Honors Track: Competitive Programming & Problem Solving Finding your way with A*-algorithm Patrick Shaw."— Presentation transcript:

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

2 intro Topics: Dijkstra (revisited) Greedy Best-First-Search A*  Heuristics  Ties  Data structures  Implementation  Speed Variations Visual examples

3 Getting home

4 Dijkstra’s algorithm Add up distance from starting point Expands evenly into every direction Pros:  Guarantees shortest path Cons:  Slow

5 Getting home with Dijkstra Start  End 

6

7

8

9

10

11

12

13 Finally home!  16 steps! End has finally been found

14 Greedy Best-First-Search Similar to Dijkstra’s algorithm Estimates distance to end (heuristic) Selects vertex closest to goal Pros:  Fast Cons:  Does not guarantee shortest path

15 Greedy best-first-search

16 Going home again Start  End 

17

18 A lot faster! …but might not be the shortest route

19 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

20 Heuristics Heuristic function h(n): estimation of minimum cost to goal  h(n) = 0  f(n) = g(n)+0  f(n) = g(n)  ??

21 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

22 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

23 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

24 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

25 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  ??

26 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

27 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

28 ties  How do we fix this? 4 4 3 3 h(n) = 4 h(n) = 3

29 ties  How do we fix this? Method 1:  Scale the heuristic 4 4 3 3 h(n) = 4 h(n) = 3

30 ties  How do we fix this? Method 1:  Scale the heuristic Example: f(n) = 4+3 = 7 f(n) = 3+4 = 7 4 4 3 3 h(n) = 4 h(n) = 3

31 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 4 4 3 3 h(n) = 4 h(n) = 3

32 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 4 4 3 3 h(n) = 4 h(n) = 3

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

34 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

35 But is it any faster? Running time: DijkstraA* O(|E|+|V|log|V|)

36 Some others Beam Search IDA* Dynamic weighting

37 examples  Dijkstra  best first search  A*  IDA  http://qiao.github.io/PathFinding.js/visual/ http://qiao.github.io/PathFinding.js/visual/  Source: http://theory.stanford.edu/~amitp/GameProgramming/AStarCompariso n.html http://theory.stanford.edu/~amitp/GameProgramming/AStarCompariso n.html


Download ppt "Honors Track: Competitive Programming & Problem Solving Finding your way with A*-algorithm Patrick Shaw."

Similar presentations


Ads by Google