Sunteți pe pagina 1din 9

A*Algorithem and why it is better then DFS.

Situations A* Algorithem: Before you understand A*, you must first understand Dijkstra's algorithm. Given a graph (a set of nodes, and edges between nodes) and the (positive) "distance" of each edge (e.g. the road distance), Dijkstra's algorithm gives the shortest distance between a certain source node and each destination node. For instance, in your graph, the nodes may be road-intersections, the edges may be the roads, and the weight/distance you put on an edge may be the length of that road, or the time it takes to traverse it, or whatever. Dijkstra's algorithm always gives the correct distance according to the weights you have put on the edges. In fact, the graph need not even be embeddable in a plane, i.e., there may be no notion of "straight line distance" in the first place. It can be any arbitrary graph. Now, A* can be thought of as a particular heuristic to speed up Dijkstra's algorithm. You can think of it as using a heuristic to decide the order in which to consider nodes in the graph. The A-Star algorithm is a best-first search algorithm that finds the least costly path from an initial configuration to a final configuration. It uses an exact estimate cost heuristic function. The exact cost is known for any previous steps, while the estimated cost to reach the final configuration from the current can be estimated. The cost function must be admissible, i.e. the estimated cost must be less than the actual cost, this produces computationally optimal results. The most essential part of the A-Star algorithm is a good heuristic estimate function. This can improve the efficiency and performance of the algorithm, and depends on the specific state space being explored. Generally, the A-Star algorithm maintains two lists, an open list and a closed list. The open list is a priority queue of states, where we can pick out the next least costly

state to evaluate. Initially, the open list contains the starting state. When we iterate once, we take the top of the priority queue, and then initially, check whether it is the goal state. If so, we are done. Otherwise, we calculate all adjacent states and their associated costs, and add them into the open queue. See figure 3 on page 12 for an example. A-Star is complete. It will find a solution if a solution exists. If it doesnt find a solution, then we can guarantee that no such solution exists. A-Star will find a path with the lowest possible cost. This will depend heavily upon the Quality of the cost function, and estimates provided.

Solved Example of A* Algorithem

Step 1: We start from Lugoi, and we have 2 options.

Lugoi

Timisoara

Mehadia

440 = 111 + 329

311 = 70 + 241

Step 2: Here we will go to Mehadia, because of its cost. Lugoi

Timisoara

Mehadia

311 = 70 + 241

440 = 111 + 329

Lugoi

Dobreta

384 = 140 + 244

387 = 145 + 242

Step 3: Here we go to Lugoi but that is a wrong choice, and we go back to Mehadia and then we go to Dobreta.

Lugoi

Timisoara

Mehadia 311 = 70 + 241

440 = 111 + 329

Lugoi 384 = 140 + 244

Dobreta 387 = 145 + 242

Mehadia

Timisoara Craiova Mehadia

451 = 210 + 241

580 = 251 + 329 425 = 265 + 160 461 = 220 + 241

Step 4: Here we go to Craiova from Dobreta.

Lugoi

Timisoara

Mehadia

311 = 70 + 241

440 = 111 + 329

Lugoi

Dobreta

Mehadia

Timisoara Mehadia

Craiova 451 = 210 + 241 580 = 251 + 329

461 = 220 + 241 Dobreta Pitesti 627 = 385 + 242 503 = 403 + 100 Riminica Vilcea 604= 411 + 193

Step 5: Here we go to Pitesti from Craiova and we reach to our goal Buchaset with minimum cost.

Lugoi

Timisoara

Mehadia

440 = 111 + 329

Lugoi

Dobreta

Mehadia

Timisoara Mehadia

Craiova 451 = 210 + 241 580 = 251 + 329

461 = 220 + 241 Dobreta Pitesti 627 = 385 + 242 Riminica Vilcea 604= 411 + 193 Craiova

Buchaset 701 = 541 + 160

Riminica Vilcea

504 = 504 + 0

693= 500 + 193

Step 5: Here we go to Pitesti from Craiova and we reach to our goal Buchaset.

Ans1: The route with the minimum cost would be: Lugoi to Mehadia to Dobreta to Craiova to Pitesti to 70 + 75 + 120 + 138 + 101 = 504 Buchaset.

Why A* is Better: The A* is an informed search which based on the BFS algorithm so it visits the every nodes once. There is no need to visit the nodes and then retrieve to the base path for revisiting of the nodes. But on the other hand in DFS is an uninformed search which does not keep record of the node visited as well as it visit the nodes depth and the retrieve the node till it find the next path which makes it costly algorithm. A* is far better algorithm in performance in many cases since it is informed search as well as it does not visit any nodes twice but in the specific cases where we can find the element at the depth of the graph DFS is better since it searches is depth, but in most generic cases A* algorithm is better. The reason for A* search algorithm performs better than DFS given the same start and goal state configurations is that A* search algorithms uses BFS, and heuristics that keeps track of the cost of each node and expands the nodes which has less cost, where as Depth first search is a uniformed search that does not use any heuristics therefore does not know which path would be costly, so it starts from the root node and goes deep into the branch of the tree while traversing each node until there is no further node, before it starts backtracking meaning going backwards to where it came from. DFS keeps record of the visited nodes so that it does not get in to a loop. But, it depends on the system requirements since there are systems that finds the data in depth end of the node but if talk about the generic solution for the search a node from graph we can say that A* algorithm is a better solution.

Choice of Algorithm Dependence onSituation: A* is better then DFS and some other Algorithms but no for all situation. There are situations in which A* is not the best algorithm to solve a problem. However, there are a number of ways to assess what comprises the best algorithm for finding a solution. If you are considering best in terms of performance of multiple searches from a single source to many destinations, then Dijkstra's algorithm is more suitable approach. If you are considering best in terms of performance, then in some special cases DFS and BFS will significantly outperform A*. From past experience, this occurs for very small, almost trivial graphs, but would require careful testing and profiling to make a stronger statement. If you are considering best in terms of path-length, that is how long the final path produced by the algorithm is, then A* is equivalent to any other optimal search algorithm. If you are considering best in terms of completeness, that is, will the algorithm always find a path to the goal if such a path exists. If so, then A* is equivalent to any other complete algorithm (for example, breadth-first-search). If you are considering best in cases where some of the weights in the graph are negative, then you will need to use a special algorithm to solve those problems (for example bellman-ford) If you are considering best in cases where the no heuristic is available then you must fall back onh(x,y)=0 forall states x and y. In this case A* is equivalent to best first search. If you are considering best in cases related to motion planning in continuous configuration spaces, then A* may work adequately in low dimensions, but storage of the search graph starts to become impractical at high dimensions, and the need to use probabilistically complete algorithms increases (for example RRT, Bi-RRT, RDT) If you are considering best in cases where the graph is partially observable, you only know a subset of all the possible vertices and edges within the graph at any time, and you need to change

state to observe more of the graph, then you need an alternative algorithm designed for this (for example, Keonig's Lifelong Planning A*, LPA*, does exactly this). If you are considering best in cases where the graph changes over time, which occurs frequently in robotics when you incorporate moving obstacles, then you need an algorithm which is designed for this (for example Stentz's D* or Koenig & Likhachev's D*-Lite).

Reference: http://www.slideshare.net/ahmad1957/straight-line-distance-heuristic-presentation http://upe.acm.jhu.edu/websites/Benny_Tsai/Introduction%20to%20AStar.htm Artificial Intelligence By Peter Norvig Second Edition.

S-ar putea să vă placă și