Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.

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 Genome 559: Introduction to Statistical and Computational Genomics Elhanan Borenstein.
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
Recursion.
Recursive methods. Recursion A recursive method is a method that contains a call to itself Often used as an alternative to iteration when iteration is.
ITEC200 – Week07 Recursion. 2 Learning Objectives – Week07 Recursion (Ch 07) Students can: Design recursive algorithms to solve.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Recursion Chapter 8. 2 Chapter Contents What Is Recursion? Tracing a Recursive Method Recursive Methods That Return a Value Recursively Processing an.
Recursion Chapter 7. Spring 2010CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn how.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Recursion. 2 CMPS 12B, UC Santa Cruz Solving problems by recursion How can you solve a complex problem? Devise a complex solution Break the complex problem.
Fall 2007CS 2251 Recursion Chapter 7. Fall 2007CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Unit 7 1 Unit 7: Recursion H Recursion is a fundamental programming technique that can provide an elegant solution to a certain kinds of problems H In.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 15 Recursive Algorithms.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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.
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.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 15: Recursion Starting Out with Java: From Control Structures.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
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.
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 Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Chapter 8 Recursion Modified.
Informal Analysis of Merge Sort  suppose the running time (the number of operations) of merge sort is a function of the number of elements to sort  let.
CSC 211 Data Structures Lecture 13
Data Structures & Algorithms Recursion and Trees Richard Newman.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive Algorithms.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
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 in Java The answer to life’s greatest mysteries are on the last slide.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
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
Chapter Topics Chapter 16 discusses the following main topics:
Recursion Version 1.0.
Chapter 15 Recursion.
Towers of Hanoi Move n (4) disks from pole A to pole C
Chapter 15 Recursion.
Recursion Chapter 11.
Java 4/4/2017 Recursion.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Data Structures & Algorithms
Basics of Recursion Programming with Recursion
Recursion Chapter 12.
Presentation transcript:

Recursion Recursion Chapter 12

Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion and arrays n Recursion and linked structures n Recursion and complexity  proof by induction

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

Recursive Structures n Getting the smallest doll from a set of Russian dolls: n If this isn’t the smallest doll  open it up  get the smallest doll from the set of Russian dolls inside

“Recursive” Images n Image where smaller part is same as whole  but smaller (duh!) n Fern has fronds  “fronds” are just smaller ferns

Recursive Algorithms n Recursive algorithm calls itself  open Russian dolls: open doll and open the (smaller) Russian dolls inside  draw a fern: draw the stem, then draw three (smaller) ferns at the end of the stem n Problem: when to stop?  Russian dolls: this doll won’t open  ferns: this fern is less than one pixel tall

