RECURSION (CH 5) A pattern for solving algorithm design problems

Similar documents
Recursion. The Recursion Pattern. Recursion 3/16/14

Using Recursion Goodrich, Tamassia. Using Recursion 1

Data Structures and Algorithms " Recursion!

Using Recursion. Linear Recursion ( 4.1.1) Recall the Recursion Pattern ( 2.5) A Simple Example of Linear Recursion

Linked Lists. Outline. COMP9024: Data Structures and Algorithms. Algorithms versus Heuristics. Data Structures and Algorithms. Abstract Data Types

CSC 344 Algorithms and Complexity. What is Recursion?

PDF created with pdffactory Pro trial version Recursion

Recursion Chapter 3.5

Data structure and algorithm in Python

CS115 INTRODUCTION TO COMPUTER SCIENCE 1. Additional Notes Module 5

8/19/2014. Most algorithms transform input objects into output objects The running time of an algorithm typically grows with input size

NCS 301 DATA STRUCTURE USING C

Divide-and-Conquer. Divide-and conquer is a general algorithm design paradigm:

Quick-Sort. Quick-sort is a randomized sorting algorithm based on the divide-and-conquer paradigm:

COMP 352 FALL Tutorial 10

Sorting and Selection

SORTING AND SELECTION

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015

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

Algorithms 4. Odd or even Algorithm 5. Greatest among three numbers Algorithm 6. Simple Calculator Algorithm

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

Parallel Sorting Algorithms


6/4/12. Recursive void Methods. Chapter 11. Vertical Numbers. Vertical Numbers. Vertical Numbers. Algorithm for Vertical Numbers

Chapter 1: Number and Operations

Comp 249 Programming Methodology Chapter 11 Recursion

Programming II (CS300)

SORTING, SETS, AND SELECTION

Algorithm Design and Recursion. Search and Sort Algorithms

Sorting Goodrich, Tamassia Sorting 1

9/10/2018 Algorithms & Data Structures Analysis of Algorithms. Siyuan Jiang, Sept

ECE 122. Engineering Problem Solving Using Java

Computer Science 4U Unit 1. Programming Concepts and Skills Algorithms

CSC Design and Analysis of Algorithms. Lecture 5. Decrease and Conquer Algorithm Design Technique. Decrease-and-Conquer

Classic Data Structures Introduction UNIT I

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Merge Sort & Quick Sort

Programming II (CS300)

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

Module 10. Recursion. Adapted from Absolute Java, Rose Williams, Binghamton University

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015

6.001 Notes: Section 4.1

Chapter Summary. Mathematical Induction Recursive Definitions Structural Induction Recursive Algorithms

For searching and sorting algorithms, this is particularly dependent on the number of data elements.

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

Multiple-choice (35 pt.)

Lecture 19 Sorting Goodrich, Tamassia

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms

Finite Math - J-term Homework. Section Inverse of a Square Matrix

8 Algorithms 8.1. Foundations of Computer Science Cengage Learning

Solutions to Assessment

DIVIDE & CONQUER. Problem of size n. Solution to sub problem 1

Chapter 13. Recursion. Copyright 2016 Pearson, Inc. All rights reserved.

Reduction & Recursion Overview

COMP Data Structures

Sorting. Data structures and Algorithms

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

Data Structures and Algorithms

II (Sorting and) Order Statistics

DESIGN AND ANALYSIS OF ALGORITHMS. Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES

Programming II (CS300)

SET DEFINITION 1 elements members

Test 1 Review Questions with Solutions

Algorithms and Data Structures

MST & Shortest Path -Prim s -Djikstra s

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

Fast Sorting and Selection. A Lower Bound for Worst Case

Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Recursive Definitions

Unit-2 Divide and conquer 2016

CSE Data Structures and Introduction to Algorithms... In Java! Instructor: Fei Wang. Mid-Term Exam. CSE2100 DS & Algorithms 1

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

Beyond Counting. Owen Kaser. September 17, 2014

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

Lecture 7. Transform-and-Conquer

