BFS Breadth First Search on Graph in DSA C - Time & Space Complexity
We want to understand how the time needed to explore a graph grows as the graph gets bigger using BFS.
How does the number of steps change when the graph has more nodes and edges?
Analyze the time complexity of the following code snippet.
void bfs(int start, int n, int adj[][n]) {
int visited[n];
for (int i = 0; i < n; i++) visited[i] = 0;
int queue[n], front = 0, rear = 0;
queue[rear++] = start;
visited[start] = 1;
while (front < rear) {
int node = queue[front++];
for (int i = 0; i < n; i++) {
if (adj[node][i] && !visited[i]) {
queue[rear++] = i;
visited[i] = 1;
}
}
}
}
This code performs a breadth-first search starting from a node, visiting all reachable nodes in the graph.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The inner loop that checks all neighbors of the current node.
- How many times: For each node dequeued, it checks all possible nodes to find neighbors.
As the graph grows, BFS visits each node once (up to n nodes) and checks all n possible neighbors for each visited node using the adjacency matrix.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 nodes | About 10 × 10 = 100 neighbor checks |
| 100 nodes | About 100 × 100 = 10,000 neighbor checks |
| 1000 nodes | About 1000 × 1000 = 1,000,000 neighbor checks |
Pattern observation: The work grows quadratically with the number of nodes, O(n²).
Time Complexity: O(n²)
This means BFS (with adjacency matrix) takes time proportional to the square of the number of nodes.
[X] Wrong: "BFS takes O(n + e) time because it only checks actual edges."
[OK] Correct: The inner loop checks neighbors, but with adjacency matrix, it scans all n possible neighbors per node (n nodes), totaling O(n²) checks even for sparse graphs.
Understanding BFS time complexity helps you explain graph traversal efficiency clearly, a key skill in many coding challenges and real projects.
"What if the graph is represented using adjacency lists instead of an adjacency matrix? How would the time complexity change?"
Practice
Solution
Step 1: Understand BFS traversal method
BFS explores nodes in layers, visiting all neighbors before going deeper.Step 2: Identify the data structure used
To achieve level-by-level traversal, BFS uses a queue to store nodes to visit next.Final Answer:
A queue to explore nodes level by level -> Option AQuick Check:
BFS uses queue = A [OK]
- Confusing BFS with DFS and choosing stack
- Thinking BFS uses priority queue for weights
- Ignoring the need for a data structure to track nodes
Solution
Step 1: Recognize C queue operation syntax
In C, queues are usually implemented with functions like enqueue(queue, value).Step 2: Identify correct enqueue function call
queue.push and queue.add are not standard C; enqueue(queue, v) is typical.Final Answer:
enqueue(queue, v); -> Option DQuick Check:
Use enqueue(queue, v) in C [OK]
- Using push or add which are not C queue functions
- Confusing queue insertion with array insertion
- Using incorrect method names from other languages
0: 1, 2 1: 0, 3 2: 0, 3 3: 1, 2If BFS starts from node 0, what is the order of nodes visited?
Solution
Step 1: Start BFS from node 0
Visit 0, enqueue neighbors 1 and 2.Step 2: Visit nodes in queue order
Dequeue 1, enqueue its unvisited neighbor 3; then dequeue 2, neighbor 3 already visited.Step 3: Visit remaining node 3
Dequeue 3, no new neighbors.Final Answer:
[0, 1, 2, 3] -> Option BQuick Check:
BFS order = [0, 1, 2, 3] [OK]
- Visiting nodes in DFS order instead
- Mixing neighbor order causing wrong sequence
- Not marking visited nodes causing repeats
void bfs(int start, int n, int adj[][n]) {
int visited[n];
for(int i=0; i<n; i++) visited[i] = 0;
int queue[n], front=0, rear=0;
queue[rear++] = start;
visited[start] = 1;
while(front < rear) {
int u = queue[front++];
printf("%d ", u);
for(int v=0; v<n; v++) {
if(adj[u][v] == 1 && visited[v] == 0) {
queue[rear++] = v;
// Missing visited mark here
}
}
}
}Solution
Step 1: Check visited array initialization
Visited array is initialized to 0 for all nodes, so no issue here.Step 2: Inspect neighbor loop for visited marking
Inside the neighbor loop, nodes are enqueued but visited[v] is not set to 1, causing repeated visits.Step 3: Confirm other options
Queue overflow is not handled but not the main bug; start node is enqueued correctly.Final Answer:
Visited mark missing inside neighbor loop -> Option CQuick Check:
Mark visited when enqueueing neighbors [OK]
- Forgetting to mark visited inside neighbor loop
- Assuming initialization fixes all visited issues
- Ignoring queue overflow but it's not main bug
Solution
Step 1: List BFS order from node 0
Start at 0, enqueue neighbors 1 and 2. Visit order so far: [0]Step 2: Visit nodes in queue order
Visit 1 (step 1), enqueue neighbor 3; visit 2 (step 2), neighbor 3 already enqueued.Step 3: Visit node 2 (step 3)
Next node dequeued is 2, which is the 3rd step (0-based index).Final Answer:
2 -> Option AQuick Check:
BFS step 3 node = 2 [OK]
- Counting steps starting at 1 instead of 0
- Visiting neighbors out of enqueue order
- Forgetting node 3 is enqueued twice but visited once
Ready to prove it?
Pass the test and this pattern moves unknown
