mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 11:09:48 +08:00
Bug fixes and improvements (#1348)
* Add "reference" for EN version. Bug fixes. * Unify the figure reference as "the figure below" and "the figure above". Bug fixes. * Format the EN markdown files. * Replace "" with <u></u> for EN version and bug fixes * Fix biary_tree_dfs.png * Fix biary_tree_dfs.png * Fix zh-hant/biary_tree_dfs.png * Fix heap_sort_step1.png * Sync zh and zh-hant versions. * Bug fixes * Fix EN figures * Bug fixes * Fix the figure labels for EN version
This commit is contained in:
@@ -6,7 +6,7 @@ The table below lists several example data, including the input arrays and their
|
||||
|
||||
<p align="center"> Table <id> Permutation examples </p>
|
||||
|
||||
| Input array | Permutations |
|
||||
| Input array | Permutations |
|
||||
| :---------- | :----------------------------------------------------------------- |
|
||||
| $[1]$ | $[1]$ |
|
||||
| $[1, 2]$ | $[1, 2], [2, 1]$ |
|
||||
@@ -22,7 +22,7 @@ From the perspective of the backtracking algorithm, **we can imagine the process
|
||||
|
||||
From the code perspective, the candidate set `choices` contains all elements of the input array, and the state `state` contains elements that have been selected so far. Please note that each element can only be chosen once, **thus all elements in `state` must be unique**.
|
||||
|
||||
As shown in the following figure, we can unfold the search process into a recursive tree, where each node represents the current state `state`. Starting from the root node, after three rounds of choices, we reach the leaf nodes, each corresponding to a permutation.
|
||||
As shown in the figure below, we can unfold the search process into a recursive tree, where each node represents the current state `state`. Starting from the root node, after three rounds of choices, we reach the leaf nodes, each corresponding to a permutation.
|
||||
|
||||

|
||||
|
||||
@@ -33,11 +33,11 @@ To ensure that each element is selected only once, we consider introducing a boo
|
||||
- After making the choice `choice[i]`, we set `selected[i]` to $\text{True}$, indicating it has been chosen.
|
||||
- When iterating through the choice list `choices`, skip all nodes that have already been selected, i.e., prune.
|
||||
|
||||
As shown in the following figure, suppose we choose 1 in the first round, 3 in the second round, and 2 in the third round, we need to prune the branch of element 1 in the second round and elements 1 and 3 in the third round.
|
||||
As shown in the figure below, suppose we choose 1 in the first round, 3 in the second round, and 2 in the third round, we need to prune the branch of element 1 in the second round and elements 1 and 3 in the third round.
|
||||
|
||||

|
||||
|
||||
Observing the above figure, this pruning operation reduces the search space size from $O(n^n)$ to $O(n!)$.
|
||||
Observing the figure above, this pruning operation reduces the search space size from $O(n^n)$ to $O(n!)$.
|
||||
|
||||
### Code implementation
|
||||
|
||||
@@ -55,7 +55,7 @@ After understanding the above information, we can "fill in the blanks" in the fr
|
||||
|
||||
Suppose the input array is $[1, 1, 2]$. To differentiate the two duplicate elements $1$, we mark the second $1$ as $\hat{1}$.
|
||||
|
||||
As shown in the following figure, half of the permutations generated by the above method are duplicates.
|
||||
As shown in the figure below, half of the permutations generated by the above method are duplicates.
|
||||
|
||||

|
||||
|
||||
@@ -63,7 +63,7 @@ So, how do we eliminate duplicate permutations? Most directly, consider using a
|
||||
|
||||
### Pruning of equal elements
|
||||
|
||||
Observing the following figure, in the first round, choosing $1$ or $\hat{1}$ results in identical permutations under both choices, thus we should prune $\hat{1}$.
|
||||
Observing the figure below, in the first round, choosing $1$ or $\hat{1}$ results in identical permutations under both choices, thus we should prune $\hat{1}$.
|
||||
|
||||
Similarly, after choosing $2$ in the first round, choosing $1$ and $\hat{1}$ in the second round also produces duplicate branches, so we should also prune $\hat{1}$ in the second round.
|
||||
|
||||
@@ -90,6 +90,6 @@ Please note, although both `selected` and `duplicated` are used for pruning, the
|
||||
- **Repeated choice pruning**: There is only one `selected` throughout the search process. It records which elements are currently in the state, aiming to prevent an element from appearing repeatedly in `state`.
|
||||
- **Equal element pruning**: Each round of choices (each call to the `backtrack` function) contains a `duplicated`. It records which elements have been chosen in the current traversal (`for` loop), aiming to ensure equal elements are selected only once.
|
||||
|
||||
The following figure shows the scope of the two pruning conditions. Note, each node in the tree represents a choice, and the nodes from the root to the leaf form a permutation.
|
||||
The figure below shows the scope of the two pruning conditions. Note, each node in the tree represents a choice, and the nodes from the root to the leaf form a permutation.
|
||||
|
||||

|
||||
|
||||
Reference in New Issue
Block a user