# Iterative Deepening DFS in all languages

#### Iterative Deepening DFS in Java

Posted: , Last Updated:

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

See Code#### Iterative Deepening DFS in C

Posted: , Last Updated:

C is a compiled language used for many purposes, although it can be primarily found in systems where importance is important. This is because C offers a lot of low-level support for optimization, at the cost of not having some of the convenient abstractions that other languages offer. C is therefore primarily found in situations where available computation power is low such as embedded systems, or situations where required computation power is high, such as simulation or deep learning.

See Code#### Iterative Deepening DFS in Javascript

Posted: , Last Updated:

JavaScript JavaScript is an interpreted scripting language previously primarily used in web pages (executed in browsers) that has since…

See Code#### Iterative Deepening DFS in Python

Posted: , Last Updated:

Python Python™ is an interpreted language used for many purposes ranging from embedded programming to web development, with one of the largest use cases being data science.

See Code#### Iterative Deepening DFS in R

Posted: , Last Updated:

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

See Code## About the algorithm:

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

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

## Example of the Algorithm

Consider the following tree:

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

- Visiting Node 0
- Visiting Node 1
- Current maximum depth reached, returning…
- Visiting Node 2
- Current maximum depth reached, returning…
- Increasing depth to 2
- Visiting Node 0
- Visiting Node 1
- Visiting Node 3
- Current maximum depth reached, returning…
- Visiting Node 4
- Current maximum depth reached, returning…
- Visiting Node 2
- Visiting Node 5
- Current maximum depth reached, returning…
- Visiting Node 6
- 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.