BreadthFirst Search in R
Posted: , Last Updated:
bfs < function(graph, start){
#' Implementation of BreadthFirstSearch (BFS) using adjacency matrix.
#' This returns nothing (yet), it is meant to be a template for whatever you want to do with it,
#' e.g. finding the shortest path in a unweighted graph.
#' This has a runtime of O(V^2) (V = number of Nodes), for a faster implementation see @see ../fast/BFS.java (using adjacency Lists)
#' @param graph an adjacencymatrixrepresentation of the graph where (x,y) is TRUE if the the there is an edge between nodes x and y.
#' @param start the node to start from.
#' @return Array array containing the shortest distances from the given start node to each other node
# A Queue to manage the nodes that have yet to be visited, intialized with the start node
queue = c(start)
# A boolean array indicating whether we have already visited a node
visited = rep(FALSE, nrow(graph))
# (The start node is already visited)
visited[start] = TRUE
# Keeping the distances (might not be necessary depending on your use case)
distances = rep(Inf, nrow(graph)) # Technically no need to set initial values since every node is visted exactly once
# (the distance to the start node is 0)
distances[start] = 0
# While there are nodes left to visit...
while(length(queue) > 0) {
cat("Visited nodes: ", visited, "\n")
cat("Distances: ", distances, "\n")
node = queue[1] # get...
queue = queue[1] # ...and remove next node
cat("Removing node ", node, " from the queue...", "\n")
# ...for all neighboring nodes that haven't been visited yet....
for(i in seq_along(graph[node,])) {
if(graph[node,i] && !visited[i]){
# Do whatever you want to do with the node here.
# Visit it, set the distance and add it to the queue
visited[i] = TRUE
distances[i] = distances[node] + 1
queue = c(queue, i)
cat("Visiting node ", i, ", setting its distance to ", distances[i], " and adding it to the queue", "\n")
}
}
}
cat("No more nodes in the queue. Distances: ", distances, "\n")
return (distances)
}
About the algorithm and language used in this code snippet:
BreadthFirst Search Algorithm
The Breadthfirst search algorithm is an algorithm used to solve the shortest path problem in a graph without edge weights (i.e. a graph where all nodes are the same “distance” from each other, and they are either connected or not). This means that given a number of nodes and the edges between them, the Breadthfirst search algorithm is 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 Breadthfirst search algorithm is to take the current node (the start node in the beginning) and then add all of its neighbors that we haven’t visited yet to a queue. Continue this with the next node in the queue (in a queue that is the “oldest” node). Before we add a node to the queue, we set its distance to the distance of the current node plus 1 (since all edges are weighted equally), with the distance to the start node being 0. This is repeated until there are no more nodes in the queue (all nodes are visited).
In more detail, this leads to the following Steps:
 Initialize the distance to the starting node as 0. The distances to all other node do not need to be initialized since every node is visited exactly once.
 Set all nodes to “unvisited”
 Add the first node to the queue and label it visited.

While there are nodes in the queue:
 Take a node out of the queue
 For all nodes next to it that we haven’t visited yet, add them to the queue, set their distance to the distance to the current node plus 1, and set them as “visited”
In the end, the distances to all nodes will be correct.
Example of the Algorithm
Consider the following graph:
The steps the algorithm performs on this graph if given node 0 as a starting point, in order, are:
 Visiting node 0

Visited nodes: [true, false, false, false, false, false], Distances: [0, 0, 0, 0, 0, 0]
 Removing node 0 from the queue…
 Visiting node 1, setting its distance to 1 and adding it to the queue
 Visiting node 2, setting its distance to 1 and adding it to the queue

Visited nodes: [true, true, true, false, false, false], Distances: [0, 1, 1, 0, 0, 0]
 Removing node 1 from the queue…
 Visiting node 3, setting its distance to 2 and adding it to the queue
 Visiting node 4, setting its distance to 2 and adding it to the queue

Visited nodes: [true, true, true, true, true, false], Distances: [0, 1, 1, 2, 2, 0]
 Removing node 2 from the queue…
 No adjacent, unvisited nodes

Visited nodes: [true, true, true, true, true, false], Distances: [0, 1, 1, 2, 2, 0]
 Removing node 3 from the queue…
 Visiting node 5, setting its distance to 3 and adding it to the queue

Visited nodes: [true, true, true, true, true, true], Distances: [0, 1, 1, 2, 2, 3]
 Removing node 4 from the queue…
 No adjacent, unvisited nodes

Visited nodes: [true, true, true, true, true, true], Distances: [0, 1, 1, 2, 2, 3]
 Removing node 5 from the queue…
 No more nodes in the queue. Final distances: [0, 1, 1, 2, 2, 3]
Runtime Complexity of the Algorithm
The runtime complexity of Breadthfirst search is O(E + V) (V = number of Nodes, E = number of Edges) if adjacencylists are used. If a we simply search all nodes to find connected nodes in each step, and use a matrix to look up whether two nodes are adjacent, the runtime complexity increases to O(V^2).
Depending on the graph this might not matter, since the number of edges can be as big as V^2 if all nodes are connected with each other.
Space Complexity of the Algorithm
The space complexity of Breadthfirst search depends on how it is implemented as well and is equal to the runtime complexity.
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.