Recursion Chapter 12.

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

Recursion Chapter 14. Overview Base case and general case of recursion. A recursion is a method that calls itself. That simplifies the problem. The simpler.
Recursion. Recursion is a powerful technique for thinking about a process It can be used to simulate a loop, or for many other kinds of applications In.
Factorial Recursion stack Binary Search Towers of Hanoi
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 14: Recursion by.
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.
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.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 14: Recursion Starting Out with C++ Early Objects
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
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.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Week 5 - Monday.  What did we talk about last time?  Linked list implementations  Stacks  Queues.
Chapter 11- Recursion. Overview n What is recursion? n Basics of a recursive function. n Understanding recursive functions. n Other details of recursion.
Analyzing Complexity of Lists OperationSorted Array Sorted Linked List Unsorted Array Unsorted Linked List Search( L, x ) O(logn) O( n ) O( n ) Insert(
1 Programming with Recursion. 2 Recursive Function Call A recursive call is a function call in which the called function is the same as the one making.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
CSC 211 Data Structures Lecture 13
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
(c) , University of Washington18a-1 CSC 143 Java Searching and Recursion N&H Chapters 13, 17.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
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.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
1 CSC 143 Recursion [Reading: Chapter 17]. 2 Recursion  A recursive definition is one which is defined in terms of itself.  Example:  Sum of the first.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
Recursion You may have seen mathematical functions defined using recursion Factorial(x) = 1 if x
Recursion Powerful Tool
Review of Recursion What is a Recursive Method?
Recursion.
Algorithm Analysis 1.
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 19: Recursion.
Recursion Version 1.0.
Recursion CENG 707.
Chapter 15 Recursion.
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Recursion notes Chapter 8.
Towers of Hanoi Move n (4) disks from pole A to pole C
Chapter 15 Recursion.
Recursion Chapter 11.
COMP 53 – Week Seven Big O Sorting.
Java 4/4/2017 Recursion.
CS1010 Discussion Group 11 Week 11 – Recursion recursion recursion….
Programming in Java: lecture 10
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion Chapter 10.
Data Structures Recursion CIS265/506: Chapter 06 - Recursion.
Chapter 14: Recursion Starting Out with C++ Early Objects
Applied Algorithms (Lecture 17) Recursion Fall-23
CS 3343: Analysis of Algorithms
Algorithm design and Analysis
Chapter 14: Recursion Starting Out with C++ Early Objects
Review of Recursion What is a Recursive Method?
Measuring “Work” Linear and Binary Search
Basics of Recursion Programming with Recursion
CSC 143 Recursion.
Review of Recursion What is a Recursive Method?
Recursive Thinking.
Review of Recursion What is a Recursive Method?
Presentation transcript:

Recursion Chapter 12

Outline What is recursion Recursive algorithms with simple variables Recursion and arrays Recursion and complexity proof by induction

What is Recursion Recursion is a kind of “Divide & Conquer” Divide problem into smaller problems Solve the smaller problems Recursion Divide problem into smaller versions of itself Smallest version(s) can be solved directly

Coding Recursively Remember the two imperatives: SMALLER! STOP! when you call the method inside itself, one of the arguments has to be smaller than it was STOP! if the argument that gets smaller is very small (usually 0 or 1), then don’t do the recursion

Recursive Countdown Print the numbers from N down to 1 recursive method (stop when N is zero) public static void countDownFrom(int n) { if (n > 0) { // STOP if n == 0! System.out.print(n + " "); countDownFrom(n - 1); // SMALLER! } to count down from 10: print 10; count down from 9 …. 10 9 8 7 6 5 4 3 2 1

Recursive Countdown Print the numbers from N down to 1 recursive method (stop when N is zero) public static void countDownFrom(int n) { if (n > 0) { // STOP if n == 0! System.out.print(n + " "); countDownFrom(n - 1); // SMALLER! } to count down from 9: print 9; count down from 8…. 10 9 8 7 6 5 4 3 2 1

Recursive Countdown Print the numbers from N down to 1 recursive method (stop when N is zero) public static void countDownFrom(int n) { if (n > 0) { // STOP if n == 0! System.out.print(n + " "); countDownFrom(n - 1); // SMALLER! } to count down from 0: (do nothing) 10 9 8 7 6 5 4 3 2 1

Recursive Functions Function defined in terms of itself one or more STOPs (“base case(s)”) one or more SMALLERs (“recursive case(s)”) n! = 1 if n == 0 n*(n-1)! otherwise Fib(n) = 1 if n == 0 1 if n == 1 Fib(n–1)+Fib(n–2) otherwise

The Factorial Method Product of numbers from N down to 1 recursive method public static int factorial(int n) { if (n > 0) { return n * factorial(n - 1); // smaller } else { return 1; // stop }

Getting Smaller 4! = 4 * 3! Recursive Case 4 * 6 = 24 0! = 1 Base Case Base Case n! = 1 if n == 0 n*(n-1)! otherwise Recursive Case

Calling a Recursive Function Just like calling any other function System.out.println(factorial(5)); The function returns the factorial of what you give it because that’s what it does it returns the factorial every time you call it including when you call it inside its definition

Towers of Hanoi // ----- s == start, f == finish, x == extra ----- // public static void hanoi(int n, char s, char f, char x) { if (n > 0) { // stop if n == 0 hanoi(n - 1, s, x, f); // smaller from start to extra System.out.println("Move a disk from " + s + " to " + f + "."); hanoi(n - 1, x, f, s); // smaller from extra to finish }

Recursion with Arrays Simple recursion Recursion with arrays Values get smaller Hanoi(64) calls Hanoi(63) Hanoi(63) calls Hanoi(62) Recursion with arrays Array length gets smaller Look at less of the array

Example Array Recursion To Print an Array in reverse Base Case (Stop) If the array has length zero, do nothing Recursive Case (Smaller) First print the last element of the array Then print the rest of the array in reverse 6 100 3 -2 8 18 5 5 18 8 -2 3 100 6 5

Print Array in Reverse Give “length” of array to print, too reduce “length” by 1 until get to 0 NOTE: we’re just pretending it’s smaller public static void printInReverse(int[] arr, int len) { if (len > 0) { // stop if len == 0 System.out.print(arr[len - 1] + " "); printInReverse(arr, len - 1); // “smaller” array }

Another Example To Find the Maximum Value in an Array Base Case if length is 1, the only element is the maximum Recursive Case Get the maximum from the rest of the array... ...& compare it to the last element Return the bigger 6 100 3 -2 8 18 5 100 5 100

Remember to use a “pretend” length Exercise Translate the (recursive) algorithm from the previous slide into Java Base Case if length is 1, the only element is the maximum Recursive Case Get the maximum from the rest of the array... ...& compare it to the last element Return the bigger Remember to use a “pretend” length

Working From Both Ends Sometimes we want to be able to shorten the array at either end Pass start and end points instead of length Done when lo > hi (for len==0), or lo == hi (for len==1) “Sub-Array processing”

Working From Both Ends Alternate way to find array maximum compare first and last elements drop the smaller out of range we’re using stop when array has only one element left maximum is that one element 6 100 3 -2 8 18 5 100

Working From Both Ends Alternate way to find array maximum public static int maximum(int[] arr, int lo, int hi) { if (lo == hi) { return arr[lo]; // stop! } else if (arr[lo] > arr[hi]) { return maximum(arr, lo, hi - 1); // smaller } else { return maximum(arr, lo + 1, hi); // smaller }

Array Splitting Sub-array processing can get rid of more than one element at a time Binary split is a common method do top “half” and bottom “half” separately combine to get answer 6 100 3 -2 8 18 5 100 18 100

Array Splitting Alternate way to find array maximum public static int maximum(int[] arr, int lo, int hi) { if (lo == hi) { return arr[lo]; // stop! } else { int mid = lo + (hi - lo) / 2; int maxLo = maximum(arr, lo, mid); // smaller! int maxHi = maximum(arr, mid+1, hi); // smaller! return Math.max(maxLo , maxHi); }

Defensive Programming Consider finding the midpoint mid = lo + (hi – lo) / 2; could just do (hi + lo) / 2 BUT what if the array is HUGE hi == 2,000,000,000; lo == 1,000,000,000 (hi + lo) / 2 == -647,483,648 (int overflow) lo + (hi – lo) / 2 == 1,500,000,000 (no overflow) hi and lo both positive, so no underflow worry

Array Recursion Exercise Given an array and a number, find out if the number is in the array (contains method) NOTE: the array is unsorted Base Case(s) ? Recursive Case(s)

Recursive Algorithm Analysis Still in terms of size of problem size of n for factorial(n), fibonacci(n), … size of array/linked structure in printInReverse, findMaximum, … Base case probably just one operation Recursive case  recursive count amount of work for n in terms of amount of work for n - 1

Work for printInReverse (Array) N is the “length” of the array (len) public static void printInReverse(int[] arr, int len) { if (len > 0) { // stop if len == 0 System.out.print(arr[len - 1] + " "); printInReverse(arr, len - 1); // “smaller” array } if len == 0: compare len to 0: W(0) = 1 if len > 0: compare len to 0, print arr[len-1], call printInReverse with len-1: W(len) = 2 + W(len - 1)

Recurrence Relation When W(n) defined in terms of W(n – 1)… or some other smaller value than n … it’s called a recurrence relation It’s a recursive definition of W has a base case (n = 0 or n = 1 or …) has a recursive case public static int W(int n) { if (n == 0) return 1; else return 2 + W(n – 1); }

Solving by Inspection Work for printInReverse: W(0) = 1 (change) W(1) = 2 + W(0) = 3 +2 W(2) = 2 + W(1) = 5 +2 W(3) = 2 + W(2) = 7 +2 W(4) = 2 + W(3) = 9 +2 … linear: factor of 2 W(N) = 2 + W(N–1) = 2N + 1?

Solving by Inspection Table with N and Work for N (W) calculate change in work (ΔW) for each step N W recurrence ΔW 1 3 W(0) + 2 2 5 W(1) + 2 7 W(2) + 2 4 9 W(3) + 2 11 W(4) + 2 ?

Solving by Inspection: Linear Change in W will be a constant > 0 probably: W(N) = (ΔW)N + W(0) N W recurrence ΔW 1 3 W(0) + 2 2 5 W(1) + 2 7 W(2) + 2 4 9 W(3) + 2 11 W(4) + 2 2N + 1

Solving by Inspection: Quadratic Add column for change in ΔW Δ(ΔW) constant  W(N) = (Δ(ΔW)/2)N2 + … N2 1 4 9 16 25 N W recurrence ΔW recurrence(2) Δ(ΔW) 1 3 W(0) + 2 2 7 W(1) + 4 4 ΔW(1) + 2 13 W(2) + 6 6 ΔW(2) + 2 21 W(3) + 8 8 ΔW(3) + 2 5 31 W(4) + 10 10 ΔW(4) + 2 N2 + ? + ? N2 + N + 1 W(N-1) + 2N 2N

Proving Your Formula You only looked at a few values will the formula you got work on all values? Can prove that a formula will work proof by induction Assume it works up to an arbitrary N – 1 use recurrence relation to show it works for N proves it works up to any N

Proof by Induction (Linear) Suppose W(n) = 2n + 1 for n < N What is W(N)? W(N) = W(N – 1) + 2 (recurrence relation) but N – 1 < N, so: W(N – 1) = 2(N – 1) + 1 = 2N – 2 +1 = 2N – 1 W(N) = (2N – 1) + 2 = 2N + 1 so W(N) also = 2N + 1 and that’s for any N > 0

Proof by Induction (Quadratic) Suppose W(n) = n2 + n + 1 for n < N What is W(N)? W(N) = W(N – 1) + 2N (recurrence relation) but N – 1 < N, so: W(N – 1) = (N – 1)2 + (N – 1) + 1 = N2 – N + 1 W(N) = (N2 – N + 1) + 2N = N2 + N + 1 so W(N) also = N2 + N + 1 and that’s for any N > 0

Exercise Given recurrence reln W(N) = W(N-1) + 4, prove that W(N) = 4N + 5 Given W(N) = W(N-1) + 2N + 1, prove that W(N) = N2 + 2N

Analyzing Array Splitting Split array into two equal(ish) pieces easiest to analyze if N is a power of 2 1, 2, 4, 8, 16, … or one less than that (if middle item removed) 0, 1, 3, 7, 15 makes exactly equal splits Need to factor in change in N

Analyzing Array Splitting Work when splitting exactly find maximum using array splitting W(8) = 2W(4) W(2N) = 2W(N) 6 100 3 -2 8 18 5 35 W(8) W(4) W(4) W(2) W(2)

Array Splitting public static int maximum(int[] arr, int lo, int hi) { if (lo == hi) { // 1 comparison return arr[lo]; // 1 array access } else { int mid = lo + (hi - lo) / 2; // 3 math ops int maxLo = maximum(arr, lo, mid); // 1 asgn + REC int maxHi = maximum(arr, mid+1, hi); // 1 asgn + REC return (maxLo > maxHi) ? maxLo : maxHi; // 1 comparison } W(1) = 2 DW / DN W(2) = 7 + 2W(1) = 7 + 2(2) = 11 + 9 / +1 9N – 7 W(4) = 7 + 2W(2) = 7 + 2(11) = 29 +18 / +2 9N – 7 W(8) = 7 + 2W(4) = 7 + 2(29) = 65 +36 / +4 9N – 7 W(16) = 7 + 2W(8) = 7 + 2(65) = 137 +72 / +8 9N – 7

findMaximum by Splitting Split array into two (nearly equal) parts look at only powers of 2 even splits all the way down looks like W(N) = 9N – 7 linear works for N <= 16 do induction on 2N instead of N+1 (later we’ll do 2N+1)

Proof by Induction (Part 1) Assume W(n) = 9n – 7 for n < 2N What’s W(2N)? W(2N) = 7 + 2W(N) but N < 2N, so W(N) = 9N – 7… … so W(2N) = 7 + 2(9N–7) = 7 + 18N – 14 = 18N – 7 = 9(2N) – 7 same formula works for all even N

Proof by Induction (Part 2) Assume W(n) = 9n – 7 for n < 2N What’s W(2N + 1)? W(2N + 1) = 7 + W(N) + W(N+1) but N < N+1 < 2N, so … W(2N + 1) = 7 + (9N – 7) + (9(N+1) – 7) = 7 + 9N – 7 + 9N + 9 – 7 = 18N + 9 – 7 = 9(2N+1) – 7 same formula works for all odd N

Towers of Hanoi N = number of disks Work = number of moves W(0) = 0 (change) W(1) = W(0) + 1 + W(0) = 1 +1 W(2) = W(1) + 1 + W(1) = 3 +2 W(3) = W(2) + 1 + W(2) = 7 +4 W(4) = W(3) + 1 + W(3) = 15 +8 W(5) = W(4) + 1 + W(4) = 31 +16

Solve by Inspection Work doubles at each step of N sounds exponential compare work with 2N 2N = W(N) + 1 so work = 2N – 1 exponential N W(N) 2N 1 2 3 4 7 8 15 16 5 31 32 2N – 1

Proof by Induction Assume W(n) = 2n – 1 for n < N What’s W(N)? W(N) = 1 + 2W(N–1) = 1 + 2(2N–1 – 1) = 1 + 2N – 2 = 2N – 1 formula works for all N

Exercise How much work is done by printInReverse for the linked structure? private void printInReverse(Node first) { if (first != null) { // stop if list is empty! printInReverse(first.next); // smaller! System.out.println(first.data + " "); } write recurrence relation solve order of magnitude

Next Time Faster sorting methods