quickest quickest
play

Quickest Quickest Exam 1 Extra Credit: Exam 1 Extra Credit: either - PDF document

Shuttle Rescue Mission Shuttle Rescue Mission PS4 Written can be turned in during Monday Feb 23 and Wednesday Feb 25 Monday Feb 23 and Wednesday Feb 25 structured office hours. MEC 205 until 5:30pm MEC 205 until 5:30pm


  1. Shuttle Rescue Mission Shuttle Rescue Mission PS4 Written can be turned in during • Monday Feb 23 and Wednesday Feb 25 Monday Feb 23 and Wednesday Feb 25 structured office hours. • MEC 205 until 5:30pm MEC 205 until 5:30pm • http://shuttle.cs.virginia.edu:8080/ http://shuttle.cs.virginia.edu:8080/ – Build and program Lego Mindstorms robot to Build and program Lego Mindstorms robot to remotely sense and navigate a barren environment remotely sense and navigate a barren environment and retrieve a life pod from a crater. and retrieve a life pod from a crater. Quickest Quickest • Exam 1 Extra Credit: Exam 1 Extra Credit: either either show up and watch show up and watch Sorting Sorting one day or or write paragraph about how to do it write paragraph about how to do it one day and and Double Deltas Double Deltas #2 One-Slide Summary Outline • Insert-sort is Θ ( n 2 ) worst case (reverse list), but is • Insert-sort Θ ( n ) best case (sorted list). Pick Up Graded • Going half-sies • A recursive function that divides its input in half each Problem Sets During Structure Hours • Sorted binary trees time is often in Θ (log n ). Today! • If we could divide our input list in half rapidly, we • Quicker-sort could do a quicker sort : Θ ( n log n ). • WWII Codebreaking • Sorted binary trees are an efficient data structure for maintaining sorted sets. • British codebreakers used cribs (guesses), brute force, and analysis to break the Lorenz cipher. Guessed wheel settings were likely to be correct if they resulted in a message with the right linguistic properties for German (e.g., repeated letters). #3 #4 How much work is insert-sort? Which is better? (define ( insert-sort lst cf) (if (null? lst) null • Is insert-sort faster than best-first-sort? (insert-one (car lst) (insert-sort (cdr lst) cf) cf))) (define ( insert-one el lst cf) (if (null? lst) (list el) (if (cf el (car lst)) (cons el lst) (cons (car lst) (insert-one el (cdr lst) cf))))) How many times does insert- running time of insert- sort evaluate insert-one? one is in Θ ( n ) n times (once for each element) insert-sort has running time in Θ ( n 2 ) where n is the number of elements in the input list #5 #6

  2. > (insert-sort < (revintsto 20)) (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20) > (best-first-sort < (intsto 20)) Requires 190 applications of < (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20) Requires 210 applications of < > (insert-sort < (intsto 20)) (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20) Requires 19 applications of < > (best-first-sort < (rand-int-list 20)) (4 4 16 18 19 20 23 32 36 51 53 59 67 69 73 75 82 82 88 > (insert-sort < (rand-int-list 20)) 89) (0 11 16 19 23 26 31 32 32 34 42 45 53 63 64 81 82 84 84 92) Requires 210 applications of < Requires 104 applications of < #7 #8 best-first-sort vs. insert-sort Can we do better? • Both are Θ ( n 2 ) worst case (reverse list) (quicker-insert < 88 (list 1 2 3 5 6 23 63 77 89 90)) • Both are Θ ( n 2 ) when sorting a randomly ordered list Suppose we had procedures – But insert-sort is about twice as fast (first-half lst) • insert-sort is Θ ( n ) best case (ordered (second-half lst) that quickly divided the list in two halves? input list) #9 #10 Evaluating quicker-sort quicker-insert using halves > (quicker-insert < 3 (list 1 2 4 5 7)) |(quicker-insert #<procedure:traced-<> 3 (1 2 4 5 7)) | (< 3 1) (define ( quicker-insert el lst cf) (define ( quicker-insert el lst cf) | #f (if (null? lst) (list el) | (< 3 5) (if (null? lst) (list el) ;; just like insert-one (if (null? (cdr lst)) | #t | (quicker-insert #<procedure:traced-<> 3 (1 2 4)) (if (cf el (car lst)) (if (null? (cdr lst)) | |(< 3 1) (cons el lst) | |#f (list (car lst) el)) (if (cf el (car lst)) (cons el lst) (list (car lst) el)) | |(< 3 4) (let ((front (first-half lst)) | |#t (let ( (front (first-half lst)) (back (second-half lst))) | |(quicker-insert #<procedure:traced-<> 3 (1 2)) (if (cf el (car back)) | | (< 3 1) (back (second-half lst)) ) (append (quicker-insert el front cf) back) | | #f (append front | | (< 3 2) (if (cf el (car back)) (quicker-insert el back cf))))))) | | #f | | (quicker-insert #<procedure:traced-<> 3 (2)) (append (quicker-insert el front cf) back) | | |(< 3 2) | | |#f (append front | | (2 3) Every time we call quicker- | |(1 2 3) (quicker-insert el back cf))))))) insert, the length of the list is | (1 2 3 4) |(1 2 3 4 5 7) approximately halved ! (1 2 3 4 5 7) #11 #12

  3. Liberal Arts Trivia: How much work is quicker-sort? • The argan tree, found (define ( quicker-insert el lst cf) Each time we call primarily in Morocco, has a (if (null? lst) (list el) quicker-insert, the size of (if (null? (cdr lst)) (if (cf el (car lst)) knobby, twisted trunk that lst halves. So doubling (cons el lst) the size of the list only (list (car lst) el)) allows these animals to climb (let ((front (first-half lst)) increases the number of (back (second-half lst))) it easily. The animals eat the calls by 1. (if (cf el (car back)) (append (quicker-insert el front cf) back) fruit, which has an (append front (quicker-insert el back cf))))))) indigestible nut inside, which List Size # quicker-insert applications is collected by farmers and 1 1 2 2 used to make argan oil: handy 4 3 8 4 in cooking and cosmetics, but 16 5 pricey at $45 per 500 ml. #13 #14 Liberal Arts Trivia: Remembering Logarithms Scandinavian Studies • This capital of and largest city in Denmark is log b n = x means b x = n situated on the islands of Zealand and Amager. It is the birthplace of Neils Bohr, Søren Kierkegaard, and Victor Borge. The city's origin What is log 2 1024 ? as a harbor and a place of commerce is What is log 10 1024 ? reflected in its name. Its original designation, from which the contemporary Danish name is Is log 10 n in Θ (log 2 n ) ? derived, was Køpmannæhafn, "merchants' harbor". The English name for the city is derived from its (similar) Low German name. #15 #16 Changing Bases Number of Applications Assuming the list is well-balanced, the number of applications of log b n = (1/log k b ) log k n quicker-insert is in Θ (log n ) where n is the number of elements in the If k and b are constants, input list. this is constant Θ (log 2 n ) ≡ Θ (log 10 n ) ≡ Θ (log n ) No need to include a constant base within asymptotic operators. #17 #18

  4. quicker-sort ? Orders of Growth 14000 (define ( quicker-insert el lst cf) (define ( quicker-sort lst cf) (if (null? lst) (list el) n 2 (if (null? (cdr lst)) 12000 (if (null? lst) null (if (cf el (car lst)) (quicker-insert (cons el lst) (car lst) 10000 (list (car lst) el)) (quicker-sort (cdr lst) cf) (let ((front (first-half lst)) (back (second-half lst))) cf))) 8000 (if (cf el (car back)) (append (quicker-insert el front cf) back) (append front 6000 (quicker-insert el back cf))))))) 4000 quicker-sort using halves would have running time in 2000 Θ ( n log n ) if we have first-half, second-half, and n log n append procedures that run in constant time 0 1 9 17 25 33 41 49 57 65 73 81 89 97 105 #19 #20 Is there a fast first-half procedure? Making it faster We need to either: • No! (at least not on lists) 1. Reduce the number of applications of • To produce the first half of a list length n , insert-one in insert-sort we need to cdr down the first n /2 Impossible – need to consider each element elements 3. Reduce the number of applications of • So, first-half on lists has running time in quicker-insert in quicker-insert Θ ( n ) Unlikely… each application already halves the list 5. Reduce the time for each application of quicker-insert Need to make first-half, second-half and append faster than Θ ( n ) #21 #22 Sorted Binary Trees el left right A tree containing A tree containing all elements x such all elements x such that (cf x el) is true that (cf x el) is false #23 #24

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend