Breadth-First Search in Java

Posted: , Last Updated:

package bfs.java.simple;

import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Queue;

/**
 * Used to perform Breadth-First-Search (BFS) using adjacency matrices.
 * For a faster implementation, see @see ../fast/BFS.java (using adjacency Lists)
 */
public class BFS {
    /**
     * Implementation of Breadth-First-Search 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 adjacency-matrix-representation 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 an array containing the shortest distances from the given start node to each other node
     */
    public static int[] bfs(boolean[][] graph, int start) {
        //A Queue to manage the nodes that have yet to be visited
        Queue<Integer> queue = new PriorityQueue<>();
        //Adding the node to start from
        queue.add(start);
        //A boolean array indicating whether we have already visited a node
        boolean[] visited = new boolean[graph.length];
        //(The start node is already visited)
        visited[start] = true;
        // Keeping the distances (might not be necessary depending on your use case)
        int[] distances = new int[graph.length]; // No need to set initial values since every node is visted exactly once
        //While there are nodes left to visit...
        while (!queue.isEmpty()) {
            System.out.println("Visited nodes: " + Arrays.toString(visited));
            System.out.println("Distances: " + Arrays.toString(distances));
            int node = queue.remove();
            System.out.println("Removing node " + node + " from the queue...");
            //...for all neighboring nodes that haven't been visited yet....
            for (int i = 1; i < graph[node].length; i++) {
                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.add(i);
                    System.out.println("Visiting node " + i + ", setting its distance to " + distances[i] + " and adding it to the queue");

                }
            }
        }
        System.out.println("No more nodes in the queue. Distances: " + Arrays.toString(distances));
        return distances;
    }
}

About the algorithm and language used in this code snippet:

Breadth-First Search Algorithm

The Breadth-first 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 Breadth-first 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 Breadth-first 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:

  1. 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.
  2. Set all nodes to “unvisited”
  3. Add the first node to the queue and label it visited.
  4. While there are nodes in the queue:

    1. Take a node out of the queue
    2. 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:

Graph for breadth first search

The steps the algorithm performs on this graph if given node 0 as a starting point, in order, are:

  1. Visiting node 0
  2. Visited nodes: [true, false, false, false, false, false], Distances: [0, 0, 0, 0, 0, 0]

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

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

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

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

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

    1. Removing node 5 from the queue…
  8. No more nodes in the queue. Final distances: [0, 1, 1, 2, 2, 3]

Runtime Complexity of the Algorithm

The runtime complexity of Breadth-first search is O(|E| + |V|) (|V| = number of Nodes, |E| = number of Edges) if adjacency-lists 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 Breadth-first search depends on how it is implemented as well and is equal to the runtime complexity.

Java

The Java Logo

Java™ is a compiled language used for many purposes, ranging from embedded systems, UI-applications to web servers.

Getting to “Hello World” in Java

The most important things first - here’s how you can run your first line of code in Java.

  1. Download and install the latest version of Java from java.com. You can also download an earlier version if your use case requires it.
  2. Open a terminal, make sure the javac and javac commands are working, and that the command your’re going to be using is referring to the version you just installed by running java -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 each platform:

  3. As soon as that’s working, copy the following snippet into a file named HelloWorld.java:

    class HelloWorld {
    public static void main(String[] args) {
        // Paste any following code snippets here.
        System.out.println("Hello World");
    }
    }
  4. Change directory by typing cd path/to/HelloWorld, then run javac HelloWorld.java to compile the file (which creates the bytecode), then run java HelloWorld (without the .java ending). This should print “Hello World” to your Terminal.

That’s it! Notice that the entry barrier is a little higher with Java than it is with e.g. Python - but Java is much faster and, in my experience, tends to have fewer bugs in large projects due to strong typing and other factors.

Fundamentals in Java

To understand algorithms and technologies implemented in Java, one first needs to understand what basic programming concepts look like in this particular language. Each of the following snippets should be surrounded by the boilerplate code of the hello world example and should be compiled and run using the commands mentioned above.

Variables and Arithmetic

Variables in Java are statically typed, meaning the content of a variable needs to be specified when writing the code. The datatype for whole numbers, for example is int. Numbers with decimal places are typed float or double depending on the required precision. The type for text ist String.

int number = 5;
double decimalNumber = 3.25;
double result = number * decimalNumber;
String callout = "The number is ";
// In this instance, the values are concatenated rather than added because one of them is a String.
System.out.println(callout + result);

Arrays

Arrays in Java are real arrays (as opposed to e.g. Python where they’re implemented as lists). The implications of that are that the size needs to be set when they are created and cannot be changed, but also that they are more efficient in Java than they are in Python.

int[] integers = new int[5];
integers[3] = 12; // Assigning values to positions in the array
// integers[4] is 0, integers[6] would give IndexOutOfBoundsException
String[] strings = {"Hello", "World"}; // Array initialization with initial values
System.out.println(strings[0] + integers[3]); // Prints "Hello12"

Conditions

Just like most programming languages, Java can do if-else statements. Additionally, Java can also do switch-case statements.

int value = 5;
if(value == 5){
    System.out.println("Value is 5");
} else if(value < 5){
    System.out.println("Value is less than 5");
} else {
    System.out.println("Value is something else");
}

switch (value){
    case 1:
        System.out.println("Value is 1");
        break; // Don't go further down the cases
    case 2:
        System.out.println("Value is 2");
        break; // Don't go further down the cases
    case 3:
        System.out.println("Value is 3");
        break; // Don't go further down the cases
    case 4:
        System.out.println("Value is 4");
        break; // Don't go further down the cases
    case 5:
        System.out.println("Value is 5");
        break; // Don't go further down the cases
    default:
        System.out.println("Value is something else");
}

The above Java code will print “Value is 5” twice.

Loops

Java supports for, while as well as do while loops. break and continue statements are also supported. The below example illustrates the differences:

int value = 2;
for (int i = 0; i < value; i++) {
    System.out.println(i);
}
while (value > 0) {
    System.out.println(value);
    value--;
}
do {
    System.out.println(value);
    value--;
} while (value > 0);

This will print the following to the terminal:

0
1
2
1
0

Note the last 0: it is printed because in the do-while-loop, compared to the while-loop. the code block is executed at least once before the condition is checked.

Functions

Functions in Java can be part of a class, or of an object of a class. For more information on object oriented programming I recommend the w3schools course. Here is a minimal example of a function as part of a class (also called a static function):

class HelloWorld {
    public static void main(String[] args) {
        System.out.println(addNumbers(3, 4));
    }

    public static int addNumbers(int numberOne, int numberTwo) {
        return numberOne + numberTwo;
    }
}

And here’s an example of calling a function of an object of a class:

class HelloWorld {
    public static void main(String[] args) {
        System.out.println(new HelloWorld().addNumbers(3, 4));
    }

    public int addNumbers(int numberOne, int numberTwo) {
        return numberOne + numberTwo;
    }
}

Note how the first example uses the static keyword, and the second example needs to instantiate on object of the class before in can call the function of that object. These are some of the differences in class methods and object functions.

Syntax

Java requires the use of curly brackets ({}) to surround code blocks in conditions, loops, functions etc.; It also requires semicolons at then end of statements. 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 Java

Java was first released in 1995 and is multi-paradigm, meaning while it is primarily object-oriented, it also has functional and reflective elements. It’s statically typed, but offers some amount of dynamic typing in recent versions. For more information, Java has a great Wikipedia) article.