1 CS1120: Recursion. 2 What is Recursion? A method is said to be recursive if the method definition contains a call to itself A recursive method is a.

Slides:



Advertisements
Similar presentations
C++ Programming:. Program Design Including
Advertisements

Recursion, Divide and Conquer Lam Chi Kit (George) HKOI2007.
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.
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. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
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.
1 Chapter 1: Introduction What you have learnt in Comp1220 or Comp1170? What will be taught in Comp 1200? - more Abstract Data Types -efficient algorithms.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
FIT FIT1002 Computer Programming Unit 20 Recursion.
Review of Recursion What is a Recursive Method? The need for Auxiliary (or Helper) Methods How Recursive Methods work Tracing of Recursive Methods.
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.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 5. Recursive Algorithms.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 17: Recursion.
Department of Computer Science Data Structures Using C++ 2E Chapter 6: Recursion Learn about recursive Definitions Algorithms Functions Explore the base.
© 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.
13.2 Recursive Definitions Objective 1) Provide the recursive definition for sequences; 2) Identify the type of a sequence from a recursive definition.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Chapter 15: Advanced Topics: Introducing Data Structures and Recursion Visual Basic.NET Programming: From Problem Analysis to Program Design.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Lecture#16 Discrete Mathematics. Recursion Now, 1 is an odd positive integer by the definition base. With k = 1, = 3, so 3 is an odd positive integer.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
1 CS1120: Recursion. 2 What is Recursion? A method is said to be recursive if the method definition contains a call to itself A recursive method is a.
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.
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,
Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.
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.
 Prentice Hall. All rights reserved. 1 CS 1120 – Computer Science II Recursion (Ch.15) Many slides modified by Prof. L. Lilien (even many without.
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
Lecture - 8 On Stacks, Recursion. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Lecture Outline Quick sort Algorithm Recursion –Calculate n factorial –Fibonacci.
 Prentice Hall. All rights reserved. 1 Recursion (Section 7.13 & Exercise7.40 from ed.3) (Sections 6.15, 6.16 from ed.1) Many slides modified.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive Algorithms.
Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms.
Tower of Hanoi Puzzle Jianying Yu. I. Introduction The Puzzle: Conditions: n disks and three pegs. Conditions: n disks and three pegs. Goal: Move disks.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
Discrete Mathematics Lecture # 22 Recursion.  First of all instead of giving the definition of Recursion we give you an example, you already know the.
 Prentice Hall. All rights reserved. 1 CS 1120 – Computer Science II Recursion (Ch.15) Many slides modified by Prof. L. Lilien (even many without.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Recursion Chapter What is recursion? Recursion occurs when a method calls itself, either directly or indirectly. Used to solve difficult, repetitive.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Chapter 6 (Lafore’s Book) Recursion Hwajung Lee.  Definition: An algorithmic technique. To solve a problem on an instance of size n, the instance is:
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.
Chapter 9 Recursion. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe recursive function is –a.
Recursion.
Chapter Topics Chapter 16 discusses the following main topics:
Recursion.
Chapter 15 Recursion.
RECURSION.
Chapter 15 Recursion.
Recursion &Faster Sorting
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
MSIS 655 Advanced Business Applications Programming
Recursive Definitions
Algorithm Analysis (for Divide-and-Conquer problems)
Recursion Chapter 11.
CS1120: Recursion.
The Hanoi Tower Problem
CSC 143 Recursion.
Dr. Sampath Jayarathna Cal Poly Pomona
ITEC324 Principle of CS III
Presentation transcript:

1 CS1120: Recursion

2 What is Recursion? A method is said to be recursive if the method definition contains a call to itself A recursive method is a method that calls itself method A (…)

3 Why Recursion? In pursuit of absolute simplicity!!! An important idea behind recursion is to Divide, Conquer, and Glue (DCG) Divide the Problem P into subproblems P1, P2,…, Pn Conquer the subproblem by solving them S1, S2,…, Sn Glue subsolutions S1, S2,…, Sn together into the solution S to the whole problem.

Example of Recursion Often these subtasks are similar, but get smaller in size. Next, we further break these subtasks into sub-subtasks until the sub-subtasks cannot be further broken. –For example, factorial(3) can be calculated if factorial(2) can be. factorial(2) can be calculated if factorial(1) can be calculated. factorial(1) is not further broken since everyone know it is 1!!! 4

5 Recursion Example Fig. 6.14Recursive evaluation of 5!. (a) Procession of recursive calls. 5! 5 * 4! 4 * 3! 3 * 2! 2 * 1! 1 (b) Values returned from each recursive call. Final value = 120 5! = 5 * 24 = 120 is returned 4! = 4 * 6 = 24 is returned 2! = 2 * 1 = 2 is returned 3! = 3 * 2 = 6 is returned 1 returned 5! 5 * 4! 4 * 3! 3 * 2! 2 * 1! 1

6 Some Terminology Base Case: the simplest case In factorial function: the value of factorial(1) is 1. Recursion Call/Recursive Step: the method calls a “fresh” copy of itself (this is used to work on a smaller version of the problem) and often includes a return statement

7 Factorial Method public static long Factorial(long number) { // base case if (number <= 1) return 1; // recursion step else return number * Factorial(number -1); }

8 public static long Factorial(long number) { if (number <= 1) return 1; else { int temp; temp = Factorial(number -1); return number * temp; } number=4 Factorial(4) is Called public static long Factorial(long number) { if (number <= 1) return 1; else { int temp; temp = Factorial(number -1); return number * temp; } number=3 public static long Factorial(long number) { if (number <= 1) return 1; else { int temp; temp = Factorial(number -1); return number * temp; } number=2 public static long Factorial(long number) { if (number <= 1) return 1; else { int temp; temp = Factorial(number -1); return number * temp; } number=1

9 Why a recursive method works? A recursive method has its own fresh copy when it is called. –All variables such as parameters and local variables have their own copies. In Factorial method, variables such as number and temp have their own copies. The copies of number and temp when number=4 is different from the copies of number and temp when number=3

10 Example Using Recursion: The Fibonacci Sequence What is importance of Fibonacci Sequence – – Fibonacci Sequence –F(0) = 0 –F(1) = 1 –F(n) = F(n - 1) + F(n - 2) –Recursion is used to evaluate F(n) Complexity theory –How hard computers need to work to perform algorithms

Example Using Recursion: The Fibonacci Sequence Fig. 6.17Set of recursive calls to method Fibonacci (abbreviated as F ). return 1return 0 F( 1 )F( 0 )return 1 F( 3 ) F( 2 )F( 1 ) + return +

12 How to Ensure DCG When Using Recursion The key to use the recursion is to find whether the subtask is the same as the original task but in a small size. To do so, you need to write the clear and unambiguous method specification. (What does the method do, what does each parameter denote, and what does the method return)

13 Exercise Given an integer array A=[2,1,44,3,2,54,1,66], calculate the sum of the first n elements CalulateSumOfArray(int[] A, int n )

Hanoi Tower There are three pegs and 3 disks are initially placed on peg I. Goal is to move all 3 disks to peg III using peg II. But every move must follow the conditions: –You can only move one disk at a time (from any peg to any other peg), and –You may not stack a larger disk on top of a smaller disk 14 I IIIII I IIIII

How to Solve the Problem Recall DCG 15 I IIIII I IIIII IIIIII I IIIII

How to Solve the Problem Specify the following method 16 moveDiscs(int n, int fromPeg, int tempPeg, int toPeg) { } n: is the number of disks to be moved fromPeg, and toPeg denote the pegs where disks are moved from and to respectively. tempPeg is the temporary peg used during the movement. if (n >0) { moveDiscs(n-1, fromPeg, toPeg, tempPeg); system.out.print(“disc’’+ n + “is moved from”+fromPeg + “ to ” + toPeg); moveDiscs(n-1, tempPeg, fromPeg, toPeg); }

What is Wrong? 17 moveDiscs(int n, int fromPeg, int tempPeg, int toPeg) { } n: is the number of disks to be moved fromPeg, and toPeg denote the pegs where disks move from and to respectively. tempPeg is the temporary peg used during the movement. if (n >0) { moveDiscs(n/2, fromPeg, toPeg, tempPeg); moveDiscs(n-n/2, fromPeg,tempPeg, toPeg); moveDiscs(n/2, tempPeg, fromPeg, toPeg); } Illegal Move!!! ??? In OO Programming, You must write a clear specification for what you have programmed!!!

What is Correct Spec? 18 moveDiscs(int n, int fromPeg, int tempPeg, int toPeg) { } All disks has been number from 1, 2,..,n with 1 denoting the smallest one and n the largest one n: is the largest disk number to be moved, i.e. disk 1, disk 2,…,disk n are to be moved fromPeg, and toPeg denote the pegs where disks move from and to respectively. tempPeg is the temporary peg used during the movement. if (n >0) { moveDiscs(n/2, fromPeg, toPeg, tempPeg); moveDiscs(n-n/2, fromPeg,tempPeg, toPeg); moveDiscs(n/2, tempPeg, fromPeg, toPeg); } This is not a correct solution!!! Based on the above specification, can you find another Solution to the Hanoi Tower Problem via DCG?