day05

# day05 - CS1: Introduction to Computation Day 5: October 13,...

This preview shows pages 1–15. Sign up to view the full content.

Caltech CS 1 -- Fall 2008 CS1: Introduction to Computation Day 5: October 13, 2008 Computational Complexity

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Caltech CS 1 -- Fall 2008 This is Spinal Tap (1984)
Caltech CS 1 -- Fall 2008 Nigel Tufnel's problem • Doesn't understand the way a function (the volume of his amplifier, V ) scales with the size of its inputs: pk : the position of the knob (0-11) pow : the power rating of the amplifier He thinks V(pk, pow) = k 1 * pk In reality: V(pk, pow) = k 1 * pk * pow (ignoring nonlinearities)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Caltech CS 1 -- Fall 2008 Last time • Described different classes of computations: – linear recursive – linear iterative – tree recursive • in terms of – how computations unfold in space and time – pending operations
Caltech CS 1 -- Fall 2008 Today • How do we quantify the resources (time, space) that our procedures require? • How do these resource requirements change with the size of the inputs?

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Caltech CS 1 -- Fall 2008 Time complexity • What we care about most is – How does the run time grow with input size? e.g. • sum-integers , ifib grew linearly • fib grew exponentially – How can we quantify this?
Caltech CS 1 -- Fall 2008 Being precise • Recall: (define (sum-integers n) (if (= n 0) 0 (+ n (sum-integers (- n 1))))) time = (n+1) * (T cmp + T if + 2*T add + T call )

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Caltech CS 1 -- Fall 2008 Not being too precise… time = (n+1) * (T cmp + T if + 2*T add + T call ) More simply: time = C 1 + n*C 2 C 1 and C 2 are constant factors • How does the run time scale as n gets larger?
Caltech CS 1 -- Fall 2008 Doing the numbers time = C 1 + n*C 2 Let’s say C 1 = 1000 , C 2 = 10 • n = 0 time = 1000 • n = 1 time = 1010 • n = 10 time = 1100 • n = 1000 time = 11000 • n = 10 6 time = 10001000 Conclusion: C 2 dominates C 1 for large n

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Caltech CS 1 -- Fall 2008 Doing the numbers time = C 1 + n*C 2 • Run time is linear in n For large n , C 2 is all that really counts C 1 has little effect • How to express this mathematically?
Caltech CS 1 -- Fall 2008 “Big O” notation • A function g(n) is O( f(n) ) if for all n greater than some n 0 – then g(n) <= C * f(n) • for some constant C i.e. g(n) grows no faster than f(n) for sufficiently large n • This is just math, not programming

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Caltech CS 1 -- Fall 2008 Example of big O • Let g(n) = 100 * n 2 + 50 * n + 2000 <= 150 * n 2 for large enough n • So g(n) is O(n 2 ) –here, C = 150 –note: C = 100.00001 would also work
Caltech CS 1 -- Fall 2008 0 3750 7500 11250 15000 0 2.5 5.0 7.5 10.0 100 n^2 + 50 n + 2000 150 n^2

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
A tighter bound… • g(n) = 100 * n 2 + 50 * n + 2000 g(n) is O(n 2 ) but g(n) is also O(n 3 ) ! g(n) <= n
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 09/25/2010 for the course CS 1 taught by Professor Chang during the Spring '08 term at UMBC.

### Page1 / 79

day05 - CS1: Introduction to Computation Day 5: October 13,...

This preview shows document pages 1 - 15. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online