Member-only story

Backtracking for Beginners — Fundamental computer science concept

In this post, we’re going to delve into one of the most powerful, versatile and widely-used techniques known as Backtracking.

Tech Sauce
6 min readJun 20, 2023

What is Backtracking?

At its core, backtracking is an algorithmic-technique for solving problems recursively by trying to build a solution incrementally, one piece at a time. If a solution that we’ve begun building can’t be completed into a valid solution (due to constraints or rules of the problem), we ‘backtrack’ to the last valid state and try a different path.

Picture yourself lost in a maze, where you can only move forward. Each time you reach a dead-end, you backtrack to the previous intersection and try a different path. Eventually, using this method, you’ll explore the whole maze. That’s backtracking!

Backtracking is used in many algorithms and computer science problems, including:

  1. Combinatorial problems (e.g., generating all permutations of a string)
  2. Constraint Satisfaction Problems (e.g., Sudoku, N-Queens Problem)
  3. Puzzles and games (e.g., maze-solving, Chess)

Let’s understand it further with different variations of backtracking.

Variations of Backtracking

1. Pure Backtracking

--

--

Tech Sauce
Tech Sauce

Written by Tech Sauce

Everything Software Engineering ...

No responses yet