The depth limited search is a variation of a well-known depth first search(DFS) traversing algorithm. It takes care of an edge case problem with DFS by implementing a depth limit.
To implement DFS with a stack, we use these steps:
Let’s say we are working with a tree whose height is either very large or infinite. In such a case, our DFS algorithm would also go on infinitely, as there would always be more child nodes to push back into the stack.
This is what the depth limited search aims to address with a level limit variable.
Let’s look at the following tree with six nodes. Our target node is 5. For this example, we’ll use a level limit of two as we traverse the tree.
We use a visited array to mark off nodes we have already traversed through. This keeps us from visiting the same node multiple times.
As we can see, we ignore Node 6 as it was below our level limit. So, what would happen if our desired node was 6 all along?
This is known as a cutoff failure. Our target exists, but it is too deep for us to traverse.
Let’s suppose our level limit was 3 instead, and our desired node was 7. We could traverse the whole tree without finding our result. That would be a standard failure.
The time complexity of depth limited search is . Where is the number of nodes and is the level limit.