Beruflich Dokumente
Kultur Dokumente
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.
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.
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.
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