# Iterative Deepening A Star in Java

Posted: , Last Updated:

``````package iterative_deepening_a_star.java.simple;

/**
* Used to perform the Iterative Deepening A* Algorithm to find the shortest path from a start to a target node.
*/
public class IterativeDeepeningAStar {
/**
* Performs iterative deepening A Star (A*).
* Can be modified to handle graphs by keeping track of already visited nodes.
*
* @param tree      An adjacency-matrix-representation of the tree 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. straight-line 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.
*/
public static double iterativeDeepeningAStar(int[][] tree, double[][] heuristic, int start, int goal) {
double threshold = heuristic[start][goal];
while (true) {
System.out.printf("Iteration with threshold: %.2f\n", threshold);
double distance = iterativeDeepeningAStar(tree, heuristic, start, goal, 0, threshold);
if (distance == Double.MAX_VALUE) {
// Node not found and no more nodes to visit
return -1;
} else if (distance < 0) {
// if we found the node, the function returns the negative distance
System.out.println("Found the node we're looking for!");
return -distance;
} else {
// if it hasn't found the node, it returns the (positive) next-bigger threshold
threshold = distance;
}
}
}

/**
* Performs DFS up to a depth where a threshold is reached (as opposed to interative-deepening DFS which stops at a fixed depth).
* Can be modified to handle graphs by keeping track of already visited nodes.
*
* @param tree      An adjacency-matrix-representation of the tree 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. straight-line distance.
* @param node      The node to continue from.
* @param goal      The node we're searching for.
* @param distance  Distance from start node to current node.
* @param threshold Until which distance to search in this iteration.
* @return The shortest distance to the goal node. Can be easily modified to return the path.
*/
private static double iterativeDeepeningAStar(int[][] tree, double[][] heuristic, int node, int goal, double distance, double threshold) {
System.out.println("Visiting Node " + node);

if (node == goal) {
// We have found the goal node we we're searching for
return -distance;
}

double estimate = distance + heuristic[node][goal];
if (estimate > threshold) {
System.out.printf("Breached threshold with heuristic: %.2f\n", estimate);
return estimate;
}

//...then, for all neighboring nodes....
double min = Double.MAX_VALUE;
for (int i = 0; i < tree[node].length; i++) {
if (tree[node][i] != 0) {
double t = iterativeDeepeningAStar(tree, heuristic, i, goal, distance + tree[node][i], threshold);
if (t < 0) {
// Node found
return t;
} else if (t < min) {
min = t;
}
}
}
return min;
}
}``````

# Iterative Deepening A Star Algorithm

The Iterative Deepening A Star (IDA*) algorithm is an algorithm used to solve the shortest path problem in a tree, but can be modified to handle graphs (i.e. cycles). It builds on Iterative Deepening Depth-First Search (ID-DFS) by adding an heuristic to explore only relevant nodes.

## Description of the Algorithm

Whereas Iterative Deepening DFS uses simple depth to decide when to abort the current iteration and continue with a higher depth, Iterative Deepening A Star uses a heuristic to determine which nodes to explore and at which depth to stop. This is similar to how Dijkstra always explores the node with the currently shortest difference and A Star adds an heuristic to this to only explore nodes that are actually closer to the goal.

In more detail, this leads to the following Steps:

1. For each child of the current node
2. If it is the target node, return
3. If the distance plus the heuristic exceeds the current threshold, return this exceeding threshold
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 threshold to the smallest of the exceeding thresholds.
7. If we have reached all leaf (bottom) nodes, the goal node doesn’t exist.

## Example of the Algorithm

Consider the following graph:

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

1. Iteration with threshold: 6.32
2. Visiting Node 0
3. Visiting Node 1
4. Breached threshold with heuristic: 8.66
5. Visiting Node 2
6. Breached threshold with heuristic: 7.00
7. Iteration with threshold: 7.00
8. Visiting Node 0
9. Visiting Node 1
10. Breached threshold with heuristic: 8.66
11. Visiting Node 2
12. Visiting Node 5
13. Breached threshold with heuristic: 8.83
14. Iteration with threshold: 8.66
15. Visiting Node 0
16. Visiting Node 1
17. Visiting Node 3
18. Breached threshold with heuristic: 12.32
19. Visiting Node 4
20. Breached threshold with heuristic: 8.83
21. Visiting Node 2
22. Visiting Node 5
23. Breached threshold with heuristic: 8.83
24. Iteration with threshold: 8.83
25. Visiting Node 0
26. Visiting Node 1
27. Visiting Node 3
28. Breached threshold with heuristic: 12.32
29. Visiting Node 4
30. Visiting Node 2
31. Visiting Node 5
32. Visiting Node 6
33. Found the node we’re looking for!

Final lowest distance from node 0 to node 6: 9

Notice how the algorithm did not continue to explore down from node 3 in the iteration it found the goal node in. If node 3 would’ve had children, whereas Iterative Deepening DFS would’ve potentially (and needlessly!) explored them, Iterative Deepening A Star did not.

## Runtime of the Algorithm

The runtime complexity of Iterative Deepening A Star is in principle the same as Iterative Deepening DFS. 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 Iterative Deepening A Star is the amount of storage needed for the tree or graph. O(|N|), |N| = number of Nodes in the tree or graph, which can be replaced with b^d for trees, where b is the branching factor and d is the depth. Additionally, whatever space the heuristic requires.

Iterative Deepening A Star Algorithm in other languages:

# Java

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;
integers = 12; // Assigning values to positions in the array
// integers is 0, integers would give IndexOutOfBoundsException
String[] strings = {"Hello", "World"}; // Array initialization with initial values
System.out.println(strings + integers); // 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.