Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Finding Shortest Paths with A* Search A* search operates similarly to Dijkstra’s algorithm, but extends the cost function to include an estimated distance.

Similar presentations


Presentation on theme: "1 Finding Shortest Paths with A* Search A* search operates similarly to Dijkstra’s algorithm, but extends the cost function to include an estimated distance."— Presentation transcript:

1 1 Finding Shortest Paths with A* Search A* search operates similarly to Dijkstra’s algorithm, but extends the cost function to include an estimated distance from the current node to the target Expands only the most promising nodes; its best-first search strategy eliminates a large portion of the solution space A* search (exploring 6 nodes) Dijkstra‘s algorithm (exploring 31 nodes) 1 2 3 4 13 56 7 8 9 10 29 11 12 14 15 16 17 18 1921 20 22 23 25 26 27 28 30 24 O O O 31 1 2 4 3 5 O O 6 O s s t t s Source t O Target Obstacle

2 Intro to STL - MAP map[key] = value – Each key has to be unique. (Same keys take the same entry) – The keys are sorted in ascending order Example – #include – map myMap; – point p1; p1.x = 0; p1.y = 1; – myMap[p1] = 0.1; //insert an element into the map Pros and Cons – Quick direct access of a single element. Fast insertion and deletion. – Map follows a strict ordering in storing elements (not flexible) Multimap (similar to map) – Each key doesn’t have to be unique – Access to a key will return an “equal range” (all values with the same key) – Multimap also follows a strict ordering (not flexible)

3 Intro to STL - SET Associative container like map – Each element stored in a set is unique – Elements are stored in ascending order – Fast Insertion and deletion, but relatively slow element access Example – #include – set mySet; – point p1; p1.x = 0; p1.y = 1; – mySet.insert(p1); //insert an element into the set Common usage – For duplicated elements removal – Quickly sort a batch of data on the fly (sorting while storing) – Merge two sets of data

4 Intro to STL - VECTOR Essentially is an array – Stores element serially in the memory – Can dynamically grow thus has safer and more convenient memory management (compared to C arrays) Example – #include – vector myVec; – point p1; p1.x = 0; p1.y = 1; – myVec.push_back(p1); //push an element into the vector Pros and Cons – Quick access of elements – The order of storing the elements is completely user defined (flexible) – Search, insertion, deletion not as fast as map or set (O(n) versus O(logn))

5 Intro to STL - QUEUE FIFO queue – Mostly used for BFS (such as for Maze Routing) – Only the first or the last element can be accessed – Access order decided by the order that the elements are pushed Example – #include – queue myQ; – point p1; p1.x = 0; p1.y = 1; – myQ.push(p1); //push an element into the queue Priority queue (C++ STL implementation) – Not suitable for routing because the elements are stored in descending order (the top element has the maximum value) – The default container to realize a priority queue is by using “vector” (will show how to do it soon!)

6 Integration in Your Code Migrating a C/C++ code to STL – Array  Vector – Linked list  STL list – …  What to change – Compiler: gcc  g++ – Add the following lines to your header files: #include … other STL containers used #include  #include … Add “using namespace std”

7 Pseudo-code From handout

8 Data structure for A*: Priority Queue Priority queue implemented using STL as a vector vector > PQ – Defines PQ with each element inside PQ made of pair, where “double” is used to store f[point] (score of vertex) How to implement enqueue of the priority queue? – Need to score elements in ascending order of the score – Note there can be cases when multiple elements in the PQ having the same score » Here, newly enqueued points should be stored on top of the older ones if they have smaller or equal f values for(it_PQ = PQ.begin(); it_PQ != PQ.end(); it_PQ++){ if(f[point] <= (*it_PQ).first){ PQ.insert(it_PQ, make_pair(f[point],point)); break; } } if(it_PQ == PQ.end()){ PQ.insert(it_PQ, make_pair(f[point], point)); }

9 Data structure for A*: Priority Queue Dequeue Erasing an element from the queue – For example erasing from the top : PQ.erase(PQ.begin()); How to extract min?  Based on the way we maintain the PQ as shown in the previous slide, just extract the top element, or the front element Min = PQ.front();

10 Data structure for A*: Strong Parents for Retracing Paths map > RP – RP[current_pnt] = – Parent point and the d values are always updated together – Note that d[current_pnt] is also used in the computation of f[current_pnt] Path retracing – Starting from the ending point, we can iteratively retrace the parent point until we reach the beginning point. vector path; point cur_pnt = end_pnt; while(cur_pnt != beg_pnt){ point temp = RP[cur_pnt].first; path.push_back(temp); cur_pnt = temp;}


Download ppt "1 Finding Shortest Paths with A* Search A* search operates similarly to Dijkstra’s algorithm, but extends the cost function to include an estimated distance."

Similar presentations


Ads by Google