A Star in R
Posted: , Last Updated:
a_star < function(graph, heuristic, start, goal) {
#' Finds the shortest distance between two nodes using the Astar (A*) algorithm
#' @param graph an adjacencymatrixrepresentation of the graph where (x,y) is the weight of the edge or 0 if there is no edge.
#' @param heuristic an estimation of distance from node x to y that is guaranteed to be lower than the actual distance. E.g. straightline distance
#' @param start the node to start from.
#' @param goal the node we're searching for
#' @return The shortest distance to the goal node. Can be easily modified to return the path.
# This contains the distances from the start node to all other nodes, initialized with a distance of "Infinity"
distances = rep(Inf, nrow(graph))
# The distance from the start node to itself is of course 0
distances[start] = 0
# This contains the priorities with which to visit the nodes, calculated using the heuristic.
priorities = rep(Inf, nrow(graph))
# start node has a priority equal to straight line distance to goal. It will be the first to be expanded.
priorities[start] = heuristic[start,goal]
# This contains whether a node was already visited
visited = rep(FALSE, nrow(graph))
# While there are nodes left to visit...
repeat {
# ... find the node with the currently lowest priority...
lowest_priority = Inf
lowest_priority_index = 1
for(i in seq_along(priorities)) {
# ... by going through all nodes that haven't been visited yet
if(priorities[i] < lowest_priority && !visited[i]){
lowest_priority = priorities[i]
lowest_priority_index = i
}
}
if (lowest_priority_index == 1){
# There was no node not yet visited > Node not found
return (1)
} else if (lowest_priority_index == goal){
# Goal node found
print("Goal node found!")
return(distances[lowest_priority_index])
}
cat("Visiting node ", lowest_priority_index, " with currently lowest priority of ", lowest_priority)
# ...then, for all neighboring nodes that haven't been visited yet....
for(i in seq_along(graph[lowest_priority_index,])) {
if(graph[lowest_priority_index,i] != 0 && !visited[i]){
# ...if the path over this edge is shorter...
if(distances[lowest_priority_index] + graph[lowest_priority_index,i] < distances[i]){
# ...save this path as new shortest path
distances[i] = distances[lowest_priority_index] + graph[lowest_priority_index,i]
# ...and set the priority with which we should continue with this node
priorities[i] = distances[i] + heuristic[i,goal]
cat("Updating distance of node ", i, " to ", distances[i], " and priority to ", priorities[i], "\n")
}
# Lastly, note that we are finished with this node.
visited[lowest_priority_index] = TRUE
cat("Visited nodes: ", visited, "\n")
cat("Currently lowest distances: ", distances, "\n")
}
}
}
}
About the algorithm and language used in this code snippet:
A Star Algorithm
The A star (A*) algorithm is an algorithm used to solve the shortest path problem in a graph. This means that given a number of nodes and the edges between them as well as the “length” of the edges (referred to as “weight”) and a heuristic (more on that later), the A* algorithm finds the shortest path from the specified start node to all other nodes. Nodes are sometimes referred to as vertices (plural of vertex)  here, we’ll call them nodes.
Description of the Algorithm
The basic principle behind the A star (A*) algorithm is to iteratively look at the node with the currently smallest priority (which is the shortest distance from the start plus the heuristic to the goal) and update all not yet visited neighbors if the path to it via the current node is shorter. This is very similar to the Dijkstra algorithm, with the difference being that the lowest priority node is visited next, rather than the shortest distance node. In essence, Dijkstra uses the distance as the priority, whereas A* uses the distance plus the heuristic.
Why does adding the heuristic make sense? Without it, the algorithm has no idea if its going in the right direction. When manually searching for the shortest path in this example, you probably prioritised paths going to the right over paths going up or down. This is because the goal node is to the right of the start node, so going right is at least generally the correct direction. The heuristic gives the algorithm this spatial information.
So if a node has the currently shortest distance but is generally going in the wrong direction, whereas Dijkstra would have visited that node next, A Star will not. For this to work, the heuristic needs to be admissible, meaning it has to never overestimate the actual cost (i.e. distance)  which is the case for straight line distance in street networks, for example. Intuitively, that way the algorithm never overlooks a shorter path because the priority will always be lower than the real distance (if the current shortest path is A, then if there is any way path B could be shorter it will be explored). One simple heuristic that fulfils this property is straight line distance (e.g. in a street network)
In more detail, this leads to the following Steps:
 Initialize the distance to the starting node as 0 and the distances to all other nodes as infinite
 Initialize the priority to the starting node as the straightline distance to the goal and the priorities of all other nodes as infinite
 Set all nodes to “unvisited”

While we haven’t visited all nodes and haven’t found the goal node:
 Find the node with currently lowest priority (for the first pass, this will be the source node itself)
 If it’s the goal node, return its distance
 For all nodes next to it that we haven’t visited yet, check if the currently smallest distance to that neighbor is bigger than if we were to go via the current node
 If it is, update the smallest distance of that neighbor to be the distance from the source to the current node plus the distance from the current node to that neighbor, and update its priority to be the distance plus its straightline distance to the goal node
Example of the Algorithm
The steps the algorithm performs on this graph if given node 0 as a starting point and node 5 as the goal, in order, are:
 Visiting node 0 with currently lowest priority of 8.0
 Updating distance of node 1 to 3 and priority to 9.32455532033676
 Updating distance of node 2 to 4 and priority to 10.32455532033676
 Visiting node 1 with currently lowest priority of 9.32455532033676
 Updating distance of node 3 to 9 and priority to 11.82842712474619
 Updating distance of node 4 to 13 and priority to 15.82842712474619
 Visiting node 2 with currently lowest priority of 10.32455532033676
 Visiting node 3 with currently lowest priority of 11.82842712474619
 Updating distance of node 5 to 12 and priority to 12.0
 Goal node found!
