Recursion: The Mirrors Chapter 2 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.

Slides:



Advertisements
Similar presentations
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Advertisements

CSC 205 Programming II Lecture 10 Towers of Hanoi.
Recursion Ellen Walker CPSC 201 Data Structures Hiram College.
COMPSCI 105 S Principles of Computer Science Recursion 1.
Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley CHAPTER 2 Recursion:
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 Chapter 8. 2 Chapter Contents What Is Recursion? Tracing a Recursive Method Recursive Methods That Return a Value Recursively Processing an.
CS102 Algorithms and Programming II1 Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. A recursive.
© 2006 Pearson Addison-Wesley. All rights reserved3-1 Chapter 3 Recursion: The Mirrors CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
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.
Ch. 3: Recursion. Recursive Solutions Recursion –An extremely powerful problem-solving technique –Breaks a problem into smaller identical problems –An.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Recursion. Recursive Solutions Recursion breaks a problem into smaller identical problems – mirror images so to speak. By continuing to do this, eventually.
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.
© 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.
COMPSCI 105 S Principles of Computer Science Recursion 3.
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-
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Chapter 14: Recursion Starting Out with C++ Early Objects
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.
Chapter 2 Recursion: The Mirrors CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
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.
© 2006 Pearson Addison-Wesley. All rights reserved 3-1 Chapter 3 Recursion: The Mirrors.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.
1 Storage Classes, Scope, and Recursion Lecture 6.
Chapter 3 Recursion: The Mirrors. © 2004 Pearson Addison-Wesley. All rights reserved 3-2 Recursive Solutions Recursion –An extremely powerful problem-solving.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
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.
Data Structures and Abstractions with Java, 4e Frank Carrano
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
COSC 2006 Data Structures I Recursion II
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 2: Recursion: The Mirrors.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 2: Recursion: The Mirrors Data Abstraction & Problem Solving.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Recursion as a Problem- Solving Technique Chapter 5 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 6 Recursion.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive Algorithms.
CMPT 225 Recursion. Objectives Understand how the Fibonacci series is generated Recursive Algorithms  Write simple recursive algorithms  Analyze simple.
Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Recursion as a Problem- Solving Technique Chapter 5 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Fibonacci Your homework was to find out about this man, so lets fill in this together.
7. RECURSIONS Rocky K. C. Chang October 12, 2015.
Ramamurthy Recursion: The Mirrors B. Ramamurthy CS114A,B.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Recursion Chapter 10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Recursion Chapter 10 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Recursion CENG 707.
Recursion CENG 707.
Recursion: The Mirrors
Recursion: The Mirrors
Recursion: The Mirrors
Recursion: The Mirrors
Recursion Chapter 11.
Recursion: The Mirrors
Recursion: The Mirrors
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Recursion: The Mirrors
Presentation transcript:

Recursion: The Mirrors Chapter 2 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Contents Recursive Solutions Recursion That Returns a Value Recursion That Performs an Action Recursion with Arrays Organizing Data More Examples Recursion and Efficiency Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Recursive Solutions Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012 Recursion breaks a problem into smaller identical problems Some recursive solutions are inefficient, impractical Complex problems can have simple recursive solutions

Recursive Solutions FIGURE 2-1 A recursive solution Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Recursive Solutions Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012 A recursive solution calls itself Each recursive call solves an identical, smaller problem Test for base case enables recursive calls to stop Eventually one of smaller calls will be base case

A Recursive Valued Function Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012 The factorial of n

A Recursive Valued Function FIGURE 2-2 fact(3) Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

The Box Trace FIGURE 2-3 A box Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012 FIGURE 2-4 The beginning of the box trace

The Box Trace FIGURE 2-5 Box trace of fact(3) Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

The Box Trace FIGURE 2-5 Box trace of fact(3) … continued Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

The Box Trace FIGURE 2-5 Box trace of fact(3) … continued Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

A Recursive Void Function FIGURE 2-6 A recursive solution Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

A Recursive Void Function Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012 The function writeBackward

A Recursive Void Function FIGURE 2-7 Box trace of writeBackward("cat") Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

A Recursive Void Function FIGURE 2-7 writeBackward("cat") continued Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

A Recursive Void Function FIGURE 2-7 writeBackward("cat") continued Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

A Recursive Void Function FIGURE 2-8 Box trace of writeBackward("cat") in pseudocode Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

