Shortest Path in Unweighted Graph Using BFS in DSA C# - Time & Space Complexity
We want to understand how long it takes to find the shortest path in an unweighted graph using BFS.
How does the time needed grow as the graph gets bigger?
Analyze the time complexity of the following code snippet.
// BFS to find shortest path in unweighted graph
void BFS(int start, List<int>[] graph, int[] distance) {
Queue<int> queue = new Queue<int>();
queue.Enqueue(start);
distance[start] = 0;
while (queue.Count > 0) {
int node = queue.Dequeue();
foreach (int neighbor in graph[node]) {
if (distance[neighbor] == -1) {
distance[neighbor] = distance[node] + 1;
queue.Enqueue(neighbor);
}
}
}
}
This code finds the shortest distance from a start node to all other nodes in an unweighted graph using BFS.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Visiting each node and checking its neighbors.
- How many times: Each node is enqueued and dequeued once; each edge is checked once.
As the graph grows, the time grows based on nodes and edges.
| Input Size (n nodes, m edges) | Approx. Operations |
|---|---|
| 10 nodes, 15 edges | About 10 + 15 = 25 operations |
| 100 nodes, 200 edges | About 100 + 200 = 300 operations |
| 1000 nodes, 5000 edges | About 1000 + 5000 = 6000 operations |
Pattern observation: The work grows roughly with the number of nodes plus edges.
Time Complexity: O(n + m)
This means the time grows linearly with the number of nodes plus edges in the graph.
[X] Wrong: "BFS takes O(n²) time because it uses nested loops."
[OK] Correct: BFS visits each node and edge only once, so it does not check all pairs of nodes.
Understanding BFS time complexity helps you explain how graph searches scale and why BFS is efficient for shortest paths in unweighted graphs.
"What if the graph was represented as an adjacency matrix instead of adjacency lists? How would the time complexity change?"
Practice
Solution
Step 1: Understand BFS traversal
BFS explores nodes level by level starting from the source node.Step 2: BFS finds shortest paths in unweighted graphs
Because BFS visits nodes in order of increasing distance, it finds shortest paths.Final Answer:
The shortest path from a start node to all other nodes -> Option DQuick Check:
BFS = Shortest path in unweighted graph [OK]
- Confusing BFS with DFS for shortest path
- Thinking BFS finds longest path
- Assuming BFS counts edges incorrectly
Solution
Step 1: Identify BFS traversal order
BFS uses FIFO order to explore nodes level by level.Step 2: Choose data structure matching FIFO
Queue<T> provides FIFO behavior, perfect for BFS.Final Answer:
Queue<T> -> Option AQuick Check:
Queue = FIFO for BFS [OK]
- Using Stack<T> which is LIFO, wrong for BFS
- Using List<T> without queue behavior
- Confusing Dictionary with queue
Graph edges: 0 - 1 0 - 2 1 - 3 2 - 3 3 - 4Assume distance to start node 0 is 0 and unreachable nodes have distance -1.
Solution
Step 1: Initialize distances and start BFS from node 0
Distance[0] = 0; others = -1 initially.Step 2: Visit neighbors level-wise
From 0: nodes 1 and 2 get distance 1. From 1 and 2: node 3 gets distance 2. From 3: node 4 gets distance 3.Final Answer:
[0, 1, 1, 2, 3] -> Option AQuick Check:
Distances = [0,1,1,2,3] [OK]
- Assigning wrong distances to nodes
- Confusing BFS with DFS distances
- Not initializing unreachable nodes as -1
int[] dist = new int[n];
for (int i = 0; i < n; i++) dist[i] = -1;
Queue<int> q = new Queue<int>();
dist[start] = 0;
q.Enqueue(start);
while (q.Count > 0) {
int u = q.Dequeue();
foreach (int v in graph[u]) {
if (dist[v] == 0) {
dist[v] = dist[u] + 1;
q.Enqueue(v);
}
}
}Solution
Step 1: Understand distance initialization and visit check
Distances start at -1 for unvisited nodes; 0 means visited or start node.Step 2: Check condition for visiting neighbors
Condition 'dist[v] == 0' wrongly treats unvisited nodes as visited; should be 'dist[v] == -1'.Final Answer:
dist[v] == -1 to check unvisited nodes -> Option CQuick Check:
Check unvisited with -1, not 0 [OK]
- Checking dist[v] == 0 instead of -1
- Using Stack instead of Queue for BFS
- Incorrect initial distance for start node
0-1, 0-2, 1-3, 3-4, 4-5What is the shortest path from node 0 to node 5 using BFS?
Solution
Step 1: Run BFS from node 0 to find shortest distances
Distances: 0(0), 1(1), 2(1), 3(2), 4(3), 5(4).Step 2: Trace shortest path back from node 5
From 5 to 4, 4 to 3, 3 to 1, 1 to 0: 0->1->3->4->5.Final Answer:
[0, 1, 3, 4, 5] -> Option BQuick Check:
Shortest path = [0,1,3,4,5] [OK]
- Attempting path via node 2 (no 2-3 edge)
- Skipping nodes in path
- Confusing BFS with DFS path
Ready to prove it?
Pass the test and this pattern moves unknown
