Graphs [Autosaved]
Graphs [Autosaved]
Lecturer
Computer Science
A graph is an abstract data type that requires two basic building
blocks: nodes and vertices.
3
Finds the shortest path to each of the other
nodes starting from one of the nodes
4
The graph has the following:
• vertices, or nodes, denoted in the algorithm by v or u;
• weighted edges that connect two nodes: (u,v) denotes an edge, and w(u,v) denotes its weight.
This is done by initializing three values:
• dist, an array of distances from the source node s to each node in the graph, initialized the following way: dist(s) = 0;
and for all other nodes v, dist(v) = ∞.
This is done at the beginning because as the algorithm proceeds, the dist from the source to each node v in the graph will
be recalculated and finalized when the shortest distance to v is found
• Q, a queue of all nodes in the graph. At the end of the algorithm's progress, Q will be empty.
• S, an empty set, to indicate which nodes the algorithm has visited.
At the end of the algorithm's run, S will contain all the nodes of the graph.
5
1. While Q is not empty, pop the node v, that is not already in S, from Q with the smallest dist (v).
In the first run, source node s will be chosen because dist(s) was initialized to 0. In the next run, the next node with the
smallest dist value is chosen.
3. Update dist values of adjacent nodes of the current node v as follows: for each new adjacent node u,
• if dist(v) + weight(u,v) < dist(u), there is a new minimal distance found for u, so update dist(u) to the new minimal distance value;
The algorithm has visited all nodes in the graph and found the smallest distance to each node. dist now contains the shortest
path tree from source s.
Note: The weight of an edge (u,v) is taken from the value associated with (u,v) on the graph. 6
7
8
• The A* search algorithm, builds on the principles of
Dijkstra’s shortest path algorithm to provide a faster
solution when faced with the problem of finding the
shortest path between two nodes.
• It achieves this by introducing a heuristic element to
help decide the next node to consider as it moves along
the path.
• The heuristic function provides an estimate of the
minimum cost between a given node and the target
node.
• The algorithm will combine
• the actual cost from the start node – g(n) 9
• the estimated cost of the target node –h(n)
and use the result to select the next node to evaluate
• Create three lists: the initial list, the open list and the closed list.
• Insert an empty record for each node in the graph into the initial list.
• Store the x and y coordinates of the node in each record of the initial list.
• Initialise the value for distance travelled to zero in each record of the initial list.
• Create a look-up table that includes an entry for each pair of nodes that have a direct connecting road.
• Store the distance of travel along that road for each pair of nodes in the table.
• Identify the target node for the travel.
• Identify the starting node for the travel and copy the record for this node into the open list.
• Delete the record for the starting node from the initial list.
Now recursively apply the following algorithm until all possibilities have been examined:
• For the parent node in the open list identify all adjacent nodes in the initial list.
• Copy the record for each of the adjacent nodes into the open list.
• Delete the records for the adjacent nodes from the initial list.
• Check if the target node is now in the open list. 10
If the target node is not in the open list continue with the following:
• For each adjacent node now added to the open list use the value in the look-up table to
• determine the distance of travel from the parent node.
• If options exist calculate a value for each route and choose the smallest value.
• For each adjacent node update the value stored for distance travelled in the record.
• For each adjacent node update the path sequence stored in the record.
• For each adjacent node use the heuristic function to calculate a value for estimated distance to
• travel.
• For each adjacent node calculate a value for estimated total distance from start node to target
• node by adding the value for distance travelled to the value for estimated distance to travel.
• Identify the adjacent node with the lowest value for total distance from start mode to target
• node. Leave the record for this node in the open list as a parent node. Copy the records for the
• remaining nodes into the closed list. Delete these records from the open list.
• Continue with the next iteration of the recursion.
11
If the target node is in the open list, do the following for this node only:
• Use the value in the look-up table to find the actual distance from the parent node to the target node.
• Calculate the total distance travelled.
• If the record for the target node has zero for the value for distance travelled enter the value now
calculated and enter the path sequence into the record.
• If the record already has a value for distance travelled compare the new value and if this indicates a
shorter route update the value in the record for distance travelled and enter the new path sequence.
• Copy the record for the target node into the initial list.
• Delete the record for the target node from the open list.
• If there is another adjacent node in the open list continue with the actions listed above for when the
target node is not in the open list.
• If the open list is now empty rewind to check nodes in the closed list that have not been included in
previous path sequences. For each of these calculate a value for estimated total distance from start
node to target node. If this estimate is greater than the value stored for distance travelled in the target
mode record continue to rewind. If the estimate is less than this stored value copy the record for the 12
node into the open list as a parent
Two method to calculate the heuristic value i.e h
• Either calculate the exact value of h
❑ Exact Heuristics
❑ Pre compute the distance between each pair of cells before running A* algorithm
❑ If there are no blocked cells then we can just find the exact value of h without any pre-computation using Euclidean formula
❑ Approximation Heuristics
❑ Manhattan Distance
❑ Diagonal Distance
❑ Euclidean Distance
❑ h = sqrt((current_cell.x – goal.x)^2 + (current_cell.y-goal.y)^2)
13
ADD A FOOTER
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. 14
Maecenas porttitor congue massa
August Bergqvist
Phone:
678-555-0128
Email:
[email protected]
Template Editing
Instructions and
Feedback
16