Iterative Deepening DFS in Java

Posted: , Last Updated:


 * Used to perform the Iterative Deepening Depth-First Search (DFS) Algorithm to find the shortest path from a start to a target node.
public class IterativeDeepeningDFS {
    private static boolean bottomReached = false; // Variable to keep track if we have reached the bottom of the tree

     * Implementation of iterative deepening DFS (depth-first search).
     * Given a start node, this returns the node in the tree below the start node with the target value (or null if it doesn't exist)
     * Runs in O(n), where n is the number of nodes in the tree, or O(b^d), where b is the branching factor and d is the depth.
     * @param start  the node to start the search from
     * @param target the value to search for
     * @return The node containing the target value or null if it doesn't exist.
    public static Node iterativeDeepeningDFS(Node start, int target) {
        // Start by doing DFS with a depth of 1, keep doubling depth until we reach the "bottom" of the tree or find the node we're searching for
        int depth = 1;
        while (!bottomReached) {
            bottomReached = true; // One of the "end nodes" of the search with this depth has to still have children and set this to false again
            Node result = iterativeDeepeningDFS(start, target, 0, depth);
            if (result != null) {
                // We've found the goal node while doing DFS with this max depth
                return result;

            // We haven't found the goal node, but there are still deeper nodes to search through
            depth *= 2;
            System.out.println("Increasing depth to " + depth);

        // Bottom reached is true.
        // We haven't found the node and there were no more nodes that still have children to explore at a higher depth.
        return null;

    private static Node iterativeDeepeningDFS(Node node, int target, int currentDepth, int maxDepth) {
        System.out.println("Visiting Node " + node.value);
        if (node.value == target) {
            // We have found the goal node we we're searching for
            System.out.println("Found the node we're looking for!");
            return node;

        if (currentDepth == maxDepth) {
            System.out.println("Current maximum depth reached, returning...");
            // We have reached the end for this depth...
            if (node.children.length > 0) {
                //...but we have not yet reached the bottom of the tree
                bottomReached = false;
            return null;

        // Recurse with all children
        for (int i = 0; i < node.children.length; i++) {
            Node result = iterativeDeepeningDFS(node.children[i], target, currentDepth + 1, maxDepth);
            if (result != null) {
                // We've found the goal node while going down that child
                return result;

        // We've gone through all children and not found the goal node
        return null;

// used to store a tree datastructure
class Node {
    Node[] children;
    int value;

About the algorithm and language used in this code snippet:

Iterative Deepening Depth-First Search Algorithm

The Iterative Deepening Depth-First Search (also ID-DFS) algorithm is an algorithm used to find a node in a tree. This means that given a tree data structure, the algorithm will return the first node in this tree that matches the specified condition. Nodes are sometimes referred to as vertices (plural of vertex) - here, we’ll call them nodes. The edges have to be unweighted. This algorithm can also work with unweighted graphs if mechanism to keep track of already visited nodes is added.

Description of the Algorithm

The basic principle of the algorithm is to start with a start node, and then look at the first child of this node. It then looks at the first child of that node (grandchild of the start node) and so on, until a node has no more children (we’ve reached a leaf node). It then goes up one level, and looks at the next child. If there are no more children, it goes up one more level, and so on, until it find more children or reaches the start node. If hasn’t found the goal node after returning from the last child of the start node, the goal node cannot be found, since by then all nodes have been traversed.

So far this has been describing Depth-First Search (DFS). Iterative deepening adds to this, that the algorithm not only returns one layer up the tree when the node has no more children to visit, but also when a previously specified maximum depth has been reached. Also, if we return to the start node, we increase the maximum depth and start the search all over, until we’ve visited all leaf nodes (bottom nodes) and increasing the maximum depth won’t lead to us visiting more nodes.

Specifically, these are the steps:

  1. For each child of the current node
  2. If it is the target node, return
  3. If the current maximum depth is reached, return
  4. Set the current node to this node and go back to 1.
  5. After having gone through all children, go to the next child of the parent (the next sibling)
  6. After having gone through all children of the start node, increase the maximum depth and go back to 1.
  7. If we have reached all leaf (bottom) nodes, the goal node doesn’t exist.

Example of the Algorithm

Consider the following tree:

Tree for the Iterative Deepening Depth-First Search algorithm

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

  1. Visiting Node 0
  2. Visiting Node 1
  3. Current maximum depth reached, returning…
  4. Visiting Node 2
  5. Current maximum depth reached, returning…
  6. Increasing depth to 2
  7. Visiting Node 0
  8. Visiting Node 1
  9. Visiting Node 3
  10. Current maximum depth reached, returning…
  11. Visiting Node 4
  12. Current maximum depth reached, returning…
  13. Visiting Node 2
  14. Visiting Node 5
  15. Current maximum depth reached, returning…
  16. Visiting Node 6
  17. Found the node we’re looking for, returning…

Runtime of the Algorithm

If we double the maximum depth each time we need to go deeper, the runtime complexity of Iterative Deepening Depth-First Search (ID-DFS) is the same as regular Depth-First Search (DFS), since all previous depths added up will have the same runtime as the current depth (1/2 + 1/4 + 1/8 + … < 1). The runtime of regular Depth-First Search (DFS) is O(|N|) (|N| = number of Nodes in the tree), since every node is traversed at most once. The number of nodes is equal to b^d, where b is the branching factor and d is the depth, so the runtime can be rewritten as O(b^d).

Space of the Algorithm

The space complexity of Iterative Deepening Depth-First Search (ID-DFS) is the same as regular Depth-First Search (DFS), which is, if we exclude the tree itself, O(d), with d being the depth, which is also the size of the call stack at maximum depth. If we include the tree, the space complexity is the same as the runtime complexity, as each node needs to be saved.


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 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

    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 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 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"


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
        System.out.println("Value is something else");

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


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++) {
while (value > 0) {
do {
} while (value > 0);

This will print the following to the terminal:


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 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.


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.