Ecs 36C Data Structures: Spring 2024 - Instructor Siena Saltzen
Ecs 36C Data Structures: Spring 2024 - Instructor Siena Saltzen
Data Structures
Spring 2024 - Instructor Siena Saltzen
Administrative
https://canvas.ucdavis.edu/courses/877554/assignments/syllabus
● No need to email me if missing class, make your best scheduling decisions
● Discussion/OH are now up on the syllabus
● Welcome Survey is worth 2 points, up until the 13th
● HW 1 Releases tonight, you will have a week to complete it
● And a practice homework assignment - Does not have autograder, but Is listed
● SDC - Acknowledged, Keep in mind you can always take the normal offering, working
on accommodations will be handled through the SDC, including weekly Quizzes
● Discord Rules - Please remember this is a professional forum and act accordingly
● Testing Dates will remain as posted
● SLAC Night! Wed 5-7 come code together!
Common Complexities and Their Names
● We can assume that the Big-O notation gives us the
algorithm’s approximate run time in the worst case.
There is a lot more math involved in the formal definition
○ ( Luckily I will only make you do some of it…)
What if the item is the first in the list? The last in the list? Not in the list?
Which run time should we report?
There are different kinds of analysis we could report:
• Best Case
• Worst Case
• Average Case Amortized:
In accounting, amortization refers to expensing the acquisition cost minus the residual value of intangible assets in a systematic
• Common Case manner over their estimated "useful economic lives" so as to reflect their consumption, expiry, and obsolescence, or other
decline in value as a result of use or the passage of time. Wikipedia
In computer science and algorithms, amortized analysis is a technique used to estimate the average time complexity of an
• Amortized algorithm over a sequence of operations, rather than the worst-case complexity of individual operations.
For example, for a dynamic array that doubles in size when needed, normal asymptotic analysis would only conclude that adding
• and so on.. an item to it costs O(n), because it might need to grow and copy all elements to the new array. Amortized analysis takes into
account that in order to have to grow, n/2 items must have been added without causing a grow since the previous grow, so
adding an item really only takes O(1) (the cost of O(n) is amortized over n/2 actions).
Big-O notation
Going back to the iterative Fibonacci example, we calculated the function for the run
time to be 4n - 5. Then we argued that the 4 and the 5 are noise for our purposes.
One way to say all that is “The running time for the Fibonacci algorithm finding the
nth Fibonacci number is on the order of n.”
In Big-O notation, that’s just T(n) is O(n)
T(n) is our shorthand for the runtime of the function being analyzed.
The O in O(n) means “order of magnitude”, so Big-O notation is clearly, and
intentionally, not precise. It’s a formal notation for the approximation of the time (or
space) requirements of running algorithms.
Formal Definition
T(n) is O(f(n)) if there are two positive constants, n0 and c, and a function f(n)
such that cf(n) >= T(n) for all n > n0
really means in a practical sense:
If you want to show that T(n) is O(f(n)) then find two positive constants, n0 and c,
and a function f(n) that satisfy the constraints above. For example...
Big-O arithmetic
* Desmos
Hmmm, let’s try 1.
1 <= 1 + 5/4
For the first section: For each problem in code, find T(n).
The same way we did for the fibonacci sequence and search examples.
It is possible that you find values for n and c where it all appears to work, but if you
then try out larger values of n and the inequality no longer holds. Your proof isn't a
proof.
So in this class, make sure that, once you have found a working n and c, you try some
larger values of n. There's an example of how you might be deceived coming up.
The Big-O definition simply(?) says that there is a point n0
such that for all values of n that are past this point, T(n) is
bounded by some multiple of f(n). Thus, if the running time
T(n) of an algorithm is O(n^2), we are guaranteeing that at
some point we can bound the running time by a quadratic
function (a function whose high-order term involves n^2).
Thus, if the running time T(n) of an algorithm is O(n2),
we are guaranteeing that at some point we can bound the running time by a
quadratic function (a function whose high-order term involves n2).
Big-O says there’s a function that is an upper bound to the worst-case performance
for the algorithm.
Note however that if T(n) is linear and not quadratic, you
could still say that the running time is O(n^2).
It’s technically correct because the inequality holds. However, O(n) would
be the more precise claim because it’s an even lower upper bound.
Desmos Example
Big-O is for expressing how run time or memory requirements grow as a function of the
problem size. Your book* has a nice table listing commonly-encountered rates.
O(1) Constant
O(log n) Logarithmic
O(n) Linear
O(n log n) Log-linear
O(n^2) Quadratic
O(n^3) Cubic
O(n^k) Polynomial – k is constant
O(2^n) Exponential
O(n!) Factorial
https://alyssaq.github.io/stl-complexities/
Recap
Let’s say we’ve calculated the run time for some bit of code as
T(n) = n^3 + n^2 - n But we don’t say that T(n) is O(n^3 + n^2 - n).
Why?(Bonus)
But we don’t say that T(n) is O(n^3 + n^2 - n).
Why?
Because as n gets very big, the growth of n^3 dominates the growth of the other terms
– the low-order terms – and those terms have no noticeable impact as n gets very big.
Low-order terms
So if the run time is this: T(n) = 42n^3 + 3n^2 - 17n - 8 Then T(n) is O(n^3) because the
dominant or high-order term is O(n^3).
● Desmos example
Order or Dominance
The “order of dominance” is in this same table. Read from the bottom up. O(n!)
dominates O(2^n). O(2^n) dominates O(n^k). And so on...
Big-O name
O(1) Constant
O(log n) Logarithmic
O(n) Linear
O(n log n) Log-linear
O(n^2) Quadratic
O(n^3) Cubic
O(n^k) Polynomial – k is constant
O(2^n) Exponential
O(n!) Factorial
Analysing Code
• Basic operations - constant time
• consecutive stmts - sum of times
• conditionals - sum of branches + condition
• loops - sum of iterations
• function calls - cost of function body*
Big-Ω
What if you want to find a lower bound to the performance of an algorithm? That’s
where Big-Ω (Big-Omega) comes in.
Big-Ω says there’s a function that is a lower bound to the worst case performance
for the algorithm. In other words, the algorithm’s performance will never get
better than that lower bound.
Formal Definition
The formal mathematical definition for Big-Ω:
T(n) is Ω(f(n)) if there are two constants, n0 and c, both > 0, and a function
f(n) such that cf(n) <= T(n) for all n > n0
If T(n) is Ω(f(n)), then we say “T(n) is at least order of f(n)” or “T(n) is bound
from below by f(n)” or “T(n) is Big-Omega of f(n)”.
Big-Θ
If we know that T(n) is both O(f(n)) and Ω(f(n)), then we may say that T(n) is Θ(f(n)) (i.e., Big-Theta of f(n)).
The formal mathematical definition for Big-Θ:
T(n) is Θ(f(n)) if and only if T(n) is O(f(n)) and T(n) is Ω(f(n)).
This says that the growth rate of T(n) equals the growth rate of f(n). If we say that T(n) is Θ(n2), we’re
saying that T(n) is bounded above and below by a quadratic function.
It won’t get worse than the upper bound, and it can’t get better than the lower bound. Big-Θ assures the
tightest possible bound on the algorithm’s performance.
Wait. Aren’t These all the same?
Big O notation (O):
It is defined as upper bound and upper bound on an algorithm is the most amount of time required ( the worst
case performance).
Big O notation is used to describe the asymptotic upper bound. An algorithm will take at MOST a certain order
of time.
Big Omega notation (Ω) :
It is define as lower bound and lower bound on an algorithm is the least amount of time required. In other words
an algorithm takes AT LEAST a certain amount of time.
Just like O notation provide an asymptotic upper bound, Ω notation provides asymptotic lower bound.
It is define as tightest bound and tightest bound is the best of all the worst case times that the algorithm can
take.
Big O
https://www.geeksforgeeks.org/difference-between-big-oh-big-omega-and-big-theta/
Big Ω
https://www.geeksforgeeks.org/difference-between-big-oh-big-omega-and-big-theta/
Big Θ
https://www.geeksforgeeks.org/difference-between-big-oh-big-omega-and-big-theta/
S. Big O Big Omega (Ω) Big Theta (Θ)
No
.
2. The upper bound of algorithm is represented by The algorithm’s lower bound is represented by The bounding of function from above and
Big O notation. Only the above function is Omega notation. The asymptotic lower bound is below is represented by theta notation. The
bounded by Big O. Asymptotic upper bound is given by Omega notation. exact asymptotic behavior is done by this
given by Big O notation. theta notation.
3. Big O – Upper Bound Big Omega (Ω) – Lower Bound Big Theta (Θ) – Tight Bound
4. It is define as upper bound and upper bound on It is define as lower bound and lower bound on It is define as tightest bound and tightest
an algorithm is the most amount of time required ( an algorithm is the least amount of time bound is the best of all the worst case times
the worst case performance). required ( the most efficient way possible, in that the algorithm can take.
other words best case).
5. Mathematically: Big Oh is 0 <= f(n) <= Cg(n) for Mathematically: Big Omega is 0 <= Cg(n) <= Mathematically – Big Theta is 0 <= C2g(n)
all n >= n0 f(n) for all n >= n0 <= f(n) <= C1g(n) for n >= n0
Example
Show that T(n) = 48n + 8 is Θ(n):
for O(n):
48n + 8 <= cn
8(6n + 1) <= cn
6n + 1 <= cn/8
6n <= cn/8 – 1
n <= cn/48 – 1/6
n <= n – 1/6? (if c = 48) no
n <= 2n – 1/6? (if c = 96) maybe
1 <= 2 – 1/6? (if n0 = 1) yes
T(n) is O(n)
for Ω(n):
48n + 8 >= cn
8(6n + 1) >= cn
6n + 1 >= cn/8
6n >= cn/8 – 1
n >= cn/48 – 1/6
n >= n – 1/6? (if c = 48) maybe
1 >= 1 – 1/6? (if n0 = 1) yes
T(n) is Ω(n) and T(n) is O(n) so T(n) is Θ(n).
A Note
I personally really like the descriptions on this website. They reference some algorithms
we haven’t covered yet but will.
https://www.freecodecamp.org/news/big-o-notation-why-it-matters-and-why-it-doesn
t-1674cfa8a23c/
It also goes more in depth for those of you who are interested.
Quick Demo!