A Recursive Void Function FIGURE 2-8 Box trace of writeBackward("cat") in pseudocode (continued) Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

A Recursive Void Function FIGURE 2-8 Box trace of writeBackward("cat") in pseudocode (continued) Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

A Recursive Void Function FIGURE 2-8 Box trace of writeBackward("cat") in pseudocode (continued) Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

A Recursive Void Function FIGURE 2-8 Box trace of writeBackward("cat") in pseudocode (continued) Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

A Recursive Void Function FIGURE 2-8 Box trace of writeBackward("cat") in pseudocode (concluded) Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

A Recursive Void Function Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012 The function writeBackward2

A Recursive Void Function FIGURE 2-9 Box trace of writeBackward2("cat") in pseudocode Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

A Recursive Void Function FIGURE 2-9 Box trace of writeBackward2("cat") in pseudocode (continued) Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

A Recursive Void Function FIGURE 2-9 Box trace of writeBackward2("cat") in pseudocode (continued) Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

A Recursive Void Function FIGURE 2-9 Box trace of writeBackward2("cat") in pseudocode (continued) Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

A Recursive Void Function FIGURE 2-9 Box trace of writeBackward2("cat") in pseudocode (concluded) Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Writing an Array’s Entries in Backward Order Pseudocode Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Writing an Array’s Entries in Backward Order Source code Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

The Binary Search A high-level binary search for the array problem Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

The Binary Search Issues to consider 1.How to pass a half array to recursive call 2.How to determine which half of array has target value 3.What is the base case? 4.How will result of binary search be indicated? Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

The Binary Search FIGURE 2-10 Box traces of binarySearch with anArray = : (a) a successful search for 9 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

The Binary Search FIGURE 2-10 Box traces of binarySearch with anArray = : (b) an unsuccessful search for 6 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

The Binary Search FIGURE 2-11 Box trace with a reference argument Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Finding the Largest Value in an Array Recursive algorithm Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Finding the Largest Value in an Array FIGURE 2-12 Recursive solution to the largest-value problem Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Finding the Largest Value in an Array FIGURE 2-13 The recursive calls that maxArray( ) generates Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Finding the k th Smallest Value of an Array FIGURE 2-14 A sample array Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012 The recursive solution proceeds by: 1.Selecting a pivot value in array 2.Cleverly arranging/partitioning, values in array about this pivot value 3.Recursively applying strategy to one of partitions

Finding the k th Smallest Value of an Array FIGURE 2-15 A partition about a pivot Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Finding the k th Smallest Value of an Array High level pseudocode solution: Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Organizing Data Towers of Hanoi FIGURE 2-16 (a) The initial state; (b) move n – 1 disks from A to C; Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Towers of Hanoi FIGURE 2-16 (c) move 1 disk from A to B; (d) move n – 1 disks from C to B Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Towers of Hanoi Pseudocode solution: Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Towers of Hanoi FIGURE 2-17 The order of recursive calls that results from solve Towers(3, A, B, C) Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Towers of Hanoi Source code for solveTowers Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

The Fibonacci Sequence (Multiplying Rabbits) Assumed “facts” about rabbits: Rabbits never die. A rabbit reaches sexual maturity exactly two months after birth Rabbits always born in male-female pairs. At the beginning of every month, each sexually mature male-female pair gives birth to exactly one male-female pair. Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

The Fibonacci Sequence (Multiplying Rabbits) Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012 MonthRabbit Population 1One pair 2One pair, still 3Two pairs 4Three pairs 5Five pairs 68 pairs

The Fibonacci Sequence (Multiplying Rabbits) FIGURE 2-18 Recursive solution to the rabbit problem Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

The Fibonacci Sequence (Multiplying Rabbits) A C++ function to compute rabbit(n) Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

The Fibonacci Sequence (Multiplying Rabbits) FIGURE 2-19 The recursive calls that rabbit(7) generates Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Choosing k Out of n Things Recursive solution: Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Choosing k Out of n Things Recursive function: Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Choosing k Out of n Things FIGURE 2-20 The recursive calls that g (4, 2) generates Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

Recursion and Efficiency Inefficiency factors  Overhead associated with function calls  Inherent inefficiency of some recursive algorithms Principle:  Do not use recursive solution if inefficient and clear, efficient iterative solution exists Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012

End Chapter 2 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012