Presentation is loading. Please wait.

Presentation is loading. Please wait.

7. RECURSIONS Rocky K. C. Chang October 12, 2015.

Similar presentations


Presentation on theme: "7. RECURSIONS Rocky K. C. Chang October 12, 2015."— Presentation transcript:

1 7. RECURSIONS Rocky K. C. Chang October 12, 2015

2 Objectives To be able to design recursive solutions to solve "simple" problems. To understand the relationship between recursion and mathematical induction. To understand that recursion is a postponement of work. To know how to design recursive algorithms systematically.

3 WHAT IS RECURSION?

4 Example 1: Computation of n! Standard method: fact(n) = 1 * 2 * … * n Recursive method: fact(n) = n * fact(n-1) fact(n-1) = (n-1) * fact(n-2) … fact(2) = 2 * fact(1) fact(1) = 1 * fact(0) fact(0) = 1

5 Implement a recursive function to compute n!. EXERCISE 7.1

6 Example 2: Birthday cake cutting Question: how many pieces of a circular cake as a result of n cuts through the center? Write down your answer here.

7 Example 2 Recursive approach:

8 Example 3: Fibonacci numbers The rabbit population problem: Start out with a male-female pair of rabbits. It takes two months for each pair to be reproductive. Each pair reproduces another male-female pair in each subsequent month. How many pairs of male-female rabbits after n months if no rabbits die during this period? Standard method: 1 st month: 1 2 nd month: 1 3 rd month: 2 4 th month: 3 …

9 Example 3 Recursive method: Just prior to the start of n th month, there are fib(n-1) pairs. However, only fib(n-2) pairs can reproduce new pairs. Therefore, fib(n) = ?

10 RECURSION AND MATHEMATICAL INDUCTION

11 Parallel w. mathematical induction Mathematical induction is a very powerful theorem- proving technique. Proving that a statement T(n) is true for n ≥ 1 requires the proofs for (Base) T(1) is true. (Induction) For every n > 1, if T(n-1) is true, then T(n) is true.

12 Mathematical induction, e.g., Prove that for all natural numbers x and n, x n – 1 is divisible by x – 1. Base: when n = 1, x n – 1 is divisible by x – 1. Induction: For n > 1, assume that x n-1 – 1 is divisible by x – 1. Write x n - 1 = x(x n-1 – 1) + (x – 1). Therefore, x n – 1 is also divisible by x – 1.

13 Mathematical induction, e.g., Prove that if n is a natural number and 1 + x > 0, then (1 + x) n ≥ 1 + nx. Base: for n = 1, LHS = RHS = 1 + x. Induction: For n ≥ 1, assume that (1 + x) n ≥ 1 + nx. (1 + x) n+1 = (1 + x)(1 + x) n (1 + x) n+1 ≥ (1 + x)(1 + nx) = 1 + (n + 1)x + nx 2 (1 + x) n+1 ≥ 1 + (n + 1)x

14 The parallels A recursive process consists of two parts: A smallest case that is processed without recursion (base case) A general method that reduces a particular case into one or more of the smaller cases (induction step) Factorials Base: Induction: The birthday cake cutting problem Base: Induction: Fibonacci numbers Base: Induction:

15 RECURSION AS A POSTPONEMENT OF WORK

16 Tracing the recursive calls

17 A recursive tree for fact(n)

18 How does recursion work? When a program is run, each execution of a method is called an activation. The data objects associated with each activation are stored in the memory as an activation record. Data objects include parameters, return values, return address and any variables that are local to the method. A stack is a data structure to store all activation records in a correct order.

19 A recursive tree for fib(n)

20 Memory requirement for recursions

21 HOW TO DESIGN RECURSIVE ALGORITHMS?

22 Several important steps Find the key step. How can this problem be divided into parts? Find a stopping rule. It is usually the small, special case that is trivial or easy to handle without recursion. Outline your algorithm. Combine the stopping rule and the key step, using an if statement to select between them. Check termination. Verify that the recursion will always terminate. Draw a recursion tree.

23 MORE COMPLEX EXAMPLES

24 Tower of Hanoi Problem: To move a stack of n disks from pole A to pole B, subjecting to 2 rules: Only one disk can be moved at a time. A larger disk can never go on top of a smaller one. n = 1, trivial n = 2, trivial again n = 3: trivial to some people? n = 4: trivial to fewer people? n = 64?

25 Think recursively …

26

27 A recursive solution solveTowers(n, A, B, C) n: the number of disks moving the disks from A to B via C no larger disks put on top of smaller disks A recursive approach: Base: trivial and solved for n = 1 Induction; for n > 1, solveTowers(n, A, B, C) can be solved by solveTowers(n - 1, A, C, B) // the top n-1 disks solveTowers(1, A, B, C) // the largest disk solveTowers(n - 1, C, B, A) // the top n-1 disks

28 A recursion tree for n = 3

29 Computing  Write R(n) = Q(1) × Q(2) × Q(3) × … × Q(n), where Q(1) = sqrt[½+ ½ sqrt(½)], Q(2) = sqrt[½+ ½ Q(1)], …, Q(n) = sqrt[½+ ½ Q(n – 1)], n > 1. Therefore, Base: R(1) = Q(1) Induction: Given R(n – 1) and Q(n – 1), Q(n) = sqrt[½+ ½ Q(n – 1)] and R(n) = R(n – 1) × Q(n).

30 The recursion tree

31 Summary Recursion is a divide-and-conquer approach to solving a complex problem. Divide, conquer, and glue Similar to mathematical induction, recursion has a “ base ” case and an “ induction ” step. A recursion implementation essentially postpones the work until hitting the base case. Recursions appear in many, many, …, different forms.

32 Acknowledgments The figures on pp. 19-20 are taken from Robert L. Kruse and Alexander J. Ryba, Data Structures and Program Design in C++, Prentice-Hall, Inc., 1999. The figures on pp. 25-26 are taken from F. Carrano and J. Prichard, Data Abstraction and Problem Solving with JAVA Wall and Mirrors, First Edition, Addison Wesley, 2003.

33 END


Download ppt "7. RECURSIONS Rocky K. C. Chang October 12, 2015."

Similar presentations


Ads by Google