More Complicated Recursion CMPSC 122

Similar documents
Foundations, Reasoning About Algorithms, and Design By Contract CMPSC 122

Introduction to Asymptotic Running Time Analysis CMPSC 122

6.001 Notes: Section 4.1

Recursion. So, just as you are allowed to call function B from within function A, you are ALSO allowed to call function A from within function A!

Recursion CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2011

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Week 02 Module 06 Lecture - 14 Merge Sort: Analysis

Recursion. Fundamentals of Computer Science

APCS-AB: Java. Recursion in Java December 12, week14 1

Recursion. Overview. Mathematical induction. Hello recursion. Recursion. Example applications. Goal: Compute factorial N! = 1 * 2 * 3...

Admin. How's the project coming? After these slides, read chapter 13 in your book. Quizzes will return

n = 1 What problems are interesting when n is just 1?

We cover recursion in 150. Why do it again in 151?

Week - 03 Lecture - 18 Recursion. For the last lecture of this week, we will look at recursive functions. (Refer Slide Time: 00:05)

Lesson 29: Fourier Series and Recurrence Relations

Recursion CS GMU

Mathematical Induction

Recursive Definitions

CS 3410 Ch 7 Recursion

Recursion Chapter 3.5

CS201 Discussion 7 MARKOV AND RECURSION

(Refer Slide Time: 00:51)

CS159. Nathan Sprague. November 9, 2015

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Module 02 Lecture - 45 Memoization

STUDENT LESSON A9 Recursion

Two Approaches to Algorithms An Example (1) Iteration (2) Recursion

Reasoning and writing about algorithms: some tips

Recursion defining an object (or function, algorithm, etc.) in terms of itself. Recursion can be used to define sequences

Algorithmics. Some information. Programming details: Ruby desuka?

Recursion. Chapter 7. Copyright 2012 by Pearson Education, Inc. All rights reserved

Figure 4.1: The evolution of a rooted tree.

Slide 1 CS 170 Java Programming 1 Multidimensional Arrays Duration: 00:00:39 Advance mode: Auto

CSCI-1200 Data Structures Fall 2017 Lecture 7 Order Notation & Basic Recursion

Module 05: Types of recursion

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

Recursion. COMS W1007 Introduction to Computer Science. Christopher Conway 26 June 2003

MITOCW watch?v=4dj1oguwtem

Subset sum problem and dynamic programming

CSCI 136 Data Structures & Advanced Programming. Lecture 9 Fall 2018 Instructors: Bills

Name: Lirong TAN 1. (15 pts) (a) Define what is a shortest s-t path in a weighted, connected graph G.

! Addition! Multiplication! Bigger Example - RSA cryptography

For 100% Result Oriented IGNOU Coaching and Project Training Call CPD: ,

The divide and conquer strategy has three basic parts. For a given problem of size n,

OVERVIEW. Recursion is an algorithmic technique where a function calls itself directly or indirectly. Why learn recursion?

Recursion & Performance. Recursion. Recursion. Recursion. Where Recursion Shines. Breaking a Problem Down

We'll dive right in with an example linked list. Our list will hold the values 1, 2, and 3.!

1. Find f(1), f(2), f(3), and f(4) if f(n) is defined recursively by f(0) = 1 and for n = 0, 1, 2,

CS 151. Recursion (Review!) Wednesday, September 19, 12

Decision-Making and Repetition

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

Identify recursive algorithms Write simple recursive algorithms Understand recursive function calling

recursive algorithms 1

Test Bank Ver. 5.0: Data Abstraction and Problem Solving with C++: Walls and Mirrors, 5 th edition, Frank M. Carrano

I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub


introduction to Programming in C Department of Computer Science and Engineering Lecture No. #40 Recursion Linear Recursion

Intro. Speed V Growth

MITOCW ocw f99-lec07_300k

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Lecture 10: Recursive Functions. Computer System and programming in C 1

Analysis of Algorithms & Big-O. CS16: Introduction to Algorithms & Data Structures Spring 2018

6.001 Notes: Section 8.1

Recursion. Comp Sci 1575 Data Structures. Introduction. Simple examples. The call stack. Types of recursion. Recursive programming

15 212: Principles of Programming. Some Notes on Induction

Jalil Boudjadar, Tommy Färnqvist. IDA, Linköping University

1 Recursion. 2 Recursive Algorithms. 2.1 Example: The Dictionary Search Problem. CSci 235 Software Design and Analysis II Introduction to Recursion

Algorithm Design and Recursion. Search and Sort Algorithms

Problem One: A Quick Algebra Review

