Recursion Chapter 8. 2 Chapter Contents What Is Recursion? Tracing a Recursive Method Recursive Methods That Return a Value Recursively Processing an.

Slides:



Advertisements
Similar presentations
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Advertisements

Recursion Ellen Walker CPSC 201 Data Structures Hiram College.
Recursive methods. Recursion A recursive method is a method that contains a call to itself Often used as an alternative to iteration when iteration is.
ITEC200 – Week07 Recursion. 2 Learning Objectives – Week07 Recursion (Ch 07) Students can: Design recursive algorithms to solve.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
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.
Recursion. Binary search example postponed to end of lecture.
© 2006 Pearson Addison-Wesley. All rights reserved3-1 Chapter 3 Recursion: The Mirrors CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
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.
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.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
COMPSCI 105 S Principles of Computer Science Recursion 3.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
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.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.
Recursion: The Mirrors Chapter 2 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Chapter 2 Recursion: The Mirrors CS Data Structures Mehmet H Gunes Modified from authors’ slides.
© 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-
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.
1 Storage Classes, Scope, and Recursion Lecture 6.
Chapter 3 Recursion: The Mirrors. © 2004 Pearson Addison-Wesley. All rights reserved 3-2 Recursive Solutions Recursion –An extremely powerful problem-solving.
Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1.
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.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 15: Recursion Starting Out with Java: From Control Structures.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
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
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 2: Recursion: The Mirrors.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Chapter 8 Recursion Modified.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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.
Recursion Chapter 10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Recursion Chapter 10 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Recursion.
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 19: Recursion.
Recursion CENG 707.
Chapter 15 Recursion.
Recursion: The Mirrors
Chapter 15 Recursion.
Java 4/4/2017 Recursion.
Recursion Chapter 10.
Recursion: The Mirrors
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Recursive Thinking.
Recursion: The Mirrors
Presentation transcript:

Recursion Chapter 8

2 Chapter Contents What Is Recursion? Tracing a Recursive Method Recursive Methods That Return a Value Recursively Processing an Array Recursively Processing a Linked Chain The Time Efficiency of Recursive Methods Time Efficiency of countDown A Simple Solution to a Difficult Problem A Poor Solution to a Simple Problem

3 What Is Recursion? It is a problem-solving process involves repetition Breaks a problem into identical but smaller problems Eventually you reach a smallest problem Answer is obvious or trivial Using the solution to smallest problem enables you to solve the previous smaller problems Eventually the original big problem is solved An alternative to iteration An iterative solution involves loops

4 What Is Recursion? New Year Eve: counting down from 10.

5 What Is Recursion? A method that calls itself is a recursive method Base case: a known case in a recursive definition. The smallest problem Eventually, one of the smaller problems must be the base case /** Task: Counts down from a given positive integer. integer an integer > 0 */ public static void countDown(int integer) {System.out.println(integer); if (integer > 1) countDown(integer - 1); } // end countDown

6 When Designing Recursive Solution Four questions to ask before construct recursive solutions. If you follow these guidelines, you can be assured that it will work. How can you define the problem in terms of a smaller problem of the same type? How does each recursive call diminish the size of the problem? What instance of the problem can serve as the base case? As the problem size diminishes, will you reach this base case?

7 When Designing Recursive Solution For the method countDown, we have the following answers. countDown displays the given integer as the part of the solution that it contribute directly. Then call countDown with smaller size. The smaller problem is counting down from integer -1. The if statement asks if the process has reached the base case. Here, the base case occurs when integer is 1.

8 Recursive Solution Guidelines Method definition must provide parameter Leads to different cases Typically includes an if or a switch statement One or more of these cases should provide a non recursive solution( infinite recursion if don’t) The base or stopping case One or more cases includes recursive invocation Takes a step towards the base case

9 Tracing a Recursive Method Given: public static void countDown(int integer) {System.out.println(integer); if (integer > 1) countDown(integer - 1); } // end countDown The effect of method call countDown(3)

10 Tracing a Recursive Method Tracing the recursive call countDown(3)

11 Compare Iterative and Recursive Programs //Recursive version public static void countDown( int integer) { if ( integer >= 1) { System.out.println(integer); countDown(integer -1); } //Iterative version public static void countDown( int integer) { while ( integer >= 1) { System.out.println(integer); integer--; }

12 Question? Could you write an recursive method that skips n lines of output, where n is a positive integer. Use System.out.println() to skip one line. Describe a recursive algorithm that draws a given number of concentric circles. The innermost or outmost circle should have a given diameter. The diameter of each of the other circles should be three-fourths the diameter of the circle just outside it.

