M180: Data Structures & Algorithms in Java

Slides:



Advertisements
Similar presentations
Recursion Chapter 11. Objectives become familiar with the idea of recursion learn to use recursion as a programming tool become familiar with the binary.
Advertisements

Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 12: Recursion.
Recursion. Binary search example postponed to end of lecture.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
1 Recursion  Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems  Chapter 11 of the book.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 12: Recursion.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Recursion.
ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
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.
Chapter 11 Recursion. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Recursion Recursion is a fundamental programming technique that can provide.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Recursion.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1Recursion. 2 Outline thinking recursively recursive algorithms iteration vs. recursion recursive functions integer exponentiation (pow) infinite recursion.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
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.
1 TCSS 143, Autumn 2004 Lecture Notes Recursion Koffman/Wolfgang Ch. 7, pp ,
CSE 501N Fall ‘09 12: Recursion and Recursive Algorithms 8 October 2009 Nick Leidenfrost.
11-1 Recursive Thinking A recursive definition is one which uses the word or concept being defined in the definition itself When defining an English word,
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
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.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Recursion A function that calls itself. Recursion A function which calls itself is said to be recursive. Recursion is a technique which will allow us.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion 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.
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.
CS162 - Topic #10 Lecture: Recursion –The Nature of Recursion –Tracing a Recursive Function –Work through Examples of Recursion Programming Project –Discuss.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Welcome to Recursion! Say what?!? Recursion is… the process of solving large problems by simplifying them into smaller ones. similar to processing using.
Recursion.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
CprE 185: Intro to Problem Solving (using C)
Recursion DRILL: Please take out your notes on Recursion
Abdulmotaleb El Saddik University of Ottawa
Java 4/4/2017 Recursion.
To understand recursion, you have to understand recursion!
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
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 12 Recursion (methods calling themselves)
Recursion Chapter 11.
Lecture 17 Recursion part 1 Richard Gesick.
Programming application CC213
Recursion (part 1) October 24, 2007 ComS 207: Programming I (in Java)
Unit 3 Test: Friday.
Basics of Recursion Programming with Recursion
Chapter 11 Recursion.
Lecture 12 Recursion part 1 CSE /26/2018.
ICS103 Programming in C Lecture 11: Recursive Functions
Recursion.
Recursion (part 1) October 25, 2006 ComS 207: Programming I (in Java)
Presentation transcript:

M180: Data Structures & Algorithms in Java Recursion Arab Open University

Recursive thinking Recursion: a programming technique in which a method can call itself to solve a problem recursive definition: one which uses the concept being defined in the definition itself In some situations, a recursive definition can be an appropriate or elegant way to express a concept. Before applying recursion to programming, it is best to practice thinking recursively

A recursive algorithm Consider the task of finding out what place you are in a long line of people. If you cannot see the front of the line, you could ask the person in front of you. To answer your question, this person could ask the person in front of him/her, and so on.

A recursive algorithm Once the front person answers their place in line (first), this information is handed back, one person at a time, until it reaches you. This is the essence of recursive algorithms; many invocations of the same method each solve a small part of a large problem.

Infinite Recursion All recursive definitions have to have a non-recursive part If they didn't, there would be no way to terminate the recursive path Such a definition would cause infinite recursion This problem is similar to an infinite loop, but the non- terminating "loop" is part of the definition itself The non-recursive part is often called the base case

Recursion Java's looping constructs make implementing this process easy. sum(1) = 1 sum(N) = N + sum(N - 1) if N > 1 Consider what happens when the definition is applied to the problem of calculating sum(4): sum(4) = 4 + sum(3) = 4 + 3 + sum(2) = 4 + 3 + 2 + sum(1) = 4 + 3 + 2 + 1 The fact that sum(1) is defined to be 1 without making reference to further invocations of sum saves the process from going on forever and the definition from being circular.

Factorial example The factorial for any positive integer N, written N!, is defined to be the product of all integers between 1 and N inclusive // not recursive public static long factorial(int n) { long product = 1; for (int i = 1; i <= n; i++) { product *= i; } return product;}

Recursive factorial factorial can also be defined recursively: A factorial is defined in terms of another factorial until the basic case of 0! is reached // recursive public static long factorial(int n) { if (n == 0) { return 1; } else { return n * factorial(n - 1); }}

Recursive factorial 5! 5 * 4! 4 * 3! 3 * 2! 2 * 1! 1 2 6 24 120

