David Evans CS150: Computer Science University of Virginia Computer Science Lecture 18: The Story So Far.

Slides:



Advertisements
Similar presentations
Class 21: Imperative Programming University of Virginia cs1120 David Evans.
Advertisements

David Evans CS200: Computer Science University of Virginia Computer Science Lecture 13: Of On and Off Grounds Sorting.
Cs1120 Fall 2009 David Evans Lecture 15: Running Practice.
Cs1120 Fall 2009 David Evans Lecture 16: Power Analysis.
David Luebke 1 4/22/2015 CS 332: Algorithms Quicksort.
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 16: Quicker Sorting.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 31: Types of Types.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 14: Asymptotic Growth.
Cs1120 Fall 2009 David Evans Lecture 20: Programming with State.
Cs1120 Fall 2009 David Evans Lecture 19: Stateful Evaluation.
David Evans Class 12: Quickest Sorting CS150: Computer Science University of Virginia Computer Science Rose Bush by Jacintha.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 5: Recursing Recursively Richard Feynman’s.
David Evans Class 15: Golden Ages and Astrophysics CS200: Computer Science University of Virginia Computer Science.
David Evans Class 13: Quicksort, Problems and Procedures CS150: Computer Science University of Virginia Computer Science.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 12: Decrypting Work Circle Fractal.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 2: Orders of Growth
15 October 2003Computer Science1 David Evans ComputerScience.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Think Globally, Mutate Locally.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
David Evans CS200: Computer Science University of Virginia Computer Science Class 17: Mutation M. C. Escher, Day and Night.
David Evans Class 20: Quick Sorting CS200: Computer Science University of Virginia Computer Science Queen’s University,
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 10: Puzzling Pegboards.
Class 37: Secret of Life CS200: Computer Science
David Evans Class 21: The Story So Far (Quicksort, Continuing Golden Ages) CS200: Computer Science University of Virginia.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 36: Modeling Computing.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
David Evans Lecture 13: Astrophysics and Cryptology CS200: Computer Science University of Virginia Computer Science.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 12: QuickSorting Queen’s University,
David Evans CS200: Computer Science University of Virginia Computer Science Class 16: Mutation M. C. Escher, Day and Night.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments.
David Evans Class 16: NP-Completeness / The Story so Far CS150: Computer Science University of Virginia Computer Science.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Mutation M. C. Escher, Day and.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 9: Of On and Off Grounds Sorting Coffee.
Cs1120 Fall 2009 David Evans Lecture 18: Changing State Sounds of Colossus:
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 15: Intractable Problems (Smiley.
David Evans Class 15: P vs. NP (Smiley Puzzles and Curing Cancer) CS150: Computer Science University of Virginia Computer.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 6: Ordered Data Abstractions
David Evans CS200: Computer Science University of Virginia Computer Science Class 32: The Meaning of Truth.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 8: Crash Course in Computational Complexity.
Lecture 3: Rules of Evaluation CS150: Computer Science
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 14: P = NP?
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 4: Programming with Data.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 8: Recursing Recursively Richard Feynman’s.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 8: Cons car cdr sdr wdr.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 9: Recursing Recursively Richard Feynman’s.
Lecture 4: Metacircles Eval Apply David Evans
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
Lecture 13: Quicksorting CS200: Computer Science
Lecture 7: List Recursion CS200: Computer Science
Class 19: Think Globally, Mutate Locally CS150: Computer Science
Lecture 8: Recursion Practice CS200: Computer Science
Lecture 16: Quickest Sorting CS150: Computer Science
Lecture 11: All Sorts CS200: Computer Science University of Virginia
David Evans Lecture 9: The Great Lambda Tree of Infinite Knowledge and Ultimate Power CS200: Computer Science University.
Lecture 13: Cost of Sorts CS150: Computer Science
8/04/2009 Many thanks to David Sun for some of the included slides!
Lecture 22: P = NP? CS200: Computer Science University of Virginia
Lecture 10: Quicker Sorting CS150: Computer Science
Lecture 9: The Great Lambda Tree of Knowledge and Power
Cs1120 Fall 2009 David Evans Lecture 10: Fracturing Fractals cs1120 Fall 2009 David Evans
Lecture 3: Rules of Evaluation CS200: Computer Science
Lecture 15: Quicker Sorting CS150: Computer Science
Lecture 11: Sorting Grounds and Bubbles
Lecture 8: Recursing Lists CS150: Computer Science
Lecture 23: Computability CS200: Computer Science
Presentation transcript:

David Evans CS150: Computer Science University of Virginia Computer Science Lecture 18: The Story So Far

2 Lecture 18: Mutation Menu Finish insert-sort-tree Course roadmap Introducing Mutation A few people have extensions on Exam 1, so no talking about the exam questions until Wednesday. If you have an extension on Exam 1, don’t read Chapter 9 until you turn in the exam.

3 Lecture 18: Mutation 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 insert-one-tree is in  (log n) where n is the number of elements in the input tree, which must be well-balanced.

4 Lecture 18: Mutation insert-sort-helper (define (insert-sort-helper cf lst) (if (null? lst) null (insert-one-tree cf (car lst) (insert-sort-helper cf (cdr lst))))) No change (other than using insert-one-tree)…but evaluates to a tree not a list! (((() 1 ()) 2 ()) 5 (() 8 ()))

5 Lecture 18: Mutation extract-elements We need to make a list of all the tree elements, from left to right. (define (extract-elements tree) (if (null? tree) null (append (extract-elements (get-left tree)) (cons (get-element tree) (extract-elements (get-right tree))))))

6 Lecture 18: Mutation Running time of insert-sort-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)))))) (define (insert-sort-tree cf lst) (define (insert-sort-helper cf lst) (if (null? lst) null (insert-one-tree cf (car lst) (insert-sort-helper cf (cdr lst))))) (extract-elements (insert-sort-helper cf lst)))  (log n) n = number of elements in tree  ( n log n ) n = number of elements in lst

7 Lecture 18: Mutation n log 2 n insert-sort-tree n 2 insert-sort Growth of time to sort random list

8 Lecture 18: Mutation What if tree is not well-balanced? A pathologically unbalanced tree is as bad as a list! insert-one worst case requires n recursive applications, so insert-sort-tree worst case is in  (n 2 )

9 Lecture 18: Mutation Comparing sorts > (testgrowth best-first-sort) n = 250, time = 110 n = 500, time = 371 n = 1000, time = 2363 n = 2000, time = 8162 n = 4000, time = ( ) > (testgrowth insert-sort) n = 250, time = 40 n = 500, time = 180 n = 1000, time = 571 n = 2000, time = 2644 n = 4000, time = ( ) > (testgrowth insert-sort-halves) n = 250, time = 251 n = 500, time = 1262 n = 1000, time = 4025 n = 2000, time = n = 4000, time = ( ) > (testgrowth insert-sort-tree) n = 250, time = 30 n = 500, time = 250 n = 1000, time = 150 n = 2000, time = 301 n = 4000, time = 1001 ( )

10 Lecture 18: Mutation Can we do better? Making all those trees is a lot of work Can we divide the problem in two halves, without making trees? This is the famous “Quicksort” algorithm invented by Sir Tony Hoare. See Chapter 8. There are lots of ways to do a little bit better, but no way to do asymptotically better. All possible sort procedure have running times in  (n log n). (We’ll explain why later in the course...)

11 Lecture 18: Mutation Course Roadmap Synthesis Analysis Ch 2: Language Ch 3: ProgrammingCh 4: ProceduresCh 5: Data Ch 6: Cost Ch 7: Time Ch 8: Sorting and Sequencing PS5, Ch 9: StatePS6, Ch 10: Objects Ch 13: Tractability Ch 11: Models PS7, Ch 13: Meta-Language Ch 12: Computability PS8, 9: Building Web Applications You are here

12 Lecture 18: Mutation Computer Science: CS150 so far How to describe information processes by defining procedures –Programming with procedures, lists, recursion –Chapters 3, 4, 5 How to predict properties about information processes –Predicting running time, , Ο, Ω How to elegantly and efficiently implement information processes –Chapter 3 (rules of evaluation)

13 Lecture 18: Mutation CS150 upcoming How to describe information processes by defining procedures –Programming with state, objects, networks How to predict properties about information processes –What is the fastest process that can solve a given problem? –Are there problems which can’t be solved by algorithms? How to elegantly and efficiently implement information processes –How to implement a Scheme interpreter

14 Lecture 18: Mutation The Liberal Arts Trivium (3 roads) language Quadrivium (4 roads) numbers GrammarRhetoricLogicArithmetic Geometry Music Astronomy From Lecture 1:

15 Lecture 18: Mutation Liberal Arts Checkup Grammar: study of meaning in written expression Rhetoric: comprehension of verbal and written discourse Logic: argumentative discourse for discovering truth Arithmetic: understanding numbers Geometry: quantification of space Music: number in time Astronomy BNF replacement rules for describing languages, rules of evaluation for meaning Not much yet… interfaces between components (PS6-9), program and user (PS8-9) Rules of evaluation, if, recursive definitions Not much yet… wait until April Curves as procedures, fractals (PS3) Yes, listen to “Hey Jude!” Friday: read Neil deGrasse Tyson’s essay Trivium Quadrivium