13 Answer to skipLines pubic static void skipLines ( int givenNumber) { if (givenNumber >= 1) { System.out.println(); skipLines(givenNumber – 1); } drawConcentricCircle( givenNumber, givenDiameter) { // what should be put inside??……. }

14 Tracing a Recursive Method Each call to a method generate an activation record that captures the state of the method’s execution and that is placed into a ADT stack. The activation-record stack remembers the history of incompleted method calls. A snapshot of a method’s state. The topmost activation record holds the data values for the currently executing method. When topmost method finishes, its activation record is popped In this way, Java can suspend the execution of a recursive method and re-invoke it when it appears on the top.

15 Tracing a Recursive Method The stack of activation records during the execution of a call to countDown(3)… continued →

16 Tracing a Recursive Method ctd. The stack of activation records during the execution of a call to countDown(3) Note: the recursive method will use more memory than an iterative method due to the stack of activation records

17 Tracing a Recursive Method Too many recursive calls can cause the error message “stack overflow”. Stack of activation records has become full. Method has used too much memory. Infinite recursion or large-size problems are the likely cause of this error.

18 Recursive Methods That Return a Value Task: Compute the sum … + n for an integer n > 0 public static int sumOf(int n) {int sum; if (n = = 1) sum = 1; // base case else sum = sumOf(n - 1) + n; // recursive call return sum; } // end sumOf

19 Recursive Methods That Return a Value The stack of activation records during the execution of a call to sumOf(3)

20 Recursively Processing an Array When processing array recursively, divide it into two pieces Last element one piece, rest of array another First element one piece, rest of array another Divide array into two halves A recursive method part of an implementation of an ADT is often private Its necessary parameters make it unsuitable as an ADT operation

21 Recursively Processing a Linked Chain To write a method that processes a chain of linked nodes recursively Use a reference to the chain's first node as the method's parameter Then process the first node Followed by the rest of the chain public void display() {displayChain(firstNode); System.out.println(); } // end display private void displayChain(Node nodeOne) {if (nodeOne != null) {System.out.print(nodeOne.data + " "); displayChain(nodeOne.next); } } // end displayChain

22 Recursively Divide the Array in Half public static void displayArray( int array[], int first, int last) { if (first == last) System.out.print(array[first]); else { int mid = (first + last) /2; displayArray(array, first, mid); displayArray(array, mid+1, last); }

23 A Simple Solution to a Difficult Problem The initial configuration of the Towers of Hanoi for three disks

24 A Simple Solution to a Difficult Problem Rules for the Towers of Hanoi game 1. Move one disk at a time. Each disk you move must be a topmost disk. 2. No disk may rest on top of a disk smaller than itself. 3. You can store disks on the second pole temporarily, as long as you observe the previous two rules.

25 A Simple Solution to a Difficult Problem The sequence of moves for solving the Towers of Hanoi problem with three disks. Continued →

26 A Simple Solution to a Difficult Problem (ctd) The sequence of moves for solving the Towers of Hanoi problem with three disks

27 A Simple Solution to a Difficult Problem The smaller problems in a recursive solution for four disks

28 A Simple Solution to a Difficult Problem Algorithm for solution with 1 disk as the base case Algorithm solveTowers(numberOfDisks, startPole, tempPole, endPole) if (numberOfDisks == 1) Move disk from startPole to endPole else { solveTowers(numberOfDisks-1, startPole, endPole, tempPole) Move disk from startPole to endPole solveTowers(numberOfDisks-1, tempPole, startPole, endPole) }

29 Recursion Efficiency How many moves occur for n disks? m(1) = 1 for n>1, two recursive calls to solve problems that have n-1 disks. m(n) = m(n-1) + m(n-1) +1 = 2*m(n-1) +1 Let’s evaluate the recurrence for m(n) for a few values of n: m(1) =1; m(2) = 3; m(3) = 7;m(4) = 15; m(5) = 31; m(6) = 63…. m(n) = 2^n -1

30 Mathematical Induction Prove this conjecture m(n) = 2^n -1 by using mathematical induction: We know that m(1) =1, which equals to 2^1-1=1, so the conjecture is true for n =1. Now assume that it is true for n=1,2,…,k, and consider m(k+1). m(k+1) = 2*m(k) +1 (use the recurrence relation) =2*(2^k-1) +1 = 2^(k+1) -1 ( we assume that m(k) = 2^k-1) Since the conjecture is true for n=k+1, it is true for all n>=1

31 Mathematical Induction Assume you want to prove some statement P, P(n) is true for all n starting with n = 1. The Principle of Math Induction states that, to this end, one should accomplish just two steps: 1). Prove that P(1) is true. 2). Assume that P(k) is true for some k. Derive from here that P(k+1) is also true. look P(1) is true and implies P(2). Therefore P(2) is true. But P(2) implies P(3). Therefore P(3) is true which implies P(4) and so on.

32 Multiplying Rabbits (The Fibonacci Sequence) “Facts” about rabbits Rabbits never die A rabbit reaches sexual maturity exactly two months after birth, that is, at the beginning of its third month of life Rabbits are always born in male-female pairs At the beginning of every month, each sexually mature male-female pair gives birth to exactly one male-female pair

33 Multiplying Rabbits (The Fibonacci Sequence) Problem How many pairs of rabbits are alive in month n? Month Month No calculation rabbit couples January = 1 February = 1 March = 2 April = 3 May = 5 June = 8 July = 13 August = 21 Recurrence relation rabbit(n) = rabbit(n-1) + rabbit(n-2)

34 A Poor Solution to a Simple Problem Fibonacci numbers First two numbers of sequence are 1 and 1 Successive numbers are the sum of the previous two 1, 1, 2, 3, 5, 8, 13, … This has a natural looking recursive solution Turns out to be a poor (inefficient) solution

35 A Poor Solution to a Simple Problem The recursive algorithm Algorithm Fibonacci(n) if (n <= 1) return 1 else return Fibonacci(n-1) + Fibonacci(n-2)

36 A Poor Solution to a Simple Problem The computation of the Fibonacci number F 6 (a) recursively; (b) iteratively Time efficiency grows exponentially with n, which is k^n Iterative solution is O(n) int fib(int n) { int f[n+1]; f[1] = f[2] = 1; for (int i = 3; i <= n; i++) f[i] = f[i-1] + f[i-2]; return f[n]; }