concept depth - first search in category algorithms

appears as: depth-first search, A depth-first search, depth-first search
Classic Computer Science Problems in Java

This is an excerpt from Manning's book Classic Computer Science Problems in Java.

This gentle introduction is followed by chapter 2, which focuses on search problems. Search is such a large topic that you could arguably place most problems in the book under its banner. Chapter 2 introduces the most essential search algorithms, including binary search, depth-first search, breadth-first search, and A*. Search algorithms are used throughout the rest of the book.

A depth-first search (DFS) is what its name suggests: a search that goes as deeply as it can before backtracking to its last decision point if it reaches a dead end. We’ll implement a generic depth-first search that can solve our maze problem. It will also be reusable for other problems. Figure 2.4 illustrates an in-progress depth-first search of a maze.

Figure 2.4 In depth-first search, the search proceeds along a continuously deeper path until it hits a barrier and must backtrack to the last decision point.
Listing 2.21 GenericSearch.java continued
public static <T> Node<T> bfs(T initial, Predicate<T> goalTest,
        Function<T, List<T>> successors) {
    // frontier is where we've yet to go
    Queue<Node<T>> frontier = new LinkedList<>();
    frontier.offer(new Node<>(initial, null));
    // explored is where we've been
    Set<T> explored = new HashSet<>();
    explored.add(initial);
 
    // keep going while there is more to explore
    while (!frontier.isEmpty()) {
        Node<T> currentNode = frontier.poll();
        T currentState = currentNode.state;
        // if we found the goal, we're done
        if (goalTest.test(currentState)) {
            return currentNode;
        }
        // check where we can go next and haven't explored
        for (T child : successors.apply(currentState)) {
            if (explored.contains(child)) {
                continue; // skip children we already explored
            }
            explored.add(child);
            frontier.offer(new Node<>(child, currentNode));
        }
    }
    return null; // went through everything and never found goal
}
Grokking Artificial Intelligence Algorithms

This is an excerpt from Manning's book Grokking Artificial Intelligence Algorithms.

Consider the maze scenario (figure 2.14). In attempting to find an optimal path to the goal, assume the following simple constraint to prevent getting stuck in an endless loop and prevent cycles in our tree: the player cannot move into a block that they have previously occupied. Because uninformed algorithms attempt every possible option at every node, creating a cycle will cause the algorithm to fail catastrophically.

Add neighbor to stack. The neighbor node is added to the stack for its children to be explored later. Again, this stacking mechanism allows nodes to be processed to the utmost depth before processing neighbors at shallow depths.

Figures 2.22 and 2.23 explore how the LIFO stack is used to visit nodes in the order desired by depth-first search. Notice that nodes get pushed onto and popped from the stack as the depths of the nodes visited progress. The term push describes adding objects to a stack, and the term pop describes removing the topmost object from the stack.

Figure 2.22 The sequence of tree processing using depth-first search (part 1)
Figure 2.23 The sequence of tree processing using depth-first search (part 2)
A close up of a map Description automatically generated