17 February Given an algorithm, compute its running time in terms of O, Ω, and Θ (if any). Usually the big-oh running time is enough.

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

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

CS473-Algorithms I. Lecture 10. Dynamic Programming. Cevdet Aykanat - Bilkent University Computer Engineering Department

Analysis of Algorithms

Searching in General

CSC 284/484 Advanced Algorithms - applied homework 0 due: January 29th, 11:59pm EST

Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment

Divide and Conquer 4-0

Computer Science 431 Algorithms The College of Saint Rose Spring Topic Notes: Decrease and Conquer

Bucket-Sort and Radix-Sort

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

Priority Queue Sorting

Recursive Definitions Structural Induction Recursive Algorithms

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

Complexity of Algorithms

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

Fundamental mathematical techniques reviewed: Mathematical induction Recursion. Typically taught in courses such as Calculus and Discrete Mathematics.

INSTITUTE OF AERONAUTICAL ENGINEERING

15.4 Longest common subsequence

Sorting race.

Algorithm Analysis. Jordi Cortadella and Jordi Petit Department of Computer Science

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?

Using Templates to Introduce Time Efficiency Analysis in an Algorithms Course

Transcription:

RECURSION (CH 5) A pattern for solving algorithm design problems Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H., Wiley, 2014 1

THE RECURSION PATTERN EXAMPLE Recursion: when a method calls itself Classic example the factorial function: n! = 1 2 3 (n-1) n Recursive definition: As a Java method: 1 f ( n) = n f ( n 1) if n = 0 else 2

CONTENT OF A RECURSIVE METHOD Base case(s) Values of the input variables for which we perform no recursive calls are called base cases (there should be at least one base case). Every possible chain of recursive calls must eventually reach a base case. Recursive calls Calls to the current method. Each recursive call should be defined so that it makes progress towards a base case. Recursion 3

VISUALIZING RECURSION Recursion trace A box for each recursive call An arrow from each caller to callee An arrow from each callee to caller showing return value Example call recursivefactorial ( 4 ) call recursivefactorial ( 3 ) call recursivefactorial ( 2 ) call recursivefactorial ( 1 ) call return 4 * 6 = 24 recursivefactorial ( 0 ) return 3 * 2 = 6 return 2 * 1 = 2 return 1 * 1 = 1 return 1 final answer Recursion 4

VISUALIZING BINARY SEARCH We consider three cases: If the target equals data[mid], then we have found the target. If target < data[mid], then we recur on the first half of the sequence. If target > data[mid], then we recur on the second half of the sequence. INPUT: Values stored in sorted order within an array (sequence is sorted and indexable,) OUTPUT: Location of the target value. 5

BINARY SEARCH Search for an integer in an ordered list 6

ANALYZING BINARY SEARCH Runs in O(log n) time. The remaining portion of the list is of size high low + 1 After one comparison, this becomes one of the following: Thus, each recursive call divides the search region in half; hence, there can be at most log n levels 7

TYPES OF RECURSION Linear recursion : If a recursive call starts at most one other. Binary recursion: If a recursive call may start two others. Multiple recursion: If a recursive call may start three or more others. Terminology reflects the structure of the recursion trace, not the asymptotic analysis of the running time. 8

LINEAR RECURSION Test for base cases Begin by testing for a set of base cases (there should be at least one). Every possible chain of recursive calls must eventually reach a base case, and the handling of each base case should not use recursion. Recur once Perform a single recursive call This step may have a test that decides which of several possible recursive calls to make, but it should ultimately make just one of these calls Define each possible recursive call so that it makes progress towards a base case. 9

EXAMPLE OF LINEAR RECURSION Algorithm linearsum(a, n): Input: Array, A, of integers Integer n such that 0 n A Output: Sum of the first n integers in A. Recursion trace of linearsum(data, 5) called on array data = [4, 3, 6, 2, 8] 10

