You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hello-algo/en/docs/chapter_backtracking/backtracking_algorithm.md

1179 lines
39 KiB

7 months ago
---
comments: true
---
# 13.1   Backtracking algorithms
<u>Backtracking algorithm</u> is a method to solve problems by exhaustive search, where the core idea is to start from an initial state and brute force all possible solutions, recording the correct ones until a solution is found or all possible choices are exhausted without finding a solution.
6 months ago
Backtracking typically employs "depth-first search" to traverse the solution space. In the "Binary Tree" chapter, we mentioned that pre-order, in-order, and post-order traversals are all depth-first searches. Next, we use pre-order traversal to construct a backtracking problem to gradually understand the workings of the backtracking algorithm.
7 months ago
!!! question "Example One"
Given a binary tree, search and record all nodes with a value of $7$, please return a list of nodes.
6 months ago
For this problem, we traverse this tree in pre-order and check if the current node's value is $7$. If it is, we add the node's value to the result list `res`. The relevant process is shown in Figure 13-1:
7 months ago
=== "Python"
```python title="preorder_traversal_i_compact.py"
def pre_order(root: TreeNode):
7 months ago
"""Pre-order traversal: Example one"""
7 months ago
if root is None:
return
if root.val == 7:
7 months ago
# Record solution
7 months ago
res.append(root)
pre_order(root.left)
pre_order(root.right)
```
=== "C++"
```cpp title="preorder_traversal_i_compact.cpp"
6 months ago
/* Pre-order traversal: Example one */
void preOrder(TreeNode *root) {
if (root == nullptr) {
return;
}
if (root->val == 7) {
// Record solution
res.push_back(root);
}
preOrder(root->left);
preOrder(root->right);
}
7 months ago
```
=== "Java"
```java title="preorder_traversal_i_compact.java"
7 months ago
/* Pre-order traversal: Example one */
7 months ago
void preOrder(TreeNode root) {
if (root == null) {
return;
}
if (root.val == 7) {
7 months ago
// Record solution
7 months ago
res.add(root);
}
preOrder(root.left);
preOrder(root.right);
}
```
=== "C#"
```csharp title="preorder_traversal_i_compact.cs"
7 months ago
[class]{preorder_traversal_i_compact}-[func]{PreOrder}
7 months ago
```
=== "Go"
```go title="preorder_traversal_i_compact.go"
7 months ago
[class]{}-[func]{preOrderI}
7 months ago
```
=== "Swift"
```swift title="preorder_traversal_i_compact.swift"
7 months ago
[class]{}-[func]{preOrder}
7 months ago
```
=== "JS"
```javascript title="preorder_traversal_i_compact.js"
7 months ago
[class]{}-[func]{preOrder}
7 months ago
```
=== "TS"
```typescript title="preorder_traversal_i_compact.ts"
7 months ago
[class]{}-[func]{preOrder}
7 months ago
```
=== "Dart"
```dart title="preorder_traversal_i_compact.dart"
7 months ago
[class]{}-[func]{preOrder}
7 months ago
```
=== "Rust"
```rust title="preorder_traversal_i_compact.rs"
7 months ago
[class]{}-[func]{pre_order}
7 months ago
```
=== "C"
```c title="preorder_traversal_i_compact.c"
7 months ago
[class]{}-[func]{preOrder}
7 months ago
```
=== "Kotlin"
```kotlin title="preorder_traversal_i_compact.kt"
7 months ago
[class]{}-[func]{preOrder}
7 months ago
```
=== "Ruby"
```ruby title="preorder_traversal_i_compact.rb"
[class]{}-[func]{pre_order}
```
=== "Zig"
```zig title="preorder_traversal_i_compact.zig"
[class]{}-[func]{preOrder}
```
6 months ago
![Searching nodes in pre-order traversal](backtracking_algorithm.assets/preorder_find_nodes.png){ class="animation-figure" }
7 months ago
6 months ago
<p align="center"> Figure 13-1 &nbsp; Searching nodes in pre-order traversal </p>
7 months ago
## 13.1.1 &nbsp; Trying and retreating
**The reason it is called backtracking is that the algorithm uses a "try" and "retreat" strategy when searching the solution space**. When the algorithm encounters a state where it can no longer progress or fails to achieve a satisfying solution, it undoes the previous choice, reverts to the previous state, and tries other possible choices.
For Example One, visiting each node represents a "try", and passing a leaf node or returning to the parent node's `return` represents "retreat".
It's worth noting that **retreat is not merely about function returns**. We expand slightly on Example One for clarification.
!!! question "Example Two"
In a binary tree, search for all nodes with a value of $7$ and **please return the paths from the root node to these nodes**.
Based on the code from Example One, we need to use a list `path` to record the visited node paths. When a node with a value of $7$ is reached, we copy `path` and add it to the result list `res`. After the traversal, `res` holds all the solutions. The code is as shown:
=== "Python"
```python title="preorder_traversal_ii_compact.py"
def pre_order(root: TreeNode):
7 months ago
"""Pre-order traversal: Example two"""
7 months ago
if root is None:
return
7 months ago
# Attempt
7 months ago
path.append(root)
if root.val == 7:
7 months ago
# Record solution
7 months ago
res.append(list(path))
pre_order(root.left)
pre_order(root.right)
7 months ago
# Retract
7 months ago
path.pop()
```
=== "C++"
```cpp title="preorder_traversal_ii_compact.cpp"
6 months ago
/* Pre-order traversal: Example two */
void preOrder(TreeNode *root) {
if (root == nullptr) {
return;
}
// Attempt
path.push_back(root);
if (root->val == 7) {
// Record solution
res.push_back(path);
}
preOrder(root->left);
preOrder(root->right);
// Retract
path.pop_back();
}
7 months ago
```
=== "Java"
```java title="preorder_traversal_ii_compact.java"
7 months ago
/* Pre-order traversal: Example two */
7 months ago
void preOrder(TreeNode root) {
if (root == null) {
return;
}
7 months ago
// Attempt
7 months ago
path.add(root);
if (root.val == 7) {
7 months ago
// Record solution
7 months ago
res.add(new ArrayList<>(path));
}
preOrder(root.left);
preOrder(root.right);
7 months ago
// Retract
7 months ago
path.remove(path.size() - 1);
}
```
=== "C#"
```csharp title="preorder_traversal_ii_compact.cs"
7 months ago
[class]{preorder_traversal_ii_compact}-[func]{PreOrder}
7 months ago
```
=== "Go"
```go title="preorder_traversal_ii_compact.go"
7 months ago
[class]{}-[func]{preOrderII}
7 months ago
```
=== "Swift"
```swift title="preorder_traversal_ii_compact.swift"
7 months ago
[class]{}-[func]{preOrder}
7 months ago
```
=== "JS"
```javascript title="preorder_traversal_ii_compact.js"
7 months ago
[class]{}-[func]{preOrder}
7 months ago
```
=== "TS"
```typescript title="preorder_traversal_ii_compact.ts"
7 months ago
[class]{}-[func]{preOrder}
7 months ago
```
=== "Dart"
```dart title="preorder_traversal_ii_compact.dart"
7 months ago
[class]{}-[func]{preOrder}
7 months ago
```
=== "Rust"
```rust title="preorder_traversal_ii_compact.rs"
7 months ago
[class]{}-[func]{pre_order}
7 months ago
```
=== "C"
```c title="preorder_traversal_ii_compact.c"
7 months ago
[class]{}-[func]{preOrder}
7 months ago
```
=== "Kotlin"
```kotlin title="preorder_traversal_ii_compact.kt"
7 months ago
[class]{}-[func]{preOrder}
7 months ago
```
=== "Ruby"
```ruby title="preorder_traversal_ii_compact.rb"
[class]{}-[func]{pre_order}
```
=== "Zig"
```zig title="preorder_traversal_ii_compact.zig"
[class]{}-[func]{preOrder}
```
In each "try", we record the path by adding the current node to `path`; before "retreating", we need to pop the node from `path` **to restore the state before this attempt**.
7 months ago
Observe the process shown in Figure 13-2, **we can understand trying and retreating as "advancing" and "undoing"**, two operations that are reverse to each other.
7 months ago
=== "<1>"
![Trying and retreating](backtracking_algorithm.assets/preorder_find_paths_step1.png){ class="animation-figure" }
=== "<2>"
![preorder_find_paths_step2](backtracking_algorithm.assets/preorder_find_paths_step2.png){ class="animation-figure" }
=== "<3>"
![preorder_find_paths_step3](backtracking_algorithm.assets/preorder_find_paths_step3.png){ class="animation-figure" }
=== "<4>"
![preorder_find_paths_step4](backtracking_algorithm.assets/preorder_find_paths_step4.png){ class="animation-figure" }
=== "<5>"
![preorder_find_paths_step5](backtracking_algorithm.assets/preorder_find_paths_step5.png){ class="animation-figure" }
=== "<6>"
![preorder_find_paths_step6](backtracking_algorithm.assets/preorder_find_paths_step6.png){ class="animation-figure" }
=== "<7>"
![preorder_find_paths_step7](backtracking_algorithm.assets/preorder_find_paths_step7.png){ class="animation-figure" }
=== "<8>"
![preorder_find_paths_step8](backtracking_algorithm.assets/preorder_find_paths_step8.png){ class="animation-figure" }
=== "<9>"
![preorder_find_paths_step9](backtracking_algorithm.assets/preorder_find_paths_step9.png){ class="animation-figure" }
=== "<10>"
![preorder_find_paths_step10](backtracking_algorithm.assets/preorder_find_paths_step10.png){ class="animation-figure" }
=== "<11>"
![preorder_find_paths_step11](backtracking_algorithm.assets/preorder_find_paths_step11.png){ class="animation-figure" }
<p align="center"> Figure 13-2 &nbsp; Trying and retreating </p>
## 13.1.2 &nbsp; Pruning
Complex backtracking problems usually involve one or more constraints, **which are often used for "pruning"**.
!!! question "Example Three"
In a binary tree, search for all nodes with a value of $7$ and return the paths from the root to these nodes, **requiring that the paths do not contain nodes with a value of $3$**.
To meet the above constraints, **we need to add a pruning operation**: during the search process, if a node with a value of $3$ is encountered, it returns early, discontinuing further search. The code is as shown:
=== "Python"
```python title="preorder_traversal_iii_compact.py"
def pre_order(root: TreeNode):
7 months ago
"""Pre-order traversal: Example three"""
# Pruning
7 months ago
if root is None or root.val == 3:
return
7 months ago
# Attempt
7 months ago
path.append(root)
if root.val == 7:
7 months ago
# Record solution
7 months ago
res.append(list(path))
pre_order(root.left)
pre_order(root.right)
7 months ago
# Retract
7 months ago
path.pop()
```
=== "C++"
```cpp title="preorder_traversal_iii_compact.cpp"
6 months ago
/* Pre-order traversal: Example three */
void preOrder(TreeNode *root) {
// Pruning
if (root == nullptr || root->val == 3) {
return;
}
// Attempt
path.push_back(root);
if (root->val == 7) {
// Record solution
res.push_back(path);
}
preOrder(root->left);
preOrder(root->right);
// Retract
path.pop_back();
}
7 months ago
```
=== "Java"
```java title="preorder_traversal_iii_compact.java"
7 months ago
/* Pre-order traversal: Example three */
7 months ago
void preOrder(TreeNode root) {
7 months ago
// Pruning
7 months ago
if (root == null || root.val == 3) {
return;
}
7 months ago
// Attempt
7 months ago
path.add(root);
if (root.val == 7) {
7 months ago
// Record solution
7 months ago
res.add(new ArrayList<>(path));
}
preOrder(root.left);
preOrder(root.right);
7 months ago
// Retract
7 months ago
path.remove(path.size() - 1);
}
```
=== "C#"
```csharp title="preorder_traversal_iii_compact.cs"
7 months ago
[class]{preorder_traversal_iii_compact}-[func]{PreOrder}
7 months ago
```
=== "Go"
```go title="preorder_traversal_iii_compact.go"
7 months ago
[class]{}-[func]{preOrderIII}
7 months ago
```
=== "Swift"
```swift title="preorder_traversal_iii_compact.swift"
7 months ago
[class]{}-[func]{preOrder}
7 months ago
```
=== "JS"
```javascript title="preorder_traversal_iii_compact.js"
7 months ago
[class]{}-[func]{preOrder}
7 months ago
```
=== "TS"
```typescript title="preorder_traversal_iii_compact.ts"
7 months ago
[class]{}-[func]{preOrder}
7 months ago
```
=== "Dart"
```dart title="preorder_traversal_iii_compact.dart"
7 months ago
[class]{}-[func]{preOrder}
7 months ago
```
=== "Rust"
```rust title="preorder_traversal_iii_compact.rs"
7 months ago
[class]{}-[func]{pre_order}
7 months ago
```
=== "C"
```c title="preorder_traversal_iii_compact.c"
7 months ago
[class]{}-[func]{preOrder}
7 months ago
```
=== "Kotlin"
```kotlin title="preorder_traversal_iii_compact.kt"
7 months ago
[class]{}-[func]{preOrder}
7 months ago
```
=== "Ruby"
```ruby title="preorder_traversal_iii_compact.rb"
[class]{}-[func]{pre_order}
```
=== "Zig"
```zig title="preorder_traversal_iii_compact.zig"
[class]{}-[func]{preOrder}
```
6 months ago
"Pruning" is a very vivid noun. As shown in Figure 13-3, in the search process, **we "cut off" the search branches that do not meet the constraints**, avoiding many meaningless attempts, thus enhancing the search efficiency.
7 months ago
![Pruning based on constraints](backtracking_algorithm.assets/preorder_find_constrained_paths.png){ class="animation-figure" }
<p align="center"> Figure 13-3 &nbsp; Pruning based on constraints </p>
## 13.1.3 &nbsp; Framework code
Next, we attempt to distill the main framework of "trying, retreating, and pruning" from backtracking to enhance the code's universality.
In the following framework code, `state` represents the current state of the problem, `choices` represents the choices available under the current state:
=== "Python"
```python title=""
def backtrack(state: State, choices: list[choice], res: list[state]):
"""Backtracking algorithm framework"""
# Check if it's a solution
if is_solution(state):
# Record the solution
record_solution(state, res)
# Stop searching
return
# Iterate through all choices
for choice in choices:
# Pruning: check if the choice is valid
if is_valid(state, choice):
# Try: make a choice, update the state
make_choice(state, choice)
backtrack(state, choices, res)
# Retreat: undo the choice, revert to the previous state
undo_choice(state, choice)
```
=== "C++"
```cpp title=""
/* Backtracking algorithm framework */
void backtrack(State *state, vector<Choice *> &choices, vector<State *> &res) {
// Check if it's a solution
if (isSolution(state)) {
// Record the solution
recordSolution(state, res);
// Stop searching
return;
}
// Iterate through all choices
for (Choice choice : choices) {
// Pruning: check if the choice is valid
if (isValid(state, choice)) {
// Try: make a choice, update the state
makeChoice(state, choice);
backtrack(state, choices, res);
// Retreat: undo the choice, revert to the previous state
undoChoice(state, choice);
}
}
}
```
=== "Java"
```java title=""
/* Backtracking algorithm framework */
void backtrack(State state, List<Choice> choices, List<State> res) {
// Check if it's a solution
if (isSolution(state)) {
// Record the solution
recordSolution(state, res);
// Stop searching
return;
}
// Iterate through all choices
for (Choice choice : choices) {
// Pruning: check if the choice is valid
if (isValid(state, choice)) {
// Try: make a choice, update the state
makeChoice(state, choice);
backtrack(state, choices, res);
// Retreat: undo the choice, revert to the previous state
undoChoice(state, choice);
}
}
}
```
=== "C#"
```csharp title=""
/* Backtracking algorithm framework */
void Backtrack(State state, List<Choice> choices, List<State> res) {
// Check if it's a solution
if (IsSolution(state)) {
// Record the solution
RecordSolution(state, res);
// Stop searching
return;
}
// Iterate through all choices
foreach (Choice choice in choices) {
// Pruning: check if the choice is valid
if (IsValid(state, choice)) {
// Try: make a choice, update the state
MakeChoice(state, choice);
Backtrack(state, choices, res);
// Retreat: undo the choice, revert to the previous state
UndoChoice(state, choice);
}
}
}
```
=== "Go"
```go title=""
/* Backtracking algorithm framework */
func backtrack(state *State, choices []Choice, res *[]State) {
// Check if it's a solution
if isSolution(state) {
// Record the solution
recordSolution(state, res)
// Stop searching
return
}
// Iterate through all choices
for _, choice := range choices {
// Pruning: check if the choice is valid
if isValid(state, choice) {
// Try: make a choice, update the state
makeChoice(state, choice)
backtrack(state, choices, res)
// Retreat: undo the choice, revert to the previous state
undoChoice(state, choice)
}
}
}
```
=== "Swift"
```swift title=""
/* Backtracking algorithm framework */
func backtrack(state: inout State, choices: [Choice], res: inout [State]) {
// Check if it's a solution
if isSolution(state: state) {
// Record the solution
recordSolution(state: state, res: &res)
// Stop searching
return
}
// Iterate through all choices
for choice in choices {
// Pruning: check if the choice is valid
if isValid(state: state, choice: choice) {
// Try: make a choice, update the state
makeChoice(state: &state, choice: choice)
backtrack(state: &state, choices: choices, res: &res)
// Retreat: undo the choice, revert to the previous state
undoChoice(state: &state, choice: choice)
}
}
}
```
=== "JS"
```javascript title=""
/* Backtracking algorithm framework */
function backtrack(state, choices, res) {
// Check if it's a solution
if (isSolution(state)) {
// Record the solution
recordSolution(state, res);
// Stop searching
return;
}
// Iterate through all choices
for (let choice of choices) {
// Pruning: check if the choice is valid
if (isValid(state, choice)) {
// Try: make a choice, update the state
makeChoice(state, choice);
backtrack(state, choices, res);
// Retreat: undo the choice, revert to the previous state
undoChoice(state, choice);
}
}
}
```
=== "TS"
```typescript title=""
/* Backtracking algorithm framework */
function backtrack(state: State, choices: Choice[], res: State[]): void {
// Check if it's a solution
if (isSolution(state)) {
// Record the solution
recordSolution(state, res);
// Stop searching
return;
}
// Iterate through all choices
for (let choice of choices) {
// Pruning: check if the choice is valid
if (isValid(state, choice)) {
// Try: make a choice, update the state
makeChoice(state, choice);
backtrack(state, choices, res);
// Retreat: undo the choice, revert to the previous state
undoChoice(state, choice);
}
}
}
```
=== "Dart"
```dart title=""
/* Backtracking algorithm framework */
void backtrack(State state, List<Choice>, List<State> res) {
// Check if it's a solution
if (isSolution(state)) {
// Record the solution
recordSolution(state, res);
// Stop searching
return;
}
// Iterate through all choices
for (Choice choice in choices) {
// Pruning: check if the choice is valid
if (isValid(state, choice)) {
// Try: make a choice, update the state
makeChoice(state, choice);
backtrack(state, choices, res);
// Retreat: undo the choice, revert to the previous state
undoChoice(state, choice);
}
}
}
```
=== "Rust"
```rust title=""
/* Backtracking algorithm framework */
fn backtrack(state: &mut State, choices: &Vec<Choice>, res: &mut Vec<State>) {
// Check if it's a solution
if is_solution(state) {
// Record the solution
record_solution(state, res);
// Stop searching
return;
}
// Iterate through all choices
for choice in choices {
// Pruning: check if the choice is valid
if is_valid(state, choice) {
// Try: make a choice, update the state
make_choice(state, choice);
backtrack(state, choices, res);
// Retreat: undo the choice, revert to the previous state
undo_choice(state, choice);
}
}
}
```
=== "C"
```c title=""
/* Backtracking algorithm framework */
void backtrack(State *state, Choice *choices, int numChoices, State *res, int numRes) {
// Check if it's a solution
if (isSolution(state)) {
// Record the solution
recordSolution(state, res, numRes);
// Stop searching
return;
}
// Iterate through all choices
for (int i = 0; i < numChoices; i++) {
// Pruning: check if the choice is valid
if (isValid(state, &choices[i])) {
// Try: make a choice, update the state
makeChoice(state, &choices[i]);
backtrack(state, choices, numChoices, res, numRes);
// Retreat: undo the choice, revert to the previous state
undoChoice(state, &choices[i]);
}
}
}
```
=== "Kotlin"
```kotlin title=""
/* Backtracking algorithm framework */
fun backtrack(state: State?, choices: List<Choice?>, res: List<State?>?) {
// Check if it's a solution
if (isSolution(state)) {
// Record the solution
recordSolution(state, res)
// Stop searching
return
}
// Iterate through all choices
for (choice in choices) {
// Pruning: check if the choice is valid
if (isValid(state, choice)) {
// Try: make a choice, update the state
makeChoice(state, choice)
backtrack(state, choices, res)
// Retreat: undo the choice, revert to the previous state
undoChoice(state, choice)
}
}
}
```
=== "Ruby"
```ruby title=""
```
=== "Zig"
```zig title=""
```
Next, we solve Example Three based on the framework code. The `state` is the node traversal path, `choices` are the current node's left and right children, and the result `res` is the list of paths:
=== "Python"
```python title="preorder_traversal_iii_template.py"
def is_solution(state: list[TreeNode]) -> bool:
7 months ago
"""Determine if the current state is a solution"""
7 months ago
return state and state[-1].val == 7
def record_solution(state: list[TreeNode], res: list[list[TreeNode]]):
7 months ago
"""Record solution"""
7 months ago
res.append(list(state))
def is_valid(state: list[TreeNode], choice: TreeNode) -> bool:
7 months ago
"""Determine if the choice is legal under the current state"""
7 months ago
return choice is not None and choice.val != 3
def make_choice(state: list[TreeNode], choice: TreeNode):
7 months ago
"""Update state"""
7 months ago
state.append(choice)
def undo_choice(state: list[TreeNode], choice: TreeNode):
7 months ago
"""Restore state"""
7 months ago
state.pop()
def backtrack(
state: list[TreeNode], choices: list[TreeNode], res: list[list[TreeNode]]
):
7 months ago
"""Backtracking algorithm: Example three"""
# Check if it's a solution
7 months ago
if is_solution(state):
7 months ago
# Record solution
7 months ago
record_solution(state, res)
7 months ago
# Traverse all choices
7 months ago
for choice in choices:
7 months ago
# Pruning: check if the choice is legal
7 months ago
if is_valid(state, choice):
7 months ago
# Attempt: make a choice, update the state
7 months ago
make_choice(state, choice)
7 months ago
# Proceed to the next round of selection
7 months ago
backtrack(state, [choice.left, choice.right], res)
7 months ago
# Retract: undo the choice, restore to the previous state
7 months ago
undo_choice(state, choice)
```
=== "C++"
```cpp title="preorder_traversal_iii_template.cpp"
6 months ago
/* Determine if the current state is a solution */
bool isSolution(vector<TreeNode *> &state) {
return !state.empty() && state.back()->val == 7;
}
7 months ago
6 months ago
/* Record solution */
void recordSolution(vector<TreeNode *> &state, vector<vector<TreeNode *>> &res) {
res.push_back(state);
}
7 months ago
6 months ago
/* Determine if the choice is legal under the current state */
bool isValid(vector<TreeNode *> &state, TreeNode *choice) {
return choice != nullptr && choice->val != 3;
}
7 months ago
6 months ago
/* Update state */
void makeChoice(vector<TreeNode *> &state, TreeNode *choice) {
state.push_back(choice);
}
7 months ago
6 months ago
/* Restore state */
void undoChoice(vector<TreeNode *> &state, TreeNode *choice) {
state.pop_back();
}
7 months ago
6 months ago
/* Backtracking algorithm: Example three */
void backtrack(vector<TreeNode *> &state, vector<TreeNode *> &choices, vector<vector<TreeNode *>> &res) {
// Check if it's a solution
if (isSolution(state)) {
// Record solution
recordSolution(state, res);
}
// Traverse all choices
for (TreeNode *choice : choices) {
// Pruning: check if the choice is legal
if (isValid(state, choice)) {
// Attempt: make a choice, update the state
makeChoice(state, choice);
// Proceed to the next round of selection
vector<TreeNode *> nextChoices{choice->left, choice->right};
backtrack(state, nextChoices, res);
// Retract: undo the choice, restore to the previous state
undoChoice(state, choice);
}
}
}
7 months ago
```
=== "Java"
```java title="preorder_traversal_iii_template.java"
7 months ago
/* Determine if the current state is a solution */
7 months ago
boolean isSolution(List<TreeNode> state) {
return !state.isEmpty() && state.get(state.size() - 1).val == 7;
}
7 months ago
/* Record solution */
7 months ago
void recordSolution(List<TreeNode> state, List<List<TreeNode>> res) {
res.add(new ArrayList<>(state));
}
7 months ago
/* Determine if the choice is legal under the current state */
7 months ago
boolean isValid(List<TreeNode> state, TreeNode choice) {
return choice != null && choice.val != 3;
}
7 months ago
/* Update state */
7 months ago
void makeChoice(List<TreeNode> state, TreeNode choice) {
state.add(choice);
}
7 months ago
/* Restore state */
7 months ago
void undoChoice(List<TreeNode> state, TreeNode choice) {
state.remove(state.size() - 1);
}
7 months ago
/* Backtracking algorithm: Example three */
7 months ago
void backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {
7 months ago
// Check if it's a solution
7 months ago
if (isSolution(state)) {
7 months ago
// Record solution
7 months ago
recordSolution(state, res);
}
7 months ago
// Traverse all choices
7 months ago
for (TreeNode choice : choices) {
7 months ago
// Pruning: check if the choice is legal
7 months ago
if (isValid(state, choice)) {
7 months ago
// Attempt: make a choice, update the state
7 months ago
makeChoice(state, choice);
7 months ago
// Proceed to the next round of selection
7 months ago
backtrack(state, Arrays.asList(choice.left, choice.right), res);
7 months ago
// Retract: undo the choice, restore to the previous state
7 months ago
undoChoice(state, choice);
}
}
}
```
=== "C#"
```csharp title="preorder_traversal_iii_template.cs"
7 months ago
[class]{preorder_traversal_iii_template}-[func]{IsSolution}
7 months ago
7 months ago
[class]{preorder_traversal_iii_template}-[func]{RecordSolution}
7 months ago
7 months ago
[class]{preorder_traversal_iii_template}-[func]{IsValid}
7 months ago
7 months ago
[class]{preorder_traversal_iii_template}-[func]{MakeChoice}
7 months ago
7 months ago
[class]{preorder_traversal_iii_template}-[func]{UndoChoice}
7 months ago
7 months ago
[class]{preorder_traversal_iii_template}-[func]{Backtrack}
7 months ago
```
=== "Go"
```go title="preorder_traversal_iii_template.go"
7 months ago
[class]{}-[func]{isSolution}
7 months ago
7 months ago
[class]{}-[func]{recordSolution}
7 months ago
7 months ago
[class]{}-[func]{isValid}
7 months ago
7 months ago
[class]{}-[func]{makeChoice}
7 months ago
7 months ago
[class]{}-[func]{undoChoice}
7 months ago
7 months ago
[class]{}-[func]{backtrackIII}
7 months ago
```
=== "Swift"
```swift title="preorder_traversal_iii_template.swift"
7 months ago
[class]{}-[func]{isSolution}
7 months ago
7 months ago
[class]{}-[func]{recordSolution}
7 months ago
7 months ago
[class]{}-[func]{isValid}
7 months ago
7 months ago
[class]{}-[func]{makeChoice}
7 months ago
7 months ago
[class]{}-[func]{undoChoice}
7 months ago
7 months ago
[class]{}-[func]{backtrack}
7 months ago
```
=== "JS"
```javascript title="preorder_traversal_iii_template.js"
7 months ago
[class]{}-[func]{isSolution}
7 months ago
7 months ago
[class]{}-[func]{recordSolution}
7 months ago
7 months ago
[class]{}-[func]{isValid}
7 months ago
7 months ago
[class]{}-[func]{makeChoice}
7 months ago
7 months ago
[class]{}-[func]{undoChoice}
7 months ago
7 months ago
[class]{}-[func]{backtrack}
7 months ago
```
=== "TS"
```typescript title="preorder_traversal_iii_template.ts"
7 months ago
[class]{}-[func]{isSolution}
7 months ago
7 months ago
[class]{}-[func]{recordSolution}
7 months ago
7 months ago
[class]{}-[func]{isValid}
7 months ago
7 months ago
[class]{}-[func]{makeChoice}
7 months ago
7 months ago
[class]{}-[func]{undoChoice}
7 months ago
7 months ago
[class]{}-[func]{backtrack}
7 months ago
```
=== "Dart"
```dart title="preorder_traversal_iii_template.dart"
7 months ago
[class]{}-[func]{isSolution}
7 months ago
7 months ago
[class]{}-[func]{recordSolution}
7 months ago
7 months ago
[class]{}-[func]{isValid}
7 months ago
7 months ago
[class]{}-[func]{makeChoice}
7 months ago
7 months ago
[class]{}-[func]{undoChoice}
7 months ago
7 months ago
[class]{}-[func]{backtrack}
7 months ago
```
=== "Rust"
```rust title="preorder_traversal_iii_template.rs"
7 months ago
[class]{}-[func]{is_solution}
7 months ago
7 months ago
[class]{}-[func]{record_solution}
7 months ago
7 months ago
[class]{}-[func]{is_valid}
7 months ago
7 months ago
[class]{}-[func]{make_choice}
7 months ago
7 months ago
[class]{}-[func]{undo_choice}
7 months ago
7 months ago
[class]{}-[func]{backtrack}
7 months ago
```
=== "C"
```c title="preorder_traversal_iii_template.c"
7 months ago
[class]{}-[func]{isSolution}
7 months ago
7 months ago
[class]{}-[func]{recordSolution}
7 months ago
7 months ago
[class]{}-[func]{isValid}
7 months ago
7 months ago
[class]{}-[func]{makeChoice}
7 months ago
7 months ago
[class]{}-[func]{undoChoice}
7 months ago
7 months ago
[class]{}-[func]{backtrack}
7 months ago
```
=== "Kotlin"
```kotlin title="preorder_traversal_iii_template.kt"
7 months ago
[class]{}-[func]{isSolution}
7 months ago
7 months ago
[class]{}-[func]{recordSolution}
7 months ago
7 months ago
[class]{}-[func]{isValid}
7 months ago
7 months ago
[class]{}-[func]{makeChoice}
7 months ago
7 months ago
[class]{}-[func]{undoChoice}
7 months ago
7 months ago
[class]{}-[func]{backtrack}
7 months ago
```
=== "Ruby"
```ruby title="preorder_traversal_iii_template.rb"
[class]{}-[func]{is_solution}
[class]{}-[func]{record_solution}
[class]{}-[func]{is_valid}
[class]{}-[func]{make_choice}
[class]{}-[func]{undo_choice}
[class]{}-[func]{backtrack}
```
=== "Zig"
```zig title="preorder_traversal_iii_template.zig"
[class]{}-[func]{isSolution}
[class]{}-[func]{recordSolution}
[class]{}-[func]{isValid}
[class]{}-[func]{makeChoice}
[class]{}-[func]{undoChoice}
[class]{}-[func]{backtrack}
```
6 months ago
As per the requirements, after finding a node with a value of $7$, the search should continue, **thus the `return` statement after recording the solution should be removed**. Figure 13-4 compares the search processes with and without retaining the `return` statement.
7 months ago
![Comparison of retaining and removing the return in the search process](backtracking_algorithm.assets/backtrack_remove_return_or_not.png){ class="animation-figure" }
<p align="center"> Figure 13-4 &nbsp; Comparison of retaining and removing the return in the search process </p>
6 months ago
Compared to the implementation based on pre-order traversal, the code implementation based on the backtracking algorithm framework seems verbose, but it has better universality. In fact, **many backtracking problems can be solved within this framework**. We just need to define `state` and `choices` according to the specific problem and implement the methods in the framework.
7 months ago
## 13.1.4 &nbsp; Common terminology
7 months ago
To analyze algorithmic problems more clearly, we summarize the meanings of commonly used terminology in backtracking algorithms and provide corresponding examples from Example Three as shown in Table 13-1.
7 months ago
<p align="center"> Table 13-1 &nbsp; Common backtracking algorithm terminology </p>
<div class="center-table" markdown>
| Term | Definition | Example Three |
| --------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
| Solution (solution) | A solution is an answer that satisfies specific conditions of the problem, which may have one or more | All paths from the root node to node $7$ that meet the constraint |
| Constraint (constraint) | Constraints are conditions in the problem that limit the feasibility of solutions, often used for pruning | Paths do not contain node $3$ |
| State (state) | State represents the situation of the problem at a certain moment, including choices made | Current visited node path, i.e., `path` node list |
| Attempt (attempt) | An attempt is the process of exploring the solution space based on available choices, including making choices, updating the state, and checking if it's a solution | Recursively visiting left (right) child nodes, adding nodes to `path`, checking if the node's value is $7$ |
| Backtracking (backtracking) | Backtracking refers to the action of undoing previous choices and returning to the previous state when encountering states that do not meet the constraints | When passing leaf nodes, ending node visits, encountering nodes with a value of $3$, terminating the search, and function return |
| Pruning (pruning) | Pruning is a method to avoid meaningless search paths based on the characteristics and constraints of the problem, which can enhance search efficiency | When encountering a node with a value of $3$, no further search is continued |
</div>
!!! tip
Concepts like problems, solutions, states, etc., are universal, and are involved in divide and conquer, backtracking, dynamic programming, and greedy algorithms, among others.
## 13.1.5 &nbsp; Advantages and limitations
The backtracking algorithm is essentially a depth-first search algorithm that attempts all possible solutions until a satisfying solution is found. The advantage of this method is that it can find all possible solutions, and with reasonable pruning operations, it can be highly efficient.
However, when dealing with large-scale or complex problems, **the operational efficiency of backtracking may be difficult to accept**.
- **Time**: Backtracking algorithms usually need to traverse all possible states in the state space, which can reach exponential or factorial time complexity.
- **Space**: In recursive calls, it is necessary to save the current state (such as paths, auxiliary variables for pruning, etc.). When the depth is very large, the space requirement may become significant.
Even so, **backtracking remains the best solution for certain search problems and constraint satisfaction problems**. For these problems, since it is unpredictable which choices can generate valid solutions, we must traverse all possible choices. In this case, **the key is how to optimize efficiency**, with common efficiency optimization methods being two types.
- **Pruning**: Avoid searching paths that definitely will not produce a solution, thus saving time and space.
- **Heuristic search**: Introduce some strategies or estimates during the search process to prioritize the paths that are most likely to produce valid solutions.
## 13.1.6 &nbsp; Typical backtracking problems
Backtracking algorithms can be used to solve many search problems, constraint satisfaction problems, and combinatorial optimization problems.
**Search problems**: The goal of these problems is to find solutions that meet specific conditions.
- Full permutation problem: Given a set, find all possible permutations and combinations of it.
- Subset sum problem: Given a set and a target sum, find all subsets of the set that sum to the target.
- Tower of Hanoi problem: Given three rods and a series of different-sized discs, the goal is to move all the discs from one rod to another, moving only one disc at a time, and never placing a larger disc on a smaller one.
**Constraint satisfaction problems**: The goal of these problems is to find solutions that satisfy all the constraints.
- $n$ queens: Place $n$ queens on an $n \times n$ chessboard so that they do not attack each other.
- Sudoku: Fill a $9 \times 9$ grid with the numbers $1$ to $9$, ensuring that the numbers do not repeat in each row, each column, and each $3 \times 3$ subgrid.
- Graph coloring problem: Given an undirected graph, color each vertex with the fewest possible colors so that adjacent vertices have different colors.
**Combinatorial optimization problems**: The goal of these problems is to find the optimal solution within a combination space that meets certain conditions.
- 0-1 knapsack problem: Given a set of items and a backpack, each item has a certain value and weight. The goal is to choose items to maximize the total value within the backpack's capacity limit.
- Traveling salesman problem: In a graph, starting from one point, visit all other points exactly once and then return to the starting point, seeking the shortest path.
- Maximum clique problem: Given an undirected graph, find the largest complete subgraph, i.e., a subgraph where any two vertices are connected by an edge.
Please note that for many combinatorial optimization problems, backtracking is not the optimal solution.
- The 0-1 knapsack problem is usually solved using dynamic programming to achieve higher time efficiency.
- The traveling salesman is a well-known NP-Hard problem, commonly solved using genetic algorithms and ant colony algorithms, among others.
- The maximum clique problem is a classic problem in graph theory, which can be solved using greedy algorithms and other heuristic methods.