A Different Solution  alternatively we can use the following algorithm: 1. if n == 0 done, otherwise I. print the string once II. print the string (n.

Slides:



Advertisements
Similar presentations
Basics of Recursion Programming with Recursion
Advertisements

C++ Programming:. Program Design Including
COMPSCI 105 S Principles of Computer Science Recursion 1.
Recursion Great fleas have little fleas upon their backs to bite 'em, And little fleas have lesser fleas, and so ad infinitum. And the great fleas themselves,
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Copyright © 2003 Pearson Education, Inc. Slide 1.
CSC 2300 Data Structures & Algorithms January 30, 2007 Chapter 2. Algorithm Analysis.
Recursion. Recursive Solutions Recursion breaks a problem into smaller identical problems – mirror images so to speak. By continuing to do this, eventually.
© 2006 Pearson Addison-Wesley. All rights reserved3-1 Chapter 3 Recursion: The Mirrors.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
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.
CENG 7071 Recursion. CENG 7072 Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. A recursive function.
Recursion.  Identify recursive algorithms  Write simple recursive algorithms  Understand recursive function calling  With reference to the call stack.
Scientific Computing Algorithm Convergence and Root Finding Methods.
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.
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 17: Recursion.
Recursion. Basic problem solving technique is to divide a problem into smaller sub problems These sub problems may also be divided into smaller sub problems.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
Chapter 3 Recursion: The Mirrors. © 2004 Pearson Addison-Wesley. All rights reserved 3-2 Recursive Solutions Recursion –An extremely powerful problem-solving.
Chapter 8. Section 8. 1 Section Summary Introduction Modeling with Recurrence Relations Fibonacci Numbers The Tower of Hanoi Counting Problems Algorithms.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
Functions and an Introduction to Recursion.  Recursive function ◦ A function that calls itself, either directly, or indirectly (through another function)
Recursion Chapter 11. The Basics of Recursion: Outline Introduction to Recursion How Recursion Works Recursion versus Iteration Recursive Methods That.
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.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
CSIS 123A Lecture 9 Recursion Glenn Stevenson CSIS 113A MSJC.
Chapter 8 With Question/Answer Animations. Chapter Summary Applications of Recurrence Relations Solving Linear Recurrence Relations Homogeneous Recurrence.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
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.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
Agenda Lecture Content:  Recurrence Relations  Solving Recurrence Relations  Iteration  Linear homogenous recurrence relation of order k with constant.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Math Review 1.
Chapter 111 Recursion Chapter Objectives become familiar with the idea of recursion learn to use recursion as a programming tool become familiar.
1 Chapter 8 Recursion. 2 Recursive Function Call a recursion function is a function that either directly or indirectly makes a call to itself. but we.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
12-CRS-0106 REVISED 8 FEB 2013 KUG1C3 Dasar Algoritma dan Pemrograman.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Recursion Great fleas have little fleas upon their backs to bite 'em, And little fleas have lesser fleas, and so ad infinitum. And the great fleas themselves,
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Recursion Version 1.0.
Recursion CENG 707.
Chapter 15 Recursion.
Dynamic Programming Sequence of decisions. Problem state.
Recursion notes Chapter 8.
Chapter 4 Divide-and-Conquer
Divide and Conquer divide and conquer algorithms typically recursively divide a problem into several smaller sub-problems until the sub-problems are.
Binary and Ternary Search
Recursion Great fleas have little fleas upon their backs to bite 'em, And little fleas have lesser fleas, and so ad infinitum. And the great fleas themselves,
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Applied Algorithms (Lecture 17) Recursion Fall-23
Root finding.
Recursion Great fleas have little fleas upon their backs to bite 'em, And little fleas have lesser fleas, and so ad infinitum. And the great fleas themselves,
Recursion Great fleas have little fleas upon their backs to bite 'em, And little fleas have lesser fleas, and so ad infinitum. And the great fleas themselves,
Recursion Chapter 11.
Announcement The fourth test will be 75 minutes, will consist of two parts and will take place next week. The programming part will be about Chapter 2-6,
Chapter 13 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Recursion: The Mirrors
Presentation transcript:

A Different Solution  alternatively we can use the following algorithm: 1. if n == 0 done, otherwise I. print the string once II. print the string (n – 1) more times 1

Recursion  a method that calls itself is called a recursive method  a recursive method solves a problem by repeatedly reducing the problem so that a base case can be reached printIt("*", 5) *printIt("*", 4) **printIt("*", 3) ***printIt("*", 2) ****printIt("*", 1) *****printIt("*", 0) base case ***** 2 Notice that the number of times the string is printed decreases after each recursive call to printIt Notice that the base case is eventually reached.

Infinite Recursion  if the base case(s) is missing, or never reached, a recursive method will run forever (or until the computer runs out of resources) printIt("*", 1) * printIt("*", 0) ** printIt("*", -1) *** printIt("*", -2)

Rabbits 4 Month 0: 1 pair 0 additional pairs Month 1: first pair makes another pair 1 additional pair Month 2: each pair makes another pair; oldest pair dies 1 additional pair Month 3: each pair makes another pair; oldest pair dies 2 additional pairs

Fibonacci Numbers  the sequence of additional pairs  0, 1, 1, 2, 3, 5, 8, 13,... are called Fibonacci numbers  base cases  F(0) = 0  F(1) = 1  recursive definition  F(n) = F(n – 1) + F(n – 2) 5

Recursive Methods & Return Values  a recursive method can return a value  example: compute the nth Fibonacci number 6

Recursive Methods & Return Values  example: write a recursive method countZeros that counts the number of zeros in an integer number n  L has 8 zeros  algorithm: if the last digit in n is a zero return 1 + countZeros(n / 10) else return countZeros(n / 10) 7

 don't forget to establish the base case(s)  when should the recursion stop? when you reach a single digit (not zero digits; you never reach zero digits!)  base case #1 : n == 0  return 1  base case #2 : n != 0 && n < 10  return 0 8

9

countZeros Call Stack callZeros( L ) 10 callZeros( L ) callZeros( 80041L ) callZeros( 8004L ) callZeros( 800L ) callZeros( 80L ) callZeros( 8L ) last infirst out

Fibonacci Call Tree 11 F(5) F(4) F(3) F(2) F(1) 1 F(0) 0 F(1) 1 F(2) F(1) 1 F(0) 0 F(3) F(2) F(1) 1 F(0) 0 F(1) 1

Compute Powers of 10  write a recursive method that computes 10 n for any integer value n  recall:  10 0 = 1  10 n = 10 * 10 n-1  10 -n = 1 / 10 n 12

13

Proving Correctness and Termination  to show that a recursive method accomplishes its goal you must prove: 1. that the base case(s) and the recursive calls are correct 2. that the method terminates 14

Proving Correctness  to prove correctness: 1. prove that each base case is correct 2. assume that the recursive invocation is correct and then prove that each recursive case is correct 15

Correctness of printIt 1. (prove the base case) If n == 0 nothing is printed; thus the base case is correct. 2. Assume that printIt(s, n-1) prints the string s (n – 1) times. Then the recursive case prints the string s (n – 1)+1 = n times; thus the recursive case is correct. 16

Proving Termination  to prove that a recursive method terminates: 1. define the size of a method invocation; the size must be a non-negative integer number 2. prove that each recursive invocation has a smaller size than the original invocation 17

Termination of printIt 1. printIt(s, n) prints n copies of the string s ; define the size of printIt(s, n) to be n 2. The size of the recursive invocation printIt(s, n-1) is n-1 (by definition) which is smaller than the original size n. 18

countZeros 19

Correctness of countZeros 1. (base cases) If the number has only one digit then the method returns 1 if the digit is zero and 0 if the digit is not zero; therefore, the base case is correct. 2. (recursive cases) Assume that countZeros(n/10L) is correct (it returns the number of zeros in the first (d – 1) digits of n ). If the last digit in the number is zero, then the recursive case returns 1 + the number of zeros in the first (d – 1) digits of n, otherwise it returns the number of zeros in the first (d – 1) digits of n ; therefore, the recursive cases are correct. 20

Termination of countZeros 1. Let the size of countZeros(n) be d the number of digits in the number n. 2. The size of the recursive invocation countZeros(n/10L) is d-1, which is smaller than the size of the original invocation. 21

Decrease and Conquer  a common strategy for solving computational problems  solves a problem by taking the original problem and converting it to one smaller version of the same problem  note the similarity to recursion  decrease and conquer, and the closely related divide and conquer method, are widely used in computer science  allow you to solve certain complex problems easily  help to discover efficient algorithms 22

Root Finding  suppose you have a mathematical function f(x) and you want to find x 0 such that f(x 0 ) = 0  why would you want to do this?  many problems in computer science, science, and engineering reduce to optimization problems  find the shape of an automobile that minimizes aerodynamic drag  find an image that is similar to another image (minimize the difference between the images)  find the sales price of an item that maximizes profit  if you can write the optimization criteria as a function g(x) then its derivative f(x) = dg/dx = 0 at the minimum or maximum of g (as long as g has certain properties) 23

Bisection Method  suppose you can evaluate f(x) at two points x = a and x = b such that  f(a) > 0  f(b) < 0 24 x f(x) f(a) f(b) 'plus' 'minus'

Bisection Method  evaluate f(c) where c is halfway between a and b  if f(c) is close enough to zero done 25 x f(x) f(a) f(b) f(c) 'plus' 'minus'

Bisection Method  otherwise c becomes the new end point (in this case, 'minus' ) and recursively search the range 'plus' – 'minus' 26 x f(x) f(a) f(b) 'plus' 'minus' f(c)

27

Divide and Conquer  bisection works by recursively finding which half of the range 'plus' – 'minus' the root lies in  each recursive call solves the same problem (tries to find the root of the function by guessing at the midpoint of the range)  each recursive call solves one smaller problem because half of the range is discarded  bisection method is decrease and conquer  divide and conquer algorithms typically recursively divide a problem into several smaller sub-problems until the sub-problems are small enough that they can be solved directly 28