BFS — algorithm, queue-based, O(V+E), shortest path in unweighted graphs
WHAT is BFS?
- WHAT it produces: a level-order visiting sequence, a BFS tree, and (for unweighted graphs) shortest-path distances from the source.
- WHAT data structures: a
queue(the frontier) + avisited/distarray.
WHY a queue gives us "rings"
If we used a stack (LIFO) instead, we'd dive deep first — that's DFS, and it does not give shortest paths.
HOW the algorithm runs (derivation from first principles)
We want: for every node , the minimum number of edges from source .
Claim we build on: if we already know all nodes at distance , then every node at distance is a neighbor of some distance- node, and is not yet visited.
Derivation of the steps:
- Start: . Put in queue. (Why? distance to itself is 0.)
- Pop a node (the oldest in queue → smallest distance). (Why oldest? to keep level order.)
- For each neighbor of that is unvisited: it's reached for the first time, so . Mark visited, push . (Why mark now, at push time? So it's never queued twice.)
- Repeat until queue empty.
BFS(s):
dist[s] = 0
visited[s] = True
queue = [s]
while queue not empty:
u = queue.popleft() # FIFO → smallest dist first
for w in adj[u]:
if not visited[w]:
visited[w] = True # mark at enqueue time!
dist[w] = dist[u] + 1
parent[w] = u # for path reconstruction
queue.append(w)

WHY shortest path? (proof sketch)
WHY is it ?
Worked Examples
Common Mistakes (Steel-manned)
Recall Feynman: explain to a 12-year-old
Pretend the graph is a metro map and you start at one station. You first write "0" on your home station. Then you visit every station one hop away and write "1". Then every new station one hop from those gets "2", and so on. You always finish all the lower numbers before any higher number — like checking everyone on floor 1 before going to floor 2. The number you wrote is the fewest hops to that station. The "queue" is just a line of stations waiting their turn — first in line goes first.
Active Recall
What data structure drives BFS and why?
Why does BFS find shortest paths in unweighted graphs?
What is the time complexity of BFS and why?
When should you mark a node visited?
Why is BFS wrong for weighted shortest paths?
How do you reconstruct the shortest path itself?
What does the queue contain at any moment during BFS?
DFS vs BFS for shortest path — which and why?
Connections
- DFS — depth-first traversal (stack/recursion, used for cycles, components, topological order)
- Dijkstra's Algorithm (BFS generalized to weighted graphs via priority queue)
- 0-1 BFS (deque trick for 0/1 edge weights)
- Graph Representations — adjacency list vs matrix (affects the in )
- Connected Components (run BFS from each unvisited node)
- Bipartite Check (BFS 2-coloring by level parity)
- Shortest Path Tree (the parent pointers form this tree)
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Socho ek talaab me patthar phenka — paani me rings (lehrein) bahar ki taraf failti hain: pehle 1 step door ke points, phir 2 step door, phir 3. BFS bilkul yahi karta hai graph me. Source node se start karke, pehle uske saare direct padosi (level 1), phir unke padosi (level 2), aise hi ring-by-ring explore karta hai. Isiliye jab BFS kisi node tak pehli baar pahunchta hai (unweighted graph me), wahi shortest path hota hai — kam se kam edges wala raasta.
Iska engine hai ek FIFO queue. Queue me jo pehle aata hai wahi pehle nikalta hai, isliye chhoti distance wale nodes pehle process hote hain — yahi level-order ka raaz hai. Agar tum stack (LIFO) use karte to deep chale jaate, aur woh DFS ban jaata, jo shortest path guarantee nahi deta. Ek important rule: node ko visited mark queue me daalte waqt karo, pop karte waqt nahi — warna same node baar-baar queue me ghus jaayega aur galat ho jaayega.
Complexity hai: har vertex ek hi baar queue me aata-jaata hai (), aur har edge ek baar scan hota hai (). Yaad rakho — add hota hai, multiply nahi, kyunki vertex ka kaam aur edge ka kaam alag-alag loops me hote hain.
Last cheez — BFS sirf unweighted (ya har edge ka weight 1) graph me shortest path deta hai. Agar edges ke alag-alag weights hain, to BFS galat answer dega kyunki woh sirf edges ginta hai, weight nahi. Tab Dijkstra use karo. Path waapas nikalne ke liye parent[] store karo aur target se source tak peeche chalte jao, phir reverse kar do.