---
title: "BFS: Looking wide before looking deep"
id: "14043"
type: "page"
slug: "grokkingaialgorithmssecondedition"
published_at: "2026-07-19T20:06:55+00:00"
modified_at: "2026-07-19T20:27:30+00:00"
url: "https://preppers-paradise.com/library/grokkingaialgorithmssecondedition/"
markdown_url: "https://preppers-paradise.com/library/grokkingaialgorithmssecondedition.md"
excerpt: "This section introduces the Breadth-First Search (BFS) algorithm, explaining how it systematically explores trees and graphs by visiting all nodes at a given depth before proceeding to the next level. Readers will learn the detailed steps of implementing BFS, including..."
taxonomy_category:
  - "AI &amp; Machine Learning"
  - "Books"
  - "Free Teaser"
taxonomy_post_tag:
  - "algorithms"
  - "bfs"
  - "breadth-first search"
  - "computer science"
  - "data structures"
  - "graph traversal"
  - "maze solving"
  - "pathfinding"
  - "python"
  - "tree traversal"
---

# BFS: Looking wide before looking deep

This section introduces the Breadth-First Search (BFS) algorithm, explaining how it systematically explores trees and graphs by visiting all nodes at a given depth before proceeding to the next level. Readers will learn the detailed steps of implementing BFS, including the critical role of a first-in, first-out (FIFO) queue for managing node processing, and how it is used to find the shortest path in unweighted graphs. Practical applications like solving mazes are illustrated with a step-by-step walkthrough and a Python code sample, demonstrating how to trace paths and prevent revisiting nodes.

This page is free — overview and chapter list only.
Full book: $2.99 USDC · Per chapter: $0.25 USDC
[Buy / open complete book](/library/complete/grokkingaialgorithmssecondedition/) · [Complete book Markdown](/library/complete/grokkingaialgorithmssecondedition.md)

## Chapters