Final lowest distance from node 0 to node 5: 12
Runtime of the Algorithm
The runtime complexity of A Star depends on how it is implemented. If a minheap is used to determine the next node to visit, and adjacency is implemented using adjacency lists, the runtime is O(E + VlogV) (V = number of Nodes, E = number of Edges). If a we simply search all distances to find the node with the lowest distance in each step, and use a matrix to look up whether two nodes are adjacent, the runtime complexity increases to O(V^2).
Note that this is the same as Dijkstra  in practice, though, if we choose a good heuristic, many of the paths can be eliminated before they are explored making for a significant time improvement.
More information on how the heuristic influences the complexity can be found on the Wikipedia Article.
Space of the Algorithm
The space complexity of A Star depends on how it is implemented as well and is equal to the runtime complexity, plus whatever space the heuristic requires.
R
R is an interpreted language first released in 1993 with a significant increase in popularity in recent years. It is primarily used for data mining and science as well as statistics, and is a popular language in noncomputer science disciplines ranging from Biology to Physics. R is dynamically typed, and has one of the widest variety of libraries for statistics, machine learning, data mining etc.
Getting to “Hello World” in R
The most important things first  here’s how you can run your first line of code in R.
 Download and install the latest version of R from rproject.org. You can also download an earlier version if your use case requires it.
 Open a terminal, make sure the
R
command is working, and that the command your’re going to be using is referring to the version you just installed by runningR version
. If you’re getting a “command not found” error (or similar), try restarting your command line, and, if that doesn’t help, your computer. If the issue persists, here are some helpful StackOverflow questions for Windows, Mac and Linux.  As soon as that’s working, you can run the following snippet:
print("Hello World")
. You have two options to run this: 3.1 RunR
in the command line, just paste the code snippet and press enter (PressCTRL + D
and typen
followed by enter to exit). 3.2 Save the snippet to a file, name it something ending with.R
, e.g.hello_world.R
, and runRscript hello_world.R
. Tip: use thels
command (dir
in Windows) to figure out which files are in the folder your command line is currently in.
That’s it! Notice how printing something to the console is just a single line in R  this low entry barrier and lack of required boilerplate code is a big part of the appeal of R.
Fundamentals in R
To understand algorithms and technologies implemented in R, one first needs to understand what basic programming concepts look like in this particular language.
Variables and Arithmetic
Variables in R are really simple, no need to declare a datatype or even declare that you’re defining a variable; R knows this implicitly. R is also able to easily define objects and their property, in multiple different ways.
some_value = 10
my_object < list(my_value = 4)
attr(my_object, 'other_value') < 3
print((some_value + my_object$my_value + attr(my_object, 'other_value'))) # Prints 17
Arrays
Working with arrays is similarly simple in R:
# Create 2 vectors of length 3
vector1 < c(1,2,3)
vector2 < c(4,5,6)
# Create names for rows and columns (optional)
column.names < c("column_1","column_2","column_3")
row.names < c("row_1","row_2")
# Concatenate the vectors (as rows) to form an array, providing dimensions and row/column names
result < array(c(vector1,vector2), dim = c(2,3), dimnames = list(row.names, column.names))
print(result)
# Prints:
# column_1 column_2 column_3
# row_1 1 3 5
# row_2 2 4 6
As those of you familiar with other programming language like Java might have already noticed, those are not native arrays, but rather lists dressed like arrays. This means that arrays in R are considerably slower than in lower level programming languages. This is a trade off R makes in favor of simplicity. There are, however, packages which implement real arrays that are considerably faster.
Conditions
Just like most programming languages, R can do ifelse
statements:
value = 1
if(value==1){
print("Value is 1")
} else if(value==2){
print("Value is 2")
} else {
print("Value is something else")
}
R can also do switch statements, although they are implemented as a function, unlike in other languages like Java:
x < switch(
1,
"Value is 1",
"Value is 2",
"Value is 3"
)
print(x)
Note that this function is pretty useless, but there are other functions for more complex use cases.
Loops
R supports both for
and while
loops as well as break
and next
statements (comparable to continue
in other languages).
Additionally, R supports repeat
loops, which are comparable to while(true)
loops in other languages, but simplify the code a little bit.
value < 0
repeat {
value < value + 1
if(value > 10) {
break
}
}
print(value)
value < 0
while (value <= 10) {
value = value + 1
}
print(value)
value < c("Hello","World","!")
for ( i in value) {
print(i)
}
for(i in 1:10){
print(i)
}
Functions
Functions in R are easily defined and, for better or worse, do not require specifying return or arguments types. Optionally, a default for arguments can be specified:
my_func < function (
a = "World"
) {
print(a)
return("!")
}
my_func("Hello")
print(my_func())
(This will print “Hello”, “World”, and then ”!“)
Syntax
R requires the use of curly brackets ({}
) to surround code blocks in conditions, loops, functions etc.;
While this can lead to some annoying syntax errors, it also means the use of whitespace for preferred formatting (e.g. indentation of code pieces) does not affect the code.
Advanced Knowledge of R
For more information, R has a great Wikipedia article. The official website is rproject.org.