Coding Recursively n Remember the two imperatives:  SMALLER! »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 n 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! if (n > 0) {// STOP if n == 0! System.out.print(n + " "); System.out.print(n + " "); countDownFrom(n - 1);// SMALLER! countDownFrom(n - 1);// SMALLER! }} print 10; count down from 9 …. to count down from 10:

Recursive Countdown n 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! if (n > 0) {// STOP if n == 0! System.out.print(n + " "); System.out.print(n + " "); countDownFrom(n - 1);// SMALLER! countDownFrom(n - 1);// SMALLER! }} print 9; count down from 8…. to count down from 9:

Recursive Countdown n 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! if (n > 0) {// STOP if n == 0! System.out.print(n + " "); System.out.print(n + " "); countDownFrom(n - 1);// SMALLER! countDownFrom(n - 1);// SMALLER! }} (do nothing) to count down from 0:

Recursive Fern Drawing public void drawFern(double x, double y, double angle, double size) { if (size > 1.0) {// STOP if size 1.0) {// STOP if size <= 1.0! double[] end; double length = size * 0.5; end = drawStem(x, y, angle, length);// private method double smaller = size * 0.5;// SMALLER! drawFern(end[0], end[1], angle+60, smaller); drawFern(end[0], end[1], angle, smaller); drawFern(end[0], end[1], angle-60, smaller); }} This will draw a fern It’s what the drawFern method does

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

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

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

Calling a Recursive Function n Just like calling any other function System.out.println(factorial(5)); n 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

Tracing the Recursion n Each call to the function is independent  it has its own argument(s)  it has its own local variables n But we very well might get confused  20 or 30 variables all named “n” lying around n The computer does not  it uses the “run-time stack” to keep track

Tracing the Recursion n Don’t try to trace the calls normally  it’s very confusing n Stack up method calls  main calls factorial(4)  in factorial(4): »4 > 0, so »return 4 * factorial(3) »in factorial(3): 3 > 0, so3 > 0, so return 3 * factorial(2)return 3 * factorial(2) main System.out.println(…) factorial(4) return 4 * … factorial(3) return 3 * … factorial(2) return 2 * … factorial(1) return 1 * … main System.out.println(24) factorial(4) return 4 * 6 factorial(3) return 3 * 2 factorial(2) return 2 * 1 factorial(1) return 1 * 1 factorial(0) return 1

Exercise n Write a recursive function to calculate the (non-negative integer) power of an integer  Hint: it’ll look a LOT like the one for factorial n k = 1if k == 0 n*n k-1 otherwise

Towers of Hanoi n Buddhist monks in Hanoi n Set the task of moving golden disks (64) around diamond needles (3)  all disks different sizes  never put a bigger disk on a littler one n When all 64 disks have been moved from the starting needle to the ending, the universe will end (In class demo – 4 disks)

What Would be Easy? n What if there were only one disk? Start PegEnd PegExtra Peg

More Disks? Start Peg (5)End Peg (5)Extra Peg (5) Get top disks “out of the way” (takes MANY moves) Start Peg (4)Extra Peg(4)End Peg (4)

Towers of Hanoi (5 disks) Start Peg (5)End Peg (5)Extra Peg (5) Move bottom disk (one move)

Towers of Hanoi (5 disks) Start Peg (5)End Peg (5)Extra Peg (5) Finish moving the top disks over to the end peg (MANY moves) Extra Peg (4)End Peg(4)Start Peg (4)

Towers of Hanoi (5 disks) Start PegEnd PegExtra Peg All done!

Towers of Hanoi (Version 1) n If there’s only one disk  move it to the end peg n Else  move the top disks out of the way »extra peg becomes end peg for one less disks  move the bottom disk to the end peg  move the top disks onto the end peg »start peg becomes extra peg

What’s Easier than One Disk? n With no disks, there’d be nothing to do!  with one disk: »move zero disks out of the way »move bottom disk to end peg »mover zero disks onto bottom disk  same as for more than one disk n Stop when n = 0 instead of when n = 1  when n = 0, do nothing

Towers of Hanoi (Simplified) // 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 if (n > 0) {// stop if n == 0 hanoi(n - 1, s, x, f);// smaller from start to extra hanoi(n - 1, s, x, f);// smaller from start to extra System.out.println("Move a disk from " + s + " to " + f + "."); System.out.println("Move a disk from " + s + " to " + f + "."); hanoi(n - 1, x, f, s);// smaller from extra to finish hanoi(n - 1, x, f, s);// smaller from extra to finish }}

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

Example Array Recursion n 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

Print Array in Reverse n 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 if (len > 0) {// stop if len == 0 System.out.print(arr[len - 1] + " "); System.out.print(arr[len - 1] + " "); printInReverse(arr, len - 1);// “smaller” array printInReverse(arr, len - 1);// “smaller” array }}

Another Example n 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

Exercise n 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 n Sometimes we want to be able to shorten the array at either end n Pass start and end points instead of length n Done when  lo > hi (for len==0), or  lo == hi (for len==1) n “Sub-Array processing”

Working From Both Ends n 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

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

Array Splitting n Sub-array processing can get rid of more than one element at a time n Binary split is a common method  do top “half” and bottom “half” separately  combine to get answer

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

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

Recursion with Linked Structures n Simple recursion  Values get smaller n Recursion with arrays  Array length gets smaller n Recursion with linked structures  look at first element  if necessary, look at the rest

Example Linked Recursion n To Print a Linked List in reverse  Base Case (Stop) » If the list is empty, do nothing  Recursive Case (Smaller) » Print the rest of the elements in reverse order » Then print the first element &100&3&-2&8&18&5/

Linked Structure Recursion n Print linked structure in reverse  give first node to the method private void printInReverse(Node first) { if (first != null) {// stop if list is empty! if (first != null) {// stop if list is empty! printInReverse(first.next); // smaller! printInReverse(first.next); // smaller! System.out.println(first.data + " "); System.out.println(first.data + " "); }}

Exercise n Write a recursive method to find whether a linked structure contains a given value  contains(8)  true  contains(15)  false  hint: contains(6): true or false? 6&100&3&-2&8&18&5/

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

Work for printInReverse (Array) n N is the “length” of the array (len) public static void printInReverse(int[] arr, int len) { if (len > 0) {// stop if len == 0 if (len > 0) {// stop if len == 0 System.out.print(arr[len - 1] + " "); System.out.print(arr[len - 1] + " "); printInReverse(arr, len - 1);// “smaller” array 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 n When W(n) defined in terms of W(n – 1)…  or some other smaller value than n n … it’s called a recurrence relation n It’s a recursive definition of W  has a base case (n = 0 or n = 1 or …)  has a recursive case

Solving by Inspection n 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) = 2N + 1?

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

Array Splitting public static int maximum(int[] arr, int lo, int hi) { if (lo == hi) {// 1 comparison if (lo == hi) {// 1 comparison return arr[lo];// 1 array access return arr[lo];// 1 array access } else { } else { int mid = lo + (hi - lo) / 2;// 3 math ops int mid = lo + (hi - lo) / 2;// 3 math ops int maxLo = maximum(arr, lo, mid);// 1 asgn + REC int maxLo = maximum(arr, lo, mid);// 1 asgn + REC int maxHi = maximum(arr, mid+1, hi);// 1 asgn + REC int maxHi = maximum(arr, mid+1, hi);// 1 asgn + REC return (maxLo > maxHi) ? maxLo : maxHi;// 1 comparison return (maxLo > maxHi) ? maxLo : maxHi;// 1 comparison }} W(1) = 2+W / +N W(2) = 6 + 2W(1) = 6 + 2(2) = / +18N – 6 W(4) = 6 + 2W(2) = 6 + 2(10) = / +28N – 6 W(8) = 6 + 2W(4) = 6 + 2(26) = / +48N – 6 W(16) = 6 + 2W(8) = 6 + 2(58) = / +88N – 6

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

Proof by Induction (Part 1) n Assume W(n) = 8n – 6 for n < 2N n What’s W(2N)?  W(2N) = 6 + 2W(N)  but N < 2N, so W(N) = 8N – 6…  … so W(2N) = 6 + 2(8N–6) = N – 12 = 16N – 6 = 8(2N) – 6  same formula works for all even N

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

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

Solve by Inspection n Work doubles at each step of N  sounds exponential  compare work with 2 N  2 N = W(N) + 1  so work = 2 N – 1  exponential NW(N)2N2N N2 N – 12N2N

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

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

Bad Recursion n Recursion often shorter than loops  especially when double recursion is needed »like in towers of Hanoi problem n Recursion usually same complexity as loops  but NOT ALWAYS  especially if you get double recursion

Fibonacci Recursion n Fibonacci uses double recursion  count number of additions required public static int fibonacci(int n) { if (n <= 1) return 1; if (n <= 1) return 1; else return fibonacci(n-1) + fibonacci(n-2); else return fibonacci(n-1) + fibonacci(n-2);} »W(0) = W(1) = 0 »W(2) = 1 + W(1) + W(0) = = 1+1 »W(3) = 1 + W(2) + W(1) = = 2+1 »W(4) = 1 + W(3) + W(2) = = 4+2 »W(5) = 1 + W(4) + W(3) = = 7+3 »W(6) = 1 + W(5) + W(4) = = 12+5

Too Complicated n Assume that the work is increasing  standard assumption n Thus W(N-1) > W(N-2) n Thus 1 + W(N-1) + W(N-2) > 1 + 2W(N-2) n So let’s solve that recurrence relation  work for fibonacci will be worse than that

Simplified Equations n Using W(N) = 1 + 2W(N-2) »W(0) = 0W(1) = 0 »W(2) = 1 + 2W(0) = 1 + 2(0) = 1W(3) = 1 + 2W(1) = 1 »W(4) = 1 + 2W(2) = 1 + 2(1) = 3W(5) = 1 + 2W(3) = 3 »W(6) = 1 + 2W(4) = 1 + 2(3) = 7W(7) = 1 + 2W(5) = 7 »W(8) = 1 + 2W(6) = 1 + 2(7) = 15W(9) = 1 + 2W(7) = 15 »W(10) = 1 + 2W(8) = 1 + 2(15) = 31…  similar to towers of Hanoi: W(N) = 2 N – 1  but here a bit smaller: W(N) = 2 N/2 – 1  but still exponential »and work for fibonacci is worse

Can We Do Better? n For Fibonacci? Yes! We can do O(N) int f1 = 1, f2 = 1; while (n > 1) { int f3 = f1 + f2; f1 = f2; f2 = f3; --n; } return f2; n For towers of Hanoi? No!  the number of moves required is exponential  there’s no way to do better than that

Questions