Explaning the backtracking algorithm

Jesús
4 min readAug 19, 2023

In the world of computer science, algorithms serve as the building blocks for solving complex problems efficiently and elegantly.

One such algorithmic technique that stands out for its ability to navigate intricate decision spaces and find solutions is the backtracking algorithm.

In this story I will provide with a graphical visualization of the algorithm and the code in Python.

To illustrate the concept concretely, we’ll take a deep dive into one of the most captivating problem-solving scenarios: the N-Queens problem.

This classic puzzle tasks us with strategically placing N queens on an N×N chessboard without any of them being able to attack each other. We’ll witness how the backtracking algorithm artfully navigates through the possibilities, creating a synergy between exploration and decision-making that leads us to valid solutions.

How does the algorithm work?

Backtracking is like solving a puzzle step by step. You make a choice, and if it leads to a problem, you go back and try a different choice. It’s like exploring a maze by trying different paths and going back when you hit a dead end.

Backtracking helps find solutions when there are many possibilities to consider. It’s a patient and systematic approach…

--

--

Jesús

I am an engineer and I love doing different projects in different programming languages, being Python and Excel the languages I use the most