mirror of
https://github.com/krahets/hello-algo.git
synced 2026-10-21 05:25:19 +08:00
docs(en): use the standard Tower of Hanoi name (#1975)
* docs(en): use the standard Tower of Hanoi name * docs(en): preserve existing Hanoi diagram step labels * docs(en): retain Problem in Tower of Hanoi title * docs(en): retain problem in the Hanoi glossary entry
This commit is contained in:
@@ -119,7 +119,7 @@ The following table lists important terms that appear in this book.
|
||||
| counting sort |
|
||||
| radix sort |
|
||||
| divide and conquer |
|
||||
| hanota problem |
|
||||
| Tower of Hanoi problem |
|
||||
| backtracking algorithm |
|
||||
| constraint |
|
||||
| solution |
|
||||
|
||||
@@ -75,7 +75,7 @@ On the one hand, divide and conquer can be used to solve many classic algorithmi
|
||||
- **Finding the closest pair of points**: This algorithm first divides the point set into two parts, then finds the closest pair of points in each part separately, and finally finds the closest pair of points that spans both parts.
|
||||
- **Large integer multiplication**: For example, the Karatsuba algorithm, which decomposes large integer multiplication into several smaller integer multiplications and additions.
|
||||
- **Matrix multiplication**: For example, the Strassen algorithm, which decomposes large matrix multiplication into multiple small matrix multiplications and additions.
|
||||
- **Hanota problem**: The hanota problem can be solved through recursion, which is a typical application of the divide and conquer strategy.
|
||||
- **Tower of Hanoi problem**: The Tower of Hanoi problem can be solved through recursion, which is a typical application of the divide and conquer strategy.
|
||||
- **Solving inversion pairs**: In a sequence, if a preceding number is greater than a following number, these two numbers form an inversion pair. Solving the inversion pair problem can utilize the divide and conquer approach with the help of merge sort.
|
||||
|
||||
On the other hand, divide and conquer is widely applied in the design of algorithms and data structures.
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# Hanota Problem
|
||||
# Tower of Hanoi Problem
|
||||
|
||||
In merge sort and building binary trees, we decompose the original problem into two subproblems, each half the size of the original problem. However, for the hanota problem, we adopt a different decomposition strategy.
|
||||
In merge sort and building binary trees, we decompose the original problem into two subproblems, each half the size of the original problem. However, for the Tower of Hanoi problem, we adopt a different decomposition strategy.
|
||||
|
||||
!!! question
|
||||
|
||||
@@ -10,9 +10,9 @@ In merge sort and building binary trees, we decompose the original problem into
|
||||
2. Only one disc can be moved at a time.
|
||||
3. A smaller disc must always be on top of a larger disc.
|
||||
|
||||

|
||||

|
||||
|
||||
**We denote the hanota problem of size $i$ as $f(i)$**. For example, $f(3)$ represents moving $3$ discs from `A` to `C`.
|
||||
**We denote the Tower of Hanoi problem of size $i$ as $f(i)$**. For example, $f(3)$ represents moving $3$ discs from `A` to `C`.
|
||||
|
||||
### Considering the Base Cases
|
||||
|
||||
@@ -68,7 +68,7 @@ Since we already know the solutions to $f(1)$ and $f(2)$, we can think from a di
|
||||
|
||||
Essentially, **we divide problem $f(3)$ into two subproblems $f(2)$ and one subproblem $f(1)$**. By solving these three subproblems in order, the original problem is solved. This shows that the subproblems are independent and their solutions can be merged.
|
||||
|
||||
From this, we can summarize the divide and conquer strategy for solving the hanota problem shown in the figure below: divide the original problem $f(n)$ into two subproblems $f(n-1)$ and one subproblem $f(1)$, and solve these three subproblems in the following order.
|
||||
From this, we can summarize the divide and conquer strategy for solving the Tower of Hanoi problem shown in the figure below: divide the original problem $f(n)$ into two subproblems $f(n-1)$ and one subproblem $f(1)$, and solve these three subproblems in the following order.
|
||||
|
||||
1. Move $n-1$ discs from `A` to `B` with the help of `C`.
|
||||
2. Move the remaining $1$ disc directly from `A` to `C`.
|
||||
@@ -76,7 +76,7 @@ From this, we can summarize the divide and conquer strategy for solving the hano
|
||||
|
||||
For these two subproblems $f(n-1)$, **we can recursively divide them in the same way** until reaching the smallest subproblem $f(1)$. The solution to $f(1)$ is known and requires only one move operation.
|
||||
|
||||

|
||||

|
||||
|
||||
### Code Implementation
|
||||
|
||||
@@ -86,12 +86,12 @@ In the code, we declare a recursive function `dfs(i, src, buf, tar)`, whose purp
|
||||
[file]{hanota}-[class]{}-[func]{solve_hanota}
|
||||
```
|
||||
|
||||
As shown in the figure below, the hanota problem forms a recursion tree of height $n$, where each node represents a subproblem corresponding to an invocation of the `dfs()` function, **therefore the time complexity is $O(2^n)$ and the space complexity is $O(n)$**.
|
||||
As shown in the figure below, the Tower of Hanoi problem forms a recursion tree of height $n$, where each node represents a subproblem corresponding to an invocation of the `dfs()` function, **therefore the time complexity is $O(2^n)$ and the space complexity is $O(n)$**.
|
||||
|
||||

|
||||

|
||||
|
||||
!!! quote
|
||||
|
||||
The hanota problem originates from an ancient legend. In a temple in ancient India, monks had three tall diamond pillars and $64$ golden discs of different sizes. The monks continuously moved the discs, believing that when the last disc was correctly placed, the world would come to an end.
|
||||
The Tower of Hanoi problem originates from an ancient legend. In a temple in ancient India, monks had three tall diamond pillars and $64$ golden discs of different sizes. The monks continuously moved the discs, believing that when the last disc was correctly placed, the world would come to an end.
|
||||
|
||||
However, even if the monks moved one disc per second, it would take approximately $2^{64} \approx 1.84×10^{19}$ seconds, which is about $585$ billion years, far exceeding current estimates of the age of the universe. Therefore, if this legend is true, we should not need to worry about the end of the world.
|
||||
|
||||
@@ -10,4 +10,4 @@
|
||||
- Compared to brute-force search, adaptive search is more efficient. Search algorithms with time complexity of $O(\log n)$ are typically implemented based on the divide and conquer strategy.
|
||||
- Binary search is another typical application of divide and conquer. It does not include the step of merging solutions of subproblems. We can implement binary search through recursive divide and conquer.
|
||||
- In the problem of building a binary tree, building the tree (original problem) can be divided into building the left subtree and right subtree (subproblems), which can be achieved by dividing the index intervals of the preorder and inorder traversals.
|
||||
- In the hanota problem, a problem of size $n$ can be divided into two subproblems of size $n-1$ and one subproblem of size $1$. After solving these three subproblems in order, the original problem is solved.
|
||||
- In the Tower of Hanoi problem, a problem of size $n$ can be divided into two subproblems of size $n-1$ and one subproblem of size $1$. After solving these three subproblems in order, the original problem is solved.
|
||||
|
||||
+1
-1
@@ -151,7 +151,7 @@ nav:
|
||||
- 12.1 Divide and Conquer Algorithms: chapter_divide_and_conquer/divide_and_conquer.md
|
||||
- 12.2 Divide and Conquer Search Strategy: chapter_divide_and_conquer/binary_search_recur.md
|
||||
- 12.3 Building a Binary Tree Problem: chapter_divide_and_conquer/build_binary_tree_problem.md
|
||||
- 12.4 Hanota Problem: chapter_divide_and_conquer/hanota_problem.md
|
||||
- 12.4 Tower of Hanoi Problem: chapter_divide_and_conquer/hanota_problem.md
|
||||
- 12.5 Summary: chapter_divide_and_conquer/summary.md
|
||||
- 12.6 Exercises: chapter_divide_and_conquer/exercises.md
|
||||
- Chapter 13. Backtracking:
|
||||
|
||||
Reference in New Issue
Block a user