0
0
DSA Typescriptprogramming

BFS Breadth First Search on Graph in DSA Typescript

Choose your learning style9 modes available
Mental Model
BFS explores all neighbors of a node before moving to the next level of nodes.
Analogy: Imagine you are in a building and want to visit every room floor by floor, starting from the entrance. You first check all rooms on the first floor, then move to the second floor, and so on.
Graph:
  1
 / \
2   3
|    |
4    5

Queue starts with [1] ↑
Dry Run Walkthrough
Input: Graph edges: 1->2, 1->3, 2->4, 3->5; start BFS from node 1
Goal: Visit all nodes in order of their distance from node 1
Step 1: Start BFS by enqueueing node 1
Queue: [1↑]
Visited: {}
Why: We begin from the start node 1
Step 2: Dequeue 1, visit it, enqueue neighbors 2 and 3
Queue: [2↑, 3]
Visited: {1}
Why: Visit node 1 and add its neighbors to queue for next visits
Step 3: Dequeue 2, visit it, enqueue neighbor 4
Queue: [3↑, 4]
Visited: {1, 2}
Why: Visit node 2 and add its neighbor 4
Step 4: Dequeue 3, visit it, enqueue neighbor 5
Queue: [4↑, 5]
Visited: {1, 2, 3}
Why: Visit node 3 and add its neighbor 5
Step 5: Dequeue 4, visit it, no new neighbors
Queue: [5↑]
Visited: {1, 2, 3, 4}
Why: Visit node 4; no new nodes to add
Step 6: Dequeue 5, visit it, no new neighbors
Queue: []
Visited: {1, 2, 3, 4, 5}
Why: Visit node 5; BFS complete as queue is empty
Result:
Visited order: 1 -> 2 -> 3 -> 4 -> 5
Annotated Code
DSA Typescript
class Graph {
  adjacencyList: Map<number, number[]>;

  constructor() {
    this.adjacencyList = new Map();
  }

  addEdge(u: number, v: number) {
    if (!this.adjacencyList.has(u)) this.adjacencyList.set(u, []);
    this.adjacencyList.get(u)!.push(v);
  }

  bfs(start: number): number[] {
    const visited = new Set<number>();
    const queue: number[] = [];
    const order: number[] = [];

    visited.add(start); // mark start node as visited
    queue.push(start); // enqueue start node

    while (queue.length > 0) {
      const node = queue.shift()!; // dequeue front node
      order.push(node); // record visit order

      const neighbors = this.adjacencyList.get(node) || [];
      for (const neighbor of neighbors) {
        if (!visited.has(neighbor)) {
          visited.add(neighbor); // mark neighbor visited when enqueueing
          queue.push(neighbor); // enqueue unvisited neighbors
        }
      }
    }

    return order;
  }
}

// Driver code
const graph = new Graph();
graph.addEdge(1, 2);
graph.addEdge(1, 3);
graph.addEdge(2, 4);
graph.addEdge(3, 5);

const bfsOrder = graph.bfs(1);
console.log(bfsOrder.join(' -> '));
visited.add(start); // mark start node as visited
mark start node visited before enqueueing to avoid duplicates
queue.push(start); // enqueue start node
initialize queue with start node to begin BFS
const node = queue.shift()!; // dequeue front node
remove front node from queue to visit it
order.push(node); // record visit order
save node in BFS visit order
const neighbors = this.adjacencyList.get(node) || [];
get neighbors of current node or empty if none
if (!visited.has(neighbor)) { visited.add(neighbor); queue.push(neighbor); }
mark neighbor visited and enqueue unvisited neighbors for future visits
OutputSuccess
1 -> 2 -> 3 -> 4 -> 5
Complexity Analysis
Time: O(V + E) because each vertex and edge is visited once during BFS
Space: O(V) for the queue and visited set storing nodes
vs Alternative: Compared to DFS which uses a stack or recursion, BFS uses a queue and explores level by level, which is better for shortest path in unweighted graphs
Edge Cases
Empty graph with no nodes
BFS returns empty list as there is no start node
DSA Typescript
queue.push(start); // enqueue start node
Graph with single node and no edges
BFS visits the single node and returns it
DSA Typescript
const neighbors = this.adjacencyList.get(node) || [];
Graph with disconnected nodes
BFS visits only nodes reachable from start node
DSA Typescript
if (!visited.has(neighbor)) { visited.add(neighbor); queue.push(neighbor); }
When to Use This Pattern
When you need to explore nodes in order of their distance from a start node or find shortest paths in unweighted graphs, reach for BFS because it visits nodes level by level.
Common Mistakes
Mistake: Not marking nodes as visited before enqueueing, causing duplicates in queue
Fix: Mark nodes as visited when enqueueing or check before enqueueing to avoid repeats
Mistake: Using stack instead of queue, which changes BFS to DFS
Fix: Use a queue data structure to maintain correct BFS order
Summary
BFS visits all nodes in a graph level by level starting from a given node.
Use BFS when you want to find shortest paths or explore nodes by distance from start.
The key insight is BFS uses a queue to explore neighbors before moving deeper.