CS1120: Recursion.

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)
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.
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.
Department of Computer Engineering Recursive Problem Solving Computer Programming for International Engineers.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
Department of Computer Science Data Structures Using C++ 2E Chapter 6: Recursion Learn about recursive Definitions Algorithms Functions Explore the base.
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.
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.
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 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.
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.
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.
Recursion Chapter 10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Recursion,
Chapter 9 Recursion. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe recursive function is –a.
Review of Recursion What is a Recursive Method?
Recursion.
Chapter Topics Chapter 16 discusses the following main topics:
Recursion.
Chapter 15 Recursion.
Chapter 10 Recursion Instructor: Yuksel / Demirer.
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)
Chapter 12 Recursion (methods calling themselves)
Recursion Chapter 11.
Review of Recursion What is a Recursive Method?
The Hanoi Tower Problem
Recursion.
CSC 143 Recursion.
Review of Recursion What is a Recursive Method?
CS148 Introduction to Programming II
Dr. Sampath Jayarathna Cal Poly Pomona
ITEC324 Principle of CS III
Review of Recursion What is a Recursive Method?
Presentation transcript:

CS1120: Recursion

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 (…)

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!!!

Recursion Example (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 Fig. 6.14 Recursive evaluation of 5!.

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

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

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

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

Example Using Recursion: The Fibonacci Sequence What is importance of Fibonacci Sequence http://en.wikipedia.org/wiki/Fibonacci_number http://people.bath.ac.uk/lec20/uses.html 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

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

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)

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 I II III I II III

How to Solve the Problem Recall DCG I II III I II III I II III I II III

How to Solve the Problem Specify the following method 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. moveDiscs(int n, int fromPeg, int tempPeg, int toPeg) { } 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? In OO Programming, You must write a clear specification 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. ??? moveDiscs(int n, int fromPeg, int tempPeg, int toPeg) { } 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? 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. moveDiscs(int n, int fromPeg, int tempPeg, int toPeg) { } 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?