Slide 1 Side Effects Duration: 00:00:53 Advance mode: Auto

Unit #2: Recursion, Induction, and Loop Invariants

Formal Methods of Software Design, Eric Hehner, segment 24 page 1 out of 5

Lecture 6: Arithmetic and Threshold Circuits

CSE200 Lecture 6: RECURSION

So on the survey, someone mentioned they wanted to work on heaps, and someone else mentioned they wanted to work on balanced binary search trees.

Computer Science 210 Data Structures Siena College Fall Topic Notes: Complexity and Asymptotic Analysis

COMP 250 Fall Recursive algorithms 1 Oct. 2, 2017

Lecture 4 CS781 February 3, 2011

P1 Engineering Computation

RECURSION, RECURSION, (TREE) RECURSION! 2

Parallel prefix scan

Halting Measures and Termination Arguments

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute

Greedy Algorithms CHAPTER 16

n! = 1 * 2 * 3 * 4 * * (n-1) * n

Lecture 3.4: Recursive Algorithms

q To develop recursive methods for recursive mathematical functions ( ).

q To develop recursive methods for recursive mathematical functions ( ).

CS 135 Lab Assignments Week 1

Recursion defining an object (or function, algorithm, etc.) in terms of itself. Recursion can be used to define sequences

CIS 194: Homework 6. Due Wednesday, 4 March. Fibonacci numbers. It s all about being lazy.

CS 206 Introduction to Computer Science II

This session. Recursion. Planning the development. Software development. COM1022 Functional Programming and Reasoning

COMP4128 Programming Challenges

CS1 Lecture 22 Mar. 6, 2019

Binary Search to find item in sorted array

CS103L SPRING 2017 UNIT 8: RECURSION

CS103 Handout 13 Fall 2012 May 4, 2012 Problem Set 5

Chapter Fourteen Bonus Lessons: Algorithms and Efficiency

We will give examples for each of the following commonly used algorithm design techniques:

Reading 8 : Recursion

ECE 2400 Computer Systems Programming Fall 2018 Topic 2: C Recursion

Transcription:

More Complicated Recursion CMPSC 122 Now that we've gotten a taste of recursion, we'll look at several more examples of recursion that are special in their own way. I. Example with More Involved Arithmetic Consider this function, with precondition n > 0: FOO(n) if n = 1 return 0 else return 1 + FOO(n/4) This is an example to show that we don't always subtract something from n in recursive calls. But, remember the motto that we want to get closer to base cases, so division will accomplish that. Let's trace the execution of this function for n = 4, n = 16, and n = 256. What does it compute? II. Example with Multiple Base Cases Recursion allows us to have multiple ways of defining both the base and recursive cases. More often than multiple recursive cases, we have multiple base cases. Consider the following sequence definition: a1 = 5 a2 = 20 a3 = 21 an = 10an-1 for n > 3 Problem: Write recursive code to compute the nth term of this sequence. Trace its execution for inputs of 5 and 2. Page 1 of 7! Prepared by D. Hogan for PSU CMPSC 122

III. Examples where Recursive Case Spawns Multiple Calls A classic example of a recursively-defined sequence is the Fibonacci sequence: f0 = 0 f1 = 1 fk = fk-1 + fk-2 for k > 2 Let's begin by computing the k = 5 term from the recursive definition: Now, let's see how this translates to a code solution: It turns out recursive algorithms that spawn multiple calls are very important in computer science. Often, we wish to solve a problem by breaking it down into smaller instances of the same problem, solving those, and combining the results together for an overall result. This strategy is known as divide-and-conquer algorithm design. Example: Suppose we have an array whose size is a power of 2. To sum that array, let's divide it in half, and sum each half independently. In summing the halves, we'll employ the same strategy. First, let's visualize an array A[p..q] that we will break apart: Page 2 of 7! Prepared by D. Hogan for PSU CMPSC 122

