Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Algorithms and Data Structures: Insert-Sort, Sorted Binary Trees, and Codebreaking - Prof., Study notes of Computer Science

Various topics in computer science, including insert-sort algorithm analysis, sorted binary trees, and codebreaking techniques used during world war ii. Explanations, examples, and code snippets. Insert-sort has a worst-case time complexity of θ(n²), but a best-case time complexity of θ(n). Sorted binary trees are an efficient data structure for maintaining sorted sets. Codebreaking techniques discussed include the use of cribs and the 'two time' pad. The document also touches upon the history of the pulitzer prize and lorenz wheels.

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-r6z
koofers-user-r6z 🇺🇸

10 documents

1 / 8

Toggle sidebar

Related documents


Partial preview of the text

Download Algorithms and Data Structures: Insert-Sort, Sorted Binary Trees, and Codebreaking - Prof. and more Study notes Computer Science in PDF only on Docsity! Quickest Sorting and Double Deltas PS4 Written can be turned in during structured office hours. #2 Shuttle Rescue Mission • Monday Feb 23 and Wednesday Feb 25 • MEC 205 until 5:30pm • http://shuttle.cs.virginia.edu:8080/ – Build and program Lego Mindstorms robot to remotely sense and navigate a barren environment and retrieve a life pod from a crater. • Exam 1 Extra Credit: either show up and watch one day or write paragraph about how to do it #3 One-Slide Summary • Insert-sort is Θ(n2) worst case (reverse list), but is Θ(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: Θ(nlog n). • 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). #4 Outline • Insert-sort • Going half-sies • Sorted binary trees • Quicker-sort • WWII Codebreaking Pick Up Graded Problem Sets During Structure Hours Today! #5 How much work is insert-sort? running time of insert- one is in Θ(n) How many times does insert- sort evaluate insert-one? n times (once for each element) insert-sort has running time in Θ(n2) where n is the number of elements in the input list (define (insert-sort lst cf) (if (null? lst) null (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))))) #6 Which is better? • Is insert-sort faster than best-first-sort? #7 > (insert-sort < (revintsto 20)) (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20) Requires 190 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 < > (insert-sort < (rand-int-list 20)) (0 11 16 19 23 26 31 32 32 34 42 45 53 63 64 81 82 84 84 92) Requires 104 applications of < #8 > (best-first-sort < (intsto 20)) (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20) Requires 210 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 89) Requires 210 applications of < #9 best-first-sort vs. insert-sort • Both are Θ(n2) worst case (reverse list) • Both are Θ(n2) when sorting a randomly ordered list – But insert-sort is about twice as fast • insert-sort is Θ(n) best case (ordered input list) #10 Can we do better? (quicker-insert < 88 (list 1 2 3 5 6 23 63 77 89 90)) Suppose we had procedures (first-half lst) (second-half lst) that quickly divided the list in two halves? #11 quicker-insert using halves (define (quicker-insert el lst cf) (if (null? lst) (list el) ;; just like insert-one (if (null? (cdr lst)) (if (cf el (car lst)) (cons el lst) (list (car lst) el)) (let ((front (first-half lst)) (back (second-half lst))) (if (cf el (car back)) (append (quicker-insert el front cf) back) (append front (quicker-insert el back cf))))))) #12 Evaluating quicker-sort > (quicker-insert < 3 (list 1 2 4 5 7)) |(quicker-insert #<procedure:traced-<> 3 (1 2 4 5 7)) | (< 3 1) | #f | (< 3 5) | #t | (quicker-insert #<procedure:traced-<> 3 (1 2 4)) | |(< 3 1) | |#f | |(< 3 4) | |#t | |(quicker-insert #<procedure:traced-<> 3 (1 2)) | | (< 3 1) | | #f | | (< 3 2) | | #f | | (quicker-insert #<procedure:traced-<> 3 (2)) | | |(< 3 2) | | |#f | | (2 3) | |(1 2 3) | (1 2 3 4) |(1 2 3 4 5 7) (1 2 3 4 5 7) Every time we call quicker- insert, the length of the list is approximately halved! (define (quicker-insert el lst cf) (if (null? lst) (list el) (if (null? (cdr lst)) (if (cf el (car lst)) (cons el lst) (list (car lst) el)) (let ((front (first-half lst)) (back (second-half lst))) (if (cf el (car back)) (append (quicker-insert el front cf) back) (append front (quicker-insert el back cf))))))) #25 Tree Example 5 2 8 741 cf is < null nullnull null null null null #26 Tree Example 5 2 8 741 3 cf is < null nullnull null null null null Where would we put 3? #27 Representing Trees (define (make-tree left el right) (cons el (cons left right)) (define (tree-element tree) (car tree)) (define (tree-left tree) (car (cdr tree))) (define (tree-right tree) (cdr (cdr tree))) left and right are trees (null is a tree) tree must be a non-null tree tree must be a non-null tree tree must be a non-null tree #28 Representing Trees 5 2 8 1 (make-tree (make-tree (make-tree null 1 null) 2 null) 5 (make-tree null 8 null)) #29 insert-one-tree (define (insert-one-tree cf el tree) (if (null? tree) (make-tree null el null) (if (cf el (get-element tree)) (make-tree (insert-one-tree cf el (get-left tree)) (get-element tree) (get-right tree)) (make-tree (get-left tree) (get-element tree) (insert-one-tree cf el (get-right tree)))))) If the tree is null, make a new tree with el as its element and no left or right trees. Otherwise, decide if el should be in the left or right subtree. insert it into that subtree, but leave the other subtree unchanged. #30 How much work is insert-one-tree? (define (insert-one-tree cf el tree) (if (null? tree) (make-tree null el null) (if (cf el (get-element tree)) (make-tree (insertel-tree cf el (get-left tree)) (get-element tree) (get-right tree)) (make-tree (get-left tree) (get-element tree) (insertel-tree cf el (get-right tree)))))) Each time we call insert-one-tree, the size of the tree approximately halves (if it is well balanced). Each application is constant time. The running time of insertel-tree is in Θ (log n) where n is the number of elements in the input tree, which must be well-balanced. #31 quicker-insert-one (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 ())) #32 Lorenz Cipher Machine #33 Liberal Arts Trivia: Classics • This ancient Greek epic poem, traditionally attributed to Homer, is widely believed to be the oldest extant work of Western literature. It describes the events of the final year of the Trojan War. The plot follows Achilles and his anger at Agamemnon, king of Mycenae. It is written in dactylic hexameter and comprises 15,693 lines of verse. It begins: – μ νιν ειδε θε Πηληϊάδεω χιλ οςῆ ἄ ὰ Ἀ ῆ – ο λομένην, μυρί' χαιο ς λγε' θηκενὐ ἣ Ἀ ῖ ἄ ἔ #34 Liberal Arts Trivia: Literature • Name the author of the Age of Innocence (1920). The novel describes the upper class in New York city in the 1870s and questions the mores and assumptions of society. The title is an ironic comment on the polished outward manners of New York society, when compared to its inward machinations. The authors was the first woman to win the Pulitzer Prize for Literature. #35 Lorenz Wheels 12 wheels 501 pins total (set to control wheels) Work to break in Θ(pw) so real Lorenz is 4112/53 ~ 1 quintillion (1018) times harder! #36 Code Breaking Intuition • Suppose we are using a simple letter substitution cipher (i.e., replace every A with Q, etc.) • You intercept these two messages: – pf150: Pbzchgre Fpvrapr sebz Nqn naq Rhpyvq gb Dhnaghz Pbzchgvat naq gur Jbeyq Jvqr Jro. – pf150: Pbzchgre Fpvrapr sebz Nqn gb gur Jbeyq Jvqr Jro. • What does the first one say? What hints did you have? #37 Breaking Fish • Gov't Communications HQ learned about first Fish link (Tunny) in May 1941 – British codebreakers used “Fish” to refer to German teleprinter traffic – Intercepted unencrypted Baudot-encoded test messages • August 30, 1941: Big Break! – Operator retransmits failed message with same starting configuration – Gets lazy and uses some abbreviations, makes some mistakes • SPRUCHNUMMER/SPRUCHNR (Serial Number) #38 “Two Time” Pad • Allies have intercepted: C1 = M1 ⊕ K1 C2 = M2 ⊕ K1 Same key used for both (same starting configuration) • Breaking message: C1 ⊕ C2 = (M1 ⊕ K1) ⊕ (M2 ⊕ K1) = (M1 ⊕ M2) ⊕ (K1 ⊕ K1) = M1 ⊕ M2 ⊕ means XOR #39 “Cribs” • Know: C1, C2 (intercepted ciphertext) C1 ⊕ C2 = M1 ⊕ M2 • Don’t know M1 or M2 – But, can make some guesses (cribs) • SPRUCHNUMMER • Sometimes allies moved ships, sent out bombers to help the cryptographers get good cribs • Given guess for M1, calculate M2 M2 = C1 ⊕ C2 ⊕ M1 • Once guesses that work for M1 and M2 K1 = M1 ⊕ C1 = M2 ⊕ C2 #40 Reverse Engineering Lorenz • From the 2 intercepted messages, Col. John Tiltman worked on guessing cribs to find M1 and M2: 4000 letter messages, found 4000 letter key K1 • Bill Tutte (recent Chemistry graduate) given task of determining machine structure – Already knew it was 2 sets of 5 wheels and 2 wheels of unknown function – Six months later new machine structure likely to generate K1 #41 Intercepting Traffic • Set up listening post to intercept traffic from 12 Lorenz (Fish) links – Different links between conquered capitals – Slightly different coding procedures, and different configurations • 600 people worked on intercepting traffic #42 Breaking Traffic • Knew machine structure, but a different initial configuration was used for each message • Need to determine wheel setting: – Initial position of each of the 12 wheels – 1271 possible starting positions – Needed to try them fast enough to decrypt message while it was still strategically valuable This is what you did for PS4 (except with fewer wheels)
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved