Recursion. Thinking Recursively. Tracing the Recursive Definition of List. CMPT 126: Lecture 10. Recursion

Similar documents
Recursion Chapter 17. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Recursive Thinking. Chapter 8: Recursion. Recursive Definitions. Recursion. Java Software Solutions for AP* Computer Science A 2nd Edition

Why use recursion? Some problems are more easily expressed in a recursive definition

Recursive Definitions

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


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

Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems. COMP Week 8 & 9 1

Sorting & Searching (and a Tower)

CMSC 132: Object-Oriented Programming II. Recursive Algorithms. Department of Computer Science University of Maryland, College Park

Recursion. Let s start by looking at some problems that are nicely solved using recursion. First, let s look at generating The Fibonacci series.

CSC 273 Data Structures

CmpSci 187: Programming with Data Structures Spring 2015

Recursion. Chapter 7

UNIT 5A Recursion: Basics. Recursion

Recursion. Chapter 5

Divide & Conquer. 2. Conquer the sub-problems by solving them recursively. 1. Divide the problem into number of sub-problems

DATA STRUCTURES AND ALGORITHMS

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

12/30/2013 S. NALINI,AP/CSE

8/5/10 TODAY'S OUTLINE. Recursion COMP 10 EXPLORING COMPUTER SCIENCE. Revisit search and sorting using recursion. Recursion WHAT DOES THIS CODE DO?

CMPSCI 187: Programming With Data Structures. Lecture #15: Thinking Recursively David Mix Barrington 10 October 2012

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

mith College Computer Science Week 13 CSC111 Spring 2018 Dominique Thiébaut

def F a c t o r i a l ( n ) : i f n == 1 : return 1 else : return n F a c t o r i a l ( n 1) def main ( ) : print ( F a c t o r i a l ( 4 ) )

KF5008 Algorithm Efficiency; Sorting and Searching Algorithms;

Introduction to Computers & Programming

Lesson 12: Recursion, Complexity, Searching and Sorting. Modifications By Mr. Dave Clausen Updated for Java 1_5

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

Announcements. Recursion and why study it. Recursive programming. Recursion basic idea

Data structure and algorithm in Python

Solving problems by recursion

C22a: Problem Solving using Recursion

CS200: Recursion and induction (recap from cs161)

Lecture 24 Tao Wang 1

Recursive Methods and Problem Solving. Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms

Data Abstraction & Problem Solving with C++: Walls and Mirrors 6th Edition Carrano, Henry Test Bank

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

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

Computer Science 252 Problem Solving with Java The College of Saint Rose Spring Topic Notes: Searching and Sorting

Sorting. Task Description. Selection Sort. Should we worry about speed?

2.3 Recursion 7/23/2015 3:06:35 PM

1.7 Recursion. Department of CSE

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

EE 368. Weeks 4 (Notes)

Sorting. CSE 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation. CSE143 Au

UNIT 5A Recursion: Basics. Recursion

CSE 2123 Recursion. Jeremy Morris

Chapter 10: Recursive Problem Solving

Announcement. Submit assignment 3 on CourSys Do not hand in hard copy Due Friday, 15:20:00. Caution: Assignment 4 will be due next Wednesday

26 How Many Times Must a White Dove Sail...

Programming II (CS300)

More Complicated Recursion CMPSC 122

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

2.3 Recursion. Overview. Mathematical Induction. What is recursion? When one function calls itself directly or indirectly.

Overview. What is recursion? When one function calls itself directly or indirectly.

COMP-202. Recursion. COMP Recursion, 2013 Jörg Kienzle and others

Sorting. CSC 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation CSC Picture

11/2/2017 RECURSION. Chapter 5. Recursive Thinking. Section 5.1

Complexity of Algorithms

Unit-2 Divide and conquer 2016

CS 112 Introduction to Programming

Recursion (Rosen, 6 th edition, Section 4.3, 4.4)

CSE11 Lecture 20 Fall 2013 Recursion

CSC 427: Data Structures and Algorithm Analysis. Fall 2006

CMSC 150 LECTURE 7 RECURSION

Recursion. Genome 559: Introduction to Statistical and Computational Genomics Elhanan Borenstein

Recursion -- Introduction

Problem solving paradigms

Recursion. Dr. Jürgen Eckerle FS Recursive Functions

Recursion (Part 2) 1

Recursion. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

Introduction to Computer Programming for Non-Majors

Recursive Algorithms. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Recursive Algorithms II

Recursion. Jordi Cortadella Department of Computer Science

Backtracking. See Section 7.7 p of Weiss

CSCI 2132 Software Development Lecture 18: Implementation of Recursive Algorithms

Fundamental problem in computing science. putting a collection of items in order. Often used as part of another algorithm

! New mode of thinking. ! Powerful programming paradigm. ! Mergesort, FFT, gcd. ! Linked data structures.

Linked Structures Chapter 13. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Computer Science E-119 Fall Problem Set 1. Due before lecture on Wednesday, September 26

CS61BL. Lecture 5: Graphs Sorting

Searching and Sorting

Programming II (CS300)

Outline. Simple Recursive Examples Analyzing Recursion Sorting The Tower of Hanoi Divide-and-conquer Approach In-Class Work. 1 Chapter 6: Recursion

Algorithm classification

VTU NOTES QUESTION PAPERS NEWS RESULTS FORUMS

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

Department of Computer Science Yale University Office: 314 Watson Closely related to mathematical induction.

Programming II (CS300)

Algorithms. Chapter 8. Objectives After studying this chapter, students should be able to:

2.3 Recursion 7/21/2014 5:12:34 PM

6.001 Notes: Section 4.1

Merge Sort. Algorithm Analysis. November 15, 2017 Hassan Khosravi / Geoffrey Tien 1

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

CIS 121 Data Structures and Algorithms with Java Spring Code Snippets and Recurrences Monday, January 29/Tuesday, January 30

Chapter 15: Recursion

Recursion. Chapter 11. Chapter 11 1

2.3 Recursion. Overview. Greatest Common Divisor. Greatest Common Divisor. What is recursion? When one function calls itself directly or indirectly.

Transcription:

Recursion CMPT 126: Lecture 10 Recursion Tamara Smyth, tamaras@cs.sfu.ca School of Computing Science, Simon Fraser University October 25, 2007 Recursion is the process of defining something in terms of itself. Andrew Plotkin (interactive fiction writer, aka Zarf) said: If you already know what recursion is, just remember the answer. Otherwise, find someone who is standing closer to Douglas Hofstadter than you are; then ask him or her what recursion is. Consider the following dictionary definitions: recursion: See recursion. decoration: n. any ornament or adornment used to decorate something. Though such recursive definitions are not always helpful when used in the dictionary, they can be very useful when representing and/or solving mathematical or computer programming problems. 1 CMPT 126: Recursion, Lecture 10 2 Thinking Recursively Tracing the Recursive Definition of List To program recursively, we must learn to think recursively. Consider the following definition of a List: a list is one or more numbers separated by commas. An alternate, equivalent recursive definition is: A List is a: number or a: number comma List The recursive definition defines each of the following lists: 24, 45, 9 2, 56 34, 45, 3, 7, 76, 94, 54, 1 20 The last of list containing a single element is described by the non-recursive part of the definition, that is, the base case. number LIST comma number LIST comma number LIST comma LIST number 24, 24, 88, 40, 37 Figure 1: Tracing through a recursive List. 88, 40, 37 88, 40, 37 40, 37 Note the definition of List contains one option that is recursive and one that is not. The non-recursive part is called the base case. Without a base case, the recursion would never end, a problem referred to as infinite recursion. 37 CMPT 126: Recursion, Lecture 10 3 CMPT 126: Recursion, Lecture 10 4

Recursion in Math Recursive Factorial Function Consider the factorial function, N!, defined as the product of all integers between 1 and N. 3! = 3 2 1 = 6 N! = N (N 1) (N 2)... 1 What is the base case? Look for the case that s not defined in terms of N: eg. 1! = 1 or 0! = 1. What is the recursive case? Define the problem in terms of itself, while reducing the problem size (i.e., reduce N! to (N 1)!) The factorial function can be expressed recursively as 1! = 1 (base case) N! = N (N 1)! for N > 1 (recursive case) Given here in pseudocode: function factorial(n) if (n<=1) return 1; else return n * factorial(n-1); Eventually the base case will be reached: 4! = 4 3! = 4 3 2! = 4 3 2 1! (base case reached) = 4 3 2 1 (recursion is terminated) The base case is 1!. All other values of N! are defined recursively as N (N 1)! CMPT 126: Recursion, Lecture 10 5 CMPT 126: Recursion, Lecture 10 6 Recursive Programming Finding a Recursive Solution Recursion is a programming technique in which a method calls itself, that is, a method is defined in tems of itself. Each call to a method creates a new environment in which all local variables and parameters are newly defined. Each time a method terminates, processing returns to the method that called it (which for recursive calls, is an earlier invocation of the same method). To ensure program termination, the method must define both a base case AND a recursive case A problem that can be solved in pieces is a good candidate for recursion. Given the following problem: Flip( recursion ) 1. Find a smaller subproblem: Flip( ecursion ) 2. Define a solution in terms of the subproblem: return Flip( ecursion ) + r 3. Combine with original problem for a general solution: return Flip(s.substring(1)) + s.charat(0) What is the base case? Flip( ) = Flip( recur ) = Flip( ecur ) + r = (Flip( cur ) + e ) + r = ((Flip( ur ) + c ) + e ) + r = (((Flip( r ) + u ) + c )) + e ) + r = ((((Flip( ) + r ) + u ))) + c )) + e ) + r CMPT 126: Recursion, Lecture 10 7 CMPT 126: Recursion, Lecture 10 8

Subproblem Recursive Sum of Numbers The key is defining the subproblem The recursive step MUST reduce the problem size (or you will end up with infinite recursion). You must be able to split the problem to make a recusive call. When the subproblem is obvious, you have the base case. Every problem must end with a base case. Consider the sum of numbers: N N 1 i = N + i i=1 What is the base case? i=1 What is the recursive case? N 2 = N + N 1 + i =... i=1 public int sum (int num) int result; if (num == 1) result = 1; else result = num + sum (num-1); return result; CMPT 126: Recursion, Lecture 10 9 CMPT 126: Recursion, Lecture 10 10 Recursion vs. Iteration Two Solutions to Powers The non-recursive solution to the summation problem compute the numbers between 1 and num in an iterative manner. sum = 0; for (int i = 1; i <= num; i++) sum += i; This solution is more straight forward. The recursive solution however, demonstrates the concept of recursion using a simple problem. In many problems (though not all!), recursion is the shorter and more elegant solution. Base case: x 0 = 1 x y = x x y 1 long pow(long x, long y) if (y==0) return 1; else return (x * pow(x, y-1) x y = x y/2 x y/2 long pow(long x, long y) long tmp; if (y==0) return 1; else if (y%2==0) //even tmp = pow(x, y/2); return tmp * tmp; else //odd tmp = pow(x, (y-1)/2); return tmp * tmp * x; CMPT 126: Recursion, Lecture 10 11 CMPT 126: Recursion, Lecture 10 12

Direct vs. Indirect Recursion Using Recursion Traversing a Maze Direct recursion occurs when a method invokes itself. Indirect recursion occurs when a method invokes another method, eventually resulting in the original method being invoked again. m1 m2 m3 Solving a maze involves a great deal of trial and error: following a path, backtracking when you reach a wall, trying other possible paths. It s an activity nicely solved using recursion. The Maze class is a two-dimensional array of integers representing a maze. m1 m2 m3 m1 m2 Figure 2: Indirect Recursion. m3 1110110001111 1011101111001 0000101010100 1110111010111 1010000111001 1011111101111 1000000000000 1111111111111 The depth of indirection may vary. Indirect recursion requires the same attention to base cases. The goal is to move from the top left corner to the bottom right corner using only valid directions: down, right, up and left. Initially, a 1 indicates a clear path and a 0 indicates a blocked path. CMPT 126: Recursion, Lecture 10 13 CMPT 126: Recursion, Lecture 10 14 As the Maze is being solved, element values are changed to indicate attempted, and ultimately successful, paths. 7770110001111 3077707771001 0000707070300 7770777070333 7070000773003 7077777003333 7000000000000 7777777777777 Recursive Maze Strategy Think of the initial conditions and the final goal: The maze can be traversed successfully if it can be traversed from position (0,0), and therefore any position adjacent, namely (1,0), (0,1), (-1,0) or (0,-1). Picking a potential next step, say (1,0), we find ourselves in the same position: the maze can be traversed successfully if it can be traversed from position (1, 0), and from any adjacent position. This process is continued recursively. CMPT 126: Recursion, Lecture 10 15 CMPT 126: Recursion, Lecture 10 16

The class Maze.java public class Maze private final int TRIED = 3; private final int PATH = 7; private int[][] grid = 1,1,1,0,1,1,0,0,0,1,1,1,1, 1,0,1,1,1,0,1,1,1,1,0,0,1, 0,0,0,0,1,0,1,0,1,0,1,0,0, 1,1,1,0,1,1,1,0,1,0,1,1,1, 1,0,1,0,0,0,0,1,1,1,0,0,1, 1,0,1,1,1,1,1,1,0,1,1,1,1, 1,0,0,0,0,0,0,0,0,0,0,0,0, 1,1,1,1,1,1,1,1,1,1,1,1,1 ; public boolean traverse(int row, int col) boolean done = false; if (valid(row,col)) grid[row][col] = TRIED; //base case;... //recursive case;... if (done) //this location is part of the final path grid[row][col] = PATH; return done; //determine if a specific location is valid private boolean valid(int row, int col) boolean result = false; //check if cell is in the bounds of the matrix... //check if cell is not blocked and not previously tried if (grid[row][col] == 1) result = true; return result; //returns the maze as a string public String tostring() String result = "\n"; for (int row=0; row < grid.length; row++) for (int col=0; col < grid[row].length; col++) result += grid[row][col] + ""; result += "\n"; return result; CMPT 126: Recursion, Lecture 10 17 CMPT 126: Recursion, Lecture 10 18 Traverse method for Maze.java Discussion of Maze // Attempts to recusively traverse the maze. Inserts special // characters indicating locations that have been tried and // that eventually become part of the solution. public boolean traverse(int row, int col) boolean done = false; if (valid(row,col)) grid[row][col] = TRIED; if (row == grid.length-1 && col == grid[0].length-1) done = true; //the maze is solved else done = traverse(row+1, col); //down if (!done) done = traverse(row, col+1); //right if (!done) done = traverse(row-1, col); //up if (!done) done = traverse(row, col-1); //left if (done) //this location is part of the final path grid[row][col] = PATH; return done; The recursive method in the Maze class is called traverse. It returns a boolean indicating whether a solution was found. The initial call to traverse passes in the upper-left location (0,0). What is the base case? There are 3 possible base cases that will terminate any particular recursive path: 1. an invalid move because the move is out of bounds 2. an invalid move becasue the move has already been tried 3. a move arrives at the final location The traverse method determines whether the maze has been completed by checking for the bottom-right location. CMPT 126: Recursion, Lecture 10 19 CMPT 126: Recursion, Lecture 10 20

Towers of Hanoi Rules for Towers of Hanoi The Towers of Hanoi puzzle was invented in the 1880s by Douard Lucas, a French mathematician. The puzzle consists of three pegs on which a set of disks, each with a different diameter, may be placed. Initially the disks are stacked on the leftmost peg, in order of size, with the largest disk on the bottom. 01 01 The goal of the puzzle is to move all the disks from the leftmost peg to the rightmost peg, adhering to the following rules: 1. Move only one disk at a time. 2. A larger disk may not be placed ontop of a smaller disk. 3. All disks, except the one being moved, must be on a peg. Figure 3: The Towers of Hanoid puzzle. CMPT 126: Recursion, Lecture 10 21 CMPT 126: Recursion, Lecture 10 22 Hanoi Strategy 01 01 The rules imply that smaller disks must be out of the way to move larger disks from one peg to another. 01 01 General strategy for moving N disks from the original peg to the destination peg: 01 01 01 01 01 01 01 01 01 1. Move the topmost N 1 disks from the original peg to the extra peg. 2. Move the largest disk from original peg to destination peg. 3. Move N 1 disks from the extra peg to the destination peg. 01 01 01 01 01 01 01 Figure 4: Three-disk solution to the Towers of Hanoi puzzle. Figure 5: Reduced problem of the Towers of Hanoi puzzle. CMPT 126: Recursion, Lecture 10 23 CMPT 126: Recursion, Lecture 10 24

Recursive Solution Code for Towers of Hanoi This strategy lends itself to a recursive solution. Step 1 and 3 are the same problems over and over again: move a stack of disks. the problem size is reduced each time; what s refered to as the desination and extra peg may change each time; The base case for this problem occurs when we want to move a stack that consists of only one disk. This step can be accomplished withouth recursion. public void solve() movetower(totaldisks, 1, 3, 2); private void movetower(int numdisks, int start, int end, int temp) if (numdisks== 1) moveonedisk(start, end); else movetower(numdisks-1, start, temp, end); moveonedisk(start, end); movetower(numdisks-1, temp, end, start); //prints instructions to move one disk private void moveonedisk(int start, int end) System.out.println("Move one disk from " + start + " to " + end); CMPT 126: Recursion, Lecture 10 25 CMPT 126: Recursion, Lecture 10 26 Discussion of Tower Solution Merge Sort The initial call indicates all disks should be moved from peg 1 to peg 3, using peg 2 as the extra position. When the base case occurs in the movetower method, one disk is moved. If the stack contains more than one disk, movetower is called recursively to 1. get N 1 disks out of the way 2. move the largest disk 3. move N 1 disks to their final destination Note the parameters to movetower describing the original, destination, and extra peg are switched. This solution is actually inefficient. To solve for N disks, we must make 2 N 1 individual disk moves. This is an example of exponential complexity. As the number of disks increases, the number of required moves increases exponentially. Now that we ve seen recursion, we can look at another sorting algorithm called merge sort, which is more efficient than insertion and selection sort. It is usually defined recursively, where the recursive calls work on smaller and smaller parts of the list. Idea: Split the unsorted list into two sublist of about half the size Recursively sort each half Merge the two sorted halves into a single sorted list CMPT 126: Recursion, Lecture 10 27 CMPT 126: Recursion, Lecture 10 28

Merge Sort Example Merge Sort Algorithm Based on a divide and conquer strategy: list is divided into two halves (divide) each half is sorted independently (conquer). two halves are merged into a sorted sequence Split original list recursively untill base case is reached 17 8 75 23 14 95 29 87 74 1 2 37 81 17 8 75 23 14 95 29 87 74 1 2 37 81 17 8 75 23 14 95 29 87 74 1 2 37 81 17 8 75 23 14 95 29 87 74 1 2 37 81 17 8 75 23 14 95 29 87 74 1 2 37 Figure 6: Recursive sort. 81 Pseudocode mergesort(array, first, last): //sort array[first] to array[last-1] if last - first <= 1: return //length 0 or 1 already sorted mid = (first + last)/2 mergesort(array, first, mid) //recursive call 1 mergesort(array, mid, last) //recursive call 2 merge(array, first, mid, last) Jave code: public static void mergesort(int[] array, int first, int last) if (last-first > 1) int mid = (first + last)/2; mergesort(array, first, mid); //recursive call 1 mergesort(array, mid, last); //recusive call 2 merge(array, first, mid, last); Merge? CMPT 126: Recursion, Lecture 10 29 CMPT 126: Recursion, Lecture 10 30 Merge Step Merge Algorithm Must put the next smallest element into the merge list at each point. Each next smallest could come from either list. Merging requires checking the smallest element of each half, requiring just one comparison step. This is where the recursive call saves time. merge(array, first, mid, last): //merge array[first to mid-1] and array[mid to last-1] leftpos = first rightpos = mid for newpos from 0 to last-first: if array[leftpos] <= array[rightpos]: newarray[newpos] = array[leftpos] leftpos++ else: newarray[newpos] = array[rightpos] rightpos++ copy newarray to array[first to (last-1)] What is wrong with the above code? The alorithm starts correctly but has an error as it finishes: Eventually one of the halves will be empty and the if will compare against the element past one of the halves. Solution: compare only until we reach the end of one half, then just copy the rest over. CMPT 126: Recursion, Lecture 10 31 CMPT 126: Recursion, Lecture 10 32

Corrected Merge Algorithm Running Time merge(array, first, mid, last): //merge array[first to mid-1] and array[mid to last-1] leftpos = first rightpos = mid newpos = 0 while leftpos<mid and rightpos<=(last-1): if array[leftpos] <= array[rightpos]: newarray[newpos] = array[leftpos] leftpos++; newpos++ else: newarray[newpos] = array[rightpos] rightpos++; newpos++ while leftppos<mid: //copy the rest left half (if any) newarray[newpos] = array[leftpos] leftpos++; newpos++ while rightpos<=last-1 //copy the rest of the right half (if any) newarray[newpos] = array[rightpos] rightpos++; newpos++ copy newarray to array[first to (last-1)] What is the running time for merge sort? recursive calls work per call? But work per call changes... We know the merge algorithm is O(N) to merge N elements. CMPT 126: Recursion, Lecture 10 33 CMPT 126: Recursion, Lecture 10 34 Merge sort recursive calls Difference in Memory Requirements? FIX: include figure here. Each level has a total of N elements. Steps to merge each level: N Number of levels: log N Total Running Time: 0(N log N) Much faster than selection/insertion sort which both take 0(N 2 ) In general, no sorting algorithm can do better than 0(N log N) (though some are faster for limited cases). Merging requires extra storage: an extra array with N elements which can be re-used by all merges. Insertion sort only requires a few storage variables An algorithm that uses at most 0(1) extra storage is called in-place. Insertion/section sort are both in-place algorithms. Mege sort is not. CMPT 126: Recursion, Lecture 10 35 CMPT 126: Recursion, Lecture 10 36

Stable Sort In a stable sorting algorithm, equal elements are kept in order. Insertion sort and merge sort have this property, selection sort does not. CMPT 126: Recursion, Lecture 10 37