Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 (Lafore’s Book) Recursion Hwajung Lee.  Definition: An algorithmic technique. To solve a problem on an instance of size n, the instance is:

Similar presentations


Presentation on theme: "Chapter 6 (Lafore’s Book) Recursion Hwajung Lee.  Definition: An algorithmic technique. To solve a problem on an instance of size n, the instance is:"— Presentation transcript:

1 Chapter 6 (Lafore’s Book) Recursion Hwajung Lee

2  Definition: An algorithmic technique. To solve a problem on an instance of size n, the instance is:  divided into two or more smaller instances.  Each of these smaller instances is recursively solved, andrecursively  the solutions are combined to produce a solution for the original instance.

3  Definition: An algorithmic technique where a function, in order to accomplish a task, calls itself with some part of the task.

4  Every recursive solution involves two major parts or cases, the second part having three components.  base case(s), in which the problem is simple enough to be solved directly, and  recursive case(s). A recursive case has three components: ▪ divide the problem into one or more simpler or smaller parts of the problem, ▪ call the function (recursively) on each part, and ▪ combine the solutions of the parts into a solution for the problem.

5  Definition: A member of the sequence of numbers such that each number is the sum of the preceding two. The first seven numbers are 1, 1, 2, 3, 5, 8, and 13.  Formal Definition: The nth Fibonacci number is F(n) = F(n-1) + F(n-2), where F(1)=1 and F(2)=1

6  Definition: The factorial is defined for a positive integer n as  n! = n*(n-1)* … *3*2*1  So, for example, 4! = 4*3*2*1.  The special case 0! is defined to have value 1.

7  Write a recursive method to calculate Factorials:

8  Definition: A member of the sequence of numbers such that the nth term in the series is obtained by adding n to the previous term. The first seven numbers are 1, 3, 6, 10, 15, 21, and 28.

9  Non-recursive program: Program with loop int tri(int n) { int total = 0; while (n > 0) { total = total + n; n = n - 1; } return total; }

10  Complete Recursive Program int tri(int n) { if (n == 1) return 1; else return n + tri(n - 1); }

11  Triangular Trace  Each call gets a new, separate copy of the parameter  Example: Trace – int t = tri(4) V 1 - tri(4): n is 4 V 2 - tri(3): n is 3 V 3 - tri(2): n is 2 V 4 - tri(1): n is 1 return 1 add n to 1 = 2 + 1 = 3 return 3 add n to 3 = 3 + 3 = 6 return 6 add n to 6 = 4 + 6 = 10 return 10 as value of tri(4)

12 1. Handles some version of the problem directly (ie without calling itself) - this version is called the base case 2. Handles some version of the problem recursively (ie by calling itself) - this version is called the recursive case 3. Recursive call solves a simpler version of the problem (what does simpler mean in this context?) 4. Uses solution from recursive call to solve original problem Example: Triangular Numbers, Factorial, Anagrams, Recursive Binary Search, The Towers of Hanoi, Mergesort

13  Arguments and return addresses must be stacked  Local variables must be allocated  Consider trace of int tri(int n) { int answer; if (n == 1) answer = 1; else answer = n + tri(n - 1); return answer; }

14  Sometimes a recursive routine uses a helper function  Triangle.java Triangle.java

15  Particularly common for algorithms involving recursively defined data structures:  Example: list is an empty list or a node followed by a list  Example: binary tree is an empty tree or a node with a left and right (sub)tree

16  Although recursion makes programming easier, does it make a language more powerful?  In other words, are there problems that require recursion to be solved?  No - recursion and while loops provide equivalent power  languages with if and loops = languages with if and recursion  Anything that can be computed with recursion can be computed with loops  Anything that can be computed with loops can be computed with recursion

17  Objective: To find all possible permutations of a specified word.  Definition: (Math) A permutation, also called an "arrangement number" or "order," is a rearrangement of the elements of an ordered list S into a one-to-one correspondence with S itself. The number of permutations on a set of n elements is given by n!

18  [One approach] Assume the word has n letters.  Anagram the rightmost n-1 letters  Rotate all n letters  Repeat these steps n times

19 WordDisplay Word? First LetterRemaining Letters Action catYescatRotate at ctaYesctaRotate ta catNocatRotate cat atcYesatcRotate tc actYesactRotate ct atcNoatcRotate atc tcaYestcaRotate ca tacYestacRotate ac tcaNotcaRotate tca catNocatDone

20  AnagramApp.java AnagramApp.java

21  Objective: To find a searchKey in an array a[]  Non-Recursive Binary Search Non-Recursive Binary Search  Recursive BinarySearch.java Recursive BinarySearch.java [Q1] What is the performance of the binary search in the big- Oh notation w.r.t. an input size n? [Q2] Prove your answer.

22  Objective: Given a series of numbers (or objects), decide its sequence.  Merge Sort Applet Merge Sort Applet  merge.java merge.java  mergeSort.java mergeSort.java [Q1] What is the performance of the merge sort in the big- Oh notation w.r.t. an input size n? [Q2] Prove your answer.


Download ppt "Chapter 6 (Lafore’s Book) Recursion Hwajung Lee.  Definition: An algorithmic technique. To solve a problem on an instance of size n, the instance is:"

Similar presentations


Ads by Google