Preface :
Up to this point, our study of recursion has involved algorithms that have alternative iterative solutions that are simple to code. The examples illustrate the design and implementation of recursive algorithms. They do not, however, let you appreciate the true importance of recursion as an algorithm design strategy. One of the most powerful features of recursion is its ability to allow a programmer to solve problems that would be difficult to design and implement as an iterative process. In this section, we illustrate this feature with an algorithm to draw tic marks on a line. A more interesting example is the famous Tower of Hanoi puzzle, which has an elegant recursive solution.
Building a Ruler :
A typical ruler is a sequence of inch-long intervals with separator marks. Each inch has a shorter mark at the 1/2-inch point and progressively shorter marks at 1/4-inch intervals and so forth. The problem is to create a program that draws marks at regular intervals on a line. The sizes of the marks differ, depending on the specific interval. The recursive method drawRuler() provides the solution. Its algorithm assumes the existance of the methoddrawMark(), which takes a point x and an integer value h as arguments and draw a vertical line at point x withc size proportional to h.
Let us trace the sequence of actions for the drawRuler() algorithm, assuming h is initially 3 and the interval is 1 inch, which low=0.0 and high=1.0. Each step draws a mark at the midpoint of an interval. Using the midpoint to separate the interval into half-lines, the step makes two recursive calls todrawRuler() to draw smaller marks at the midpoint of each half-line :
The drawRuler() method takes the endpoints of an interval and the level h as arguments. The first action is to draw a mark at the midpoint and then make two recursive calls to draw the marks in the two half-intervals, below is the sample code :
Towers of Hanoi :
Puzzle fans have long been fascinated with the Towers of Hanoi problem, which involves a stack of n graduated disks and a set of three needles called A, B, and C. The initial setup places the n disks on needle A. The task is to move the disks once at a time from needle to needle until the process rebuilds the original stack, but on needle C. In moving a disk, a larger disk may never be placed on top of a smaller disk.
In general, the algorithm that moves n disks require 2^n-1 moves. At first glance, you might think that solving the Towers of Hanoi puzzle would be daunting in terms of both time and strategy. Somewhat surprisingly, perhaps, the Tower of Hanoi has a relative simple relatively simple recursive solution. We illustrate the algorithm by looking at the simple three-disk Hanoi puzzle. Watch the steps as we move disks from needle A to C by way of the intermediate needle B. For discussion purposes, we break up the moves into separate stages encompassing several steps. These stages will be used later to develop the recursive method hanoi() :
As expected, n=3 so it takes 2^3-1=7 moves. To understand the recursive native of the process, note that Stage 1 and Stage 3 both describe separate Towers of Hanoi problems with two disks. In the first stage, two disks move from needle A to needle B, with needle C serving as temporary storage. In the third stage, two disks move from needle B to needle C, with needle A serving as temporary storage. The fact that the Towers of Hanoi algorithm involves a smaller version of the algorithm makes it recursive. The three-disk problem reduces to two two-disks problems.
The Recursive Method hanoi() :
The recursive process translates into the method called hanoi(). The arguments include n, the number of disks, and three string arguments that denote the name of the starting needle(initNeedle), the destination needle(endNeedle), and the intermediate needle(tempNeedle) that temporarily holds disks during the moves. The following is the method implementation :
Actually the hanoi() can be break into three steps. Considering n disks, the first step is to move n-1 disks from initNeedl to tempNeedle :
hanoi(n-1, initNeedle, tempNeedle, endNeedle); // recursive call for stage1
The second step is to move the largest disk from initNeedle to endNeedle :
hanoi(1, initNeedle, endNeedle, tempNeedle); // Here use n=1 because we only take the largest disk into account
The last step, of course, is to move the last disks(n-1) from tempNeedle to endNeedle :
hanoi(n-1, tempNeedle, endNeedle, initNeedle); // Complete the n-disks hanoi problem.
This is a blog to track what I had learned and share knowledge with all who can take advantage of them
標籤
- [ 英文學習 ]
- [ 計算機概論 ]
- [ 深入雲計算 ]
- [ 雜七雜八 ]
- [ Algorithm in Java ]
- [ Data Structures with Java ]
- [ IR Class ]
- [ Java 文章收集 ]
- [ Java 代碼範本 ]
- [ Java 套件 ]
- [ JVM 應用 ]
- [ LFD Note ]
- [ MangoDB ]
- [ Math CC ]
- [ MongoDB ]
- [ MySQL 小學堂 ]
- [ Python 考題 ]
- [ Python 常見問題 ]
- [ Python 範例代碼 ]
- [心得扎記]
- [網路教學]
- [C 常見考題]
- [C 範例代碼]
- [C/C++ 範例代碼]
- [Intro Alg]
- [Java 代碼範本]
- [Java 套件]
- [Linux 小技巧]
- [Linux 小學堂]
- [Linux 命令]
- [ML In Action]
- [ML]
- [MLP]
- [Postgres]
- [Python 學習筆記]
- [Quick Python]
- [Software Engineering]
- [The python tutorial]
- 工具收集
- 設計模式
- 資料結構
- ActiveMQ In Action
- AI
- Algorithm
- Android
- Ansible
- AWS
- Big Data 研究
- C/C++
- C++
- CCDH
- CI/CD
- Coursera
- Database
- DB
- Design Pattern
- Device Driver Programming
- Docker
- Docker 工具
- Docker Practice
- Eclipse
- English Writing
- ExtJS 3.x
- FP
- Fraud Prevention
- FreeBSD
- GCC
- Git
- Git Pro
- GNU
- Golang
- Gradle
- Groovy
- Hadoop
- Hadoop. Hadoop Ecosystem
- Java
- Java Framework
- Java UI
- JavaIDE
- JavaScript
- Jenkins
- JFreeChart
- Kaggle
- Kali/Metasploit
- Keras
- KVM
- Learn Spark
- LeetCode
- Linux
- Lucene
- Math
- ML
- ML Udemy
- Mockito
- MPI
- Nachos
- Network
- NLP
- node js
- OO
- OpenCL
- OpenMP
- OSC
- OSGi
- Pandas
- Perl
- PostgreSQL
- Py DS
- Python
- Python 自製工具
- Python Std Library
- Python tools
- QEMU
- R
- Real Python
- RIA
- RTC
- Ruby
- Ruby Packages
- Scala
- ScalaIA
- SQLAlchemy
- TensorFlow
- Tools
- UML
- Unix
- Verilog
- Vmware
- Windows 技巧
- wxPython
訂閱:
張貼留言 (Atom)
[Git 常見問題] error: The following untracked working tree files would be overwritten by merge
Source From Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 # git clean -d -fx 方案2: 今天在服务器上 gi...
-
前言 : 為什麼程序管理這麼重要呢?這是因為: * 首先,本章一開始就談到的,我們在操作系統時的各項工作其實都是經過某個 PID 來達成的 (包括你的 bash 環境), 因此,能不能進行某項工作,就與該程序的權限有關了。 * 再來,如果您的 Linux 系統是個...
-
屬性 : 系統相關 - 檔案與目錄 語法 : du [參數] [檔案] 參數 | 功能 -a | 顯示目錄中個別檔案的大小 -b | 以bytes為單位顯示 -c | 顯示個別檔案大小與總和 -D | 顯示符號鏈結的來源檔大小 -h | Hum...
-
來源自 這裡 說明 : split 是 Perl 中非常有用的函式之一,它可以將一個字串分割並將之置於陣列中。若無特別的指定,該函式亦使用 RE 與 $_ 變數 語法 : * split /PATTERN/,EXPR,LIMIT * split /...
沒有留言:
張貼留言