quickest
play

Quickest Sorted binary trees are an efficient data structure - PDF document

One-Slide Summary Guest Lecture Insert-sort is ( n 2 ) worst case (reverse list), but is by Kinga Dobolyi ( n ) best case (sorted list). A recursive function that divides its input in half each time is often in (log n ). If


  1. One-Slide Summary Guest Lecture • Insert-sort is Θ ( n 2 ) worst case (reverse list), but is by Kinga Dobolyi Θ ( n ) best case (sorted list). • A recursive function that divides its input in half each time is often in Θ (log n ). • If we could divide our input list in half rapidly, we could do a quicker sort : Θ ( n log n ). Quickest • Sorted binary trees are an efficient data structure Quickest for maintaining sorted sets. Sorting Sorting • British codebreakers used cribs (guesses), brute and force, and analysis to break the Lorenz cipher. and Guessed wheel settings were likely to be correct if Double Deltas Double Deltas they resulted in a message with the right linguistic properties for German (e.g., repeated letters). #2 How much work is insert-sort? Outline • Insert-sort (define ( insert-sort lst cf) (if (null? lst) null Pick Up Graded • Going half-sies Problem Sets! (insert-one (car lst) (insert-sort (cdr lst) cf) cf))) • Sorted binary trees There is a “holding fee”. (define ( insert-one el lst cf) Web page has a map. (if (null? lst) (list el) • Quicker-sort (if (cf el (car lst)) (cons el lst) (cons (car lst) (insert-one el (cdr lst) cf))))) • WWII Codebreaking 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 #3 #4 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) #5 #6

  2. 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) #7 #8 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. #9 #10 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. #11 #12

  3. 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. #13 #14 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)) (if (null? lst) null 12000 (if (cf el (car lst)) (quicker-insert (cons el lst) (car lst) (list (car lst) el)) 10000 (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 #15 #16 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 2. 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 3. Reduce the time for each application of quicker-insert Need to make first-half, second-half and append faster than Θ ( n ) #17 #18

  4. 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 #19 #20 Tree Example Tree Example cf is < cf is < 5 5 Where would we put 3? 3 2 8 2 8 null null 4 7 4 7 1 1 null null null null null null null null null null null null #21 #22 Representing Trees Representing Trees 5 (define ( make-tree left el right) left and right are trees (cons el (cons left right)) ( null is a tree) 2 8 (define ( tree-element tree) tree must be a non-null tree (car tree)) 1 (define ( tree-left tree) tree must be a non-null tree (car (cdr tree))) (make-tree (make-tree (make-tree null 1 null) 2 (define ( tree-right tree) null) (cdr (cdr tree))) 5 tree must be a non-null tree (make-tree null 8 null)) #23 #24

  5. insert-one-tree How much work is insert-one-tree? (define ( insert-one-tree cf el tree) Each time we call (define ( insert-one-tree cf el tree) (if (null? tree) insert-one-tree, the size (if (null? tree) (make-tree null el null) If the tree is null, make a new tree of the tree approximately (if (cf el (get-element tree)) (make-tree null el null) with el as its element and no left or right trees. (make-tree halves (if it is well (if (cf el (get-element tree)) (insert-one-tree cf el (get-left tree)) balanced). (make-tree (get-element tree) (get-right tree)) (insert-one-tree cf el (get-left tree)) (make-tree (get-left tree) Otherwise, decide Each application is (get-element tree) (get-element tree) if el should be in constant time. (insert-one-tree cf el (get-right tree)))))) (get-right tree)) the left or right subtree. insert it into that (make-tree subtree, but leave the (get-left tree) other subtree unchanged. The running time of insert-one-tree is in (get-element tree) Θ (log n ) where n is the number of elements in (insert-one-tree cf el (get-right tree)))))) the input tree, which must be well-balanced. #25 #26 Lorenz Lorenz quicker-insert-one Cipher Cipher Machine Machine (define ( quicker-insert-one cf lst) (if (null? lst) null (insert-one-tree cf (car lst) (quicker-insert-one cf (cdr lst))))) No change (other than using insert-one-tree)…but evaluates to a tree not a list! (((() 1 ()) 2 ()) 5 (() 8 ())) #27 #28 Liberal Arts Trivia: Classics Liberal Arts Trivia: Chemistry • This violet variety of • This ancient Greek epic poem, traditionally quartz, often used in attributed to Homer, is widely believed to be jewelry, takes its name the oldest extant work of Western literature. from the ancient Greek (a It describes the events of the final year of the ("not") and methustos Trojan War. The plot follows Achilles and his ("intoxicated")), a reference anger at Agamemnon, king of Mycenae. It is to the belief that it written in dactylic hexameter and comprises protected its own from 15,693 lines of verse. It begins: drunkenness; ancient – μ νιν ειδε θε Πηληϊάδεω χιλ ος ῆ ἄ ὰ Ἀ ῆ Greeks and Romans made drinking vessels of it to – ο λομένην, μυρί' χαιο ς λγε' θηκεν ὐ ἣ Ἀ ῖ ἄ ἔ prevent intoxication. #29 #30

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