week 6 - wednesday. what did we talk about last time? exam 1 post-mortem recursive running time

36
CS221 Week 6 - Wednesday

Upload: damian-casey

Post on 31-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time

CS221Week 6 - Wednesday

Page 2: Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time

Last time

What did we talk about last time? Exam 1 post-mortem Recursive running time

Page 3: Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time

Questions?

Page 4: Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time

Recursion

Assignment 3

Page 5: Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time

Infix to Postfix Converter

Project 2

Page 6: Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time

Wittman's Guide to Quick Debugging

1. a = b; a and b have to have the same type Exception: b is a subtype of a

2. == is a comparison operator that produces a boolean, = is a left-pointing arrow

3. if statements always have parentheses4. No semicolons after if or while headers (and rarely after for

headers)5. The following is always wrong:

x = y;x = z;

6. Know your String methods7. A non-void method has to return something8. The new keyword is always followed by a type and either

parentheses (possibly with arguments) or square brackets with integers inside

9. Local variables cannot be declared private, public, or protected

Page 7: Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time

Know your syntax

By now, you should know your syntax inside and out

If you don't know how something in Java works, find out!

Syntax is your basic tool for everything Read about some syntax once? Doesn't help!

Write some code to see it in practice!

Syntax

Problem

Solving

Design

CS121

CS122

CS221

Page 8: Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time

Exponentiation

We want to raise a number x to a power n, like so: xn

We allow x to be real, but n must be an integer greater than or equal to 0

Example: (4.5)13 = 310286355.9971923828125

Page 9: Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time

Recursion for Exponentiation

Base case (n = 0): Result = 1

Recursive case (n > 0): Result = x ∙ x(n – 1)

Page 10: Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time

Code for Exponentiation

public static double power( double x, int n )

{if( n == 0 )

return 1;else

return x * power( x, n – 1);}

Base Case

RecursiveCase

Page 11: Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time

Running time for power

Each call reduces n by 1n total calls What's the running time?

Θ(n)

Page 12: Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time

Can we do better?

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 xn = x ∙ x(n – 1)

But, it is also true that xn = x(n/2) ∙ x(n/2)

Page 13: Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time

New Recursion for Exponentiation

Assume that n is a power of 2 Base case (n = 1):

Result = x Recursive case (n > 1):

Result = (x(n/2))2

Page 14: Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time

Code for Better Exponentiation

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

RecursiveCase

Page 15: Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time

Running time for power2

Each call reduces n by half log2(n) total calls Just like binary search Can we expand the algorithm to

even and odd values of n?

Page 16: Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time

Even Newer Recursion for Exponentiation

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

Page 17: Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time

Code for Even Better Exponentiation

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

RecursiveCases

Page 18: Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time

Running time for power3

Each call reduces n by half (more or less) Θ(log2 n) total calls Does as well as power2() Better yet, we can use this solution to get

a logarithmic time answer for Fibonacci!

The nth term of the Fibonacci sequence is:

Where

Page 19: Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time

Merge Sort

Page 20: Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time

Merge Sort algorithm (recursive)

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 But how long does it take?

Page 21: Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time

Master Theorem

Page 22: Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time

Master Theorem

Has a great name… Allows us to determine the Big Theta

running time of many recursive functions that would otherwise be difficult to determine

Page 23: Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time

Basic form that recursion must take

where

a is the number of recursive calls made b is how much the quantity of data is

divided by each recursive call f(n) is the non-recursive work done at

each step

Page 24: Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time

Case 1

If for some constant , then

Page 25: Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time

Case 2

If for some constant , then

Page 26: Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time

Case 3

If for some constant , and if

for some constant and sufficiently large , then

Page 27: Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time

Stupid Sort

Page 28: Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time

Stupid Sort algorithm (recursive)

Base case: List has size less than 3 Recursive case:

Recursively sort the first 2/3 of the list Recursively sort the second 2/3 of the

list Recursively sort the first 2/3 of the list

again

Page 29: Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time

Let's code up Stupid Sort!

Page 30: Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time

But, how long does it take?

Page 31: Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time

Stupid Sort

We need to know logb aa = 3b = 3/2 = 1.5 Because I’m a nice guy, I’ll tell you

that the log1.5 3 is about 2.7

Page 32: Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time

Binary Search

We know that binary search takes O(log n) time

Can we use the Master Theorem to check that?

Page 33: Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time

Quiz

Page 34: Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time

Upcoming

Page 35: Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time

Next time…

More on the Master theorem Symbol tables Read Chapter 3.1

Page 36: Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time

Reminders

Finish Assignment 3 Due Friday, October 2, 2015

Keep working on Project 2 Due Friday, October 9, 2015