Recursion vs. iteration every recursive solution has a corresponding iterative solution For example, N ! can be calculated with a loop For some problems, recursive solutions are often more simple and elegant than iterative solutions You must be able to determine when recursion is appropriate

Recursive Programming Consider the problem of computing the sum of all the numbers between 1 and any positive integer N This problem can be recursively defined as:

Recursive Programming // This method returns the sum of 1 to num public int sum (int num) { int result; if (num == 1) result = 1; else result = num + sum (n-1); return result; }

Recursive Programming main sum sum(3) sum(1) sum(2) result = 1 result = 3 result = 6

Fibonacci Recursion Consider the definition of Fibonacci numbers below. The first and second numbers in the Fibonacci sequence are 1. Thereafter, each number is the sum of its two immediate predecessors, as follows: 1 1 2 3 5 8 13 21 34 55 89 144 233 ... Or in other words: fibonacci(1) = 1 fibonacci(2) = 1 fibonacci(N) = fibonacci(N - 1) + fibonacci(N - 2) if N > 2 This is a recursive definition, and it is hard to imagine how one could express it no recursively.

Fibonacci Recursion As a second example of recursion, below is a method that calculates Fibonacci numbers: int fibonacci (int n){ if (n <= 2) return 1; else return fibonacci (n - 1) + fibonacci (n - 2); }

Malformed Recursive Method Here is a subtler example of a malformed recursive method: This method works fine if n is odd, but when n is even, the method passes through the stopping state and keeps on going. int badMethod (int n){ if (n == 1) return 1; else return n * badMethod(n - 2); }

Guidelines for Writing Recursive Methods A recursive method must have a well-defined termination or stopping state. For the factorial method, this was expressed in the lines: The recursive step, in which the method calls itself, must eventually lead to the stopping state. For the factorial method, the recursive step was expressed in the lines: if (n == 1) return 1; else return n * factorial(n - 1);

Guidelines for Writing Recursive Methods Because each invocation of the factorial method is passed a smaller value, eventually the stopping state must be reached. Had we accidentally written: the method would describe an infinite recursion. Eventually, the user would notice and terminate the program, or else the Java interpreter would run out memory, and the program would terminate with a stack overflow error. else return n * factorial(n + 1);

Number of Zeros in a Number Example: 2030 has 2 zeros If n has two or more digits the number of zeros is the number of zeros in n with the last digit removed plus an additional 1 if the last digit is zero Examples: number of zeros in 20030 is number of zeros in 2003 plus 1 number of zeros in 20031 is number of zeros in 2003 plus 0 recursive

numberOfZeros Recursive Design numberOfZeros in the number N K = number of digits in N Decomposition: numberOfZeros in the first K - 1 digits Last digit Composition: Add: numberOfZeros in the first K – 1 digits 1 if the last digit is zero Base case: N has one digit (K = 1)

numberOfZeros Method public static int numberOfZeros(int n) { int zeroCount; if (n==0) zeroCount = 1; else if (n < 10) zeroCount = 0; else if (n%10 == 0) zeroCount = numberOfZeros(n/10) + 1; else // n%10 != 0 zeroCount = numberOfZeros(n/10); return zeroCount; } Which is (are) the base case(s)? Why? Decompostion, Why? Composition, why?

Execution Trace (decomposition) public static int numberOfZeros(int n) { int zeroCount; if (n==0) zeroCount = 1; else if (n < 10) zeroCount = 0; else if (n%10 == 0) zeroCount = numberOfZeros(n/10) + 1; else // n%10 != 0 zeroCount = numberOfZeros(n/10); return zeroCount; } Execution Trace (decomposition) Each method invocation will execute one of the if-else cases shown at right. numberOfZeros(2005) numberOfZeros(200) 5 numberOfZeros(20) 0 numberOfZeros(2) 0

Execution Trace (composition) public static int numberOfZeros(int n) { int zeroCount; if (n==0) zeroCount = 1; else if (n < 10) zeroCount = 0; else if (n%10 == 0) zeroCount = numberOfZeros(n/10) + 1; else // n%10 != 0 zeroCount = numberOfZeros(n/10); return zeroCount; } Execution Trace (composition) Recursive calls return numberOfZeros(2005)->2 numberOfZeros(200)->2 5->0 numberOfZeros(20)->1 0->1 numberOfZeros(2)->0 0->1 + + +