REVERSING AN ARRAY Algorithm reversearray(a, i, j): Input: An array A and nonnegative integer indices i and j Output: The reversal of the elements in A starting at index i and ending at Problem: Reverse the n elements of an array, so that the first element becomes the last, the second element becomes second to the last, and so on. if i < j then Swap A[i] and A[ j] reversearray(a, i + 1, j 1) return Terminates after a total of (1 + fffff n ) recursive calls. Because 2 each call involves a constant amount of work, the entire process runs in O(n) time. 11

DEFINING ARGUMENTS FOR RECURSION In creating recursive methods, it is important to define the methods in ways that facilitate recursion. This sometimes requires we define additional parameters that are passed to the method. For example, we defined the array reversal method as reversearray(a, i, j), not reversearray(a) 12

RECURSIVE ALGORITHMS FOR COMPUTING POWERS Problem: Raise a number x to an arbitrary nonnegative integer n. The power function, power(x,n)=x n, can be defined recursively: power( x, n) = x 1 power( x, n 1) if n = 0 else This leads to an power function that runs in O(n) time (for we make n recursive calls) We can do better than this, however 13

RECURSIVE SQUARING OBSERVATION: We consider the expression x k 2, where k = fffff( n 2 ). When n is odd, fffff n, 2 2 xk 2 = x n 1 and therefore x n = x k 2 x. When n is even, fffff( n ) = n and therefore x k 2 = x n 2 2 = x n. 2 2 = n 1 We can derive a more efficient linearly recursive algorithm by using repeated squaring: For example, 2 4 = 2 (4/2)2 = (2 4/2 ) 2 = (2 2 ) 2 = 4 2 = 16 2 5 = 2 1+(4/2)2 = 2(2 4/2 ) 2 = 2(2 2 ) 2 = 2(4 2 ) = 32 2 6 = 2 (6/ 2)2 = (2 6/2 ) 2 = (2 3 ) 2 = 8 2 = 64 2 7 = 2 1+(6/2)2 = 2(2 6/2 ) 2 = 2(2 3 ) 2 = 2(8 2 ) = 128 14

RECURSIVE SQUARING METHOD Algorithm Power(x, n): Input: A number x and integer n = 0 Output: The value x n if n = 0 then return 1 if n is odd then y = Power(x, (n - 1)/ 2) return x y y else y = Power(x, n/ 2) return y y O(logn) recursive calls. Each time we make a recursive call we halve the value of n; hence, we make log n recursive calls. That is, this method runs in O(log n) time. It is important that we use a variable twice here rather than calling the method twice. 15

RECURSIVE SQUARING METHOD Example: power(2, 13) 16

BINARY RECURSION Binary recursion occurs whenever there are two recursive calls for each non-base case. Example from before: the drawinterval method for drawing ticks on an English ruler. 18

EXAMPLE: DRAWING ENGLISH RULER Print the ticks and numbers like an English ruler: 2-inch ruler with major tick length 4; 1-inch ruler with major tick length 5; 3-inch ruler with major tick length 3. Recursion 19

USING RECURSION Slide by Matt Stallmann included with permission. drawinterval(length) Input: length of a tick Output: ruler with tick of the given length in the middle and smaller rulers on either side drawinterval(length) if( length > 0 ) then drawinterval ( length 1 ) draw line of the given length drawinterval ( length 1 ) 20

RECURSIVE DRAWING METHOD Recursion The drawing method is based on the following recursive definition An interval with a central tick length L >1 consists of: An interval with a central tick length L 1 An single tick of length L An interval with a central tick length L 1 21

A RECURSIVE METHOD FOR DRAWING TICKS ON AN ENGLISH RULER Note the two recursive calls 22

ANOTHER BINARY RECURSIVE METHOD Problem: Add all the numbers in an integer array A Solution strategy: Recursively compute the sum of the first half, and the sum of the second half, and add those sums together. Algorithm BinarySum(A, i, n): Input: An array A and integers i and n Output: The sum of the n integers in A starting at index i if n = 1 then return A[i] return BinarySum(A, i, n/ 2) + BinarySum(A, i + n/ 2, n/ 2) Example trace: linearsum 0, 2 method. 2, 2 0, 1 BinarySum(data, 0, 8) 0, 8 Space: binarysum uses O(logn) amount of additional space, which 0 is, 4 a big improvement 4, 4 over the O(n) space used by the 1, 1 2, 1 3, 1 4, 1 4, 2 5, 1 6, 1 6, 2 Time : However, the running time of is O(n). 7, 1 23

