CS 106 Introduction to Computer Science I 03 / 28 / 2008 Instructor: Michael Eckmann.

Slides:



Advertisements
Similar presentations
Basics of Recursion Programming with Recursion
Advertisements

CS 106 Introduction to Computer Science I 02 / 29 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 26 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 11 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 09 / 2007 Instructor: Michael Eckmann.
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.
CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann.
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.
CS 106 Introduction to Computer Science I 04 / 02 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 01 / 30 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 09 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 06 / 2006 Instructor: Michael Eckmann.
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.
Recursion. 2 CMPS 12B, UC Santa Cruz Solving problems by recursion How can you solve a complex problem? Devise a complex solution Break the complex problem.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 25 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 27 / 2008 Instructor: Michael Eckmann.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 30 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
© 2004 Pearson Addison-Wesley. All rights reserved October 27, 2006 Recursion (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL 2006.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Recursion Review.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 19 Recursion.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
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.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Principles of Programming Chapter 11: Recursive Function  In this chapter, you will learn about  Recursion function 1 NI S1 2009/10.
Chapter 8 Recursion Modified.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
Recursion Part 3 CS221 – 2/27/09. Recursion A function calls itself directly: Test() { … Test(); … }
CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
IB Computer Science Unit 5 – Advanced Topics Recursion.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
CS 106 Introduction to Computer Science I 10 / 10 / 2007 Instructor: Michael Eckmann.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
Recursion.
Chapter 19: Recursion.
Chapter 15 Recursion.
Abdulmotaleb El Saddik University of Ottawa
Chapter 15 Recursion.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Chapter 8: Recursion Java Software Solutions
Java Software Structures: John Lewis & Joseph Chase
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursive Definitions
Chapter 8: Recursion Java Software Solutions
Chapter 8: Recursion Java Software Solutions
Dr. Sampath Jayarathna Cal Poly Pomona
Recursion (part 2) March 22, 2006 ComS 207: Programming I (in Java)
Presentation transcript:

CS 106 Introduction to Computer Science I 03 / 28 / 2008 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Spring 2008 Today’s Topics Comments and/or Questions? recursion –definition –factorial example –Towers of Hanoi example

Factorial method using iteration // non-recursive solution to the factorial problem public int compute_factorial(int num)‏ { int temp_factorial=1; while (num > 0)‏ { temp_factorial = temp_factorial * num; num--; } return temp_factorial; }

Factorial method using iteration Let's put this in a program and call it.

Recursion A recursive method is a method that calls itself (directly or indirectly.)‏ Recursion is often used when the problem to be solved can be easily implemented with recursion as opposed to an iterative technique. Another fact is all problems that can be solved recursively can be solved iteratively. Sometimes though the iterative solution might be very long and require lots of code. In that case recursion might be preferred because it is more readable and easier to write.

Recursion Let’s say we know the solution to some case of a problem. If we want to find a solution to a slightly more complex case of the problem, we might be able to use the solution to the easier problem as part of our solution. For example, if we know the factorial of the number 6, to find the factorial of 7, we can just multiply 7 * the factorial of 6, so 7! = 7 * 6!. Here, the solution we know (6! = 720) is used as part of the solution to 7!, which is 7*720 = 5040.

Recursion So, more generally, if we know the solution to (n-1)! We can easily find the solution to n! by just multiplying n*(n-1)!. In addition to knowing this though, we need to also know that 1! = 1. This is the simplest factorial. Now, powered with the knowledge that – 1! = 1 and – the fact that given (n-1)!, we can compute n! we have enough information to compute the factorial of any positive integer. Do we all agree?

Recursion This is a recursive solution to the factorial problem because –we know how to solve the simplest case (1!) and –we know how to solve a more complex case given a solution to a slightly less complex case n! = n*(n-1)!

Factorial method using recursion public int compute_factorial ( int num )‏ { if ( num <= 1 )‏ return 1; else return num * compute_factorial ( num - 1) ; } // this recursive method is based on figure 6.12 // in Deitel and Deitel.

Factorial method using recursion When the recursive method is called, if the number passed in as an argument is less than or equal to 1, then 1 is returned and the method ends. That is known as the base case. If the number passed in as an argument is greater than 1, then the else portion of the method is executed. This says to return that number times the result of another call to the method with the argument of one less than the original value. The else portion in this method is known as the recursion step.

Factorial method using recursion Let’s examine what happens when the number 2 is passed in as an argument to compute_factorial. int fact_value; fact_value = compute_factorial( 2 ); The method would check if 2 <= 1 and since that is false, the else portion would execute. The else portion calls compute_factorial(1). This second call to the method invokes another “copy” of the method. This second call checks if 1 <= 1 and this is true so it returns 1.

Factorial method using recursion This value of 1 gets returned to the most recent call (and hence the second call to the method ends its execution.)‏ This value of 1 gets multiplied by 2. This happens in the else portion of the method. The result, 2, gets returned to the original call of the method (and hence the first call to the method ends its execution.)‏ If compute_factorial was called with the number 3 as an argument, the method would end up being called three times.

Recursion When the method is recursively called in the else portion, the prior call to the method has not finished executing. It is waiting on the result of this new method call. There will be multiple active copies of the recursive method when the else portion executes. The recursion ends when the base case is finally met. The base case then returns a result to the previous call of the method and a sequence of returns occurs until the original method call gets its returned value.

Factorial method using recursion Let’s look at a program that will illustrate, through print statements, what happens in what order when a method is called recursively.

Recursion Understand that the Factorial example, was for instructional purposes only, since it is a very simple example of recursion. For the factorial method, in practice, we would always prefer the non-recursive (iterative) solution. It is more efficient in terms of both time and space. Recursive solutions often take more time to run, because making a method call is slower and requires more memory than doing the same statements without a method call.

Recursion Direct vs. indirect recursion – When a method calls itself, it is direct recursion – When method1 calls method2 and method2 calls method1, it is indirect. – There can be more than just two methods involved in the indirection.

Towers of Hanoi Three pegs Start with different size discs on first peg Move them all to the third peg using the following rules – Move only one disc at a time – Can't place a larger disc on a smaller disc – All discs must be on some peg except for the disc in transit between pegs Let's see an applet on the web.

Towers of Hanoi Do a solution with 3 discs. Do a solution with 4 discs. See any patterns emerging? Is it recursive?

Towers of Hanoi To move N discs from first peg to third – Move topmost N-1 discs from first peg to second – Move the largest disc from the first peg to third – Move the N-1 discs from the second peg to the third

Towers of Hanoi During the solution, each peg takes on a role – Either the start peg, end peg, or intermediate peg. – Notice: In the first and third parts, notice that they are the same as the whole, with the roles of the pegs changing. First part: – Move from: first peg (start) – To: second peg (end)‏ – Using as intermediate: third peg (intermediate)‏ Third part: – Move from: second peg (start) – To: third peg (end)‏ – Using as intermediate: first peg (intermediate)‏

Towers of Hanoi void moveTower(int numDiscs, int start, int end, int intermed)‏ { if (numDiscs == 1)‏ moveOneDisc(start, end); else { moveTower(numDiscs – 1, start, intermed, end); moveOneDisc(start, end); moveTower(numDiscs – 1, intermed, end, start); } void moveOneDisc(int p1, int p2)‏ { System.out.println(“Move the disc from peg “ + p1 + “ to peg “ + p2); }

Towers of Hanoi Let's put this in a program and run it. Does anyone think that an iterative solution would be possible? If so, do you think it'll be easier? You can think about this on your own if you wish and try to come up with an iterative (that is, non-recursive) solution.