📖 Scenario: You are working with a simple map of connected cities. Each city is connected to some other cities by roads. You want to visit all cities starting from a specific city, exploring neighbors first before going deeper.
🎯 Goal: Build a program that uses BFS (Breadth First Search) to visit all cities starting from city 0 and print the order in which cities are visited.
📋 What You'll Learn
Create an adjacency list to represent the graph of 5 cities with given connections
Create a queue to help with BFS traversal
Implement BFS traversal starting from city 0
Print the order of cities visited by BFS
💡 Why This Matters
🌍 Real World
BFS is used in social networks to find friends at different levels, in GPS to find shortest paths, and in web crawlers to explore websites layer by layer.
💼 Career
Understanding BFS is essential for software engineers working on graph problems, network analysis, and algorithms for search and traversal.
Progress0 / 4 steps
1
Create the graph adjacency list
Create a vector of vectors called graph with 5 elements. Add these edges exactly: city 0 connected to 1 and 2, city 1 connected to 2 and 3, city 2 connected to 4, city 3 connected to 4.
DSA C++
Hint
Use a vector of vectors to store neighbors for each city. Assign neighbors exactly as given.
2
Create a queue and visited list
Create a std::queue<int> called q and a std::vector<bool> called visited with 5 elements initialized to false. Then push city 0 into q and mark visited[0] as true.
DSA C++
Hint
Use a queue to keep track of cities to visit next. Use a boolean vector to mark visited cities.
3
Implement BFS traversal loop
Write a while loop that runs while q is not empty. Inside, pop the front city into current. For each neighbor neighbor in graph[current], if visited[neighbor] is false, push neighbor into q and mark visited[neighbor] as true. Also, add each visited city to a vector order in the order they are visited. Initialize order with city 0 before the loop.
DSA C++
Hint
Use a while loop to process cities in the queue. For each city, add unvisited neighbors to the queue and mark them visited.
4
Print the BFS visit order
Print the cities in order separated by spaces on one line.
DSA C++
Hint
Use a for loop to print each city in order separated by spaces.
Practice
(1/5)
1. What does BFS (Breadth First Search) primarily use to keep track of nodes to visit next in a graph?
easy
A. A queue data structure
B. A stack data structure
C. A priority queue
D. A hash map
Solution
Step 1: Understand BFS traversal method
BFS explores nodes level by level, visiting all neighbors before moving deeper.
Step 2: Identify the data structure used
To maintain the order of visiting nodes level-wise, BFS uses a queue which follows FIFO (First In First Out).
Final Answer:
A queue data structure -> Option A
Quick Check:
BFS uses queue = B [OK]
Hint: BFS uses FIFO order, so it needs a queue [OK]
Common Mistakes:
Confusing BFS with DFS which uses a stack
Thinking BFS uses a priority queue
Assuming BFS uses a hash map for order
2. Which of the following is the correct way to declare a queue for BFS traversal of integers in C++?
easy
A. vector<int> q;
B. queue<int> q;
C. stack<int> q;
D. list<int> q;
Solution
Step 1: Identify the correct STL container for queue
In C++, the standard queue container is declared as queue<type>.
Step 2: Match the declaration for integer queue
queue<int> q; correctly declares a queue of integers.
Final Answer:
queue<int> q; -> Option B
Quick Check:
Correct queue syntax = queue<int> q; [OK]
Hint: Use queue<int> for BFS integer queue in C++ [OK]
Common Mistakes:
Using stack instead of queue
Using vector or list which are not queues
Missing template angle brackets
3. Consider the following graph represented as adjacency list:
0: 1, 2
1: 0, 3
2: 0, 3
3: 1, 2
If BFS starts from node 0, what is the order of nodes visited?
medium
A. [0, 1, 3, 2]
B. [0, 2, 1, 3]
C. [0, 3, 1, 2]
D. [0, 1, 2, 3]
Solution
Step 1: Start BFS from node 0
Visit node 0 first, enqueue neighbors 1 and 2.
Step 2: Visit neighbors in queue order
Dequeue 1, enqueue its neighbor 3 (0 already visited). Then dequeue 2, enqueue 3 but already visited.
Step 3: Visit node 3
Finally dequeue 3.
Final Answer:
[0, 1, 2, 3] -> Option D
Quick Check:
BFS order = [0, 1, 2, 3] [OK]
Hint: Visit neighbors in queue order, avoid revisits [OK]
Common Mistakes:
Visiting neighbors in wrong order
Revisiting nodes already visited
Confusing BFS with DFS order
4. The following BFS code snippet has a bug. What is the error?
void bfs(int start, vector& adj) {
vector<bool> visited(adj.size(), false);
queue<int> q;
q.push(start);
while (!q.empty()) {
int node = q.front();
q.pop();
cout << node << " ";
for (int neighbor : adj[node]) {
if (!visited[neighbor]) {
q.push(neighbor);
}
}
}
}
medium
A. Neighbors are not iterated correctly
B. Queue is not initialized properly
C. Visited array is never updated to true for visited nodes
D. Start node is not pushed into the queue
Solution
Step 1: Check visited array usage
Visited array is created but nodes are never marked visited after visiting.
Step 2: Understand impact on BFS
Without marking visited true, nodes can be enqueued multiple times causing infinite loops or repeated visits.
Final Answer:
Visited array is never updated to true for visited nodes -> Option C
Quick Check:
Must mark visited[node] = true after popping [OK]
Hint: Always mark nodes visited after dequeuing [OK]
Common Mistakes:
Forgetting to mark visited nodes
Pushing start node multiple times
Incorrect loop conditions
5. Given a graph with nodes 0 to 4 and edges: 0-1, 0-2, 1-3, 2-3, 3-4. Using BFS starting from node 0, which node is visited at the 3rd position in the BFS order?
hard
A. 2
B. 4
C. 1
D. 3
Solution
Step 1: List neighbors and BFS order
Start at 0, enqueue neighbors 1 and 2. BFS order so far: [0]