Presentation is loading. Please wait.

Presentation is loading. Please wait.

ITEC324 Principle of CS III

Similar presentations


Presentation on theme: "ITEC324 Principle of CS III"— Presentation transcript:

1 ITEC324 Principle of CS III
Chapter 6 (Lafore’s Book) Recursion Hwajung Lee ITEC324 Principle of CS III

2 Divide and Conquer (algorithmic technique)
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, and the solutions are combined to produce a solution for the original instance.

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

4 Structure of Recursive Solution
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 Example of Recursion Fibonacci number
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 Example of Recursion Factorials (1)
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 Example of Recursion Factorials (2)
Write a recursive method to calculate Factorials:

8 Example of Recursion Triangular Numbers (1)
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 Example of Recursion Triangular Numbers (2)
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 Example of Recursion Triangular Numbers (3)
Complete Recursive Program int tri(int n) { if (n == 1) return 1; else return n + tri(n - 1); }

11 Example of Recursion Triangular Numbers (4)
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 V 4 - tri(1): n is return 1 add n to 1 = = 3 return 3 add n to 3 = = 6 return 6 add n to 6 = = 10 return 10 as value of tri(4)

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

13 Efficiency of Recursive Methods
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 Helper Functions Sometimes a recursive routine uses a helper function
Triangle.java

15 Why We Use Recursive Methods (cont.)
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 Power of Recursion 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 Example of Recursion Recursive Binary Search
Objective: To find a searchKey in an array a[] Non-Recursive Binary Search 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.

18 Example of Recursion Merge sort
Objective: Given a series of numbers (or objects), decide its sequence. Merge Sort Applet merge.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 "ITEC324 Principle of CS III"

Similar presentations


Ads by Google