Cs1120 Fall 2009 David Evans Lecture 16: Power Analysis.

Slides:



Advertisements
Similar presentations
Cs1120 Fall 2009 David Evans Lecture 11: Generalizing List Procedures 1.
Advertisements

David Evans cs302: Theory of Computation University of Virginia Computer Science Lecture 11: Parsimonious Parsing.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 26: Proving Uncomputability Visualization.
Class 21: Imperative Programming University of Virginia cs1120 David Evans.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 13: Of On and Off Grounds Sorting.
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
Cs1120 Fall 2009 David Evans Lecture 15: Running Practice.
Penn ESE370 Fall DeHon 1 ESE370: Circuit-Level Modeling, Design, and Optimization for Digital Systems Day 24: November 4, 2011 Synchronous Circuits.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 18: The Story So Far.
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 40: Computing with Glue and Photons.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 14: Asymptotic Growth.
Lecture 31 CSE 331 Nov 16, Jeff is out of town this week No regular recitation or Jeff’s normal office hours I’ll hold extra Question sessions Mon,
Administrivia Interviews this week: ex-TA Paul Hale Nate's office hours, as usual: Wed 2-4 The final survey will be up by Thursday: –you NEED to do this,
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 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.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 11: CS Logo, by Lincoln Hamilton and.
Week 12 - Wednesday.  What did we talk about last time?  Asymptotic notation.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Think Globally, Mutate Locally.
David Evans CS150: Computer Science University of Virginia Computer Science Class 33: Computing with Photons From The.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 12: Something about Sneezewort From.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 10: Puzzling Pegboards.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 9: Strange Loops and Sinister Repeaters.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 3: Levels of Abstraction
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 36: Modeling Computing.
Class 8: Recursing on Lists David Evans cs1120 Fall 2009.
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 CS150: Computer Science University of Virginia Computer Science Lecture 5: Recursing on Lists.
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 Class 15: P vs. NP (Smiley Puzzles and Curing Cancer) CS150: Computer Science University of Virginia Computer.
David Evans CS200: Computer Science University of Virginia Computer Science Class 32: The Meaning of Truth.
Class 7: List Procedures David Evans cs1120 Fall 2009.
Lecture 3: Rules of Evaluation CS150: Computer Science
Penn ESE370 Fall DeHon 1 ESE370: Circuit-Level Modeling, Design, and Optimization for Digital Systems Day 24: November 5, 2012 Synchronous Circuits.
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 Class 32: Computability in Theory and Practice.
Lecture 4: Metacircles Eval Apply David Evans
Lecture 17: Environments CS200: Computer Science
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
CS 3343: Analysis of Algorithms
Lecture 8: Recursion Practice CS200: Computer Science
Lecture 16: Quickest Sorting CS150: Computer Science
Lecture 6: Programming with Data 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
Lecture 22: P = NP? CS200: Computer Science University of Virginia
Lecture 10: Quicker Sorting CS150: Computer Science
Class 33: Making Recursion M.C. Escher, Ascending and Descending
Lecture 9: The Great Lambda Tree of Knowledge and Power
Class 24: Computability Halting Problems Hockey Team Logo
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:

cs1120 Fall 2009 David Evans Lecture 16: Power Analysis

Menu Finishing Analyzing Flattening Power Reminders: Review Session, Wednesday 6:30 in Olsson 001 Extra Office Hours, Thursday 1:30-3 in Olsson 236A

Recap: Flatten Running Time (define (flatten-commands ll) (if (null? ll) ll (if (is-lsystem-command? (car ll)) (cons (car ll) (flatten-commands (cdr ll))) (flat-append (car ll) (flatten-commands (cdr ll)))))) First: determine running times of all the procedures applied in flatten-commands. null?, car, cons, cdr, and is-lsystem-command? are constant time flat-append has running time in  (N 1 ) where N 1 is the number of elements in the first input. Second: determine running time for each application except for recursive call. Need to consider both paths: (if (is-lsystem-command? (car ll)) (cons (car ll) (flatten-commands (cdr ll))) (flat-append (car ll) (flatten-commands (cdr ll))))))

Paths to Flattening (if (is-lsystem-command? (car ll)) (cons (car ll) (flatten-commands (cdr ll))) (flat-append (car ll) (flatten-commands (cdr ll)))))) Each recursive call involves constant work. Each recursive call reduces the number of elements in ll by one. For input list that is all lsystem commands of length N : flatten-commands has running time in  (N) where N is the number of lcommands in the input list.

