What is the main idea behind using BFS to find the shortest path in an unweighted graph?
BFS explores nodes level by level, ensuring the first time we reach a node is via the shortest path from the start node.
Click to reveal answer
beginner
In BFS for shortest path, what data structure is used to keep track of nodes to visit next?
A queue is used to keep nodes in the order they are discovered, ensuring level-wise traversal.
Click to reveal answer
intermediate
How do we keep track of the shortest distance from the start node to every other node in BFS?
We maintain a distance array where distance[node] is updated the first time the node is visited, representing the shortest path length.
Click to reveal answer
intermediate
Why does BFS guarantee the shortest path in an unweighted graph but not necessarily in a weighted graph?
Because BFS treats all edges as equal cost (1 step), it finds the minimum number of edges. Weighted graphs require algorithms like Dijkstra's to consider edge weights.
Click to reveal answer
intermediate
What is the time complexity of BFS when finding the shortest path in an unweighted graph with V vertices and E edges?
The time complexity is O(V + E) because each vertex and edge is processed at most once.
Click to reveal answer
Which data structure is essential for BFS to find the shortest path in an unweighted graph?
AHash Map
BStack
CPriority Queue
DQueue
✗ Incorrect
BFS uses a queue to explore nodes level by level, which is key to finding the shortest path.
What does the distance array store in BFS shortest path algorithm?
AWeight of edges
BNumber of edges from start node to each node
CNumber of nodes in the graph
DOrder of node discovery
✗ Incorrect
The distance array stores the shortest number of edges from the start node to each node.
Why can't BFS be used directly to find shortest paths in weighted graphs?
ABecause BFS does not consider edge weights
BBecause BFS uses a stack
CBecause BFS is too slow
DBecause BFS only works on trees
✗ Incorrect
BFS assumes all edges have equal weight, so it does not handle weighted edges correctly.
What is the initial distance value assigned to all nodes except the start node in BFS shortest path?
A0
B1
C-1 or Infinity
DUndefined
✗ Incorrect
Nodes are initialized with -1 or Infinity to indicate they are not yet visited.
What is the time complexity of BFS for shortest path in an unweighted graph?
AO(V + E)
BO(V^2)
CO(E^2)
DO(log V)
✗ Incorrect
BFS visits each vertex and edge once, so the complexity is O(V + E).
Explain step-by-step how BFS finds the shortest path in an unweighted graph starting from a given node.
Think about how BFS explores neighbors and records distances.
You got /5 concepts.
Describe why BFS is suitable for shortest path in unweighted graphs but not for weighted graphs, and name an algorithm used for weighted graphs.
Compare edge treatment in BFS vs weighted graph algorithms.
You got /3 concepts.
Practice
(1/5)
1. What does BFS (Breadth-First Search) help find in an unweighted graph?
easy
A. The total number of nodes in the graph
B. The shortest path from a start node to all other nodes
C. The longest path between two nodes
D. The heaviest edge in the graph
Solution
Step 1: Understand BFS traversal
BFS explores nodes level by level starting from the source node, visiting all neighbors before moving deeper.
Step 2: BFS finds shortest paths in unweighted graphs
Because BFS visits nodes in order of increasing distance, it finds the shortest path in terms of edges from the start node.
Final Answer:
The shortest path from a start node to all other nodes -> Option B
Quick Check:
BFS = shortest path in unweighted graph [OK]
Hint: BFS explores neighbors level-wise to find shortest paths [OK]
Common Mistakes:
Confusing BFS with DFS for shortest path
Thinking BFS finds longest path
Assuming BFS works only for weighted graphs
2. Which of the following is the correct way to initialize a queue for BFS in C?
easy
A. int queue[MAX]; int front = 0, rear = 0;
B. queue<int> q; // C++ syntax
C. Queue q = new Queue(); // Java syntax
D. var q = []; // JavaScript syntax
Solution
Step 1: Identify C syntax for queue
C does not have built-in queue; we use arrays with front and rear indices to simulate queue.
Step 2: Check options for C correctness
int queue[MAX]; int front = 0, rear = 0; uses array and indices, which is valid in C. Others use non-C syntax.
Final Answer:
int queue[MAX]; int front = 0, rear = 0; -> Option A
Quick Check:
C queue uses array + front/rear indices [OK]
Hint: Use array with front/rear for queue in C [OK]
Common Mistakes:
Using C++ or Java syntax in C code
Not initializing front and rear indices
Assuming queue is built-in in C
3. What is the output distance array after running BFS from node 0 on this graph?
Graph edges:
0 - 1
0 - 2
1 - 3
2 - 3
3 - 4
Assume nodes are 0 to 4 and distance to unreachable nodes is -1.
medium
A. [0, 1, 2, 3, 4]
B. [0, 1, 1, 1, 2]
C. [0, 1, 1, 2, 3]
D. [0, -1, -1, -1, -1]
Solution
Step 1: Start BFS from node 0 and set distance[0] = 0
Initialize distances: distance[0]=0, others = -1.
Step 2: Visit neighbors level-wise
Neighbors of 0: 1 and 2 get distance 1. Then neighbors of 1 and 2: node 3 gets distance 2. Finally, neighbor of 3: node 4 gets distance 3.
Final Answer:
[0, 1, 1, 2, 3] -> Option C
Quick Check:
Distances increase by 1 per BFS level [OK]
Hint: Distance increments by 1 each BFS level from start [OK]
Common Mistakes:
Assigning wrong distances to nodes
Confusing BFS with DFS order
Not initializing unreachable nodes to -1
4. Identify the bug in this BFS code snippet for shortest path: