PATH FINDING ALGORITHMS 12/02/2020 PACMAN 1980 2 CITIES SKYLINES 2015 3 ADD A FOOTER MM.DD.20XX AGE OF EMPIRES 3 2004 4 ADD A FOOTER MM.DD.20XX FAR CRY 4 2014 5 ADD A FOOTER MM.DD.20XX AGENDA What are we going to discuss? 6 ALGORITHMS WE WILL DISCUSS THE FOLLOWING ALGORITHMS Breadth First Search 7 PATH FINDING ALGORITHMS Depth First Search Dijkstra's Algorithm 09.25.2018 6 ALGORITHMS WE WILL DISCUSS THE FOLLOWING ALGORITHMS Greedy Search 8 PATH FINDING ALGORITHMS A* D* 09.25.2018 PROBLEM STATEMENT o Shizuka has invited Nobita to have cake o But because Gian had forced Nobita to play baseball, he is already late o Shizuka will get upset if he gets any more late. o There are chances that she will invite Dekisuki if Nobita doesn’t show up o As Nobita’s friend, Doraemon (you) have to find the optimal path to Shizuka’s home. 9 PATH FINDING ALGORITHMS 09.25.2018 BREADTH FIRST SEARCH Treat the neighbourhood as layers o Explore the neighbourhood layer wise o Proceed to next layer once all nodes in a layer are complete o If destination is found, no need to search further. That is the nearest distance. 10 PATH FINDING ALGORITHMS 09.25.2018 Path Visited Tentative 11 PATH FINDING ALGORITHMS 09.25.2018 Path Visited Tentative 12 PATH FINDING ALGORITHMS 09.25.2018 Path Visited Tentative 13 PATH FINDING ALGORITHMS 09.25.2018 Path Visited Tentative 14 PATH FINDING ALGORITHMS 09.25.2018 Path Visited Tentative 15 PATH FINDING ALGORITHMS 09.25.2018 Path Visited Tentative 16 PATH FINDING ALGORITHMS 09.25.2018 Path Visited Tentative 17 PATH FINDING ALGORITHMS 09.25.2018 Path Visited Tentative 18 PATH FINDING ALGORITHMS 09.25.2018 Path Visited Tentative 19 PATH FINDING ALGORITHMS 09.25.2018 Path Visited Tentative 20 PATH FINDING ALGORITHMS 09.25.2018 Path Visited Tentative 21 PATH FINDING ALGORITHMS 09.25.2018 BIDIRECTIONAL BREADTH FIRST SEARCH LET’S COOPERATE o Both start state and goal state search towards each other o Approximately halves the search time 22 PATH FINDING ALGORITHMS 09.25.2018 Path Visited Tentative 23 PATH FINDING ALGORITHMS 09.25.2018 Path Visited Tentative 24 PATH FINDING ALGORITHMS 09.25.2018 Path Visited Tentative 25 PATH FINDING ALGORITHMS 09.25.2018 Path Visited Tentative 26 PATH FINDING ALGORITHMS 09.25.2018 DFS “Don’t care about results just choose a path which you see first, if you fail trace back” 27 PATH FINDING ALGORITHMS 09.25.2018 DFS • Depth-first Search (DFS) is an algorithm for searching a graph or tree data structure. • The algorithm starts at the root (top) node of a tree and goes as far as it can down a given branch (path), and then backtracks until it finds an unexplored path, and then explores it. • The algorithm does this until the entire graph has been explored. • Depth-first searches are often used as subroutines in other more complex algorithms. 28 PATH FINDING ALGORITHMS 09.25.2018 Depth first search in Trees A tree is an undirected graph in which any two vertices are connected by exactly one path. In other words, any acyclic connected graph is a tree. For a tree, we have below traversal methods – • Preorder: visit each node before its children. • Postorder: visit each node after its children. • Inorder (for binary trees only): visit left subtree, node, right subtree. 29 PATH FINDING ALGORITHMS 09.25.2018 Depth first search in Graph Recursive Approach:Depth first search is a way of traversing graphs, which is closely related to preorder traversal of a tree. Below is recursive implementation of preorder traversal To turn this into a graph traversal algorithm, we basically replace “child” by “neighbor”. But to prevent infinite loops we keep track of the vertices are already discovered and not visit them again. procedure preorder(treeNode v) { visit(v); for each child u of v preorder(u); } procedure dfs(vertex v) { visit(v); for each neighbor u of v if u is undiscovered call dfs(u); } 30 PATH FINDING ALGORITHMS 09.25.2018 Depth first search in Graph Iterative Approach:The non-recursive implementation of DFS is similar to the non-recursive implementation of BFS, but differs from it in two ways: • It uses a stack instead of a queue • The DFS should mark discovered only after popping the vertex not before pushing it. • It uses reverse iterator instead of iterator to produce same results as recursive DFS. 31 PATH FINDING ALGORITHMS 09.25.2018 Tic Tac Toe Using DFS Using Recursive Method:1. Check if the game is over — if either player won or if the board is fully filled 1. If so, return the result of the game 2. Iterate through all board squares 1. If the square is occupied, continue to the next one 2. Set the square to either an ‘X’ or an ‘O’ depending on the current player’s color 3. Get the outcome of the game by recursing, calling the same method with the updated board, and with the turn boolean swapped 4. Update the best result of this branch, trying to maximize the result for the current player 32 PATH FINDING ALGORITHMS 09.25.2018 Path Visited Tentative 33 PATH FINDING ALGORITHMS 09.25.2018 Path Visited Tentative 34 PATH FINDING ALGORITHMS 09.25.2018 Path Visited Tentative 35 PATH FINDING ALGORITHMS 09.25.2018 Path Visited Tentative 36 PATH FINDING ALGORITHMS 09.25.2018 Path Visited Tentative 37 PATH FINDING ALGORITHMS 09.25.2018 Dijkstra’s Algorithm “Finding shortest path from source by building a set of nodes that have minimum distance from the source.” 38 Dijkstra’s Algorithm • Create a set of shortest path tree that keeps track of vertices included in shortest path tree, i.e., whose minimum distance from source is calculated and finalized. Initially, this set is empty. • Assign a distance value to all vertices in the input graph. Initialize all distance values as ∞. Assign distance value as 0 for the source vertex so that it is picked first. 39 Continue… • While the set doesn’t include all vertices • Pick a vertex u which is not there in set and has minimum distance value. • Include u to set. • Update distance value of all adjacent vertices of u. To update the distance values, iterate through all adjacent vertices. For every adjacent vertex v, if sum of distance value of u (from source) and weight of edge u-v, is less than the distance value of v, then update the distance value of v. 40 41 D A 42 C B D A A 43 ADD A FOOTER C B B C D D 0 A 44 ADD A FOOTER C B A B C D 6 2 ∞ ∞ ∞ D 0 A 45 ADD A FOOTER C B B A B C D 6 2 ∞ ∞ ∞ 6 2 3B ∞ ∞ D A B C D 6 2 ∞ ∞ ∞ B 6 2 3B ∞ ∞ C 6 0 A 46 ADD A FOOTER C B 3B 5C ∞ D A B C D 6 2 ∞ ∞ ∞ B 6 2 3B ∞ ∞ C 6 D 6 0 A 47 ADD A FOOTER C B 3B 5C ∞ 5C 8D D A B C D 6 2 ∞ ∞ ∞ B 6 2 3B ∞ ∞ C 6 D 6 5C 8D A 6 8D 0 A 48 ADD A FOOTER C B 3B 5C ∞ D 0 A C B A B C D 6 2 ∞ ∞ ∞ 2 3B ∞ ∞ B C 6 3B 5C ∞ D 6 5C 8D A 6 8D 8D 49 ADD A FOOTER D 0 A C B A B C D 6 2 ∞ ∞ ∞ 2 3B ∞ ∞ B C 6 3B 5C ∞ D 6 5C 8D A 6 8D 8D 50 ADD A FOOTER Application Google maps!! It uses more complex and efficient algorithms.. But Dijkstra is the basis. Network Routing Protocols!!! Dijkstra's algorithm is widely used in the routing protocols required by the routers to update their forwarding table. The algorithm provide the shortest cost path from source router to other routers in the network. 51 Greedy “Focus on choosing the next best choice whether it gives the best solution or not” 52 PATH FINDING ALGORITHMS 09.25.2018 Greedy • Greedy algorithms are fast • A greedy algorithm is an algorithmic paradigm that follows the problem solving heuristic of making the locally optimal choice at each stage with the intent of finding a global optimum. • In many problems, a greedy strategy does not usually produce an optimal solution, but nonetheless a greedy heuristic may yield locally optimal solutions that approximate a globally optimal solution in a reasonable amount of time. 53 PATH FINDING ALGORITHMS 09.25.2018 Crystal Quest • Crystal Quest is an action game originally for the Macintosh. It was written by Patrick Buckland for Casady & Greene in 1987, and was ported to the Apple IIgs in 1989 by Bill Heineman. Ports were also made to the Amiga, Game Boy, iOS, and Palm. It was notable for being the first game to support the color displays on the Macintosh II. • In the Macintosh computer game Crystal Quest the objective is to collect crystals, in a fashion similar to the travelling salesman problem. The game has a demo mode, where the game uses a greedy algorithm to go to every crystal. The artificial intelligence does not account for obstacles, so the demo mode often ends quickly. 54 PATH FINDING ALGORITHMS 09.25.2018 A 55 PATH FINDING ALGORITHMS B 09.25.2018 A H=2 56 PATH FINDING ALGORITHMS B H=6 09.25.2018 A 57 PATH FINDING ALGORITHMS B 09.25.2018 A 58 PATH FINDING ALGORITHMS B 09.25.2018 A* Algorithm “Cost and Heuristics both matter” 59 PATH FINDING ALGORITHMS 25/09/2018 A* Algorithm • Informally speaking, A* Search algorithms, unlike other traversal techniques, it has “brains”. • What it means is that it is really a smart algorithm which separates it from the other conventional algorithms. 60 PATH FINDING ALGORITHMS 25/09/2018 A* Algorithm - What it does? What A* Search Algorithm does is that at each step it picks the node according to a value-‘f’ which is a parameter equal to the sum of two other parameters – ‘g’ and ‘h’. At each step it picks the node/cell having the lowest ‘f’, and process that node/cell. g = the movement cost to move from the starting point to a given square on the grid, following the path generated to get there. h = the estimated movement cost to move from that given square on the grid to the final destination. This is often referred to as the heuristic (smart guess) 61 PATH FINDING ALGORITHMS 25/09/2018 A* Algorithm 1. Initialize the open list successor.h = distance from goal to successor 2. Initialize the closed list successor.f = successor.g + successor.h put the starting node on the open ii) if a node with the same position as list (you can leave its f at zero) successor is in the OPEN list which has a 3. while the open list is not empty lower f than successor, skip this successor a) find the node with the least f on the open list, call it "q" iii) if a node with the same position as successor is in the CLOSED list which has b) pop q off the open list a lower f than successor, skip this successor c) generate q's 8 successors and set their otherwise, add the node to the open list parents to q end (for loop) d) for each successor i) if successor is the goal, stop search 62 successor.g = q.g + distance between successor and q e) push q on the closed list end (while loop) 25/09/2018 A* Algorithm Games don’t need lot of accuracy, but speed is a crucial thing. So most of the games use A* algorithm which uses heuristics to speed up the calculations but with accuracy trade off. In most cases A* is more than sufficient. 63 PATH FINDING ALGORITHMS 25/09/2018 A H=2 G=6 64 PATH FINDING ALGORITHMS B H=6 G=2 25/09/2018 D H=3 G=9 A 65 PATH FINDING ALGORITHMS C H=5 G=3 B 25/09/2018 D H=3 G=5 A H=2 G=6 66 PATH FINDING ALGORITHMS C B 25/09/2018 D A 67 PATH FINDING ALGORITHMS C B 25/09/2018 D A 68 PATH FINDING ALGORITHMS C B 25/09/2018 D* Algorithm behaves like A* except that the arc costs can change as the algorithm runs 69 PATH FINDING ALGORITHMS 25/09/2018 D* Algorithm • This is the algo that most games use for games when the terrain is not known/visible to the player. • There are three types to this algo viz., original D* (informed incremental search algorithm), Focussed D* (informed incremental heuristic search algorithm) and the D* Lite (Lifelong Planning A* algorithm). 70 PATH FINDING ALGORITHMS 25/09/2018 D* Algorithm – An Example Imagine exploring an unknown planet using a robotic vehicle. The robot moves along the rugged terrain while using a range scanner to make precise measurements of the ground in its vicinity. As the robot moves, it may discover that some parts were easier to traverse than it originally thought. In other cases, it might realize that some direction it was intending to go is impassable due to a large bolder or a ravine. If the goal is to arrive at some specified coordinates, this problem can be viewed as a navigation problem in an unknown environment. Sounds like pure Artificial Intelligence isn’t? 71 PATH FINDING ALGORITHMS 25/09/2018 72 PATH FINDING ALGORITHMS MM.DD.20XX The Automated Cross-Country Unmanned Vehicle (XUV) is equipped with laser radar and other sensors, and uses Stentz's algorithm (D*) to navigate (courtesy of General Dynamics Robotic Systems). 73 PATH FINDING ALGORITHMS 25/09/2018 D* Algorithm Games don’t need lot of accuracy, but speed is a crucial thing. So most of the games use A* algorithm which uses heuristics to speed up the calculations but with accuracy trade off. In most cases A* is more than sufficient. Very well, but what if the heuristic of a path keeps changing? Or there is a enemy roaming around that needs to be avoided. Or the position of the goal state itself changes without you having any knowledge of the situation? This is a problem of dynamic heuristic 74 PATH FINDING ALGORITHMS 25/09/2018 Bye – Bye Nobita! I am not going to help you anymore, you have do it yourself now! 5 6 8 6 75 PATH FINDING ALGORITHMS 25/09/2018 4 5 76 PATH FINDING ALGORITHMS 5 6 7 25/09/2018 3 4 4 5 77 PATH FINDING ALGORITHMS 8 6 7 25/09/2018 a 4 3 3 6 78 PATH FINDING ALGORITHMS 4 5 5 6 25/09/2018 a 79 PATH FINDING ALGORITHMS 8 9 9 10 7 9 8 25/09/2018 a 80 PATH FINDING ALGORITHMS 9 8 10 9 6 8 7 25/09/2018 Bye – Bye Nobita! I am not going to help you anymore, you have do it yourself now! a 4 81 PATH FINDING ALGORITHMS 8 7 9 8 5 7 7 25/09/2018 Bye – Bye Nobita! I am not going to help you anymore, you have do it yourself now! a 3 4 4 82 PATH FINDING ALGORITHMS 7 6 8 7 7 25/09/2018 a 2 3 5 83 PATH FINDING ALGORITHMS 7 6 8 7 7 25/09/2018 a 84 PATH FINDING ALGORITHMS 2 4 5 7 6 8 7 7 25/09/2018 a 85 PATH FINDING ALGORITHMS 1 3 4 5 7 6 8 7 7 25/09/2018 a 2 3 4 86 PATH FINDING ALGORITHMS 5 25/09/2018 a 2 3 4 87 PATH FINDING ALGORITHMS 25/09/2018 D* Algorithm D* and its variants have been widely used for mobile robot and autonomous vehicle navigation. Such navigation systems include a prototype system tested on the Mars rovers Opportunity and Spirit and the navigation system of the winning entry in the DARPA Urban Challenge, both developed at Carnegie Mellon University. 88 PATH FINDING ALGORITHMS 25/09/2018 89 PATH FINDING ALGORITHMS 09.25.2018 THANK YOU! Guess which of these algorithms did Dr Strange use to search for the winning possibility!