Sie sind auf Seite 1von 26

Graphs

-A* Path finding algorithm-

Before we start we need to know..

What is a GRAPH?

A graph is a common data structure consisting of a set of nodes (which contains data) and edges (which connects the nodes to each other).

What is a
weighted graph?

A weighted graph is a graph for which each edge has an associated weight usually weight, given by a weight function f: E p R.

What does path finding do?

Path finding allows us to determine if and how we can reach one node from another. Additionally we can consider not only if two nodes are connected but also assign some form of cost(weight) to travel between them. We can then search for not only the shortest path but also the cheapest, fastest, or safest path.

Introduction to A*Path-finding
The A*(pronounced A-star) algorithm seems very complicated at first view. We can say that A* search for a sub-graph ( a minimum spanning tree). Uses for A*: 1. Shortest path 2. Flood fill 3. Decision making

Shortest path
A* is an algorithm which search for the shortest or most efficient path between two nodes. Example: A board game may need to know if a piece can reach some tile and how many moves it would require to get there.

Flood Fill
If we ask a path finding algorithm to search for a path to an unreachable destination we won't get a result but we still get useful information. The set of nodes the algorithm explored trying to find a path gives us all the nodes that are reachable from our starting location. Example: If our graph represents a map we can use this to identify if two land masses are connected or find all the locations which are part of a lake.

Decision making
Our graph does not need to represent a set of physical locations. locations. Example: Instead suppose each node represents some form of technology in our game's tech tree.

Properties of A* algorithm
The A* algorithm has three important properties:
 It will always return the least expensive path if a path exists to the destination, other algorithms may find a path faster but it is not necessarily the best path we can take.  A* uses a heuristic (a "guess") to search nodes considered more likely to lead to the destination first, allowing us to often find the best path without having to search the entire map and making the algorithm much faster. faster.  A* is based on the idea that each node has some cost associated with it. If the costs for all nodes are the same then the best path returned by A* will also be the shortest path but A* can easily allow us to add different costs to moving through each node.

The Search Area


Let`s assume that we have someone who wants to get from point A to point B and a wall separates the two points.

The Search Area


The first step you should divide our search area into a square grid, into a two dimensional array.(simplifies the search area). Each element in the array represents one of the squares on the grid, and its status is recorded as accessible or not. In our example, once the path is found, our person moves from the center of one square to the center of the next until the target is reached.

The Search Area


Once we have simplified our search area into a manageable number of nodes, as we have done with the grid layout above, the next step is to conduct a search to find the shortest path.

The Search Area


We do this by starting at point A, checking the adjacent squares, and generally searching the closest nodes until we find squares our target. For every point we apply a method which verify in 4-way or 8-way if any of the adjacent node: is the ending point; is accessible or not; is visited already or not;

Path scoring
The key to determine which squares to use when figuring out the path is the following equation: F = G + H . G = the movement cost to move from the starting point A to a given square on the grid, following the path generated to get there. F = the estimated movement cost to move from that given square on the grid to the final destination, point B.

The G
As described above, G is the movement cost to move from the starting point to the given square using the path generated to get there. In this example, we will assign a cost of 10 to each horizontal or vertical square moved, and a cost of 14 for a diagonal move. We use these numbers because the actual distance to move diagonally is the square root of 2 (sqrt(2) = 1.414) times 10. We use 10 and 14 for simplicity s sake. The ratio is about right, and we avoid having to calculate square roots and we avoid decimals. This isn t just because we are dumb and don t like math. Using whole numbers like these is a lot faster for the computer, too. As you will soon find out, path finding can be very slow if you don t use shortcuts like these. Since we are calculating the G cost along a specific path to a given square, the way to figure out the G cost of that square is to take the G cost of its parent, and then add 10 or 14 depending on whether it is diagonal or orthogonal (non-diagonal) from that parent square. The need for this method will become apparent a little further on in this example, as we get more than one square away from the starting square.

.. and the H
H can be estimated in a variety of ways. The method we use here is called the Manhattan method, where you calculate the total number of squares method moved horizontally and vertically to reach the target square from the current square, ignoring diagonal movement, and ignoring any obstacles that may be in the way. We then multiply the total by 10, our cost for moving one square horizontally or vertically. This is (probably) called the Manhattan method because it is like calculating the number of city blocks from one place to another, where you can t cut across the block diagonally.

Let`s analyze the next picture:

The method for searching area


1. Begin at the starting point A and add it to an open list of squares to be considered. The open list is kind of like a shopping list. Right now there is just one item on the list, but we will have more later. It contains squares that might fall along the path you want to take, but maybe not. Basically, this is a list of squares that need to be checked out. 2. Look at all the reachable squares adjacent to the starting point, ignoring squares with walls. Add them to the open list, too. For each of these squares, save point A as its parent square . This parent square stuff is important when we want to trace our path. It will be explained more later. 3. Drop the starting square A from your open list, and add it to a closed list of squares that you don t need to look at again for now. 4. Next, we choose one of the adjacent squares with the lowest path scoring ( F ) from the open list and apply recursion .

Summary-Steps of A* algorithm
1. Add the starting square to the open list 2. The recursive method 3. Save the path

Pseudo-code A*
create the open list of nodes, initially containing only our starting node create the closed list of nodes, initially empty while (we have not reached our goal) { consider the best node in the open list (the node with the lowest f value) if (this node is the goal) { then we're done } else { move the current node to the closed list and consider all of its neighbors for each( neighbor) { if (this neighbor is in the closed list and our current g value is lower) { update the neighbor with the new, lower, g value change the neighbor's parent to our current node } else if (this neighbor is in the open list and our current g value is lower) { update the neighbor with the new, lower, g value change the neighbor's parent to our current node } else if(this neighbor is not in either the open or closed list ) { add the neighbor to the open list and set its g value } } } }

Finally

THE END

Das könnte Ihnen auch gefallen