Now let's write a recursive definition for the sum of an array A[p..q], where p and q are nonnegative integers such that : Base: Recursion: Now, let's translate that to code (as an in-class problem, you start this and I'll help): Homework Exercise 1: Trace the execution of this algorithm for the array (1, 2, 3, 4, 5, 6, 7, 8). Note: I'm not advocating you sum an array this way. This solution turns out to be equally efficient as the solution you'd naturally think to do, so it only overcomplicates. But it serves as a nice example of recursion that leads to something deeper we'll do later too. Page 3 of 7! Prepared by D. Hogan for PSU CMPSC 122

IV. Example with Multiple Parameters Just like any ordinary function can have multiple parameters, so can a recursive function. Sometimes, recursive functions bring multiple parameters "along for the ride" and only one parameter drives the recursion, such as in our first example: Example: Consider this function (whose precondition is that n is nonnegative and a is initialized and which doesn't do anything of practical significance): RECDEMO(n, a) if n = 0 // base case return a if n > 0 and n mod 2 = 0 return RECDEMO(n-2, a) + 3 if n > 0 and n mod 2 = 1 return RECDEMO(n-1, a) + 1 // recursive case for even n // recursive case for odd n Trace the action of the call RECDEMO(5, 10). Homework Exercise 2: Trace the action of the call RECDEMO(6, 1). Sometimes, multiple parameters mean recursion in multiple dimensions. Applications include problems with two-way tables that arise in optimization problems, problems involving matrix processing, and problems in processing images (after all, images are really matrices that store mathematical representations of color or grayscale). Example: Consider this recursive sequence definition: A[0, 0] = 2 A[row, 0] = A[row-1, 0] + 2 row for row 1 A[0, col] = A[0, col-1] + 4 for col 1 A[row 1, col]+ A[row, col 1] A[row, col] = A[row 1, col 1]+ for row 1 and col 1 2 Using this definition, fill in a table A[0..3, 0..2] Page 4 of 7! Prepared by D. Hogan for PSU CMPSC 122

Problem: Suppose we wanted to compute A[50, 75]. What terms would we need directly? What all recursive calls would be made? Homework Exercise 3: Write pseudocode for a recursive function that does the same thing as the table-filling example we just saw. Assume A is passed by reference and inputs are a row and column index. Homework Exercise 4: What recursive call would you need to make to your function to fill a table? V. Lab and Homework First, on paper, solve the homework exercises that have been scattered through the notes so far. You should find 4 of them. Next, solve the following exercise on paper: Homework Exercise 5: Suppose you are building a program which draws several stars on the console. On the first three lines, there should be 1, 2, and 3 stars, respectively. On all subsequent lines, the number of stars should either be twice the number of stars 2/3 of the way up the screen, for lines numbers that are exact multiples of three, or otherwise 2 more than the preceding line. Write a recursive definition for the number of stars on each line. Then, as a lab exercise, implement the recursive function from Homework Exercise 5, add a method to draw any number of stars, and call it enough times print stars on 54 lines. Prepare a report with the code and a sample run and call this Lab 11. The first few lines should look like this: * ** *** ***** ******* Page 5 of 7! Prepared by D. Hogan for PSU CMPSC 122

VI. Recursion vs. Iteration and Further Directions First, recall the classic factorial example. You could certainly write code that computes factorials via a loop: FACT(n) result = 1 for i = 1 to n result = result i return result And, we could visualize its execution in a different way than call stacks: Recall the Fibonacci numbers problem we looked at earlier. We wrote a nice recursive function for Fibonacci numbers, but it turns out we could also have written a function that uses a loop. As the process of using loops is sometimes called iteration, a solution based on loops is called an iterative solution. Consider this pseudocode for an iterative solution, which works for any nonnegative integer n: FIB(n) allocate array F[0..n] F[0] = 0 F[1] = 1 for i = 2 to n F[i] = F[i-1] + F[i-2] Page 6 of 7! Prepared by D. Hogan for PSU CMPSC 122

Let's now try to visualize the execution of the recursive function: What do you observe? We'll study the running times of iterative algorithms in more detail in this course, as well as learn more about representing trees (which are inherently recursive) as well. We'll also use recursion in deriving a few sorting algorithms that turn out to be more efficient than the ones you know already. More on recursion comes in the courses to follow. In 360, we'll look at recursion in much greater detail, in particular, deriving closed forms of recursively-defined ideas and proving things about them via recursion's cousin induction. We'll continue that in 465, also looking at many more algorithms that are naturally recursive, as well as studying recursion vs. iteration in more depth via an algorithm design technique called dynamic programming. This is just the theory side of things where we study recursion itself; you'll see applications of recursion in most every remaining course in the curriculum. VII. Another Lab Problem Recall the binary search algorithm from your CS1 course or look it up in the review materials. There, you looked at iterative solution to binary search, but it can also very naturally be implemented recursively. Your task is to do so. Begin by building pseudocode on paper for a recursive version of binary search. Also, illustrate on paper the action of the recursive binary search for two significantly different kinds of executions. Then, turn your pseudocode into code and test it for the inputs you illustrated. Turn in both a lab report and your work on paper. Staple your lab report on top of your handwritten work. Page 7 of 7! Prepared by D. Hogan for PSU CMPSC 122