Lecture 12 Recursion part 1

Slides:



Advertisements
Similar presentations
Basics of Recursion Programming with Recursion
Advertisements

Chapter 20 Recursion.
Solve problems with Java code Algorithms with Java.
Slide 1 Insert your own content. Slide 2 Insert your own content.
0 - 0.
ALGEBRAIC EXPRESSIONS
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
MULT. INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Addition Facts
Introduction to Programming
Lecture 10 Methods COMP1681 / SE15 Introduction to Programming.
Lecture 10 Flow of Control: Loops (Part 2) COMP1681 / SE15 Introduction to Programming.
CSC 205 Programming II Lecture 10 Towers of Hanoi.
Chapter 17 Recursion.
Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.
5.9 + = 10 a)3.6 b)4.1 c)5.3 Question 1: Good Answer!! Well Done!! = 10 Question 1:
1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t)
Absolute-Value Equations and Inequalities
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 7: Recursion Java Software Structures: Designing and Using.
Addition 1’s to 20.
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Week 1.
C OMP 110 R ECURSION Instructor: Jason Carter. 2 R ECURSION English Return (Oxford/Webster) procedure repeating itself indefinitely or until condition.
Starting Out with Java: From Control Structures through Objects
Bottoms Up Factoring. Start with the X-box 3-9 Product Sum
Introduction to Recursion and Recursive Algorithms
CS 106 Introduction to Computer Science I 03 / 28 / 2008 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.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
RECURSION.
Recursion.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Chapter 15 Recursion INTRODUCTION Recursion is a program-solving technique that expresses the solution of a problem in terms of the solutions of.
Recursion AP Computer Science A Mr. Langner By: Thomas Robbins.
CIS 068 Welcome to CIS 068 ! Stacks and Recursion.
Lecture 12 Recursion part 1 Richard Gesick. Recursion A recursive method is a method that calls itself. A recursive method is capable of solving only.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Chapter 8 Recursion Modified.
Advanced Computer Architecture and Parallel Processing Rabie A. Ramadan http:
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
IB Computer Science Unit 5 – Advanced Topics Recursion.
1 Recursion n what is it? n how to build recursive algorithms n recursion analysis n tracing simple recursive functions n hands on attempts at writing.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 12 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/ George Koutsogiannakis 1.
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
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,
OBJECT ORIENTED PROGRAMMING II LECTURE 21 GEORGE KOUTSOGIANNAKIS
Sections 4.1 & 4.2 Recursive Definitions,
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
Recursion what is it? how to build recursive algorithms
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Recursion A problem solving technique where an algorithm is defined in terms of itself A recursive method is a method that calls itself A recursive algorithm.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Functions.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursion Output Input
Lecture 17 Recursion part 1 Richard Gesick.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
The Hanoi Tower Problem
CS302 - Data Structures using C++
OBJECT ORIENTED PROGRAMMING II LECTURE 22 GEORGE KOUTSOGIANNAKIS
Lecture 12 Recursion part 1 CSE /26/2018.
Fundaments of Game Design
Dr. Sampath Jayarathna Cal Poly Pomona
Recursion.
Recursive Thinking.
Presentation transcript:

Lecture 12 Recursion part 1 Richard Gesick

Topics Simple Recursion Recursion with a Return Value

Simple Recursion Sometimes large problems can be solved by transforming the large problem into smaller and smaller problems until you reach an easily solved problem. This methodology of successive reduction of problems is called recursion. That easy-to-solve problem is called the base case. The formula that reduces the size of a problem is called the general case.

Recursive Methods A recursive method calls itself, i.e. in the body of the method, there is a call to the method itself. The arguments passed to the recursive call are smaller in value than the original arguments.

Simple Recursion When designing a recursive solution for a problem, we need to do two things: Define the base case. Define the rule for the general case.

Printing “Hello World” n Times Using Recursion In order to print “Hello World” n times (n is greater than 0), we can do the following: Print “Hello World” Print “Hello World” (n – 1) times This is the general case. We have reduced the size of the problem from size n to size (n – 1).

