C++ Programming: Program Design Including Data Structures, Third Edition Chapter 16: Recursion.

Slides:



Advertisements
Similar presentations
Basics of Recursion Programming with Recursion
Advertisements

C++ Programming:. Program Design Including
Recursion. Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn about recursive algorithms Lecture.
16/23/2015 9:48 AM6/23/2015 9:48 AM6/23/2015 9:48 AMRecursion Recursion Recursion is when a function calls itself to implement an algorithm. Really a paradigm.
Data Structures Using C++1 Chapter 6 Recursion. Data Structures Using C++2 Chapter Objectives Learn about recursive definitions Explore the base case.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 16: Recursion.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
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.
Prof. S.M. Lee Department of Computer Science. Answer:
Data Structures Using C++ 2E Chapter 6 Recursion.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Data Structures Using C++ 2E Chapter 6 Recursion.
Data Structures Using C++1 Chapter 6 Recursion. Data Structures Using C++2 Chapter Objectives Learn about recursive definitions Explore the base case.
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.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
Prof. S.M. Lee Department of Computer Science.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 15: Recursion Starting Out with Java: From Control Structures.
RECURSION.
Chapter 14: Recursion J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second Edition.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 12 Recursion.
1 CS 132 Spring 2008 Chapter 6 Recursion Read p Skip example 6-3 (Fibonacci), 6-4 (Hanoi) Read example 6-5 (p. 387)
Functions and an Introduction to Recursion.  Recursive function ◦ A function that calls itself, either directly, or indirectly (through another function)
Chapter 12 Recursion, Complexity, and Searching and Sorting
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 17: Recursion.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Advanced Computer Architecture and Parallel Processing Rabie A. Ramadan http:
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 6 Recursion.
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 13 Recursion.
Data Structures Using Java1 Chapter 5 Recursion. Data Structures Using Java2 Chapter Objectives Learn about recursive definitions Explore the base case.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 6 Questions Quick Quiz
Recursion. 2 Overview  Learn about recursive definitions  Explore the base case and the general case of a recursive definition  Discover recursive.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 16: Recursion.
COP INTERMEDIATE JAVA Recursion. The term “recursion” refers to the fact that the same computation recurs, or occurs repeatedly, as a problem is.
Chapter 15: Recursion. Recursive Definitions Recursion: solving a problem by reducing it to smaller versions of itself – Provides a powerful way to solve.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
Maitrayee Mukerji. Factorial For any positive integer n, its factorial is n! is: n! = 1 * 2 * 3 * 4* ….* (n-1) * n 0! = 1 1 ! = 1 2! = 1 * 2 = 2 5! =
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Program Development and Design Using C++, Third Edition
Recursion Chapter 10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Recursion CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Chapter Topics Chapter 16 discusses the following main topics:
Topic 6 Recursion.
Chapter 15 Recursion.
Recursion DRILL: Please take out your notes on Recursion
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Chapter 15 Recursion.
Java Programming: Program Design Including Data Structures
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Recursion Chapter 11.
Recursion Data Structures.
Recursion Chapter 18.
Recursion Chapter 11.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 17: Recursion.
Java Programming: Chapter 9: Recursion Second Edition
Recursion Chapter 11.
Dr. Sampath Jayarathna Cal Poly Pomona
Recursion.
Presentation transcript:

C++ Programming: Program Design Including Data Structures, Third Edition Chapter 16: Recursion

Let us determine how long it would take to move all 64 disks from needle 1 to needle 3. If needle 1 contains 3 disks, then the number of moves required to move all 3 disks from needle 1 to needle 3 is 2 3  1 = 7. If needle 1 contains 64 disks, then the number of moves required to move all 64 disks from needle 1 to needle 3 is 2 64  1. Because 2 10 = 1024 ≈ 1000 = 10 3, we have 2 64 = 2 4 × 2 60 ≈ 2 4 × = 1.6 × The number of seconds in one year is approximately 3.2 × Suppose the priests move one disk per second and they do not rest. Now: 1.6 × = 5 × 3.2 × = 5 × (3.2 × 10 7 ) × = (3.2 × 10 7 ) × (5 × ) The time required to move all 64 disks from needle 1 to needle 3 is roughly 5 × years.

It is estimated that our universe is about 15 billion years old (1.5 × ). Also, 5 × = 50 × ≈ 33 × (1.5 × ). This calculation shows that our universe would last about 33 times as long as it already has. Assume that a computer can generate 1 billion (10 9 ) moves per second. Then the number of moves that the computer can generate in one year is: (3.2 × 10 7 ) × 10 9 = 3.2 × So the computer time required to generate 2 64 moves is: 2 64 ≈ 1.6 × = 1.6 × × 10 3 = (3.2 × ) × 500 Thus, it would take about 500 years for the computer to generate 2 64 moves at the rate of 1 billion moves per second.

Recursion or Iteration? There are usually two ways to solve a particular problem: −Iteration (looping) −Recursion Which method is better—iteration or recursion? In addition to the nature of the problem, the other key factor in determining the best solution method is efficiency

Memory allocation Whenever a function is called −Memory space for its formal parameters and (automatic) local variables is allocated When the function terminates −That memory space is then deallocated Every (recursive) call has its own set of parameters and (automatic) local variables

Efficiency Overhead associated with executing a (recursive) function in terms of −Memory space −Computer time A recursive function executes more slowly than its iterative counterpart

Efficiency (continued) On slower computers, especially those with limited memory space −The slow execution of a recursive function would be visible Today’s computers are fast and have inexpensive memory −Execution of a recursion function is not noticeable

Efficiency (continued) The choice between the two alternatives depends on the nature of the problem For problems such as mission control systems −Efficiency is absolutely critical and dictates the solution method

Efficiency (continued) An iterative solution is more obvious and easier to understand than a recursive solution If the definition of a problem is inherently recursive −Consider a recursive solution

Programming Example Use recursion to convert a non-negative integer in decimal format (base 10) into the equivalent binary number (base 2) Define some terms : −Let x be an integer −The remainder of x after division by 2 is the rightmost bit of x −The rightmost bit of 33 is 1 because 33 % 2 is 1 −The rightmost bit of 28 is 0 because 28 % 2 is 0

Programming Example (continued) To find the binary representation of 35 −Divide 35 by 2 −The quotient is 17 and the remainder is 1 −Divide 17 by 2 −The quotient is 8 and the remainder is 1 −Divide 8 by 2 −The quotient is 4 and the remainder is 0 −Continue this process until the quotient becomes 0

Programming Example (continued) The rightmost bit of 35 cannot be printed until we have printed the rightmost bit of 17 The rightmost bit of 17 cannot be printed until we have printed the rightmost bit of 8, and so on

Programming Example (continued) The binary representation of 35 is the binary representation of 17 (the quotient of 35 after division by 2) followed by the rightmost bit of 35

Summary The process of solving a problem by reducing it to smaller versions of itself is called recursion A recursive definition defines a problem in terms of smaller versions of itself Every recursive definition has one or more base cases A recursive algorithm solves a problem by reducing it to smaller versions of itself Every recursive algorithm has one or more base cases

Summary (continued) The solution to the problem in a base case is obtained directly A function is called recursive if it calls itself Recursive algorithms are implemented using recursive functions Every recursive function must have one or more base cases The general solution breaks the problem into smaller versions of itself

Summary (continued) The general case must eventually be reduced to a base case The base case stops the recursion Directly recursive: a function calls itself Indirectly recursive: A function calls another function and eventually calls the original Tail recursive: the last statement executed is the recursive call