liveBook · Manning

Breadth-first search

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

Breadth-first search explores all reachable states at one depth before exploring states at greater depth.

Breadth-first search (BFS) is a blind graph-traversal technique that expands a search tree layer by layer: it explores every node at the current depth before moving to deeper nodes. It starts from a source node and continues until it finds a destination or exhausts the queue. section 3.2 section 3.3.1

Implement BFS with a first-in, first-out queue and an explored collection:

  • Initialize the queue with the source, keep explored empty, and set found to false. section 3.3.1
  • Dequeue the next node, mark it explored, and expand its successors. section 3.3.1
  • Enqueue each successor that is neither explored nor already queued. section 3.3.1
  • Stop when the destination is found, reconstructing its route, or when the queue becomes empty. section 3.3.1

Solving an 8-puzzle

Represent each puzzle configuration as a state, with the blank tile’s legal moves producing successors. BFS begins with the initial configuration, explores all one-move states, then all two-move states, and so on until the ordered goal state appears. A Python implementation checks solvability, calls BFS, and reports processing time, memory, explored states, and the resulting route. section 3.3.1

BFS can find a solution when each node has finitely many successors, but blind search does not use information about the search space and does not select the best route among feasible alternatives. Its queue and explored set can grow rapidly as depth increases, making time and memory practical constraints. section 3.2

Algorithm 3.1 Breadth-first search (BFS)
Inputs: Source node, Destination node
Output: Route from source to destination
  
Initialize queue ← a FIFO initialized with source node
Initialize explored ← empty
Initialize found ← False
  
While queue is not empty and found is False do
    Set node ← queue.dequeue()
    Add node to explored
    For child in node.expand() do
        If child is not in explored and child is not in queue then
            If child is destination then
                Update route ← child route()
                Update found ← True
            Add child to queue
Return route

Use the compact pseudocode to trace queue updates, explored-state checks, successor expansion, and route reconstruction. section 3.3.1

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

How deep it goes
Explains queue operations, duplicate checks, route reconstruction, puzzle modeling, and rapidly increasing time and memory demands.
Examples
Demonstrates 8-puzzle solving, route reconstruction, path planning, and a Python run reporting time, memory, and explored states.
What it assumes
Assumes basic graph and queue concepts while defining states, successors, goals, paths, and stopping criteria through the 8-puzzle.
What you can do after
Apply BFS via Python to solve a modeled state-space problem and inspect its route, runtime, memory, and explored-state results.
How it connects
Connects graph traversal with blind search, depth-first search, Dijkstra’s algorithm, uniform-cost search, and bidirectional search.

Also explained in

Compare how 8 books explain breadth-first search →

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.