COSC 2006 Data Structures I Recursion II

Slides:



Advertisements
Similar presentations
CS 1031 Recursion (With applications to Searching and Sorting) Definition of a Recursion Simple Examples of Recursion Conditions for Recursion to Work.
Advertisements

Recursion Ellen Walker CPSC 201 Data Structures Hiram College.
COMPSCI 105 S Principles of Computer Science Recursion 1.
Recursive Functions The Fibonacci function shown previously is recursive, that is, it calls itself Each call to a recursive method results in a separate.
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.
Recursion. Binary search example postponed to end of lecture.
© 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.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
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.
Chapter 19 Recursion.
Recursion.
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.
1 Chapter 7 Recursion. 2 What Is Recursion? l Recursive call A method call in which the method being called is the same as the one making the call l Direct.
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.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.
Recursion: The Mirrors Chapter 2 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Chapter 2 Recursion: The Mirrors CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
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.
Chapter 3 Recursion: The Mirrors. © 2004 Pearson Addison-Wesley. All rights reserved 3-2 Recursive Solutions Recursion –An extremely powerful problem-solving.
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.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
Recursion Chapter 11. The Basics of Recursion: Outline Introduction to Recursion How Recursion Works Recursion versus Iteration Recursive Methods That.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 2: Recursion: The Mirrors.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
1 Chapter 13 Recursion. 2 Chapter 13 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing Recursive.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
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.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 2: Recursion: The Mirrors Data Abstraction & Problem Solving.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 9 Searching Arrays.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
Chapter 7 Programming with Recursion. What Is Recursion? Recursive call A method call in which the method being called is the same as the one.
CS212: DATASTRUCTURES Lecture 3: Searching 1. Lecture Contents  searching  Sequential search algorithm.  Binary search algorithm. 2.
Chapter 111 Recursion Chapter Objectives become familiar with the idea of recursion learn to use recursion as a programming tool become familiar.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Ramamurthy Recursion: The Mirrors B. Ramamurthy CS114A,B.
Recursion ITI 1121 N. El Kadri. Reminders about recursion In your 1 st CS course (or its equivalent), you have seen how to use recursion to solve numerical.
Recursion COMP x1 Sedgewick Chapter 5. Recursive Functions problems can sometimes be expressed in terms of a simpler instance of the same problem.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
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 Powerful Tool
Recursion.
CSC 205 Programming II Lecture 8 Recursion.
Recursion CENG 707.
Recursion Version 1.0.
Recursion CENG 707.
Chapter 15 Recursion.
Recursion: The Mirrors
Recursion: The Mirrors
Chapter 15 Recursion.
Chapter 14 Recursion. Chapter 14 Recursion Overview 14.1 Recursive Functions for Tasks 14.2 Recursive Functions for Values 14.3 Thinking Recursively.
Recursion: The Mirrors
Recursion: The Mirrors
Chapter 18 Recursion.
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Recursion: The Mirrors
Presentation transcript:

COSC 2006 Data Structures I Recursion II

Topics More Recursive Examples Writing Strings backward Binary Search

Recursive Functions Criteria 1. Function calls itself 2. Each call solves an simpler (smaller in size), but identical problem 3. Base case is handled differently from all other cases and enables recursive calls to stop 4. The size of the problem diminishes and ensures reaching the base case

What Should be Considered in Recursive Solutions? How to define the problem in terms of smaller problems of the same type? How will each recursive call diminish the size of the problem? What instance will serve as the base case? As the problem size diminishes, will it reach the base case?

Example: Writing Strings Backward Given: A string of characters Required: Write the string in reverse order Recursive Algorithm: Idea: Divide the string of length n into two parts; one of length 1 and the other of length n-1, Exchange both strings and perform the same operation on the n-1 length string; Stop when the length of the n-1 string becomes either 0 or 1 (base case)

Example: Writing Strings Backward A recursive solution: WriteBackward ( S ) WriteBackward ( S minus last character )

Example: Writing Strings Backward Algorithm: First Draft WriteBackward1 (in s: string) if (string is empty) Do nothing - - Base case else { write the last character of S writeBackward1( S minus its last character) }

Writing a String Backward void writeBackward1(string s, int size) // --------------------------------------------------- // Writes a character string backward. // Precondition: The string s contains size characters, where size>= 0 // Postcondition: s is written backward, but remains unchanged. { if (size > 0) // Enforcing the pre-condition { // write the last character System.out.print( s.substr (size-1, 1)); // write the rest of the string backward writeBackward1 (s, size-1); // Point A } // end if // size == 0 is the base case - do nothing } // end writeBackward

