CS201 - Spring 2014 - Class 12
Quiz
IOException
- When you open a FileReader(i.e. BufferedReader) it can throw a FileNotFoundException
- When you read from a BufferedReader it can throw an IOException, which is a type Exception
- I've only caught the IOException. Why is Java letting me do this?
- IOException is a superclass of FileNotFoundException!
Exception hierarchy
- There are a lot of Exceptions!
- All Exceptions, inherit from the class Exception
- it's kind of like Object for Exceptions (though remember that Exceptions are classes and actually do inherit from Object)
- If you put:
catch(Exception e){
}
- you will catch *ALL* exceptions that are thrown!
- however, it's better style to specifically list the Exception you're trying to catch
look at the sum method in
AsymptoticsExamples code
- How long will it take to if we pass it an array with 1000 numbers in it? 10,000 numbers?
- We don't know!
- We could time it and find out
- Even if you time it, can you say conclusively how long it will take?
- No!
- Variation from run to run
- Depends on the computer
- etc.
- If I tell you the time it took on 10,000 numbers was x. Could you tell me approximately how long it would take on 20,000 numbers?
- would take about twice as long
- why?
- does a linear pass through the data
- doubling the size of the data, means about twice as much work
Asymptotics
- Precisely calculating the actual cost of a method is tedious and not generally useful
- Different operations take different amounts of time. Even from run to run, things such as caching, etc. will complicate things
- Want to identify categories of algorithmic runtimes
- What we really want to do is compare different algorithms
- Want to know which algorithms will be much worse (or much better) than others
Big O
- We write that an algorithms run-time is O(g(n)) if the run-time can be bounded as n gets larger by some constant times g(n)
- For example, sum is O(n), i.e. linear
look at the lastElement and sumProduct methodd in
AsymptoticsExamples code
- What do they do?
- What are their Big O running times?
- Put another way, how does their run-time grow as you increase the size of the input?
- lastElement
- O(1), aka constant
- no matter how large the array is (assuming .length is a constant time operation) it always does the same amount of work
- sumProduct
- O(n^2), aka quadratic
- for each element, it must do a linear amount of work
How does Big-O notation allow us to ignore irrelevant details?
- look at the doubleSum method in
AsymptoticsExamples code
- What is the Big O runtime of this method?
- O(n)
- How does it's runtime compare to that of sum?
- twice as long (calls sum twice)
- Even though doubleSum is twice as slow as sum, they're still in the same category since they will roughly grow at the same rate
Show running time table (see pg. 2 of Big-O notes from algorithms:
http://www.cs.middlebury.edu/~dkauchak/classes/algorithms/lectures/2_BigO.pdf
)
look at search1 method in
BinarySearchExamples code
- what does this method do?
- what is the running time, using Big-O notation?
- depends! sometimes an method/approach always has the same running time
- look at search1a method in
BinarySearchExamples code
- In general, we'll talk about three different things for a method:
- best case running time
- worst case running time
- average case running time
- what are the best, worst and average case running times
- best: O(1), constant, when the example is the first element
- worse: O(n), linear, when the example is the last element
- average: O(n), linear, on average, we'll need to traverse half of the elements (~n/2) which is still O(n)
look at search2 method in
BinarySearchExamples code
- what does this method do?
- uses a helper method
- does the same thing as the previous, but using recursion
- which version is better?
- what is the running time, using Big-O notation?
- same as for the iterative version
can we do better than either of these procedures?
- without any preprocessing of the data, no
number guessing game
what if I told you the data was in sorted order?
- how do you find information in a phonebook (where the data is in essence, sorted)?
show binarySearch method in
BinarySearchExamples code
- what does the code do?
- keeps a low and a high value
- we know that if findMe is in the array, then nums[low] <= findMe <= nums[high]
- picks the middle element between low and high
- compares that middle element to our value and then either finds the data or DISCARDS HALF OF THE REMAINING DATA
- an example
- 1, 3, 7, 15, 16, 18, 21, 40, 45, 50
- what's the running-time?
- best case: O(1) it's the midpoint in the ArrayList
- worst case: not found
- how many times do we iterate through the while loop?
- let's consider the case where the number of elements is a power of 2
- we could always pad it up do the next largest power of 2
- at each iteration we throw away half of the data, n/2, n/4, n/8
- when will it be done?
- when n/2^i = 1
log n/2^i = log 1
log n - log 2^i = 0
log n - 2 log i = 0
log n = 2 log i
i = log_2 n
- runtime is O(log_2 n)
- average case: half as many iterations through the while loop... still O(log_2 n)
how would we write a recursive version?
- we'd need a helper method
- rather than keeping low and high as variables, they'll be parameters
- then, rather than adjust them, we just call recursively call our method with smaller values
show binarySearchRecursive in
BinarySearchExamples code
why is binary search useful? The cost to sort data is O(n log n), which is more expensive than just the linear search!
Last comments on binary search
- It‚ is easy to get indices wrong, so be careful!
- "Although the basic idea of binary search is comparatively straightforward, the details can be surprisingly tricky... Professor Donald Knuth (taken from
http://en.wikipedia.org/wiki/Binary_search_algorithm
)
-
http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html
- How hard is it?
http://portal.acm.org/citation.cfm?doid=358476.358484,
only accessible on campus
- What if we wanted to return the first one in the list?
methods in the different Big-O categories:
- O(1), constant time: regardless of the size of the input, there is a fixed amount of
work
- add two 32 bit numbers
- determine if a number is even or odd
- sum the first 20 elements of an array
- O(log n) - logarithmic: at each iteration of the algorithm, discard some proportion of
the input (often half)
- O(n) - linear: do some constant amount of work on each element of the input
- find an item in an Vector (or ArrayList)
- determine the largest element in the array
- O(n log n): divide and conquer algorithms with a linear amount of work to
recombine
- sorting a list of numbers (with some algorithms)
- FFT
- O(n^2): double nested loops that iterate over the data
- matrix addition
- some other sorting algorithms
- O(2^n)
- Enumerate all possible subsets
- Traveling salesman using dynamic programming
- O(n!)
- Enumerate all permutation
- determinant of a matrix with expansion by minors