Paths to Flattening (if (is-lsystem-command? (car ll)) (cons (car ll) (flatten-commands (cdr ll))) (flat-append (car ll) (flatten-commands (cdr ll)))))) Each recursive call involves  (P) work where P is the number of elements in (car ll). Each recursive call reduces the number of elements in ll by one. For input list that is all lists of length P : flatten-commands has running time in  (QP) where Q is the number of sub-lists (of length P ) in the input list.

Combining the Paths For input list that is all lsystem-commands: flatten-commands has running time in  (N) where N is the number of elements in the input list. (define (flatten-commands ll) (if (null? ll) ll (if (is-lsystem-command? (car ll)) (cons (car ll) (flatten-commands (cdr ll))) (flat-append (car ll) (flatten-commands (cdr ll)))))) For input list that is all lists of length P : flatten-commands has running time in  (QP) where Q is the number of sub-lists (of length P ) in the input list. For any input: flatten-commands has running time in  (M) where M is the size of the input list (the total number of lcommands in ll and all its sub-lists, not counting elements in offshoot command lists).

Power Define and analyze the asymptotic running time of a procedure power that takes two numbers, a and n, and input, and outputs a n. Hint: a 0 = 1 a n = a * a n-1 for n > 0

Simple Power (define (power a n) (if (= n 0) 1 (* a (power a (- n 1))))) What is the asymptotic running time of power?

Running Time Analysis (define (power a n) (if (= n 0) 1 (* a (power a (- n 1))))) 1.What are the running times of procedures applied by power? 2.What is the running time of evaluating the body except for the recursive call? 3.How many recursive calls are there? n, the value of the second input n

Running Time Analysis (define (power a n) (if (= n 0) 1 (* a (power a (- n 1))))) 1.What are the running times of procedures applied by power? 2.What is the running time of evaluating the body except for the recursive call? 3.How many recursive calls are there? n, the value of the second input n = and – with one input constant: constant running time

What about *? Cannot be constant time Multiplication must at least look at all the digits in both numbers Grade-school multiplication algorithm has running time in  (W 2 ) (* a (power a (- n 1))) * is in  (W) where W is the total length (number of bits) of the inputs. * is in O(W 2 ) where W is the total length (number of bits) of the inputs. Note: O instead of  since there may be faster * algorithms, and we don’t know what Scheme interpreter actually does.

What about *? (* a (power a (- n 1))) * is in  (W) where W is the total length (number of bits) of the inputs. * is in O(W 2 ) where W is the total length (number of bits) of the inputs. What can we say about * as it is used here? (what is W?) Worst case: W = bits in a + bits in a n-1 * has running time in O((a b n v ) 2 ) where a b is number of bits in a and n v is value of n.

Running Time Analysis (define (power a n) (if (= n 0) 1 (* a (power a (- n 1))))) Each body evaluation: Number of recursive calls: n v, the value of the second input n = and – with one input constant: constant running time * has running time in O((a b n v ) 2 ) and  (a b n v ) Running time for power is in O(n v (a b n v ) 2 ) = O(a b 2 n v 3 ) and  (a b n v 2 )

Bits and Values Running time for power is in O(a b 2 n v 3 ) and  (a b n v 2 ) where a b is number of bits in a and n v is value of n. What is the running time in terms of the size of the input? n v = 2 n b Running time for power is in O(a b 2 (2 n b ) 3 ) and  (a b 2 (2 n b ) 2 ).

Testing Power Analysis > (time (power )) cpu time: 47 real time: 38 gc time: 0 > (time (power )) cpu time: 1529 real time: 1533 gc time: 763 n b = 14 n b = 16 > (/ (power (power 2 16) 3) (power (power 2 14) 3)) 64 > (/ (power (power 2 16) 2) (power (power 2 14) 2)) 16 > (/ ) 32 25/47 Running time for power is in O(a b 2 (2 n b ) 3 ) and  (a b 2 (2 n b ) 2 ).

> (time (power )) n d = 7 Running time for power is in O(a b 2 (2 n b ) 3 ) and  (a b 2 (2 n b ) 2 ).

Faster Powering? a 2n = a n * a n Gold Star Challenge Problem: define and analyze (correctly!) an asymptotically faster power procedure.

Charge ACs’ Exam Review Session: Tonight at 6:30, Olsson 001 Extra Office Hours, Thursday 1:30-3pm Exam 1 out Friday