MULTIPLE RECURSION Motivating example: summation puzzles pot + pan = bib dog + cat = pig boy + girl = baby Multiple recursion: makes potentially many recursive calls not just one or two Multiple recursion : a process in which a method may make more than two recursive calls. To solve such a puzzle, we need to assign a unique digit (that is, 0,1,...,9) to each letter in the equation, in order to make the equation true. 24

ALGORITHM FOR MULTIPLE RECURSION If the number of possible configurations is not too large, however, we can use a computer to simply enumerate all the possibilities and test each one. 25

Recursion trace for an execution of PuzzleSolve(3, S, U), where S is empty and U ={a,b,c}. 26

EXAMPLE Slide by Matt Stallmann included with permission. cbb + ba = abc 799 + 98 = 997 [a] {b,c} a=7 [] {a,b,c} [b] {a,c} b=7 a,b,c stand for 7,8,9; not necessarily in that order [c] {a,b} c=7 [ab] {c} a=7,b=8 c=9 [ac] {b} a=7,c=8 b=9 [ba] {c} b=7,a=8 c=9 [ca] {b} c=7,a=8 b=9 [bc] {a} b=7,c=8 a=9 [cb] {a} c=7,b=8 a=9 might be able to stop sooner 27

PITFALLS OF RECURSION Recursion can easily be misused in various ways. 28

ELEMENT UNIQUENESS PROBLEM, REVISITED RROBLEM: Given an array with n elements, are all the elements of that collection are distinct from each other? O(n 2 ) brute force method O(nlogn) sorting based O(2 n ) Recursive unique3 for testing element uniqueness 29

ANALYSIS OF RECURSIVE UNIQUE3 unique3 is a terribly inefficient use of recursion!! Let n denote the number of entries under consideration: n = 1 + high low Base case (n = 1): running time of unique3 is O(1) since there are no recursive calls and the nonrecursive part of each call uses O(1) time. General case (n>1): a single call to unique3 for a problem of size n may result in two recursive calls on problems of size n 1, and so on. Thus, in the worst case, the total number of method calls is given by the geometric summation: 1 + 2 + 4 + + 2 n 1 = 2 n 1 = O(2 n ) 30

COMPUTING FIBONACCI NUMBERS Fibonacci numbers are defined recursively: F 0 = 0 F 1 = 1 F i = F i-1 + F i-2 for i > 1. Recursive algorithm (inefficiently): Algorithm BinaryFib(k): Input: Nonnegative integer k Output: The kth Fibonacci number F k if k = 1 then return k else return BinaryFib(k 1) + BinaryFib(k 2) 31

ANALYSIS Let n k be the number of recursive calls by BinaryFib(k) n 0 = 1 n 1 = 1 n 2 = n 1 + n 0 + 1 = 1 + 1 + 1 = 3 n 3 = n 2 + n 1 + 1 = 3 + 1 + 1 = 5 n 4 = n 3 + n 2 + 1 = 5 + 3 + 1 = 9 n 5 = n 4 + n 3 + 1 = 9 + 5 + 1 = 15 n 6 = n 5 + n 4 + 1 = 15 + 9 + 1 = 25 n 7 = n 6 + n 5 + 1 = 25 + 15 + 1 = 41 n 8 = n 7 + n 6 + 1 = 41 + 25 + 1 = 67. Note that n k at least doubles every other time That is, n k > 2 k/2. It is exponential! 32

A BETTER FIBONACCI ALGORITHM Use linear recursion instead Algorithm LinearFibonacci(k): Input: A nonnegative integer k Output: Pair of Fibonacci numbers (F k, F k 1 ) if k = 1 then return (k, 0) else (i, j) = LinearFibonacci(k 1) Each invocation 1) makes only one recursive call and 2) decreases the argument n by 1. return (i +j, i) runs in O(n) time. 33