What if you could explore every path in a maze without ever getting lost or repeating steps?
Why DFS Depth First Search on Graph in DSA C?
Imagine you have a huge maze with many paths and rooms. You want to find if there is a way to reach a certain room starting from the entrance. Trying to check every possible path by hand would be confusing and take forever.
Manually exploring each path can be slow and easy to get lost. You might repeat the same paths again and again or miss some rooms. It is hard to keep track of where you have been and where to go next without a clear plan.
Depth First Search (DFS) helps by exploring one path deeply before backtracking. It uses a simple rule: go as far as possible along one path, then back up and try another. This way, you systematically visit all connected rooms without missing or repeating.
void explore(int current) {
// try all neighbors manually
if (neighbor1 not visited) explore(neighbor1);
if (neighbor2 not visited) explore(neighbor2);
// ...
}void dfs(int current) {
visited[current] = 1;
for (int i = 0; i < graph[current].size(); i++) {
int neighbor = graph[current][i];
if (!visited[neighbor]) dfs(neighbor);
}
}DFS enables efficient and complete exploration of all connected parts of a graph or maze, making complex problems manageable.
Finding all friends connected to you in a social network by exploring friend connections deeply before moving to other groups.
Manual path checking is slow and error-prone.
DFS explores deeply along one path before backtracking.
DFS systematically visits all connected nodes without repetition.
Practice
Solution
Step 1: Understand DFS traversal
DFS explores a graph by going deep into one branch before backtracking to explore others.Step 2: Compare with other traversal methods
BFS visits nodes level by level, but DFS goes deep first.Final Answer:
Explore as far as possible along each branch before backtracking -> Option CQuick Check:
DFS = deep exploration first [OK]
- Confusing DFS with BFS
- Thinking DFS visits nodes level-wise
- Assuming DFS visits nodes randomly
adj and visited array visited?Solution
Step 1: Mark current node as visited
Set visited[node] = 1 (or true) to avoid revisiting.Step 2: Traverse neighbors only if not visited
Check if neighbor is unvisited before recursive DFS call to prevent infinite loops.Final Answer:
void dfs(int node) { visited[node] = 1; for(int i=0; i -> Option DQuick Check:
Mark visited and check before recursion [OK]
- Not checking if neighbor is visited
- Using wrong visited value (0 instead of 1)
- Using .size() instead of .length in C arrays
0: [1, 2]
1: [2]
2: [0, 3]
3: [3]Starting DFS from node 2, what is the order of nodes visited?
Solution
Step 1: Start DFS at node 2
Visit 2, mark visited. Neighbors: 0 and 3.Step 2: Visit neighbor 0 first
From 2, go to 0. Visit 0, neighbors: 1 and 2 (2 already visited).Step 3: Visit neighbor 1 from 0
Visit 1, neighbor: 2 (visited). Backtrack.Step 4: Back to 2, visit neighbor 3
Visit 3, neighbor: 3 (already visited). End.Final Answer:
[2, 0, 1, 3] -> Option BQuick Check:
DFS order from 2 = [2,0,1,3] [OK]
- Visiting nodes multiple times
- Visiting neighbors in wrong order
- Ignoring visited checks causing loops
void dfs(int node) {
visited[node] = 1;
for(int i=0; i<adj[node].length; i++) {
dfs(adj[node][i]);
}
}Solution
Step 1: Analyze DFS recursion calls
The code calls dfs on all neighbors without checking if they are visited.Step 2: Identify risk of infinite recursion
Without visited check, DFS may revisit nodes endlessly causing stack overflow.Final Answer:
Missing check if neighbor is already visited before recursive call -> Option AQuick Check:
Always check visited before recursion [OK]
- Assuming visited check is optional
- Setting visited after recursion
- Ignoring infinite recursion risk
Solution
Step 1: Understand cycle detection in DFS
Detect cycles by tracking nodes currently in recursion (call) stack.Step 2: Use recursion stack array
Maintain an array marking nodes in current DFS path; if a neighbor is in this stack, a cycle exists.Step 3: Differentiate from visited array
Visited marks all visited nodes; recursion stack tracks current path only.Final Answer:
Use a recursion stack array to track nodes in the current path and check if a node is revisited in this stack -> Option AQuick Check:
Recursion stack detects cycles in DFS [OK]
- Confusing visited with recursion stack
- Using BFS for cycle detection in directed graphs
- Not tracking current path nodes
Ready to prove it?
Pass the test and this pattern moves unknown
