1 / 8

More on the Cost of Computation

More on the Cost of Computation. Examples from Last Time. add m n (on naturals): m recursive calls append l_m l_n: m recursive calls Cross l_m l_n: m*n recursive calls map f l_n: n recursive calls, and calls to f find-root: n logn recursive calls

daria
Download Presentation

More on the Cost of Computation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. More on the Cost of Computation

  2. Examples from Last Time • add m n (on naturals): m recursive calls • append l_m l_n: m recursive calls • Cross l_m l_n: m*n recursive calls • map f l_n: n recursive calls, and calls to f • find-root: n logn recursive calls • insert l_n: upto n recursive calls to insert • insert-sort l_n: upto n*n/2 calls to insert • maxi l_n: ? (Homework problem)

  3. Order of Complexity • Consider three algorithms • CostA(n) = 2*n3 + n2 + 1 • CostB(n) = 3*n2 + 10 • CostC(n) = 2n • Which one is better?

  4. Order of Complexity • Consider three algorithms • CostA(n) = 2*n3 + n2 + 1 • CostB(n) = 3*n2 + 10 • CostC(n) = 2n • One with smaller dominator is best • Can we formalize this notion?

  5. Order of Complexity • We'll say that "CostZ is order f (n) " if • CostZ(n+offset) < factor * f (n+offset) • More concisely: "CostZ is O (f (n))" • Examples: • CostA(n) = 2*n3 + n2 + 1 CostA is O(n3) • CostB(n) = 3*n2 + 10 CostB is O(n2) • CostC(n) = 2n CostC is O(2n) • Essentially in the theory and practice of "Algorithms", and in "Complexity theory".

  6. Famous "Complexity Classes" • O (1) constant-time (head, tail) • O (log n) logarithmic (binary search) • O (n) linear (vector multiplication) • O (n * log n) "n log n" (sorting) • O (n2) quadratic (matrix addition) • O (n3) cubic (matrix multiplication) • nO(1) polynomial (…many! …) • 2O(n) exponential (guess password) • Are there more?

  7. Plug for the lab: Vectors • Speaking of cost… • Anything ever bother you about lists? • Vectors • (build-vector N f) • (vector-ref V i) • (vector-length V) • (vector? e) • Home work problem on vectors

  8. "The Next Big Thing" • Say we want to "integrate" a sequence: • Given: [1,1,0,2,1] • Return: [1,2,2,4,5] • How can we implement this? • How fast is our program? • Can we do any better?

More Related