1 / 14

Big-O

Big-O. Algorithm Analysis. Exact analysis: produce a function f ( n) measuring how many basic steps are needed for a given inputs n On any input of size n, the algorithm runs for at most 7n 2 + 3n + 8 steps Do we really need such a precise measurement?

fausta
Download Presentation

Big-O

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. Big-O

  2. Algorithm Analysis • Exact analysis: produce a function f(n) measuring how many basic steps are needed for a given inputs n On any input of size n, the algorithm runs for at most 7n2+ 3n + 8 steps • Do we really need such a precise measurement? • To compare two algorithm, we are concerned with how well the algorithm scales, • Too much details of f(n) is meaningless! • (asymptotic) analysis

  3. Asymptotic Analysis • Asymptotic analysis of an algorithm describes the relative efficiency of an algorithm when n gets very large

  4. Asymptotic Analysis (cont’d) N For large values of n, Algorithm 1 grows faster than Algorithm 2

  5. Why does Complexity Matter? • Assume that you have 3 algorithms to sort a list • f(n) = nlog2n • g(n) = n2 • h(n) = n3 • Assume that each step takes 1 microsecond (i.e., 10-6 seconds)

  6. Asymptotic Analysis (cont’d)

  7. Bound • f(n) is contributed from N^2 • The grow rate is bounded • with such bound, we only need to refer to well known functions and do not need to be concerned with the details of the function • Big-O notion: upper bound on the growth rate of a function, for sufficiently large values of n.

  8. Big-O Notation • Definition: f(n)= O(g(n))if there exist positive constants c and N such that f(n)  cg(n) for all n  N • Big-O expresses an upper bound on the growth rate of a function, for sufficiently large values of n. • Graph to understand Big-O

  9. An Example • Prove that 2n2 + 1 = O(n2)

  10. Proof with Induction

  11. Typical functions

  12. Properties of Big-O Notation • Fact 1 (Transitivity) • If f(n)=O(g(n)) and g(n)=O(h(n)), then f(n)=O(h(n)) • Prove it (done in class) • Fact 2 • If f(n)=O(h(n)) and g(n)=O(h(n)), then f(n)+g(n)=O(h(n)) • Fact 3 • The function ank=O(nk) • Fact 4 • The function nk=O(nk+j)for any positive j

  13. Properties of Big-O Notation (cont’d) • It follows from those facts that every polynomial is big-O of n raised to the largest power f(n) = aknk + ak-1nk-1 + . . . + a1n1 + a0 = O(nk)

  14. Properties of Big-O Notation (cont’d) • Fact 5 • If f(n)=cg(n), then f(n)=O(g(n)) • Fact 6 • The function logan = O(logbn)for any positive numbers a and b 1 • WHY? • Fact 7 • logan = O(lgn)for any positive a  1, where lg n = log2n

More Related