Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ramamurthy Recursion: The Mirrors B. Ramamurthy CS114A,B.

Similar presentations


Presentation on theme: "Ramamurthy Recursion: The Mirrors B. Ramamurthy CS114A,B."— Presentation transcript:

1 Ramamurthy Recursion: The Mirrors B. Ramamurthy CS114A,B

2 Ramamurthy Introduction  Recursion is one of most powerful methods of solution available to computer scientists.  We will study application of recursion for computing, counting and searching.  We will also discuss the mechanics of recursion.

3 Ramamurthy Topics to be Covered  Recursive Solutions : Methodology  Computing using Recursion  Counting using Recursion  Searching using Recursion  Summary

4 Ramamurthy Recursive Solutions  Recursion is an important problem solving approach that is an alternative to iteration.  These are questions to answer when using recursive solution: 1. How can you define the problem in terms of a smaller problem of the same type? 2. How does each recursive call diminish the size of the problem? 3. What instance of the problem can serve as the base case? 4. As the problem size diminishes, will you reach this base case?

5 Ramamurthy Example 1: Factorial of N  Iterative definition: Factorial(N) = N * (N-1) * (N-2) *…1 for any N > 0. Factorial(0) = 1  Recursive solution: 1. Factorial(N) = N * Factorial(N-1) 2. Problem diminishing? yes. 3. Base case: Factorial(0) = 1; base case does not have a recursive call. 4. Can reach base case as problem diminishes? yes

6 Ramamurthy Factorial of N : C++ solution int Fact(int N) //Computes factorial of non-negative integer //PRE: N must be greater than or equal to 0 //POST: Returns factorial of N; N unchanged { if (N==0) return 1; else return N * Fact(N-1); }

7 Ramamurthy Factorial of N cout << Fact(3); return 3*Fact(2); return 2*Fact(1); return 1*Fact(0); return 1; 1 * 1 2 * 1 3 * 2 6

8 Ramamurthy Invariants  An invariant is a condition that is always True at a particular point in an algorithm.  For example, an invariant for factorial function above can be specified after else as shown below: if (N == 0) return 1; else //INV: N > 0, so N-1 >= 0 return N * Fact(N-1);  Write invariants as documentation for your code.

9 Ramamurthy Example 2: Writing String Backwards 1. Write a string of length N backwards in terms of writing a string of length (N-1) backwards. 2. Base case : Choice 1: Writing empty string. Choice 2: Writing a string of length 1.

10 Ramamurthy WriteBackward(S); if string is empty do nothing; // this is the base case else { write the last character of S; WriteBackward(S - minus its last character;}  Lets now look at the code for WriteBackward(S).

11 Ramamurthy Variations of WriteBackward(S);  First character and the rest of the string backward.  Base case is printing one character.  Try these.

12 Ramamurthy Computing Using Recursion  Raising integer to a power: int Pow2 (int X, int N) { if (N == 0) return 1; else return X * Pow2(X, N-1); }

13 Ramamurthy Power Function Using Recursion  A more efficient solution: int Pow3(int X, int N) { if (N==0) return 1; else { int HalfPower = Pow3(X, N/2); if ( N % 2 == 0) return HalfPower * HalfPower; else return X * HalfPower * HalfPower; }}

14 Ramamurthy Fibonacci Sequence  Fib (N) = 1 if N <= 2 (two base cases) = Fib (N-1) + Fib (N-2); if N > 2  Recursive solution: int Fib( int N) { if (N <= 2) return 1; else Fib(N-1) + Fib(N-2); }

15 Ramamurthy Counting Using Recursion  Choosing K out of N items. Need to write in terms of choosing from N-1 items;  Choose 1 and then choose K-1 from rest (N-1) + Eliminate 1 and choose K from N-1 items.  C(N,K) = 1 if K = 0 = 1 if K = N = 0 if K > N = C(N-1,K-1) + C(N-1,K) if 0 < K < N  Try it with a real-life example, say, marbles.  Reading Assignment: Mad Scientist Problem(79- 81)

16 Ramamurthy Searching Using Recursion BinSearch( A, Value) if (A is of size 1) Determine if its element is equal to Value; else { Find Midpoint of A; Determine which half of A contains Value If (Value is in first half of A) BinSearch(first half of A, Value) Else BinSearch(second half of A, Value)}

17 Ramamurthy Implementation Considerations  How will you pass “half of A” to the recursive calls to BinSearch? –Pass entire array but send also the limits for search. –For example, BinSearch(A, First, Mid -1, Value);  How do you determine which half of the array contains Value? –Test of equality and comparison between MidValue and Search Value. –For example, if (Value < A[mid])  What should the base case(s) be? –First > Last (termination without finding) –Value == A[Mid] (Found Search Value)

18 Ramamurthy C++ implementation int BinSearch(const int A[], int First, int Last, int Value) // POST: if Value is in the array, returns the // index of the array element that // equals Value; Otherwise returns -1. { if (First > Last) return -1; // base case 1

19 Ramamurthy C++ implementation (Contd.) else { int Mid = (First + Last)/2; if (Value == A[Mid]) return Mid; // base case 2 else if (Value < A[Mid]) return BinSearch (A, First, Mid -1, Value); else return BinSearch(A, Mid+1, Last, Value); } //else }//BinSearch

20 Ramamurthy Find the kth smallest element of an array  This problem is different from the ones we examined: You cannot predict in advance the size of either the smaller problems or the base case in the recursive solution to the kth smallest-element problem.  Recursive solution: 1. Decide a pivot element in the array P with index Pindex. 2. Partition the array into three parts: elements P (call it S2).

21 Ramamurthy Find the Kth smallest… (Contd.) 3. If there are k or more elements in S1 = A[First…Pindex-1] then S1 contains k smallest elements of array A[First…Lats]. kth smallest is in S1. 4. If there k-1 elements in S1, the pivot element is the required element. 5. If there are fewer than k-1 elements in S1, then kth element is S2 = A[Pindex+1 -L]. Since we have eliminates Pindex-First elements, now we are looking for (k-Pindex-First+1)th element in S2.

22 Ramamurthy Find the kth smallest.. algo element_type Ksmall(k, A, First, Last) 1. Choose a pivot element p from A[First…Last]; 2. if (k < Pindex - F + 1) return Ksmall(k, A, First, Pindex-1); 3. else if (k == Pindex - First + 1) return p; 4. else return Ksmall(k - (Pindex - First + 1), A, Pindex+1, L);

23 Ramamurthy Recursion and Efficiency  Overhead associated with function calls and the inherent inefficiency of some recursive algorithms contribute to inefficiency in some recursive solutions.  Example for inherent inefficiency: Rabbit(n) = Rabbit(n-1) + Rabbit(n-2);  Recursion is not always appropriate, particularly when a clear, efficient iterative solution exists.  True value of recursion is as a way to solve problems for which there is is no simple nonrecursive solutions.

24 Ramamurthy Summary  Recursion is a powerful problem-solving technique that often produces very clean solutions to many complex problems.  As with most powerful tools, if it is not used properly, it may lead to inefficiency.  Go thru’ self-test exercises, and regular exercises at the end of chapter 2.


Download ppt "Ramamurthy Recursion: The Mirrors B. Ramamurthy CS114A,B."

Similar presentations


Ads by Google