Breadth-First Search (BFS)

Introduction

Breadth-First Search (BFS) is a graph traversal strategy that explores a graph in layers: starting from a source vertex, it visits every neighbor at distance one, then every vertex at distance two, and so on. The engine behind this level-by-level behavior is the queue, a first-in-first-out structure that holds vertices waiting to be explored. Alongside the queue you also need a visited set (or boolean array) so that cycles and shared neighbors do not cause a vertex to be processed twice.

BFS matters in this course because it is one of the two canonical traversals you will implement on top of your adjacency list representation, and because its level order gives it properties DFS does not have — most notably, in an unweighted graph BFS finds the shortest path in number of edges from the source. It also forces you to think about graphs that are not fully connected: a single BFS call only reaches the component containing the start vertex, so a complete traversal requires looping over all vertices and launching a new BFS from each unvisited one.

As you watch the videos below, focus on four things: exactly when a vertex is marked visited (on enqueue, not on dequeue, if you want to avoid duplicates), how the queue contents change at each step, how the traversal order depends on the order neighbors appear in the adjacency list, and where BFS shows up in practice — shortest paths, level/distance computation, web crawling, social network degrees of separation. Try tracing a small graph by hand before and after watching, and compare your order against the one in the video.

Video 1: Breadth First Search Explained and Implemented in Java | BFS | Graph Traversal & Theory | Geekific

Geekific · 6:22 · 72,911 views · 5y ago · captions — Watch on YouTube

About this video

Geekific presents the theory of BFS and then implements it in Java, connecting the conceptual queue-based traversal to actual code. Expect coverage of how the queue drives exploration, how visited tracking prevents revisiting vertices, and how the traversal is written against a graph structure, with the full source available in the linked repository.

Why this video

It pairs the algorithm logic with a concrete implementation, which is exactly the bridge you need when writing your own Graph class traversal method in this module.

Video 2: Breadth-first search in 4 minutes

Michael Sambol · 3:59 · 603,429 views · 4y ago · captions — Watch on YouTube

About this video

A compact, tightly edited explanation of breadth-first search that walks through the algorithm on a sample graph in under four minutes. It focuses on the core mechanics — the queue, marking vertices as visited, and the resulting level-by-level visit order — with linked Python code for those who want to see an implementation.

Why this video

Michael Sambol's algorithm shorts are widely used and known for clean, correct visual traces, making this the fastest way to get the BFS logic and traversal order straight before diving into longer treatments.

Video 3: Breadth First Search (BFS) Graph Traversal in Data Structures

CodeWithHarry · 19:00 · 367,697 views · 4y ago · captions — Watch on YouTube

About this video

A longer, classroom-style lecture from CodeWithHarry that develops BFS as a graph traversal in a data structures context, including discussion of the BFS spanning tree produced by the traversal. The extra length allows for slower, more detailed stepping through examples and the data structures involved.

Why this video

The pace and depth suit students who want the step-by-step reasoning spelled out, and the spanning-tree framing adds useful intuition for why BFS order relates to shortest distances from the source.

How These Videos Support the Learning Outcomes

These three videos concentrate on MO3 and MO5. The short Sambol video and the detailed CodeWithHarry lecture let you practice tracing BFS visit order on a given graph and reasoning about the queue state at each step, which is the core tracing skill in MO3; the longer lecture's example work also helps you see why an outer loop over all vertices is needed when a graph has multiple components. The Geekific implementation supports MO4 by showing BFS written as code over a graph structure, which you can translate into your JavaScript Graph class built on an adjacency list (MO2). Discussion of BFS uses — shortest paths in unweighted graphs, level or distance computation — gives you the evidence needed for MO5 when you argue whether BFS or DFS fits a given problem. The videos assume a graph is already defined, so pair them with the module's earlier material for MO1 and MO2.

About These Videos

These videos were chosen from YouTube on October 2, 2026. Each has captions; the player turns them on. Videos on YouTube can be changed or removed — if one no longer plays, tell your instructor. If a player does not appear, use its Watch on YouTube link.