Printing “Hello World” n Times Using Recursion Printing “Hello World” (n – 1) times will be done by Printing “Hello World” Printing “Hello World” (n – 2) times … and so on Eventually, we will arrive at printing “Hello World” 0 times: that is easy to solve; we do nothing. That is the base case.

Pseudocode for Printing “Hello World” n Times Using Recursion printHelloWorldNTimes( int n ) { if ( n is greater than 0 ) print “Hello World” printHelloWorldNTimes( n – 1 ) } // else do nothing

Coding the Recursive Method public static void printHelloWorldNTimes(int n) { if ( n > 0 ) System.out.println( “Hello World” ); printHelloWorldNTimes( n – 1 ); } // if n is 0 or less, do nothing

Recursion with a Return Value In a value-returning method, the return statement can include a call to another value-returning method. For example, public int multiplyAbsoluteValueBy3( int n ) { return ( 3 * Math.abs( n ) ); }

Recursion with a Return Value In a recursive value-returning method, the return statement can include a call to the method itself. The return value of a recursive value-returning method often consists of an expression that includes a call to the method itself: return ( expression including a recursive call to the method );

Calculating a Factorial The factorial of a positive number is defined as factorial( n ) = n! = n * ( n – 1 ) * ( n – 2 ) * … 3 * 2 * 1 By convention, factorial( 0 ) = 0! = 1 (The factorial of a negative number is not defined.) Can we find a relationship between the problem at hand and a smaller, similar problem?

Calculating a Factorial factorial( n ) = n! = n * ( n – 1 ) * ( n – 2 ) * … 3 * 2 * 1 factorial( n - 1 ) = ( n – 1 )! = ( n – 1 ) * ( n – 2 ) * … 3 * 2 * 1 So we can write: factorial( n ) = n * factorial( n – 1 ) That formula defines the general case.

Calculating a Factorial factorial( n ) = n * factorial( n – 1 ) At each step, the size of the problem is reduced by 1: we progress from a problem of size n to a problem of size (n – 1) A call to factorial( n ) will generate a call to factorial( n – 1 ), which in turn will generate a call to factorial( n – 2 ), …. Eventually, a call to factorial( 0 ) will be generated; this is our easy-to-solve problem. We know that factorial( 0 ) = 1. That is the base case.

Code for a Recursive Factorial Method public static int factorial( int n ) { if ( n <= 0 ) // base case return 1; else // general case return ( n * factorial( n – 1 ) ); }

Common Error Trap In coding a recursive method, failure to code the base case will result in a run-time error. If the base case is not coded, the recursive calls continue indefinitely because the base case is never reached. This eventually generates a StackOverflowError.

Greatest Common Divisor The Greatest Common Divisor (gcd) of two numbers is the greatest positive integer that divides evenly into both numbers. The Euclidian algorithm finds the gcd of two positive numbers a and b. It is based on the fact that: gcd( a, b ) = gcd ( b, remainder of a / b ) assuming: b is not 0

GCD: Euclidian Algorithm Step 1: r0 = a % b if ( r0 is equal to 0 ) gcd( a, b ) = b stop else go to Step 2 Step 2: Repeat Step 1 with b and r0,instead of a and b.

GCD Example: Euclidian Algorithm If a = 123450 and b = 60378, then … 123450 % 60378 = 2694 (different from 0) 60378 % 2694 = 1110 (different from 0) 2694 % 1110 = 474 (different from 0) 1110 % 474 = 162 (different from 0) 474 % 162 = 150 (different from 0) 162 % 150 = 12 (different from 0) 150 % 12 = 6 (different from 0) 12 % 6 = 0  gcd( 123450, 60378 ) = 6

GCD Code { if ( dividend % divisor == 0 ) return divisor; public static int gcd( int dividend, int divisor ) { if ( dividend % divisor == 0 ) return divisor; else // general case return ( gcd( divisor, dividend % divisor ) ); }