Graph Theory37 sections · 1633 units
Open in Course

The BFS Algorithm Step 1

Setup

Before running BFS, set up three data structures. First, a queue to hold nodes waiting to be processed. Start with an empty queue. Second, a visited array to track which nodes you have seen. Initialize all entries to false. Third, a distance array to record how far each node is from the source.

Set all distances to infinity except the source, which gets distance 00. These three structures work together: the queue controls the order, visited prevents cycles, and distance tracks shortest paths.