Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 6 - Monday.  What did we talk about last time?  Exam 1!  Before that:  Recursion.

Similar presentations


Presentation on theme: "Week 6 - Monday.  What did we talk about last time?  Exam 1!  Before that:  Recursion."— Presentation transcript:

1 Week 6 - Monday

2  What did we talk about last time?  Exam 1!  Before that:  Recursion

3

4 Recursion

5 Infix to Postfix Converter

6

7

8  Recursion is a great technique  One of its strengths is in writing concise code to solve a problem  Some recursive solutions are very efficient  Some are not  It pays to be aware of both

9  Find the sum of the integers 1 through n  Example: n = 8  sum(8) = 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1  sum(8) = 36

10  Base case (n = 1):   Recursive case (n > 1): 

11 public static int sum( int n ) { if( n == 1 ) return 1; else return n + sum( n – 1 ); } Base Case Recursive Case

12  Recursive summing takes linear time (summing n takes n function calls)  Is there another way to find this sum?  Closed form equation   Constant time!  Remember the story of young Gauss

13  The sequence: 1 1 2 3 5 8 13 21 34 55…  Studied by Leonardo of Pisa to model the growth of rabbit populations

14  Find the n th term of the Fibonacci sequence  Simple approach of summing two previous terms together  Example: n = 7  1 1 2 3 5 8 13 1 2 3 4 5 6 7

15  Base cases (n = 1 and n = 2):  Result = 1  Recursive case (n > 2):  Result = fibonacci(n – 1) + fibonacci(n – 2)

16 public static int fib( int n ) { if( n <= 2 ) return 1; else return fib(n – 1) + fib(n – 2); } Base Case Recursive Case

17  Example: fib(6) fib(6) fib(4) fib(5) fib(4) fib(3) fib(2) fib(1) fib(2) fib(1) fib(3) fib(2) fib(1) Uh oh.

18  For most cases, calling fib() makes calls two more calls to fib(), which each make two more calls to fib(), and so on…  Many values are redundantly computed  The final running time is O(2 n/2 )

19  The recursion is fine from a mathematical perspective  We just need to avoid recomputing lower terms in the sequence  We can use the idea of carrying along both the (n – 1) term and the (n – 2) term in each recursive step

20 public static int fib2( int a, int b, int n ) { if( n <= 2 ) return b; else return fib2(b, a + b, n - 1); } //proxy method int fib( int n ) { return fib2(1, 1, n); } Base Case Recursive Case

21  We want to raise a number x to a power n, like so: x n  We allow x to be real, but n must be an integer greater than or equal to 0  Example: (4.5) 13 = 310286355.9971923828125

22  Base case (n = 0):  Result = 1  Recursive case (n > 0):  Result = x ∙ x (n – 1)

23 public static double power( double x, int n ) { if( n == 0 ) return 1; else return x * power( x, n – 1); } Base Case Recursive Case

24  Each call reduces n by 1  n total calls  What's the running time?  O(n)

25  We need to structure the recursion differently  Instead of reducing n by 1 each time, can we reduce it by a lot more?  It’s true that x n = x ∙ x (n – 1)  But, it is also true that x n = x (n/2) ∙ x (n/2)

26  Assume that n is a power of 2  Base case (n = 1):  Result = x  Recursive case (n > 1):  Result = (x (n/2) ) 2

27 public static double power2( double x, int n ) { double temp; if( n == 1 ) return x; else { temp = power2( x, n/2 ); return temp * temp; } } Base Case Recursive Case

28  Each call reduces n by half  log 2 (n) total calls  Just like binary search  Can we expand the algorithm to even and odd values of n?

29  Base case (n = 1):  Result = x  Recursive cases (n > 1):  If n is even, result = (x (n/2) ) 2  If n is odd, result = x ∙ (x ((n – 1)/2) ) 2

30 public static double power3( double x, int n ) { double temp; if( n == 1 ) return x; else if( n % 2 == 0 ) { temp = power3( x, n/2 ); return temp * temp; } else { temp = power3( x, (n – 1)/2 ); return x * temp * temp; } } Base Case Recursive Cases

31

32

33  Beautiful divide and conquer  Base case: List has size 1  Recursive case:  Divide your list in half  Recursively merge sort each half  Merge the two halves back together in sorted order

34  Great. Now, how long does it take?

35

36  Recursive running time  Master Theorem

37  Keep working on Project 2  Keep working on Assignment 3


Download ppt "Week 6 - Monday.  What did we talk about last time?  Exam 1!  Before that:  Recursion."

Similar presentations


Ads by Google