What if you could explore a huge maze without ever getting lost or wasting time on dead ends?
Why BFS Breadth First Search on Graph in DSA Typescript?
Imagine you are in a huge maze and want to find the shortest path to the exit. You try to explore every path by yourself, going deep into one path before checking others.
This can be confusing and you might get lost or take a very long time to find the exit.
Manually exploring each path deeply means you might waste time going down long dead ends first.
You can forget where you came from or miss shorter paths nearby.
This approach is slow and error-prone when the maze or network is big.
BFS (Breadth First Search) helps by exploring all paths one step at a time, level by level.
It uses a queue to remember which places to visit next, ensuring you find the shortest path quickly and without confusion.
function exploreMaze(start) {
// Go deep first, might get lost
if (start is exit) return;
for each neighbor {
exploreMaze(neighbor);
}
}function bfs(start) {
let queue = [start];
while (queue.length > 0) {
let current = queue.shift();
visit(current);
add unvisited neighbors to queue;
}
}BFS enables you to find the shortest path or explore all connected points efficiently and clearly.
Social networks use BFS to find friends of friends or suggest connections by exploring nearby people step by step.
BFS explores neighbors level by level using a queue.
It finds shortest paths in unweighted graphs.
It avoids getting lost by visiting nodes in order of distance.
Practice
Solution
Step 1: Understand BFS traversal method
BFS explores nodes level by level, so it needs a structure that processes nodes in the order they are discovered.Step 2: Identify the data structure used
A queue follows First-In-First-Out (FIFO) order, which matches BFS's level order traversal.Final Answer:
A queue to process nodes level by level -> Option DQuick Check:
BFS uses a queue = A [OK]
- Confusing BFS with DFS which uses a stack
- Thinking BFS uses a priority queue
- Assuming BFS only uses a visited set without a queue
Solution
Step 1: Recognize TypeScript array initialization
To create a queue using an array, declare an array with type annotation and initialize it empty.Step 2: Check each option
const queue: number[] = []; correctly declares an empty number array. const queue = new Queue<number>(); uses a class Queue which is not built-in. let queue = {}; initializes an object, not a queue. var queue = new ArrayQueue(); uses a non-existent ArrayQueue.Final Answer:
const queue: number[] = []; -> Option BQuick Check:
Array queue syntax = A [OK]
- Using undefined classes like Queue without import
- Initializing queue as an object {} instead of array
- Using var instead of const or let (less preferred)
const graph = {
0: [1, 2],
1: [3],
2: [3],
3: []
};
Solution
Step 1: Start BFS from node 0
Visit node 0 first, then enqueue its neighbors 1 and 2.Step 2: Visit neighbors in queue order
Dequeue 1, visit it, enqueue its neighbor 3. Then dequeue 2, visit it, enqueue 3 again but already visited. Finally, dequeue 3.Final Answer:
[0, 1, 2, 3] -> Option AQuick Check:
BFS order = [0, 1, 2, 3] [OK]
- Visiting neighbors in wrong order
- Visiting node 3 twice
- Starting BFS from wrong node
function bfs(graph: Record, start: number) {
const queue: number[] = [start];
const visited = new Set<number>();
while (queue.length > 0) {
const node = queue.shift();
if (!visited.has(node)) {
visited.add(node);
for (const neighbor of graph[node]) {
queue.push(neighbor);
}
}
}
return Array.from(visited);
}Solution
Step 1: Analyze queue.shift() usage
queue.shift() returns number | undefined. If queue is empty, node can be undefined, causing errors when accessing graph[node].Step 2: Identify missing check for undefined
Code should check if node is undefined before using it. Without this, runtime error can occur.Final Answer:
queue.shift() can return undefined, causing errors -> Option AQuick Check:
Check for undefined after shift = B [OK]
- Ignoring possible undefined from shift()
- Assuming visited set is wrong when it is correct
- Thinking neighbors are not added when they are
const graph = {
0: [1, 2],
1: [3],
2: [3, 4],
3: [4],
4: []
};
Which BFS modification helps find the shortest path length correctly?Solution
Step 1: Understand BFS for shortest path
BFS finds shortest paths in unweighted graphs by exploring neighbors level by level.Step 2: Track distance when visiting nodes
Maintain a distance map or array to record the number of edges from start node to each node when first visited.Final Answer:
Track distance for each node when first visited -> Option CQuick Check:
Distance tracking in BFS = D [OK]
- Using stack breaks BFS order
- Visiting nodes multiple times wastes time
- Ignoring visited set causes infinite loops
Ready to prove it?
Pass the test and this pattern moves unknown
