Recursion B.Ramamurthy Chapter 4. Ramamurthy Introduction  Recursion is one of most powerful methods of solution available to computer scientists. 

Slides:



Advertisements
Similar presentations
CSC 205 Programming II Lecture 10 Towers of Hanoi.
Advertisements

COSC 2006 Data Structures I Recursion III
Recursion Ellen Walker CPSC 201 Data Structures Hiram College.
COMPSCI 105 S Principles of Computer Science Recursion 1.
Recursion Chapter 7. Spring 2010CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn how.
CS102 Algorithms and Programming II1 Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. A recursive.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 14: Recursion by.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
© 2006 Pearson Addison-Wesley. All rights reserved6-1 Chapter 6 Recursion as a Problem- Solving Technique.
Fall 2007CS 2251 Recursion Chapter 7. Fall 2007CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method.
Section 5.2 Defining Languages. © 2005 Pearson Addison-Wesley. All rights reserved5-2 Defining Languages A language –A set of strings of symbols –Examples:
28-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Recursion. Recursive Solutions Recursion breaks a problem into smaller identical problems – mirror images so to speak. By continuing to do this, eventually.
© 2006 Pearson Addison-Wesley. All rights reserved3-1 Chapter 3 Recursion: The Mirrors.
Chapter 5 Recursion as a Problem-Solving Technique.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Unit 7 1 Unit 7: Recursion H Recursion is a fundamental programming technique that can provide an elegant solution to a certain kinds of problems H In.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
1 Applications of Recursion (Walls & Mirrors - Chapter 5)
CENG 7071 Recursion. CENG 7072 Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. A recursive function.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
© 2006 Pearson Addison-Wesley. All rights reserved 3-1 Chapter 3 Recursion: The Mirrors.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
Chapter 3 Recursion: The Mirrors. © 2004 Pearson Addison-Wesley. All rights reserved 3-2 Recursive Solutions Recursion –An extremely powerful problem-solving.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
Data Structures and Abstractions with Java, 4e Frank Carrano
COSC2007 Data Structures II
COSC 2006 Data Structures I Recursion II
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 5: Recursion as a Problem-Solving Technique Data Abstraction.
Recursion Chapter 11. The Basics of Recursion: Outline Introduction to Recursion How Recursion Works Recursion versus Iteration Recursive Methods That.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 2: Recursion: The Mirrors.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Recursion as a Problem- Solving Technique Chapter 5 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
EXAMPLES OF RECURSION Towers of Hanoi Writing Linked Lists Backwards Recursive Insert 8 Queens Recognizing Simple Languages Prefix Expressions Conversion.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Recursion as a Problem- Solving Technique Chapter 5 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 111 Recursion Chapter Objectives become familiar with the idea of recursion learn to use recursion as a programming tool become familiar.
Ramamurthy Recursion: The Mirrors B. Ramamurthy CS114A,B.
Recursion Chapter What is recursion? Recursion occurs when a method calls itself, either directly or indirectly. Used to solve difficult, repetitive.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
1 CSC 143 Recursion [Reading: Chapter 17]. 2 Recursion  A recursive definition is one which is defined in terms of itself.  Example:  Sum of the first.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
Chapter 19: Recursion.
Recursion CENG 707.
Recursion Topic 5.
Computing with C# and the .NET Framework
Chapter 5 Recursion as a Problem-Solving Technique
Recursion: The Mirrors
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Chapter 14: Recursion Starting Out with C++ Early Objects
Ch. 6 Recursion as a Problem Solving Technique
Recursion B.Ramamurthy 2/23/2019 Ramamurthy.
Recursion: The Mirrors
B.Ramamurthy Chapter 9 CSE116A,B
Recursion as a Problem-Solving Technique
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Recursive Thinking.
Recursion: The Mirrors
Presentation transcript:

Recursion B.Ramamurthy Chapter 4

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.

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

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?

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

sumSquares problem int sumSquares(int m, int n) { if (m < n) return sumSquares(m, n-1) + n*n; else return n*n; }

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); }

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.

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;}

Write List Backwards zTry actual code with 1.2 LinkedList class.

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).

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.

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.

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); }

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)

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.

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

A grammar for Java identifiers = | | = a | b …|z| A | B | C…|Z|_ = 0 | 1 | …|9

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; }

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)

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); }

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.