mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-20 10:44:03 +08:00
Retranslate the complete English edition from Simplified Chinese (#2010)
* Retranslate the complete English edition from Simplified Chinese * Polish three English explanations after reader-focused review
This commit is contained in:
@@ -1,45 +1,45 @@
|
||||
# Backtracking Algorithm
|
||||
# Backtracking algorithms
|
||||
|
||||
<u>The backtracking algorithm</u> is a method for solving problems through exhaustive search. Its core idea is to start from an initial state and exhaustively search all possible solutions. When a correct solution is found, it is recorded. This process continues until a solution is found or all possible choices have been tried without finding a solution.
|
||||
A <u>backtracking algorithm</u> solves a problem through exhaustive search. Starting from an initial state, it explores possible solutions by brute force, recording valid solutions as it finds them, until a solution is found or every possible choice has been tried without success.
|
||||
|
||||
The backtracking algorithm typically employs "depth-first search" to traverse the solution space. In the "Binary Tree" chapter, we mentioned that preorder, inorder, and postorder traversals all belong to depth-first search. Next, we will construct a backtracking problem using preorder traversal to progressively understand how the backtracking algorithm works.
|
||||
Backtracking usually traverses the solution space through depth-first search. In the binary tree chapter, we learned that preorder, inorder, and postorder traversal are all forms of depth-first search. We will use preorder traversal to build a backtracking problem and explore the approach step by step.
|
||||
|
||||
!!! question "Example 1"
|
||||
|
||||
Given a binary tree, search and record all nodes with value $7$, and return a list of these nodes.
|
||||
Given a binary tree, find and record every node with value $7$. Return a list of these nodes.
|
||||
|
||||
For this problem, we perform a preorder traversal of the tree and check whether the current node's value is $7$. If it is, we add the node to the result list `res`. The relevant implementation is shown in the following figure and code:
|
||||
Traverse the tree in preorder and check whether the current node's value is $7$. If so, add that node's value to the result list `res`. The code and figure below show the process:
|
||||
|
||||
```src
|
||||
[file]{preorder_traversal_i_compact}-[class]{}-[func]{pre_order}
|
||||
```
|
||||
|
||||

|
||||

|
||||
|
||||
## Attempt and Backtrack
|
||||
## Trying choices and backtracking
|
||||
|
||||
**The reason it is called a backtracking algorithm is that it employs "attempt" and "backtrack" strategies when searching the solution space**. When the algorithm encounters a state where it cannot continue forward or cannot find a solution that satisfies the constraints, it will undo the previous choice, return to a previous state, and try other possible choices.
|
||||
**Backtracking gets its name from its pattern of trying choices and undoing them while searching the solution space**. When the search reaches a state that cannot advance or lead to a valid solution, it undoes the last choice, returns to the previous state, and tries other possibilities.
|
||||
|
||||
For Example 1, visiting each node represents an "attempt", while skipping over a leaf node or the `return` that brings the traversal back to the parent node represents a "backtrack".
|
||||
In Example 1, visiting a node is a trial, while a `return` after passing a leaf or returning to a parent is backtracking.
|
||||
|
||||
It is worth noting that **backtracking is not limited to function returns alone**. To illustrate this, let's extend Example 1 slightly.
|
||||
**Backtracking involves more than returning from a function**. To see why, let's extend Example 1.
|
||||
|
||||
!!! question "Example 2"
|
||||
|
||||
In a binary tree, search all nodes with value $7$, **and return the paths from the root node to these nodes**.
|
||||
Find every node with value $7$ in a binary tree, and **return the paths from the root to those nodes**.
|
||||
|
||||
Based on the code from Example 1, we need to use a list `path` to record the path of visited nodes. When we reach a node with value $7$, we copy `path` and add it to the result list `res`. After traversal is complete, `res` contains all the solutions. The code is as follows:
|
||||
Extend the first solution with a list `path` recording the current path of visited nodes. When a node with value $7$ is reached, append a copy of `path` to the result list `res`. After traversal, `res` contains all solutions. The code is as follows:
|
||||
|
||||
```src
|
||||
[file]{preorder_traversal_ii_compact}-[class]{}-[func]{pre_order}
|
||||
```
|
||||
|
||||
In each "attempt", we record the path by adding the current node to `path`; before "backtracking", we need to remove the node from `path`, **to restore the state before this attempt**.
|
||||
Each time we try a node, append it to `path`. Before backtracking, pop that node from `path` **to restore the state from before the trial**.
|
||||
|
||||
Observing the process shown in the following figure, **we can understand attempt and backtrack as "advance" and "undo"**, two operations that are the reverse of each other.
|
||||
As the figure below illustrates, **trying a choice and backtracking are like moving forward and undoing that move**: they are inverse operations.
|
||||
|
||||
=== "<1>"
|
||||

|
||||

|
||||
|
||||
=== "<2>"
|
||||

|
||||
@@ -73,70 +73,70 @@ Observing the process shown in the following figure, **we can understand attempt
|
||||
|
||||
## Pruning
|
||||
|
||||
Complex backtracking problems usually contain one or more constraints. **Constraints can typically be used for "pruning"**.
|
||||
Complex backtracking problems usually have one or more constraints, **which can often be used for pruning**.
|
||||
|
||||
!!! question "Example 3"
|
||||
|
||||
In a binary tree, search all nodes with value $7$ and return the paths from the root node to these nodes, **but require that the paths do not contain nodes with value $3$**.
|
||||
Find every node with value $7$ in a binary tree and return the paths from the root to those nodes, **requiring that no path contain a node with value $3$**.
|
||||
|
||||
To satisfy the above constraints, **we need to add pruning operations**: during the search process, if we encounter a node with value $3$, we return early and do not continue searching. The code is as follows:
|
||||
To enforce this constraint, **add a pruning step**: when a node with value $3$ is encountered, return immediately without searching farther. The code is as follows:
|
||||
|
||||
```src
|
||||
[file]{preorder_traversal_iii_compact}-[class]{}-[func]{pre_order}
|
||||
```
|
||||
|
||||
"Pruning" is a vivid term. As shown in the following figure, during the search process, **we "prune" search branches that do not satisfy the constraints**, avoiding many meaningless attempts and thus improving search efficiency.
|
||||
Pruning is an apt name. As shown in the figure below, **we cut off search branches that violate the constraints**, avoiding pointless trials and improving efficiency.
|
||||
|
||||

|
||||
|
||||
## Framework Code
|
||||
## A code template
|
||||
|
||||
Next, we attempt to extract a general framework centered on backtracking's "attempt, backtrack, and pruning" to improve code generality.
|
||||
We will now extract the core pattern of trying, undoing, and pruning into a more general backtracking template.
|
||||
|
||||
In the following framework code, `state` represents the current state of the problem, and `choices` represents the choices available in the current state:
|
||||
In the following template, `state` is the current problem state, and `choices` contains the choices available from that state:
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title=""
|
||||
def backtrack(state: State, choices: list[choice], res: list[state]):
|
||||
"""Backtracking algorithm framework"""
|
||||
# Check if it is a solution
|
||||
"""Backtracking template"""
|
||||
# Check whether the state is a solution
|
||||
if is_solution(state):
|
||||
# Record the solution
|
||||
record_solution(state, res)
|
||||
# Stop searching
|
||||
# Stop searching this branch
|
||||
return
|
||||
# Traverse all choices
|
||||
# Try every choice
|
||||
for choice in choices:
|
||||
# Pruning: check if the choice is valid
|
||||
# Prune: check whether the choice is valid
|
||||
if is_valid(state, choice):
|
||||
# Attempt: make a choice and update the state
|
||||
# Try: make the choice and update the state
|
||||
make_choice(state, choice)
|
||||
backtrack(state, choices, res)
|
||||
# Backtrack: undo the choice and restore to the previous state
|
||||
# Backtrack: undo the choice and restore the previous state
|
||||
undo_choice(state, choice)
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title=""
|
||||
/* Backtracking algorithm framework */
|
||||
/* Backtracking template */
|
||||
void backtrack(State *state, vector<Choice *> &choices, vector<State *> &res) {
|
||||
// Check if it is a solution
|
||||
// Check whether the state is a solution
|
||||
if (isSolution(state)) {
|
||||
// Record the solution
|
||||
recordSolution(state, res);
|
||||
// Stop searching
|
||||
// Stop searching this branch
|
||||
return;
|
||||
}
|
||||
// Traverse all choices
|
||||
// Try every choice
|
||||
for (Choice choice : choices) {
|
||||
// Pruning: check if the choice is valid
|
||||
// Prune: check whether the choice is valid
|
||||
if (isValid(state, choice)) {
|
||||
// Attempt: make a choice and update the state
|
||||
// Try: make the choice and update the state
|
||||
makeChoice(state, choice);
|
||||
backtrack(state, choices, res);
|
||||
// Backtrack: undo the choice and restore to the previous state
|
||||
// Backtrack: undo the choice and restore the previous state
|
||||
undoChoice(state, choice);
|
||||
}
|
||||
}
|
||||
@@ -146,23 +146,23 @@ In the following framework code, `state` represents the current state of the pro
|
||||
=== "Java"
|
||||
|
||||
```java title=""
|
||||
/* Backtracking algorithm framework */
|
||||
/* Backtracking template */
|
||||
void backtrack(State state, List<Choice> choices, List<State> res) {
|
||||
// Check if it is a solution
|
||||
// Check whether the state is a solution
|
||||
if (isSolution(state)) {
|
||||
// Record the solution
|
||||
recordSolution(state, res);
|
||||
// Stop searching
|
||||
// Stop searching this branch
|
||||
return;
|
||||
}
|
||||
// Traverse all choices
|
||||
// Try every choice
|
||||
for (Choice choice : choices) {
|
||||
// Pruning: check if the choice is valid
|
||||
// Prune: check whether the choice is valid
|
||||
if (isValid(state, choice)) {
|
||||
// Attempt: make a choice and update the state
|
||||
// Try: make the choice and update the state
|
||||
makeChoice(state, choice);
|
||||
backtrack(state, choices, res);
|
||||
// Backtrack: undo the choice and restore to the previous state
|
||||
// Backtrack: undo the choice and restore the previous state
|
||||
undoChoice(state, choice);
|
||||
}
|
||||
}
|
||||
@@ -172,23 +172,23 @@ In the following framework code, `state` represents the current state of the pro
|
||||
=== "C#"
|
||||
|
||||
```csharp title=""
|
||||
/* Backtracking algorithm framework */
|
||||
/* Backtracking template */
|
||||
void Backtrack(State state, List<Choice> choices, List<State> res) {
|
||||
// Check if it is a solution
|
||||
// Check whether the state is a solution
|
||||
if (IsSolution(state)) {
|
||||
// Record the solution
|
||||
RecordSolution(state, res);
|
||||
// Stop searching
|
||||
// Stop searching this branch
|
||||
return;
|
||||
}
|
||||
// Traverse all choices
|
||||
// Try every choice
|
||||
foreach (Choice choice in choices) {
|
||||
// Pruning: check if the choice is valid
|
||||
// Prune: check whether the choice is valid
|
||||
if (IsValid(state, choice)) {
|
||||
// Attempt: make a choice and update the state
|
||||
// Try: make the choice and update the state
|
||||
MakeChoice(state, choice);
|
||||
Backtrack(state, choices, res);
|
||||
// Backtrack: undo the choice and restore to the previous state
|
||||
// Backtrack: undo the choice and restore the previous state
|
||||
UndoChoice(state, choice);
|
||||
}
|
||||
}
|
||||
@@ -198,23 +198,23 @@ In the following framework code, `state` represents the current state of the pro
|
||||
=== "Go"
|
||||
|
||||
```go title=""
|
||||
/* Backtracking algorithm framework */
|
||||
/* Backtracking template */
|
||||
func backtrack(state *State, choices []Choice, res *[]State) {
|
||||
// Check if it is a solution
|
||||
// Check whether the state is a solution
|
||||
if isSolution(state) {
|
||||
// Record the solution
|
||||
recordSolution(state, res)
|
||||
// Stop searching
|
||||
// Stop searching this branch
|
||||
return
|
||||
}
|
||||
// Traverse all choices
|
||||
// Try every choice
|
||||
for _, choice := range choices {
|
||||
// Pruning: check if the choice is valid
|
||||
// Prune: check whether the choice is valid
|
||||
if isValid(state, choice) {
|
||||
// Attempt: make a choice and update the state
|
||||
// Try: make the choice and update the state
|
||||
makeChoice(state, choice)
|
||||
backtrack(state, choices, res)
|
||||
// Backtrack: undo the choice and restore to the previous state
|
||||
// Backtrack: undo the choice and restore the previous state
|
||||
undoChoice(state, choice)
|
||||
}
|
||||
}
|
||||
@@ -224,23 +224,23 @@ In the following framework code, `state` represents the current state of the pro
|
||||
=== "Swift"
|
||||
|
||||
```swift title=""
|
||||
/* Backtracking algorithm framework */
|
||||
/* Backtracking template */
|
||||
func backtrack(state: inout State, choices: [Choice], res: inout [State]) {
|
||||
// Check if it is a solution
|
||||
// Check whether the state is a solution
|
||||
if isSolution(state: state) {
|
||||
// Record the solution
|
||||
recordSolution(state: state, res: &res)
|
||||
// Stop searching
|
||||
// Stop searching this branch
|
||||
return
|
||||
}
|
||||
// Traverse all choices
|
||||
// Try every choice
|
||||
for choice in choices {
|
||||
// Pruning: check if the choice is valid
|
||||
// Prune: check whether the choice is valid
|
||||
if isValid(state: state, choice: choice) {
|
||||
// Attempt: make a choice and update the state
|
||||
// Try: make the choice and update the state
|
||||
makeChoice(state: &state, choice: choice)
|
||||
backtrack(state: &state, choices: choices, res: &res)
|
||||
// Backtrack: undo the choice and restore to the previous state
|
||||
// Backtrack: undo the choice and restore the previous state
|
||||
undoChoice(state: &state, choice: choice)
|
||||
}
|
||||
}
|
||||
@@ -250,23 +250,23 @@ In the following framework code, `state` represents the current state of the pro
|
||||
=== "JS"
|
||||
|
||||
```javascript title=""
|
||||
/* Backtracking algorithm framework */
|
||||
/* Backtracking template */
|
||||
function backtrack(state, choices, res) {
|
||||
// Check if it is a solution
|
||||
// Check whether the state is a solution
|
||||
if (isSolution(state)) {
|
||||
// Record the solution
|
||||
recordSolution(state, res);
|
||||
// Stop searching
|
||||
// Stop searching this branch
|
||||
return;
|
||||
}
|
||||
// Traverse all choices
|
||||
// Try every choice
|
||||
for (let choice of choices) {
|
||||
// Pruning: check if the choice is valid
|
||||
// Prune: check whether the choice is valid
|
||||
if (isValid(state, choice)) {
|
||||
// Attempt: make a choice and update the state
|
||||
// Try: make the choice and update the state
|
||||
makeChoice(state, choice);
|
||||
backtrack(state, choices, res);
|
||||
// Backtrack: undo the choice and restore to the previous state
|
||||
// Backtrack: undo the choice and restore the previous state
|
||||
undoChoice(state, choice);
|
||||
}
|
||||
}
|
||||
@@ -276,23 +276,23 @@ In the following framework code, `state` represents the current state of the pro
|
||||
=== "TS"
|
||||
|
||||
```typescript title=""
|
||||
/* Backtracking algorithm framework */
|
||||
/* Backtracking template */
|
||||
function backtrack(state: State, choices: Choice[], res: State[]): void {
|
||||
// Check if it is a solution
|
||||
// Check whether the state is a solution
|
||||
if (isSolution(state)) {
|
||||
// Record the solution
|
||||
recordSolution(state, res);
|
||||
// Stop searching
|
||||
// Stop searching this branch
|
||||
return;
|
||||
}
|
||||
// Traverse all choices
|
||||
// Try every choice
|
||||
for (let choice of choices) {
|
||||
// Pruning: check if the choice is valid
|
||||
// Prune: check whether the choice is valid
|
||||
if (isValid(state, choice)) {
|
||||
// Attempt: make a choice and update the state
|
||||
// Try: make the choice and update the state
|
||||
makeChoice(state, choice);
|
||||
backtrack(state, choices, res);
|
||||
// Backtrack: undo the choice and restore to the previous state
|
||||
// Backtrack: undo the choice and restore the previous state
|
||||
undoChoice(state, choice);
|
||||
}
|
||||
}
|
||||
@@ -302,23 +302,23 @@ In the following framework code, `state` represents the current state of the pro
|
||||
=== "Dart"
|
||||
|
||||
```dart title=""
|
||||
/* Backtracking algorithm framework */
|
||||
/* Backtracking template */
|
||||
void backtrack(State state, List<Choice>, List<State> res) {
|
||||
// Check if it is a solution
|
||||
// Check whether the state is a solution
|
||||
if (isSolution(state)) {
|
||||
// Record the solution
|
||||
recordSolution(state, res);
|
||||
// Stop searching
|
||||
// Stop searching this branch
|
||||
return;
|
||||
}
|
||||
// Traverse all choices
|
||||
// Try every choice
|
||||
for (Choice choice in choices) {
|
||||
// Pruning: check if the choice is valid
|
||||
// Prune: check whether the choice is valid
|
||||
if (isValid(state, choice)) {
|
||||
// Attempt: make a choice and update the state
|
||||
// Try: make the choice and update the state
|
||||
makeChoice(state, choice);
|
||||
backtrack(state, choices, res);
|
||||
// Backtrack: undo the choice and restore to the previous state
|
||||
// Backtrack: undo the choice and restore the previous state
|
||||
undoChoice(state, choice);
|
||||
}
|
||||
}
|
||||
@@ -328,23 +328,23 @@ In the following framework code, `state` represents the current state of the pro
|
||||
=== "Rust"
|
||||
|
||||
```rust title=""
|
||||
/* Backtracking algorithm framework */
|
||||
/* Backtracking template */
|
||||
fn backtrack(state: &mut State, choices: &Vec<Choice>, res: &mut Vec<State>) {
|
||||
// Check if it is a solution
|
||||
// Check whether the state is a solution
|
||||
if is_solution(state) {
|
||||
// Record the solution
|
||||
record_solution(state, res);
|
||||
// Stop searching
|
||||
// Stop searching this branch
|
||||
return;
|
||||
}
|
||||
// Traverse all choices
|
||||
// Try every choice
|
||||
for choice in choices {
|
||||
// Pruning: check if the choice is valid
|
||||
// Prune: check whether the choice is valid
|
||||
if is_valid(state, choice) {
|
||||
// Attempt: make a choice and update the state
|
||||
// Try: make the choice and update the state
|
||||
make_choice(state, choice);
|
||||
backtrack(state, choices, res);
|
||||
// Backtrack: undo the choice and restore to the previous state
|
||||
// Backtrack: undo the choice and restore the previous state
|
||||
undo_choice(state, choice);
|
||||
}
|
||||
}
|
||||
@@ -354,23 +354,23 @@ In the following framework code, `state` represents the current state of the pro
|
||||
=== "C"
|
||||
|
||||
```c title=""
|
||||
/* Backtracking algorithm framework */
|
||||
/* Backtracking template */
|
||||
void backtrack(State *state, Choice *choices, int numChoices, State *res, int numRes) {
|
||||
// Check if it is a solution
|
||||
// Check whether the state is a solution
|
||||
if (isSolution(state)) {
|
||||
// Record the solution
|
||||
recordSolution(state, res, numRes);
|
||||
// Stop searching
|
||||
// Stop searching this branch
|
||||
return;
|
||||
}
|
||||
// Traverse all choices
|
||||
// Try every choice
|
||||
for (int i = 0; i < numChoices; i++) {
|
||||
// Pruning: check if the choice is valid
|
||||
// Prune: check whether the choice is valid
|
||||
if (isValid(state, &choices[i])) {
|
||||
// Attempt: make a choice and update the state
|
||||
// Try: make the choice and update the state
|
||||
makeChoice(state, &choices[i]);
|
||||
backtrack(state, choices, numChoices, res, numRes);
|
||||
// Backtrack: undo the choice and restore to the previous state
|
||||
// Backtrack: undo the choice and restore the previous state
|
||||
undoChoice(state, &choices[i]);
|
||||
}
|
||||
}
|
||||
@@ -380,23 +380,23 @@ In the following framework code, `state` represents the current state of the pro
|
||||
=== "Kotlin"
|
||||
|
||||
```kotlin title=""
|
||||
/* Backtracking algorithm framework */
|
||||
/* Backtracking template */
|
||||
fun backtrack(state: State?, choices: List<Choice?>, res: List<State?>?) {
|
||||
// Check if it is a solution
|
||||
// Check whether the state is a solution
|
||||
if (isSolution(state)) {
|
||||
// Record the solution
|
||||
recordSolution(state, res)
|
||||
// Stop searching
|
||||
// Stop searching this branch
|
||||
return
|
||||
}
|
||||
// Traverse all choices
|
||||
// Try every choice
|
||||
for (choice in choices) {
|
||||
// Pruning: check if the choice is valid
|
||||
// Prune: check whether the choice is valid
|
||||
if (isValid(state, choice)) {
|
||||
// Attempt: make a choice and update the state
|
||||
// Try: make the choice and update the state
|
||||
makeChoice(state, choice)
|
||||
backtrack(state, choices, res)
|
||||
// Backtrack: undo the choice and restore to the previous state
|
||||
// Backtrack: undo the choice and restore the previous state
|
||||
undoChoice(state, choice)
|
||||
}
|
||||
}
|
||||
@@ -406,98 +406,98 @@ In the following framework code, `state` represents the current state of the pro
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title=""
|
||||
### Backtracking algorithm framework ###
|
||||
### Backtracking template ###
|
||||
def backtrack(state, choices, res)
|
||||
# Check if it is a solution
|
||||
# Check whether the state is a solution
|
||||
if is_solution?(state)
|
||||
# Record the solution
|
||||
record_solution(state, res)
|
||||
return
|
||||
end
|
||||
|
||||
# Traverse all choices
|
||||
# Try every choice
|
||||
for choice in choices
|
||||
# Pruning: check if the choice is valid
|
||||
# Prune: check whether the choice is valid
|
||||
if is_valid?(state, choice)
|
||||
# Attempt: make a choice and update the state
|
||||
# Try: make the choice and update the state
|
||||
make_choice(state, choice)
|
||||
backtrack(state, choices, res)
|
||||
# Backtrack: undo the choice and restore to the previous state
|
||||
# Backtrack: undo the choice and restore the previous state
|
||||
undo_choice(state, choice)
|
||||
end
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
Next, we solve Example 3 based on the framework code. The state `state` is the node traversal path, the choices `choices` are the left and right child nodes of the current node, and the result `res` is a list of paths:
|
||||
Now apply this template to Example 3. The `state` is the current path of visited nodes, `choices` consists of the current node's left and right children, and `res` is the list of paths:
|
||||
|
||||
```src
|
||||
[file]{preorder_traversal_iii_template}-[class]{}-[func]{backtrack}
|
||||
```
|
||||
|
||||
As per the problem statement, we should continue searching after finding a node with value $7$. **Therefore, we need to remove the `return` statement after recording the solution**. The following figure compares the search process with and without the `return` statement.
|
||||
The problem requires us to keep searching after finding a node with value $7$, **so remove the `return` after recording a solution**. The figure below compares the searches with and without that `return`.
|
||||
|
||||

|
||||

|
||||
|
||||
Compared to code based on preorder traversal, code based on the backtracking algorithm framework appears more verbose, but is more general. In fact, **many backtracking problems can be solved within this framework**. We only need to define `state` and `choices` for the specific problem and implement each method in the framework.
|
||||
The template-based implementation is more verbose than the preorder version, but more general. **Many backtracking problems can be solved within this template**: define `state` and `choices` for the problem, then implement each helper method.
|
||||
|
||||
## Common Terminology
|
||||
## Common terminology
|
||||
|
||||
To analyze algorithmic problems more clearly, we summarize the meanings of common terminology used in backtracking algorithms and provide corresponding examples from Example 3, as shown in the following table.
|
||||
The table below summarizes common backtracking terms and illustrates them with Example 3, helping us analyze problems more clearly.
|
||||
|
||||
<p align="center"> Table <id> Common Backtracking Algorithm Terminology </p>
|
||||
<p align="center"> Table <id> Common backtracking terminology </p>
|
||||
|
||||
| Term | Definition | Example 3 |
|
||||
| ------------------------- | ---------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------- |
|
||||
| Solution (solution) | A solution is an answer that satisfies the specific conditions of a problem; there may be one or more solutions | All paths from root to nodes with value $7$ that satisfy the constraint |
|
||||
| Constraint (constraint) | A constraint is a condition in the problem that limits the feasibility of solutions, typically used for pruning | Paths do not contain nodes with value $3$ |
|
||||
| State (state) | State represents the situation of a problem at a certain moment, including the choices already made | The currently visited node path, i.e., the `path` list of nodes |
|
||||
| Attempt (attempt) | An attempt is the process of exploring the solution space according to available choices, including making choices, updating state, and checking if it is a solution | Recursively visit left (right) child nodes, add nodes to `path`, check if node value is $7$ |
|
||||
| Backtrack (backtracking) | Backtracking refers to undoing previous choices and returning to a previous state when encountering a state that does not satisfy constraints | Stop searching when passing over leaf nodes, ending node visits, or encountering nodes with value $3$; function returns |
|
||||
| Pruning (pruning) | Pruning is a method of avoiding meaningless search paths according to problem characteristics and constraints, which can improve search efficiency | When encountering a node with value $3$, do not continue searching |
|
||||
| Term | Definition | Example 3 |
|
||||
| ---------------------- | -------------------------------------------------------------------------- | -------------------------------------------------------------------- |
|
||||
| Solution | An answer satisfying the problem's conditions; there may be one or more | All paths from the root to a node valued $7$ that satisfy the constraint |
|
||||
| Constraint | A condition restricting which solutions are feasible, often used for pruning | The path contains no node valued $3$ |
|
||||
| State | The situation at a particular point, including the choices already made | The current path of visited nodes, stored in `path` |
|
||||
| Trial | Exploring the solution space by making a choice, updating the state, and checking for a solution | Recursively visit a left or right child, append it to `path`, and check whether its value is $7$ |
|
||||
| Backtracking | Undoing an earlier choice and returning to the previous state when a constraint cannot be met | Stop searching and return after passing a leaf, finishing a node, or encountering value $3$ |
|
||||
| Pruning | Using problem properties and constraints to avoid pointless search paths and improve efficiency | Stop searching when a node valued $3$ is encountered |
|
||||
|
||||
!!! tip
|
||||
|
||||
The concepts of problem, solution, state, etc. are universal and appear in divide-and-conquer, backtracking, dynamic programming, greedy algorithms, and others.
|
||||
Concepts such as problems, solutions, and states are shared by divide and conquer, backtracking, dynamic programming, greedy algorithms, and other approaches.
|
||||
|
||||
## Advantages and Limitations
|
||||
## Strengths and limitations
|
||||
|
||||
The backtracking algorithm is essentially a depth-first search algorithm that tries all possible solutions until it finds one that satisfies the conditions. The advantage of this approach is that it can find all possible solutions, and with reasonable pruning operations, it achieves high efficiency.
|
||||
Backtracking is fundamentally a depth-first search that tries possible solutions until it finds ones meeting the conditions. It can find all possible solutions and can be highly efficient with effective pruning.
|
||||
|
||||
However, when dealing with large-scale or complex problems, **the running efficiency of the backtracking algorithm may be unacceptable**.
|
||||
However, for large or complex problems, **backtracking may be unacceptably slow**.
|
||||
|
||||
- **Time**: The backtracking algorithm usually needs to traverse all possibilities in the state space, and the time complexity can reach exponential or factorial order.
|
||||
- **Space**: During recursive calls, the current state needs to be saved (such as paths, auxiliary variables used for pruning, etc.), and when the depth is large, the space requirement can become very large.
|
||||
- **Time**: Backtracking often explores every possibility in the state space, leading to exponential or factorial time complexity.
|
||||
- **Space**: Recursive calls must preserve state, such as the path and auxiliary pruning variables. Deep recursion can therefore require substantial space.
|
||||
|
||||
Nevertheless, **the backtracking algorithm is still the best solution for certain search problems and constraint satisfaction problems**. For these problems, since we cannot predict which choices will generate valid solutions, we must traverse all possible choices. In this case, **the key is how to optimize efficiency**. There are two common efficiency optimization methods.
|
||||
Even so, **backtracking remains the best approach for some search and constraint satisfaction problems**. When we cannot predict which choices lead to valid solutions, we must explore every possibility. **The key is then to improve efficiency**, commonly through two techniques.
|
||||
|
||||
- **Pruning**: Avoid searching paths that are guaranteed not to produce solutions, thereby saving time and space.
|
||||
- **Heuristic search**: Introduce certain strategies or estimation values during the search process to prioritize searching paths that are most likely to produce valid solutions.
|
||||
- **Pruning**: Avoid paths that cannot produce a solution, saving time and space.
|
||||
- **Heuristic search**: Use strategies or estimates to explore paths most likely to yield valid solutions first.
|
||||
|
||||
## Typical Backtracking Examples
|
||||
## Typical backtracking problems
|
||||
|
||||
The backtracking algorithm can be used to solve many search problems, constraint satisfaction problems, and combinatorial optimization problems.
|
||||
Backtracking can solve many search, constraint satisfaction, and combinatorial optimization problems.
|
||||
|
||||
**Search problems**: The goal of these problems is to find solutions that satisfy specific conditions.
|
||||
**Search problems** seek solutions satisfying specified conditions.
|
||||
|
||||
- Permutation problem: Given a set, find all possible permutations and combinations.
|
||||
- Subset sum problem: Given a set and a target sum, find all subsets in the set whose elements sum to the target.
|
||||
- Tower of Hanoi: Given three pegs and a series of disks of different sizes, move all disks from one peg to another, moving only one disk at a time, and never placing a larger disk on a smaller disk.
|
||||
- Permutations: Given a collection, find every possible ordering of its elements.
|
||||
- Subset sum: Given a collection and a target sum, find every subset whose sum equals the target.
|
||||
- Tower of Hanoi: Given three rods and disks of different sizes, move all disks from one rod to another, moving only one at a time and never placing a larger disk on a smaller one.
|
||||
|
||||
**Constraint satisfaction problems**: The goal of these problems is to find solutions that satisfy all constraints.
|
||||
**Constraint satisfaction problems** seek solutions meeting all constraints.
|
||||
|
||||
- N-Queens: Place $n$ queens on an $n \times n$ chessboard such that they do not attack each other.
|
||||
- Sudoku: Fill numbers $1$ to $9$ in a $9 \times 9$ grid such that each row, column, and $3 \times 3$ subgrid contains no repeated digits.
|
||||
- Graph coloring: Given an undirected graph, color each vertex with the minimum number of colors such that adjacent vertices have different colors.
|
||||
- $n$-queens: Place $n$ queens on an $n \times n$ chessboard so that no two attack each other.
|
||||
- Sudoku: Fill a $9 \times 9$ grid with digits $1$ ~ $9$ so that no digit repeats in any row, column, or $3 \times 3$ subgrid.
|
||||
- Graph coloring: Color an undirected graph's vertices with as few colors as possible, ensuring adjacent vertices have different colors.
|
||||
|
||||
**Combinatorial optimization problems**: The goal of these problems is to find an optimal solution that satisfies certain conditions in a combinatorial space.
|
||||
**Combinatorial optimization problems** seek an optimal solution satisfying conditions within a space of combinations.
|
||||
|
||||
- 0-1 Knapsack: Given a set of items and a knapsack, each item has a value and weight. Under the knapsack capacity constraint, select items to maximize total value.
|
||||
- Traveling Salesman Problem: Starting from a point in a graph, visit all other points exactly once and return to the starting point, finding the shortest path.
|
||||
- Maximum Clique: Given an undirected graph, find the largest complete subgraph, i.e., a subgraph where any two vertices are connected by an edge.
|
||||
- 0-1 knapsack: Given items with values and weights and a knapsack, choose items to maximize total value within the capacity limit.
|
||||
- Traveling salesperson: Starting at a vertex in a graph, visit every other vertex exactly once and return to the start along the shortest possible route.
|
||||
- Maximum clique: Given an undirected graph, find its largest complete subgraph, in which every pair of vertices is connected by an edge.
|
||||
|
||||
Note that for many combinatorial optimization problems, backtracking is not the optimal solution.
|
||||
For many combinatorial optimization problems, backtracking is not the best approach.
|
||||
|
||||
- The 0-1 Knapsack problem is usually solved using dynamic programming to achieve higher time efficiency.
|
||||
- The Traveling Salesman Problem is a famous NP-Hard problem; common solutions include genetic algorithms and ant colony algorithms.
|
||||
- The Maximum Clique problem is a classical problem in graph theory and can be solved using heuristic algorithms such as greedy algorithms.
|
||||
- 0-1 knapsack is usually solved more efficiently with dynamic programming.
|
||||
- The traveling salesperson problem is a well-known NP-hard problem. Common approaches include genetic algorithms and ant colony algorithms.
|
||||
- Maximum clique is a classic graph theory problem that can be approached with heuristics such as greedy algorithms.
|
||||
|
||||
Reference in New Issue
Block a user