160 likes | 310 Views
Recursion. Guilin Wang The School of Computer Science 13 Oct 2009 ( L05 ). Outline. Recursion - Concept - Examples - Complexity. 1. Recursion. ■ Three basic forms of algorithm construction: - Sequence, Selection, and Iteration.
E N D
Recursion Guilin Wang The School of Computer Science 13 Oct 2009 (L05)
Outline • Recursion - Concept - Examples - Complexity
1. Recursion ■ Three basic forms of algorithm construction: - Sequence, Selection, and Iteration. ■ These three forms are in fact sufficient for constructing any algorithm. ■ However, there are other constructs which usefully supplement them. - Modularity: - Recursion:
1. Recursion Example 1: Let’s consider an alternative algorithm for calculating factorial N, i.e., N!=1•2•3• • •N. Observation: N!=N • (N-1)!, and 1!=1. An alternative algorithm: modulefactorial(N) {Computes the factorial of N for any integer N > 0} ifN = 1 thenanswer is 1 elsemultiply N by factorial(N-1) endif endmodule
1. Recursion if 3=1 then … 6 else multiply 3 by factorial(2) if 2=1 then … 2 else multiply 2 by factorial(1) if 1=1 then answer is 1 1 else …
1. Recursion ■ The form of expression used here is called recursion, i.e., a recursive algorithm which calls itself. ■ To avoid circularity of recursion, the input for successive recursive calls must be progressively “simpler” in some sense. ■ There must be a limiting case, in which the input is so “simple” that the process can be carried out without calling itself further. ■ The factorial example: the input N is progressively smaller and finally becomes 1.
1. Recursion Example 2: Consider the process of reserving the order of letters in an English word. e.g. star rats. ■ Initial Algorithm: Remove first letter Reserve rest of word Append removed letter ■ Refined Algorithm: (2.28), page 44.
1. Recursion Example 2: Calculate the GCD of two integers x and y GCD(x, y) = GCD(y, reminder of x/y) if y > 0 GCD(x, y) = x if y = 0 ■ An Recursive Algorithm for GCD: moduleGCD(x, y) {Calculates GCD of non-negative integers x and y} ify = 0 thenanswer isx elsecalculate GCD (y, reminder of x/y) endif endmodule
1. Recursion Example 3: The Towers of Hanoi. According to legend an order of Buddhist monks in Hanoi has been engaged for many years in the following task:
1. Recursion ■ There are three vertical rods and a set of 64 disks of different sizes. ■ Each disk has a hole in the centre so that it can be slid onto any of the rods. ■ Initially, all disks are placed on a single rod, one on the top of the other in order of decreasing size. ■ The task for the monks is to transfer this original tower of disks to one of the other rods. ■ As required, only one disk can be moved at a time and no disk ever rests on top of a smaller one. The monks believe that the completion of this task will herald the end of the world!
1. Recursion Can we design an algorithm for the monks? ■ Assumption: Call the three rods as s: source, t: target, w: workspace. ■ Observation: To move the biggest disk to rod t, we have to move the other 63 disks to rod w first. ■ Initial Algorithm: transfer the top 63 disks from rod s to rod w move the bottom disk from rod s to rod t transfer the 63 disks from rod w to rod t
1. Recursion modulemovetower(N, source, target, workspace) {Moves a number of N discs from source to target using workspace where necessary} if N = 1 thenmove the disk from source to target else movetower(N-1, source, workspace, target) move 1 disk from source to target movetower(N-1, workspace, target, source) endif endmodule
1. Recursion ■Complexity (execution steps or time) of the above algorithm for the Tower of Hanoi is 2N-1 (why?). ■ Complexity is an very important aspect of an algorithm. ■ For N=64, the complete tower requires 264-1 steps for executing the above algorithm. ■ If the monks can move one disk in a second, and never make a mistake, they need about 600 000 000 000 years to finish their task! ■ Moreover, it can be proved there is no faster algorithm.
1. Recursion Recursion vs. Iteration: ■ Recursion is simply iteration in another guise, i.e, recursive algorithms can be replaced by iterative algorithms. ■ However, a recursive algorithm for describing a particular process is often far more concise than an iterative one. ■ In many cases (e.g. The tower of Hanoi), an recursive algorithm is much easier to derive and understand.
1. Recursion The strategy for designing recursive algorithm: ■ Determine how the process to be carried out can be expressed recursively: How the process can be expressed in terms of similar processes which differ from the original only by having an input which is in some sense simpler (e.g. a smaller number, or a shorter sequence) ■ Ensure that there is an escape route from recursion: There is a limiting case in which the input is so simple that the process can be executed without recourse to further recursive calls.
Summary This Lecture: - Recursion Next Lecture: -Parallelism