Download presentation
Presentation is loading. Please wait.
Published byVirgil Preston Modified over 9 years ago
1
Recursion B.Ramamurthy Chapter 4
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 such as divide and conquer, spilt into first (/last) and the rest.
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
sumSquares problem int sumSquares(int m, int n) { if (m < n) return sumSquares(m, n-1) + n*n; else return n*n; }
7
sumSquares (split) int sumSquares( int m, int n) { int middle; if (m == n) return n*n; else { middle = (m+n)/2; sumSquares(m,middle) + sumSquares(middle+1, n); }
8
Ramamurthy 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.
9
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;}
10
Write List Backwards zTry actual code with 1.2 LinkedList class.
11
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).
12
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.
13
Tower of Hanoi zPole A with heavy metal disks of various sizes stacked on top of each other. zObjective: Transfer disks from Pole A to Pole B. zConstraint: Disks are so heavy that stacking to be done with largest at the bottom and in order. zSupport: You may use a spare pole during transfer.
14
Towers(Count, Source, Dest, Spare); if (Count is 1) Move disk from Source to Dest pole. else { Solve Towers( Count -1, Source, Spare, Dest); Solve Towers(1, Source, Dest, Spare); Solve Towers(Count -1, Spare, Dest, Source); }
15
Recursive call structure for Towers Towers(3,A,B,C) Towers(1,A,B,C)Towers(1,B,C,A) Towers(1,A,C,B) Towers(1,C,A,B)Towers(1,A,B,C) Towers(1,C,B,A) Towers(2,A,C,B) Towers(1,A,B,C)Towers(2,C,B,A)
16
How can we use this? zIn a real problem, model the problem as Tower of Hanoi. zThen a ready solution is available. zExample: Sheep, Hay and Wolf problem. zExample: Any transportation problem. zRecognize a general class of problems that can be solved using this model.
17
Defining Languages zA grammar states the rules of a language. zA recursive algorithm can be written based on this grammar that determines whether a given string is a member of the language ==> recognition algorithm. zFor expressing a grammar we use special symbols: X | Y means X or Y X.Y or simply X Y means X followed by Y an instance of entity xyz
18
A grammar for Java identifiers = | | = a | b …|z| A | B | C…|Z|_ = 0 | 1 | …|9
19
Recognition algorithm for identifiers boolean IsId (w) { // Returns TRUE if w is a legal Java identifier, // Otherwise returns FALSE if (w is of length 1) if (w is a letter) return true; else return false; else if (last char of w is a letter or a digit) return IsId(w minus its last char); else return false; }
20
Algebraic expressions zInfix : The term infix indicates that every binary operator appears between its operands : a + b zPrefix : An operator precedes its operands: + a b zPostfix : An operator follows its operands: a b + zAdvantage of prefix and postfix expressions : they don’t need precedence rules, association rules or parenthesis for the order of evaluation. Order of evaluation is embedded in the notation itself. One more example: (a + b) * c (infix) * + a b c (prefix) a b + c * (postfix)
21
Processing Prefix Expression double prefixValue(Expression e) { //base case if (e.length == 1) return value of firstElement; else { if (secondElement is operand) return (firstElementOperate (secondElement, prefixValue(e - firstTwoElements) else return (firstElementOperate (prefixValue (e - firstElement, lastElement), lastElement); }
22
Summary zRecursion allows you to solve problems whose iterative solutions are difficult to conceptualize. Example: Tower of Hanoi. zLinked list (or Collection ADT) traversal, backtracking, grammar recognition are some example where recursion can be applied.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.