liveBook · Manning

Stack

As explained in Optimization Algorithms: AI techniques for design, planning, and control problems

A last-in, first-out data structure in which elements are added to and removed from the same end.

A stack is a last-in, first-out structure: newly discovered elements are pushed onto the top, and the next element to process is popped from the top. This ordering drives depth-first search toward the deepest available node before backtracking. section 3.3.2

In depth-first search, the stack supports a simple processing cycle:

  • Initialize it with the source node. section 3.3.2
  • Pop the most recently discovered node and expand it. section 3.3.2
  • Push eligible successors onto the stack, continuing until the destination is found or the stack is empty. section 3.3.2
Algorithm 3.2 Depth-first search (DFS)
Inputs: Source node, Destination node
Output: Route from source to destination
  
Initialize Stack ← a LIFO initialized with sourcenode
Initialize Explored ← empty
Initialize Found ← False
  
While stack is not empty and found is False do
    Set node ← stack.pop()
    Add node to explored
    For child in node.expand() do
        If child is not in explored and child is not in stack then
            If child is destination then
               Update route ← child.route()
               Update found ← True
            Add child to stack
Return route

The DFS pseudocode makes the stack’s push, pop, explored-node checks, and route construction explicit. section 3.3.2

8-puzzle search

DFS uses a stack to explore puzzle states deeply, then returns to the last decision point when it reaches a dead end. A depth bound can limit expansion. section 3.3.2

How Optimization Algorithms: AI techniques for design, planning, and control problems covers it

How deep it goes
Explains initialization, push and pop operations, successor filtering, and termination within DFS, without examining implementation internals.
Examples
Uses depth-first search on the 8-puzzle and path-planning problems to demonstrate stack-based exploration.
What you can do after
Apply it via depth-first search to prioritize deep graph exploration and return to earlier decision points after dead ends.
How it connects
Connects stacks to depth-first search, breadth-first search, backtracking, graph search, and explored-node tracking.

Also explained in

Compare how 14 books explain stack →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Optimization Algorithms: AI techniques for design, planning, and control problems. The section links point to the parts of the book it draws on.