1. [Bfs Looking Wide Before Looking Deep](/library/grokkingaialgorithmssecondedition/01-bfs-looking-wide-before-looking-deep/) (Free teaser)
1. [Dfs Looking Deep Before Looking Wide](/library/grokkingaialgorithmssecondedition/02-dfs-looking-deep-before-looking-wide/) ($0.25)
1. [Use Cases For Informed Search Algorithms](/library/grokkingaialgorithmssecondedition/03-use-cases-for-informed-search-algorithms/) ($0.25)
1. [What Is Evolution](/library/grokkingaialgorithmssecondedition/04-what-is-evolution/) ($0.25)
1. [Problems That Evolutionary Algorithms Can Solve](/library/grokkingaialgorithmssecondedition/05-problems-that-evolutionary-algorithms-can-solve/) ($0.25)
1. [Life Cycle Of Genetic Algorithms](/library/grokkingaialgorithmssecondedition/06-life-cycle-of-genetic-algorithms/) ($0.25)
1. [Encoding The Solution Spaces](/library/grokkingaialgorithmssecondedition/07-encoding-the-solution-spaces/) ($0.25)
1. [Exercise What Is A Possible Encoding For The Following Probl](/library/grokkingaialgorithmssecondedition/08-exercise-what-is-a-possible-encoding-for-the-following-probl/) ($0.25)
1. [Measuring Fitness Of Individuals In A Population](/library/grokkingaialgorithmssecondedition/09-measuring-fitness-of-individuals-in-a-population/) ($0.25)
1. [Using Population Models](/library/grokkingaialgorithmssecondedition/10-using-population-models/) ($0.25)
1. [Bit String Mutation For Binary Encoding](/library/grokkingaialgorithmssecondedition/11-bit-string-mutation-for-binary-encoding/) ($0.25)
1. [Alternative Selection Strategies](/library/grokkingaialgorithmssecondedition/12-alternative-selection-strategies/) ($0.25)
1. [Order Encoding Working With Sequences](/library/grokkingaialgorithmssecondedition/13-order-encoding-working-with-sequences/) ($0.25)
1. [Tree Encoding Working With Hierarchies](/library/grokkingaialgorithmssecondedition/14-tree-encoding-working-with-hierarchies/) ($0.25)
1. [In This Chapter](/library/grokkingaialgorithmssecondedition/15-in-this-chapter/) ($0.25)
1. [Choose The Next Visit For Each Ant](/library/grokkingaialgorithmssecondedition/16-choose-the-next-visit-for-each-ant/) ($0.25)
1. [Update The Pheromone Trails](/library/grokkingaialgorithmssecondedition/17-update-the-pheromone-trails/) ($0.25)
1. [Python Code Sample For Expressing A Particle](/library/grokkingaialgorithmssecondedition/18-python-code-sample-for-expressing-a-particle/) ($0.25)
1. [Calculate The Fitness Particles](/library/grokkingaialgorithmssecondedition/19-calculate-the-fitness-particles/) ($0.25)
1. [Python Code Sample For Calculating Fitness](/library/grokkingaialgorithmssecondedition/20-python-code-sample-for-calculating-fitness/) ($0.25)
1. [Updating Velocity](/library/grokkingaialgorithmssecondedition/21-updating-velocity/) ($0.25)
1. [Updating Position](/library/grokkingaialgorithmssecondedition/22-updating-position/) ($0.25)
1. [Continued](/library/grokkingaialgorithmssecondedition/23-continued/) ($0.25)
1. [Summary Of Swarm Intelligence Particles](/library/grokkingaialgorithmssecondedition/24-summary-of-swarm-intelligence-particles/) ($0.25)
1. [Missing Data](/library/grokkingaialgorithmssecondedition/25-missing-data/) ($0.25)
1. [What Are Artificial Neural Networks](/library/grokkingaialgorithmssecondedition/26-what-are-artificial-neural-networks/) ($0.25)
1. [The Perceptron A Representation Of A Neuron](/library/grokkingaialgorithmssecondedition/27-the-perceptron-a-representation-of-a-neuron/) ($0.25)
1. [Exercise Calculate The Output Of The Following Input For The](/library/grokkingaialgorithmssecondedition/28-exercise-calculate-the-output-of-the-following-input-for-the/) ($0.25)
1. [Forward Propagation Using A Trained Ann](/library/grokkingaialgorithmssecondedition/29-forward-propagation-using-a-trained-ann/) ($0.25)
1. [Exercise Calculate The Prediction For The Example By Using F](/library/grokkingaialgorithmssecondedition/30-exercise-calculate-the-prediction-for-the-example-by-using-f/) ($0.25)
1. [Exercise Calculate The New Weights For The Highlighted Weigh](/library/grokkingaialgorithmssecondedition/31-exercise-calculate-the-new-weights-for-the-highlighted-weigh/) ($0.25)
1. [Hidden Layers And Nodes](/library/grokkingaialgorithmssecondedition/32-hidden-layers-and-nodes/) ($0.25)
1. [The Goldilocks Principle Of Initialization](/library/grokkingaialgorithmssecondedition/33-the-goldilocks-principle-of-initialization/) ($0.25)
1. [The Hidden Layer As Matrix Multiplication](/library/grokkingaialgorithmssecondedition/34-the-hidden-layer-as-matrix-multiplication/) ($0.25)
1. [Expressing Backpropagation Mathematically](/library/grokkingaialgorithmssecondedition/35-expressing-backpropagation-mathematically/) ($0.25)
1. [Generative Adversarial Networks](/library/grokkingaialgorithmssecondedition/36-generative-adversarial-networks/) ($0.25)
1. [The Inspiration For Reinforcement Learning](/library/grokkingaialgorithmssecondedition/37-the-inspiration-for-reinforcement-learning/) ($0.25)
1. [Problems That Reinforcement Learning Can Solve](/library/grokkingaialgorithmssecondedition/38-problems-that-reinforcement-learning-can-solve/) ($0.25)
1. [Simulation And Data The Agents Environment](/library/grokkingaialgorithmssecondedition/39-simulation-and-data-the-agents-environment/) ($0.25)
1. [Training With The Simulation Using Q Learning](/library/grokkingaialgorithmssecondedition/40-training-with-the-simulation-using-q-learning/) ($0.25)
1. [Exercise Calculate The Change In Values For The Q Table](/library/grokkingaialgorithmssecondedition/41-exercise-calculate-the-change-in-values-for-the-q-table/) ($0.25)
1. [Use Cases For Reinforcement Learning](/library/grokkingaialgorithmssecondedition/42-use-cases-for-reinforcement-learning/) ($0.25)
1. [Financial Trading](/library/grokkingaialgorithmssecondedition/43-financial-trading/) ($0.25)
1. [What Are Llms](/library/grokkingaialgorithmssecondedition/44-what-are-llms/) ($0.25)
1. [The Intuition Behind Language Prediction](/library/grokkingaialgorithmssecondedition/45-the-intuition-behind-language-prediction/) ($0.25)
1. [Why The Sizes Of Tokens And Parameters Matter](/library/grokkingaialgorithmssecondedition/46-why-the-sizes-of-tokens-and-parameters-matter/) ($0.25)
1. [Prepare The Training Data](/library/grokkingaialgorithmssecondedition/47-prepare-the-training-data/) ($0.25)
1. [Encoding From Text To Numbers](/library/grokkingaialgorithmssecondedition/48-encoding-from-text-to-numbers/) ($0.25)
1. [Creating A Trainable Embedding Matrix](/library/grokkingaialgorithmssecondedition/49-creating-a-trainable-embedding-matrix/) ($0.25)
1. [Exercise What Is The Final Input Vector For Token 2](/library/grokkingaialgorithmssecondedition/50-exercise-what-is-the-final-input-vector-for-token-2/) ($0.25)
1. [Calculating Attention Weights](/library/grokkingaialgorithmssecondedition/51-calculating-attention-weights/) ($0.25)
1. [Making A Prediction](/library/grokkingaialgorithmssecondedition/52-making-a-prediction/) ($0.25)
1. [Exercise What Is The Logit For Token 31](/library/grokkingaialgorithmssecondedition/53-exercise-what-is-the-logit-for-token-31/) ($0.25)
1. [Training Epochs](/library/grokkingaialgorithmssecondedition/54-training-epochs/) ($0.25)
1. [Few Shot And Zero Shot Learning](/library/grokkingaialgorithmssecondedition/55-few-shot-and-zero-shot-learning/) ($0.25)
1. [Fine Tuning The Llm With Reinforcement Learning](/library/grokkingaialgorithmssecondedition/56-fine-tuning-the-llm-with-reinforcement-learning/) ($0.25)
1. [Content Generation](/library/grokkingaialgorithmssecondedition/57-content-generation/) ($0.25)
1. [Enhancement Of Digital Products](/library/grokkingaialgorithmssecondedition/58-enhancement-of-digital-products/) ($0.25)
1. [Selecting And Collecting Image Data](/library/grokkingaialgorithmssecondedition/59-selecting-and-collecting-image-data/) ($0.25)
1. [Cleaning And Preprocessing Image Data](/library/grokkingaialgorithmssecondedition/60-cleaning-and-preprocessing-image-data/) ($0.25)
1. [Forward Diffusion](/library/grokkingaialgorithmssecondedition/61-forward-diffusion/) ($0.25)
1. [Exercise What Is The Noise For Pixel 1 1 If The Random Numbe](/library/grokkingaialgorithmssecondedition/62-exercise-what-is-the-noise-for-pixel-1-1-if-the-random-numbe/) ($0.25)
1. [Timestep Embedding](/library/grokkingaialgorithmssecondedition/63-timestep-embedding/) ($0.25)
1. [Text Label Embedding](/library/grokkingaialgorithmssecondedition/64-text-label-embedding/) ($0.25)
1. [Cnn Input Shape](/library/grokkingaialgorithmssecondedition/65-cnn-input-shape/) ($0.25)
1. [Feature Map](/library/grokkingaialgorithmssecondedition/66-feature-map/) ($0.25)
1. [U Net](/library/grokkingaialgorithmssecondedition/67-u-net/) ($0.25)
1. [Encoder Downsampling Layers](/library/grokkingaialgorithmssecondedition/68-encoder-downsampling-layers/) ($0.25)
1. [Python Code Sample For A Convolutional Layer](/library/grokkingaialgorithmssecondedition/69-python-code-sample-for-a-convolutional-layer/) ($0.25)
1. [Bridge](/library/grokkingaialgorithmssecondedition/70-bridge/) ($0.25)
1. [Upsampling](/library/grokkingaialgorithmssecondedition/71-upsampling/) ($0.25)
1. [Python Code Sample For Upsampling](/library/grokkingaialgorithmssecondedition/72-python-code-sample-for-upsampling/) ($0.25)
1. [Python Code Sample For The Skip Connection And Final Convolu](/library/grokkingaialgorithmssecondedition/73-python-code-sample-for-the-skip-connection-and-final-convolu/) ($0.25)
1. [Calculating Loss](/library/grokkingaialgorithmssecondedition/74-calculating-loss/) ($0.25)
1. [Denoising The Data](/library/grokkingaialgorithmssecondedition/75-denoising-the-data/) ($0.25)
1. [Exercise What Is The Timestep Scale For T 3](/library/grokkingaialgorithmssecondedition/76-exercise-what-is-the-timestep-scale-for-t-3/) ($0.25)
1. [Training Data Composition And Diversity](/library/grokkingaialgorithmssecondedition/77-training-data-composition-and-diversity/) ($0.25)
1. [Training Epochs 2](/library/grokkingaialgorithmssecondedition/78-training-epochs-2/) ($0.25)
1. [Creative Ideation And Concept Art](/library/grokkingaialgorithmssecondedition/79-creative-ideation-and-concept-art/) ($0.25)
1. [A New Online Reading Experience](/library/grokkingaialgorithmssecondedition/80-a-new-online-reading-experience/) ($0.25)
