Your First Library Job

When you apply for a job, it is a good idea to know what to expect. Working in a library is a great job. Preparing for your interview so that you can stand out from other applicants is very…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Simple Sudoku with Backtracking

When I first started preparing for technical interviews, I was spending tons of time learning different data structures, algorithms and time complexity. I was able to get a general grasp on different concepts through amazing medium articles and hacker rank problems, but I was unable to get a good grasp on the concept of backtracking because it was so theoretical and most of the problems were complex interview problems.

This article hopes to be that missing piece that allows you to use a real-world example with simple explanations so you can quickly get back to interview preparation, or use backtracking in your own program to do something cool!

Through the course of this article, we are going to explore the principle of backtracking by using it to solve a game of Sudoku. If you have never heard of Sudoku before, it is a logic-based puzzle game that requires you to combine numbers to come completely fill the board. The objective is for you to fill a 9x9 grid so that each column, each row, and each of the nine 3x3 subgrids that compose the grid contain all the digits from 1–9. When starting, some of the grids are partially filled in. Here is an example of a starting board.

And here is the solution to the above board. Notice that every row and every column have the digits from 1 to 9.

So far I have mentioned the term “backtracking” a fair amount, but what the hell is it? Backtracking is a general algorithm for finding all (or some) solutions to a problem that incrementally builds candidates to the solution. As soon as it determines that a candidate can not possibly be the solution to the problem, it abandons it (“backtracks”).

When the algorithm abandons a candidate, it will typically return to a previous stage in the problem-solving process. This is the key to the algorithm and also where it gets its name.

The most common example of this, one that you have probably heard of, and one that is very theoretical is the eight queens puzzle. Eight queens asks for an arrangement of eight chess queens on a standard chessboard so that no queen attacks any of the others.

In the common backtracking approach to this problem, the partial candidates are arrangements of k queens in the first k rows of the board, all in different rows and columns. Any partial solution that contains two mutually attacking queens can be abandoned.

N-Queens Problem — Source

Apart from the original problem and end goal, backtracking usually has a set of constraints that the program must also follow to be effective.

The most simple (and ‘dumbest’) implementations of backtracking will often use very little “logic” or have very little “insight” into the problem. Instead of being an efficient solution, this will typically lead to the program guessing randomly and will often be no better than a straight-up brute force solution.

Now that you know what Sudoku is and what backtracking is, let's go over the constraints and the goal that our algorithm will need to solve Sudoku, and then we will walk through a simple 3x3 problem as if we were the algorithm. The following are the constraints and goals for our backtracking algorithm.

Remember, the constraints for the project are defined by verifying each individual candidate. For Sudoku, a candidate is valid if the following constraints are true.

The goal of our algorithm is also the goal of the game, as well as being very similar to our constraints. In Sudoku, we only have one goal.

Most backtracking algorithms also have some sort of termination condition so that they do not loop infinitely looking for a solution to a problem that has no solution. In the case of Sudoku, we only have two termination conditions.

The above link links to a trinket that contains the code, and an example, of the Sudoku Solver in action. It shows you when the algorithm backtracks when a candidate is no longer viable.

I hope that this visualization will show you how the backtrack works in action, and the code provides a nice step by step with the added comments on how the backtracking process works.

So now that you are a master of solving Sudoku, where should you go next? If you want to check out some other problems that you can solve, I would recommend the following two to really cement your knowledge of backtracking.

If you want to learn more about backtracking and its use cases, here are a few varying articles to help deepen your understanding.

Thanks for reading. I hope you now have a deeper understanding of backtracking a useful case for it!

If you want to check out other stuff that I have done, check me out on Github.

Add a comment

Related posts:

Put This To Bed

It would be entirely appropriate to define the word settle in modern terms as “put it to bed.” This thing is laid down, at rest. It’s not active in any way; it’s not changing posture, position, or…

Digital Marketing. A Journey!

Marketing can be termed as the teaching process of creating, communicating, delivering, and exchanging which offers value for customers. It is the process of convincing consumers to opt for your…

Changes

Today my brother and nephew came over to help me move furniture. Basically, we turned our one bedroom one bathroom apartment into a two bedroom (no living room) two “bathroom” apartment. I’m so…