Class 17
Final Analysis Review of a few things about types The truth about lambda Set equality and equality testing
Class 17 Final Analysis Review of a few things about types The - - PowerPoint PPT Presentation
Class 17 Final Analysis Review of a few things about types The truth about lambda Set equality and equality testing Revisit big O Main ideas Two more useful theorems Example of a complete analysis of a procedure or two Big-O, take
Final Analysis Review of a few things about types The truth about lambda Set equality and equality testing
simpler for us,
numbers with the property that for , we have
denotes the set of all functions that are eventually less than , up to constants.
If and the function satisfies a recurrence
. NB: Also works if
If and the function satisfies
.
Proof: very similar NB: also works for <, as in theorem 1.
Big-O categories are what computer scientists use to speak about algorithms
" is shorthand for "the operation-counting function for the merge-sort algorithm is an element of the set "
, then
we can prove is, say, exponential time
here linear-time? Constant time? Quadratic time?"
If and then there are numbers with the property that for all natural numbers , we have
having tells us , so can't we just choose ?
, then ?
, in green
, in green
than , in blue
less than , for some .
How much?
If and then there are numbers with the property that for all natural numbers , we have Proof:
with the property that for , we have , hence . Let
, where is the first integer greater than . Let be one more than the largest of these numbers. Then for , we have
, we have So for all natural numbers , we have as claimed!
If and
with the property that for all natural numbers , we have
Essentially the same.
π. Then π·: β β β+, because the number of operations is always positive.
boolean, etc., are all unit-time operations.
in the recursive call in line 4, is some other small constant, for similar reasons; the time taken in the recursive call is no more than π·(π β 1), because the recursive argument has length π β 1, and π·(n β 1) represents the greatest possible number of operations on such a list.
π. Then π·: β β β+, because the number of operations is always positive.
Let be the largest number of operations involved in evaluating
(member? x alod) on any list alod of length ," then
β+, because the number of operations is always positive.
boolean, etc., are all unit-time operations.
the recursive call in line 4, is some other small constant, for similar reasons, plus the time taken by member? on a list of length n-1. The time taken in the recursive call is no more than S(π β 1), because the recursive argument has length π β 1, and S(n β 1) represents the greatest possible number of operations
1. Let S(π) be the largest number of operations involved in evaluating set? on any list of length π. Then π: β β β+, because the number of operations is always positive. 2. π 0 = π΅ > 0 3. π π < πΆ + π π β 1 + π(π β 1), where πΆ > 0, and π is the operation-counting function for member? , and we know that π β π π β¦ π . 4. Big-O theorem 3 tells us there are numbers π·, πΈ > 0 such that π π < π·π + πΈ for all π, so π π β 1 < π· π β 1 + D 5. π π < πΆ + π· π β 1 + πΈ + π π β 1 = πΆ + πΈ β π· + π·π + π π β 1 < πΆ + πΈ + π·π + π(π β 1) 6. Note that (B+D) and C are both positive, so big-O theorem 2 applies! 7. Applying big O theorem 2, we see that π β π π β¦ π QED
(define (set-equal? b c) (and (subset? b c) (subset? c b)))
(define (subset? b c) (foldr (lambda (item result) (and (member? item c) result)) true b))
"equality" is defined
means "produces same result given same input(s)"
(let ((len (lambda (alod) (cond [(empty? alod) 0] [(cons? alod) (+ 1 (len (rest alod)))])))) (len (list 1 2 3)))
(letrec ((len (lambda (alod) (cond [(empty? alod) 0] [(cons? alod) (+ 1 (len (rest alod)))])))) (len (list 1 2 3))) => 3
"bindings" in the current environment except those in the top-level- environment
(define (incrementer b) (lambda (x) (+ x b)) (define (add2 x) (incrementer 2)) (add2 5)
Without the "local environment", we'd get "b undefined" error! This will be, for many of you, the single hardest part of the next project.