| Difficulty | Easy | ||
|---|---|---|---|
| Source | 160 Days of Problem Solving | ||
| Tags |
|
The problem can be found at the following link: Question Link
Given a connected undirected graph represented by a 2D adjacency list adj[][], where adj[i] represents the list of vertices connected to vertex i.
Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right as per the given adjacency list.
Return a list containing the BFS traversal of the graph.
Note: Traverse nodes in the same order as given in the adjacency list.
Note: Sorry for uploading late, my exam is going on.
adj = [[1, 2, 3],
[0],
[0, 4],
[0],
[2]]
[0, 1, 2, 3, 4]
Starting from vertex 0:
- Visit 0 → Output:
[0] - Visit 2 (first neighbor of 0) → Output:
[0, 2] - Visit 3 (next neighbor of 0) → Output:
[0, 2, 3] - Visit 1 (next neighbor of 0) → Output:
[0, 2, 3] - Visit 4 (neighbor of 2) → Final Output:
[0, 2, 3, 1, 4]
adj = [[1, 2],
[0, 3],
[0, 3, 4],
[1, 2],
[2]]
[0, 1, 2, 3, 4]
Starting from vertex 0:
- Visit 0 → Output:
[0] - Visit 1 (first neighbor of 0) → Output:
[0, 1] - Visit 2 (next neighbor of 0) → Output:
[0, 1, 2] - Visit 3 (first unvisited neighbor of 2) → Output:
[0, 1, 2, 3] - Visit 4 (next neighbor of 2) → Final Output:
[0, 1, 2, 3, 4]
-
$1 \leq$ adj.size()$\leq 10^4$ -
$1 \leq$ adj[i][j]$\leq 10^4$
- Maintain a visited array to track visited nodes.
- Use a queue to process nodes in a FIFO manner.
- Start BFS traversal from node
0and enqueue it. - Process nodes from the queue and visit their unvisited neighbors in order.
- Store the BFS traversal sequence in a list.
- Expected Time Complexity: O(V + E), since each vertex and edge is visited once.
- Expected Auxiliary Space Complexity: O(V), as we store the visited array and queue.
class Solution {
public:
vector<int> bfs(vector<vector<int>>& adj) {
int V = adj.size();
vector<int> res;
vector<bool> vis(V, false);
queue<int> q;
q.push(0);
vis[0] = true;
while (!q.empty()) {
int v = q.front();
q.pop();
res.push_back(v);
for (int u : adj[v]) {
if (!vis[u]) {
vis[u] = true;
q.push(u);
}
}
}
return res;
}
};- Use a helper function for recursion.
- Process the front element of the queue.
- Enqueue unvisited neighbors and call the function recursively.
class Solution {
public:
void bfsUtil(queue<int>& q, vector<vector<int>>& adj, vector<int>& res, vector<bool>& vis) {
if (q.empty()) return;
int v = q.front();
q.pop();
res.push_back(v);
for (int u : adj[v]) {
if (!vis[u]) {
vis[u] = true;
q.push(u);
}
}
bfsUtil(q, adj, res, vis);
}
vector<int> bfs(vector<vector<int>>& adj) {
vector<int> res;
vector<bool> vis(adj.size(), false);
queue<int> q;
q.push(0);
vis[0] = true;
bfsUtil(q, adj, res, vis);
return res;
}
};- ✅ Time Complexity: O(V + E) - Each vertex and edge are processed once.
- ✅ Space Complexity: O(V) - Due to the recursion stack.
- Uses recursion instead of iteration, which may be preferred in some functional programming paradigms.
- However, recursion depth could lead to stack overflow for large graphs.
- Iterate through all vertices to ensure that all components are covered.
- If a vertex is not visited, initiate BFS from it.
- This ensures traversal of all disconnected components.
class Solution {
public:
vector<int> bfs(vector<vector<int>>& adj) {
int V = adj.size();
vector<int> res;
vector<bool> vis(V, false);
for (int i = 0; i < V; i++) {
if (!vis[i]) {
queue<int> q;
q.push(i);
vis[i] = true;
while (!q.empty()) {
int v = q.front();
q.pop();
res.push_back(v);
for (int u : adj[v]) {
if (!vis[u]) {
vis[u] = true;
q.push(u);
}
}
}
}
}
return res;
}
};- ✅ Time Complexity: O(V + E) - Each vertex and edge are processed once.
- ✅ Space Complexity: O(V) - Due to the queue and visited array.
- Handles disconnected graphs, ensuring all components are explored.
- Slightly more complex than basic BFS but necessary for completeness.
- Instead of
queue<int>, we usedeque<int>for optimized front and back operations. - The traversal logic remains the same as the standard BFS approach.
class Solution {
public:
vector<int> bfs(vector<vector<int>>& adj) {
int V = adj.size();
vector<int> res;
vector<int> vis(V, 0);
deque<int> q;
vis[0] = 1;
q.push_back(0);
while (!q.empty()) {
int v = q.front();
q.pop_front();
res.push_back(v);
for (int u : adj[v]) {
if (!vis[u]) {
vis[u] = 1;
q.push_back(u);
}
}
}
return res;
}
};- ✅ Time Complexity: O(V + E) - Each vertex and edge are processed once.
- ✅ Space Complexity: O(V) - Due to the deque and visited array.
- Using a
dequecan slightly improve performance in some cases due to optimized operations compared toqueue<int>. - Useful when frequent push/pop operations from both ends are required.
| Approach | ⏱️ Time Complexity | 🗂️ Space Complexity | ✅ Pros | |
|---|---|---|---|---|
| Standard BFS (Queue) | 🟢 O(V + E) | 🟡 O(V) | Simple and widely used | Fails for disconnected graphs |
| Recursive BFS | 🟢 O(V + E) | 🟡 O(V) | Recursive and intuitive | Risk of stack overflow for large graphs |
| BFS for Disconnected Graphs | 🟢 O(V + E) | 🟡 O(V) | Ensures traversal of all components | Slightly more complex than basic BFS |
| BFS Using (Deque) | 🟢 O(V + E) | 🟡 O(V) | Optimized performance using deque |
Marginal improvement over normal queue |
✅ Best Choice?
- Use Standard BFS if the graph is connected and efficiency is the priority.
- Use BFS for Disconnected Graphs when handling multiple components.
- Use Recursive BFS only if recursion depth is not an issue.
- Use Deque BFS if frequent front and back operations are needed.
class Solution {
public ArrayList<Integer> bfs(ArrayList<ArrayList<Integer>> adj) {
ArrayList<Integer> r = new ArrayList<>();
boolean[] v = new boolean[adj.size()];
Queue<Integer> q = new LinkedList<>();
q.add(0);
v[0] = true;
while (!q.isEmpty()) {
int i = q.poll();
r.add(i);
for (int j : adj.get(i)) {
if (!v[j]) {
v[j] = true;
q.add(j);
}
}
}
return r;
}
}from collections import deque
class Solution:
def bfs(self, adj):
r, v = [], [False] * len(adj)
q = deque([0])
v[0] = True
while q:
i = q.popleft()
r.append(i)
for j in adj[i]:
if not v[j]:
v[j] = True
q.append(j)
return rFor discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Let’s make this learning journey more collaborative!
⭐ If you find this helpful, please give this repository a star! ⭐