# The Framework for Backtracking Algorithm

**Translator**: xiaodp

**Author**: labuladong

This article is an advanced version of "Details of Backtracking Algorithms" before. The previous one isn't clear enough, so you don't need to read it and just read this article.

Ponder carefully and you will find that the backtracking problems follow the same pattern, that is, have the same framework.

Let's go straight to the framework backtracking follows. **Solving a backtracking problem is actually a traversal process of a decision tree.** Now you only need to think about 3 terms:

**Path**: the selection that have been made.**Selection List**: the selection you can currently make.**End Condition**: the condition under which you reach the bottom of the decision tree, and can no longer make a selection.

It doesn’t matter if you don’t understand the explanation of the 3 terms. I will use the two classic backtracking algorithm problems,`Permutation`

and `N Queen Problem`

to help you understand what they mean. Before this, you just keep them in mind.

Here shows the pseudocode of the framework:

**The core is the recursion in the for loop. It ****makes a selection**** before the recursive call and ****undoes the selection**** after the recursive call**, which is especially simple.

Then what `makes a selection`

and `undo the selection`

means? and what is the underlying principle of this framework? Let's use `Permutation`

to solve your questions and explore the underlying principle in detail.

## Permutation

You must have learned the permutations and combinations. As we know, for $N$ unique numbers, the number of full permutations is $N!$.

`note`

: For simplicity and clarity, **the full permutation problem we are discussing this time does not contain duplicate numbers**.

Think about how we find out all the permutations. If you are given three numbers `[1,2,3]`

, you may follow these steps:

Fix the first number to 1;

Then the second number can be 2;

If the second number is 2, then the third number can only be 3;

Then you can change the second number to 3 and the third number can only be 2;

Then you can only change the first place,and repeat 2-4.

In fact, this is the ''backtracking''. You can use it even without a teacher! The following figure shows the backtracking tree:

Just traverse this tree from the root to the leaves and record the numbers on the paths, and you will get all the permutations. **We might as well call this tree a “decision tree” for backtracking** for you're actually making decisions on each node. For instance, if you are now at the red node, you will making a decision between the "1" branch and "3" branch. Why only 1 and 3? Because the "2" branch is behind you, you have made this selection before, and the full permutation is not allowed to reuse numbers.

**Now you can understand the terms mentioned before more specifically: ****[2]**** is the “Path”, which records the selections you have made; ****[1,3]**** is the “Selection List”, which means the current selections you can make; ****End Condition**** is to traverse to the bottom of the decision tree(here is when the Selection List is empty)**.

If you understand these terms, **you can use the "Path" and "Selection List" as attributes of each node in the decision tree**. For example, the following figure lists the attributes of several nodes

**The function ****backtrack()**** we defined is actually like a pointer. It is necessary to walk on the tree and maintain the attributes of each node correctly. Whenever it reaches the bottom of the tree, its “Path” is a full permutation**.

Furthermore, how to traverse a tree? it should not be difficult. Recall from the previous article *Framework Thinking of Learning Data Structures*, various search problems are actually tree traversal problems, and the multi-tree traversal framework is:

The so-called preorder traversal and postorder traversal are just two very useful time points. The following picture will make you more clear:

**Preorder travers is executed at the time point before entering a node, and postorder traversal is executed at the time point after leaving a node**.

Recalling what we just said:"Path" and "Selection List" are attributes of each node. If want the function to maintain the attributes of the node correctly, we must do something at these two special time points:

Now, do you understand the core framework of backtracking?

**As long as we make a selection before recursion and undo the previous selection after recursion**, we can get the Selection List and Path of each node correctly.

Here shows the code for the full permutation:

We made a few changes here: instead of explicitly recording the "selection List", we use `nums`

and `track`

to deduce the current selection list:

So far, we have explained the underlying principle of the backtracking through the full permutation problem. Of course, this algorithm is not very efficient, and using the `contains`

method for linked list requires $O(N)$ time complexity. There are better ways to achieve the purpose by exchanging elements which are more difficult to understand. I won't discuss them in this article. If you are interested, you can google related knowledge by yourself.

However, it must be noted that no matter how optimized, it conforms to the backtracking framework, and the time complexity cannot be lower than $O (N!)$.Because exhaustion of the entire decision tree is unavoidable. **This is also a feature of backtracking. Unlike dynamic programming having overlapping subproblems which can be optimized, backtracking is purely violent exhaustion, and time complexity is generally high**.

After understanding the full permutation problem, you can directly use the backtracking framework to solve some problems. Let's take a brief look at the `N Queen`

problem.

## N Queen Problem

This is a classical problem: place $N$ non-attacking queens on an $N{\times}N$ chessboard. Thus, a solution requires that no two queens share the same row, column, or diagonal.

This problem is essentially similar to the full permutation problem. If we build a decision tree, each layer of the decision tree represents each row on the chessboard. And the selection that each node can make is to place a queen on any column of the row.

Apply the backtracking framework directly:

This part of the code is actually similar to the full permutation problem. The implementation of the `isValid()`

is also very simple.：

The function `backtrack()`

still looks like a pointer walking in the decision tree. The position traversed by the `backtrack()`

can be represented by`row`

and `col`

, and the unqualified condition can be pruned by the `isValid()`

:

If you are facing such a chunk of solution code directly, you may feel very puzzled. But if you understand the framework of backtracking, it is not difficult to understand the solution code. Based on the framework, the changes are just the way of making selection and excluding illegal selections. As long as you keep the framework in mind, you are left with only minor issues.

When $N = 8$, it is the eight queens problem. Gauss, the mathematics prince , spent his whole life not counting all possible ways to place, but our algorithm only needs one second .But don't blame Gauss, the complexity of this problem is indeed very high. Look at our decision tree, although there is a pruning by the `isValid()`

, the worst time complexity is still $O (N ^ {N + 1})$.And it cannot be optimized. If $N = 10$, the calculation is already rather time consuming.

**When we don't want to get all legal answers but only one answer, what should we do ?** For example, the algorithm for solving Sudoku is too complicated to find all the solutions and one solution is enough.

In fact, it is very simple. Just modify the code of the backtracking slightly:

After this modification, as long as an answer is found, subsequent recursion of the for loop will be blocked. Maybe you can slightly modify the code of the N queen problem and write an algorithm to solve Sudoku?

## Conclusion

Backtracking is a multi-tree traversal problem. The key is to do some operations at the positions of pre-order traversal and postorder traversal. The algorithm framework is as follows:

**When writing the ****backtrack()**** function, you need to maintain the “Path” you have traveled and the "selection List” you currently have. When the “End Condition” is triggered, record the “Path” in the result set**.

Think carefully, is the backtracking and dynamic programming somehow similar? We have repeatedly emphasized in the series of articles about dynamic planning that the three points that need to be clear in dynamic programming are "State", "selection" and "Base Case". Do they correspond to the "Path" that has passed, and the current "selection List" And "End Condition "?

To some extent, the brute-force solution phase of dynamic programming is a backtracking. When some problems have overlapping sub-problems, you can use dp table or memo to greatly prune the recursive tree, which becomes dynamic programming. However, today's two problems do not have overlapping subproblems, that is, the problem of backtracking, and the high complexity is inevitable.

Last updated