1 of 27

Fa17 CS 61B Discussion 6

2 of 27

Announcements

  • Project 1 is due 10/13!
    • Start early!

3 of 27

Asymptotics

4 of 27

Asymptotics is the whole reason for 61B

  • If we did not care about efficiency of our code, why use different data structures at all?
    • Just use arrays and be done with it!

5 of 27

Key Idea

  • Asymptotics describe how a function grows as input size grows.

6 of 27

Example

  • Consider the following example.

7 of 27

Example

  • As the size of our array grows, how many more print statements do we execute?
  • Thus, what is our runtime?

8 of 27

Example

  • As the size of our array grows, how many more print statements do we execute?
  • If we plot this, we will see a linear relationship between total print calls and array size.
  • Our runtime is linear with respect to array size.

9 of 27

Another Example

10 of 27

Another Example

  • If we increase array size by 1, the total number of calls to our print statement increases by 5.
  • What is our runtime?

11 of 27

Another Example

  • If we increase array size by 1, the total number of calls to our print statement increases by 5.
  • But if we plot it, we still get a linear relationship!
  • Therefore, the runtime is still linear.

12 of 27

Another Example

  • Notice I did not say 5 x linear, just linear.
  • This brings us to our next key idea...

13 of 27

Key Idea (The most key-est of them all)

  • Asymptotics are approximations.
  • To put it another way, we are not finding the runtime of a function.
  • We are finding the family of runtimes that this function belongs to.

14 of 27

Example

  • Say somehow, you’ve calculated that your Java function runs in f(n) = 100n4 + 0.2n3 + n2 + log2 n + 10500
  • What is your runtime?

15 of 27

Example

  • Our runtime is still Θ(n4).
    • We’ll learn about the Θ notation later.
  • We didn’t care about anything except the highest order term, and we dropped our constants.
  • What we did was classify f(n) into the family containing all n4 functions.

16 of 27

Repeat after me...

  • I PROMISE TO DROP MY CONSTANTS.
    • Number one way to lose points on an asymptotic question.

17 of 27

18 of 27

Big-O, Theta, Omega

  • Classifying functions into families is nice, but we want to find an upper limit or lower limit of our runtime.
  • Same idea as before, using families.

19 of 27

Big-O, Theta, Omega

  • Big-O: Upper bound.
  • Big-Omega: Lower bound.
  • Big-Theta: Tight bound (Upper bound == Lower bound)

20 of 27

Big-O

  • If f(n) = n, g(n) = 100n2, then we say f(n) is in O(g(n)).
    • This says: f(n) is upper bounded by the family that g(n) is in.
    • If I pick any function inside that family, it will be slower than f(n).
    • Also called the worst-case bound.

21 of 27

Big-Ω

  • If f(n) = 100n3, g(n) = n2, then we say f(n) is in Ω(g(n)).
    • This says: f(n) is lower bounded by the family that g(n) is in.
    • If I pick any function inside that family, it will be faster than f(n).
    • Also called the best-case bound.

22 of 27

Big-Θ

  • If f(n) = 100n2, g(n) = n2, then we say f(n) is in Θ(g(n)).
    • This says: f(n) is upper and lower bounded by the family that g(n) is in.
    • Also called the tight bound.

23 of 27

Key Idea: Be Thick, Solid, and TIGHT

  • If my function runs in f(n) = n time, then technically I could say that it is bounded by O(n100).
    • No credit on an exam!
    • Also defeats the purpose of asymptotics.
  • Be as tight as possible. Say Θ(n).

24 of 27

On to Discussion Q1, Q2

25 of 27

Common Paradigms

  • 1 + 2 + 3 + 4 + … + n -> O(n2)
    • Summation is equal to n(n + 1) / 2
  • 1 + 2 + 4 + 8 + … + n -> O(n)
    • To see this, take a look at the 8.
    • Then, take a look at the numbers to the left of 8.
    • Sums up to 7. Therefore we sum to 2n -> O(n)

26 of 27

Bits

27 of 27

Common Paradigms

  • 1 + 2 + 3 + 4 + … + n -> O(n2)
    • Summation is equal to n(n + 1) / 2
  • 1 + 2 + 4 + 8 + … + n -> O(n)
    • To see this, take a look at the 8.
    • Then, take a look at the numbers to the left of 8.
    • Sums up to 7. Therefore we sum to 2n -> O(n)