Example: Writing Strings Backward Algorithm box trace: Figure 2-7a: Box trace of writeBackward("cat", 3)

Example: Writing Strings Backward Algorithm box trace: Figure 2-7b: Box trace of writeBackward("cat", 3)

Example: Writing Strings Backward Algorithm box trace: Figure 2-7c: Box trace of writeBackward("cat", 3)

Example: Writing Strings Backward 2rd Option: (WriteBackward2) Attach first character to the end WriteBackward2 (in s: string) if (string is empty) Do nothing - - Base case else { writeBackward2( S minus its first character) System.out.print( “About to write last character of string: “+S); write the first character of S } System.out.println(“Leave WriteBackward with string: “+S );

Writing a String Backward Observations: The 1-length string can be chosen either as the first character from the n-length string last character from the n-length string Recursive calls to WriteBackward function use smaller values of Size WriteBackward1 writes a character just before generating a new box WriteBackward2 writes character after returning from recursive call

Example: Binary Search Assumptions: Array must be sorted Size = size of the array A[0]  A[1]  A[3]  . . .  A[Size-1] Idea: Divide the array into 3 parts One half from A [First] to A [Mid - 1] An element A [Mid] Another half from A [Mid + 1] to A [Last] Check if A[Mid] equals, less than, or greater than the value you are seeking

Example: Binary Search Pseudocode binarySearch(in A: ArrayType, in Value: ItemType) { if (A is of size 1) Determine if A’s only item = Value // Base-case else { Find the midpoint of A Determine which half of A contains Value if (Value in first half of A) binarySearch(first half of A, Value) binarySearch (second half of A, Value) }

Binary Search Details Split around a middle value array(mid) How do you pass half an array? first, last parameters How do you determine which half contains the value? Split around a middle value array(mid) What should the base case(s) be? Value found at mid Array empty How to indicate the result, including failure? Return index or negative number

Example: Binary Search Two base cases First > Last: Value not found in original array Search fails Return either a Boolean value or a negative index Value == A [Mid]: Value found Search succeeds Return the index corresponding to Value The array should be passed to the function by reference. It shouldn't be considered as part of the local environment.

Binary Search Code (abbreviated) binarySearch (int anArray[], int first, int last, int value) { int index; if (first > last) index = -1; else { int mid = (first + last)/2; if (value == anArray[mid]) index = mid; else if (value < anArray[mid]) index = binarySearch(anArray, first, mid-1, value); else index = binarySearch(anArray, mid+1, last, value); } return index;

Example: Binary Search Using Run-Time Stack to trace Box contents: Value First Last Mid The array is not considered a part of the box. It is passed by reference

Example: Binary Search Searching for 9 Searching for 6 Value = 9 First = 0 Last = 7 Mid =(0+7)/2=3 Value < A[3] Last = 2 Mid = (0+2)/2=1 Value < A[1] First = 2 Mid = (2+2)/2=2 Value = A[2] return 2 Value = 6 First = 0 Last = 7 Mid =(0+7)/2=3 Value < A[3] Last = 2 Mid =(0+2)/2=2 Value < A[2] First = 2 Mid =(2+2)/2=2 Last = 1 First > Last return -1

Review In a recursive method that writes a string of characters in reverse order, the base case is ______. a string with a length of 0 a string whose length is a negative number a string with a length of 3 a string that is a palindrome

Review Which of the following is a precondition for a method that accepts a number n and computes the nth Fibonacci number? n is a negative integer n is a positive integer n is greater than 1 n is an even integer

Review The midpoint of a sorted array can be found by ______, where first is the index of the first item in the array and last is the index of the last item in the array. first / 2 + last / 2 first / 2 – last / 2 (first + last) / 2 (first – last) / 2

Review If the value being searched for by a recursive binary search algorithm is in the array, which of the following is true? the algorithm cannot return a nonpositive number the algorithm cannot return a nonnegative number the algorithm cannot return a zero the algorithm cannot return a negative number

Review An array is a(n) ______. class method object variable

Review For anArray = <2, 3, 5, 6, 9, 13, 16, 19>, what is the value returned by a recursive binary search algorithm if the value being searched for is 10? –1 1 10

Review A recursive binary search algorithm always reduces the problem size by ______ at each recursive call. 1 2 half one-third