CS62 - Spring 2011 - Lecture 7
Admin
- How did the first assignment go?
- Lab today playing with timing
- read the lab before hand!
- you may work in pairs (we can pair you up once you get there if you'd like)
Look at
StopWatch code
- We'll be using this in the lab this week
- three private instance variables:
- startTime: the last time the start button was pressed
- accumulatedTime: the time accumulated between start and stops
- running: whether or not the stop watch is running
- what happens if we:
- start, stop, getTime?
- start, stop, start, stop, getTime?
- start, stop, start, getTime?
- start, reset, stop, getTime?
- start, stop, start, reset, stop, getTime?
- start, start, stop, getTime?
Proofs by induction
- what is a proof?
- A proof is a way providing a logical argument that a particular statement is true
- A proof by induction goes as follows:
- base case: we show that the argument holds for a very simple case (generally the smallest or one of the smallest possible situations/occurrences)
- inductive case: we assume that is holds for some intermediary point, then us this to show that the the next point in the sequence also holds
- how does this prove something?
- we showed the base case is true
- by induction, if the base case is true then the case after the base case is true
- by induction, the next case is then true,
- etc.
- For example, how would you prove that if you have a line of dominoes stacked up in a row, if you tip the first one over, they'll all fall over (you can assume you've lined them up appropriately)?
- base case: if we tip the first domino over, it will fall down
- inductive case: assume that the ith domino will fall down. prove that the i+1th domino (i.e. the next domino) will fall down
- we know by physics that this is true assuming they're close
- we've not proved that if you push down the first one, they'll all fall down
- what is the sum of the numbers 1 through n?
- prove that the sum of 1, 2, ..., n = (n+1)n/2
- what is the base case?
- n = 1
- 2*1/2 = 1
- verified
- what is the inductive case?
- assume 1, 2, ..., i-1 = i(i-1)/2
- show that it holds for i, i.e. 1, 2, ..., i = (i+1)i/2
1, 2, ..., i =
1, 2, ..., i-1, i =
i(i-1)/2 + i (by our inductive assumption) =
i(i-1)/2 + 2i/2 =
(i(i-1) + 2i)/2 =
(i^2 -i + 2i)/2 =
(i^2+i)/2 =
i(i+1)/2
- done!
- why does this work?
- we know it holds for 1
- because we know it holds for 1 and we know that if it holds for i-1 then it holds for i, then it holds for 2
- which means it holds for 3
- etc, etc up to n
Algorithms
- What is an algorithm?
- way for solving a problem
- method of steps to accomplish a task
- What are some algorithms you've heard of (or talked about)?
- "Algorithms" in computer science is the subfield interested in developing and analyzing algorithms
- Someone gives you an algorithm to accomplish a task. What are some of the questions you might want to ask/prove about it?
- is it correct? does it do what we expect it to do?
- what is its run-time?
- best, worst, average cases
- what are the memory/space costs?
- does it finish?
- can we do it faster?
- how difficult is it to implement?
- Last time you talked about big-O notation and algorithmic analysis
- Anybody know of algorithms that fall into these 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 firrst 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
Sorting
- Input: An array (or ArrayList) of numbers nums
- Output: the array of numbers such that nums[i] <= nums[j] for all i < j
- what approaches have people heard of?
- Let's start off looking at insertion sort:
- look at the code in
Sort code
for insertion sort
- how does it work?
- How could we prove something like this is correct?
- loop invariant: A statement about the algorithm that is always true, regardless of where we are in the algorithm
- Ideas for insertion sort?
- At the start of each iteration of the outer loop nums[0...j-1] is a sorted version of the original elements of nums[0...j-1]
- can we prove this?
- base case: invariant is true before we enter the loop
- inductive case: it is true after each iteration (by looking at the code)
- how does the loop invariant help us?
- when i = n (at the end, then the whole array is sorted)
- what is the running time?
- How many times do we iterate through the while loop?
- in the best case: no times
- when does this happen?
- what is the running time? linear, O(n)
- in the worst case: j - 1 times
- when does this happen?
- what is the running time?
- \sum_{j=1}^n-1 j = ((n-1)n)/2
- O(n^2)
- average case: (j-1)/2 times
- O(n^2)
- Now take a look at selection sort:
- how does it work?
- is it correct?
- loop invariant?
- running times?