# Using Iterative deepening depth-first search in Python

Iterative deepening depth-first search (IDDFS) is an extension to the ‘vanilla’ depth-first search algorithm, with an added constraint on the total depth explored per iteration. This addition produces equivalent results to what can be achieved using breadth-first search, without suffering from the large memory costs. Due to BFS’s storage of fringe vertices in memory, Od^b memory space may be required (b = branching factor), this is a stark contrast to IDDFS’s O(bd) worst-case memory requirements. On a per-iteration interval vertex successors at the depth-cap level are ignored, and if the goal has not been found, the maximum level is increased (by one) and the processes repeated. Similarly to BFS, it has the guarantee to find an optimal path between two subject vertices, as the shallowest goal vertex will be the depth-cap first, resulting in no exploration of subsequent, unnecessary branches.

## n-Puzzle example

Within the field of Artificial Intelligence the sliding puzzle problem is a great way to explore the effectiveness of different searching algorithms. Consisting of a superficial border with symboled tiles in a random order and one tile missing, the objective is to rearrange the puzzle in the least amount of moves to a goal state (typically natural order). The border must be taken into consideration upon each move, with only a maximum of four possible legal moves available (up, down, left and right).

Below is an example of the IDDFS algorithm, implemented to help solve the discussed puzzle problem. Providing the user with the requirement to specify a ‘get_moves’ function enables the ability to use the same implementation for different puzzle problems.

Looking at the sample code above you will notice the use of ‘itertools’ infinite counter (starting from zero). The depth will continue to increment until a successful path is generated and returned to the user.

### Number Matrix

The first problem we will solve with the above implementation is the common place numbered (3x3) 8-puzzle. Using the method below we are able generate the specified sized puzzle and goal matrices, which is useful to testing.

So as to make sure that the algorithm checks for only legal moves in our problem domain, the function below has been created that returns a partially-applied ‘get_moves’ method, based on the grid size and discussed restraints.

We are now able to use all three of these functions to solve and return the optimal path to a contrived puzzle and goal state.

### String Matrix

Now that we are confident that the implementation is working correctly we can now move onto creating a solution to the string matrix problem. Similar to the problem above we are still keeping hold of the border property, however, this time we are going to add the requirement to store and compare the subject paths in string format (compared to a multi-dimensional list). We first create a function which generate a contrived puzzle and goal state for us to then solve.

With this function now in place we then provide the ability to return valid present moves, based on a subject position.

Finally, we can produce a similar example to the number matrix example to see the solution in action.