0
0
DSA Cprogramming~10 mins

Word Search in Grid Using Backtracking in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Word Search in Grid Using Backtracking
Start at each cell in grid
Check if cell matches first letter
If match: Explore neighbors (up, down, left, right)
Mark cell as visited
Recursively check next letter in neighbors
If all letters matched -> Success
If dead end -> Backtrack (unmark cell)
Try next neighbor or next start cell
End: Word found or all cells tried
The algorithm tries each cell as a start, then explores neighbors recursively to match the word letters, backtracking when stuck.
Execution Sample
DSA C
bool exist(char** board, int rows, int cols, char* word) {
  for (int i = 0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {
      if (backtrack(board, rows, cols, word, i, j, 0)) return true;
    }
  }
  return false;
}
This code tries each cell as a start and calls backtrack to find the word recursively.
Execution Table
StepOperationCurrent Cell (row,col)Word IndexVisited CellsActionVisual State
1Start search--{}Check cell (0,0)[A][B][C] [D][E][F] [G][H][I]
2Match first letter 'A'(0,0)0{(0,0)}Mark visited, check neighbors for 'B'[*A][B][C] [D][E][F] [G][H][I]
3Check neighbor (0,1)(0,1)1{(0,0),(0,1)}Match 'B', continue[*A][*B][C] [D][E][F] [G][H][I]
4Check neighbor (0,2)(0,2)2{(0,0),(0,1),(0,2)}Match 'C', continue[*A][*B][*C] [D][E][F] [G][H][I]
5Word complete-3{(0,0),(0,1),(0,2)}All letters matched[*A][*B][*C] [D][E][F] [G][H][I]
6Return success---Stop search[*A][*B][*C] [D][E][F] [G][H][I]
💡 Word found at cells (0,0) -> (0,1) -> (0,2), all letters matched
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
Current Cell-(0,0)(0,1)(0,2)--
Word Index001233
Visited Cells{}{(0,0)}{(0,0),(0,1)}{(0,0),(0,1),(0,2)}{(0,0),(0,1),(0,2)}{(0,0),(0,1),(0,2)}
ActionStartMark (0,0)Mark (0,1)Mark (0,2)CompleteStop
Key Moments - 3 Insights
Why do we mark cells as visited during backtracking?
Marking cells as visited prevents revisiting the same cell in the current path, avoiding infinite loops. See Step 2 and Step 3 where cells are marked before exploring neighbors.
What happens if a neighbor cell does not match the next letter?
The algorithm backtracks by unmarking the current cell and tries other neighbors or start cells. This is implied after Step 4 if no match is found, but in this example, all letters matched.
Why do we try every cell as a starting point?
Because the word can start anywhere in the grid, the algorithm tries all cells to ensure no possible start is missed. This is shown in Step 1 where search begins at (0,0).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the set of visited cells at Step 3?
A{(0,0),(0,1)}
B{(0,1)}
C{(0,0)}
D{}
💡 Hint
Check the 'Visited Cells' column at Step 3 in the execution_table.
At which step does the algorithm confirm the word is fully matched?
AStep 4
BStep 3
CStep 5
DStep 6
💡 Hint
Look for the 'Word complete' operation in the execution_table.
If the first letter did not match at (0,0), what would happen next?
ABacktrack and try neighbors of (0,0)
BTry next cell (0,1) as start
CStop search immediately
DMark (0,0) as visited anyway
💡 Hint
Refer to the concept_flow where each cell is tried as a start if no match.
Concept Snapshot
Word Search using Backtracking:
- Start from each cell in grid
- Check if cell matches current letter
- Mark cell visited and explore neighbors (up, down, left, right)
- Recursively match next letter
- Backtrack if no match
- Success if all letters matched
Full Transcript
This visualization shows how the word search algorithm uses backtracking to find a word in a grid. It starts from each cell, checks if the letter matches the first letter of the word, then recursively explores neighbors for the next letters. Cells are marked visited to avoid reuse in the same path. If a path fails, it backtracks by unmarking cells and tries other paths. The execution table tracks each step, showing current cell, word index, visited cells, and the grid state. Key moments clarify why marking visited is important, how backtracking works, and why all cells are tried as start points. The quiz tests understanding of visited cells, completion step, and search flow. The snapshot summarizes the backtracking approach in simple steps.