ECS 36C Midterm 1 Study Guide || with Complete Solutions.
How linear search works. correct answers In linear search, we are iterating through a list (starting
at the beginning) until we find the target element.
How binary search works. When it can be used. correct answers The algorithm for binary search:
Splitting the ordered list in half, check if the middle item in the list is equal to the target. If it is:
return true.
If not:
If target < middle, repeat the process on the left-side of the list
If target > middle, repeat the process on the right-side of the list.
Repeat the above steps until you either find the target or there are no more elements to look at.
Binary search can only be used on ordered/sorted lists (it does not matter whether sorted from
smallest to largest or largest to smallest).
Big-O Formal Definition. Proving that a big-O holds. Big-O is an upper-bound. correct answers
It indicated how fast a function grows as n increases, so the rate of change of f(n) as n increases.
Classifies growth rate of a mathematical function f(n) by placing an upper bound on said growth
rate. Focuses on dominant part of the function, dismissing non-dominant terms and leading
coefficients. Can be thought of as an "upper-bound".
Definition of big-Ω (omega). What kinds of bounds big-Ω has. Proving that it holds. correct
answers Big Omega is a lower bound.
Definition of big-Θ. What kinds of bounds it is. Proving that it holds. correct answers T(n) is
Θ(f(n)) if both of the following are true:
1. T(n) is O(f(n))
2. T(n) is Ω(f(n)).
This means that T is asymptotically tight-bounded by f.
Example:
T(n) = 2n^2 +6
T(n) is Θ(n^2)
T(n) is not Θ(n^3) because it is not Ω(n^3)
T(n) is not Θ(n) because it is not O(n).
Identifying worst-case time complexity, best-case time complexity, worst-case space complexity,
and best-case space complexity (with each of big-O, big-Ω, and big-Θ of a given segment of
code (or function). correct answers na
Common categories of functions: constant, logarithmic, or polynomial time. correct answers na
Different Classes of Functions * correct answers - Logarithms (regardless of base) are upper-
bounded by polynomials.
- Polynomials are upper-bounded by exponential functions.
, - Exponential functions are upper-bounded by factorial
- n! is O(n^n)
Effect of the change-of-base formula on using logarithmic functions with asymptotic notation.
correct answers
Logarithmic Functions correct answers A common example of a logarithmic function is found in
binary search. The worst-case scenario for logarithmic functions is the case where the target is
not found, meaning we would go through every iteration. The number of iterations scales
logarithmically as n increases. Doubling the input size will increase the number of times the list
can be cut in half by 1 which then increases the number of iterations by 1. The time complexity
of a binary search: Θ(lg n) (Θ(log2n)).
Change of Base Formula correct answers To go from base of b to base of a (and vice versa):
EX: log(5)n is Θ(log(7)n)
log(7)n is Θlog(5)n
Using asymptotic notation to create a placeholder for an anonymous function, as was done on
slide #49. correct answers Abuses of notation such as T(n) = O(f(n)) permits certain
conveniences, such as making formulas containing an anonymous function that need not be
named.
EXAMPLE:
f(n) = 2n^2 + Θ(n) is Θ(n^2)
- "Θ(n)" refers to some function satisfying this order of growth.
Properties correct answers Symmetry: f(n) is Θ(g(n)) if and only if g(n) is Θ(f(n)).
Transpose symmetry: f(n) is O(g(n)) if and only if g(n) is Ω(f(n)).
Auxiliary space* correct answers Space used by the algorithm, as a function of n(the input size),
not including the input size.
Vectors:
- Why inserting at the front of a vector takes linear time.
- Implementation of std::vector. Capacity. When the capacity changes. Why changing the
capacity may invalidate
references.
- Why inserting at the back of a vector takes linear time in the worst case.
- You will not be asked about amortized analysis. correct answers When we insert an item into a
vector at full capacity, we can't assume that there will be an open slot at the end of the vector
where we'll be able to place the new item. In order to correct this, we need to allocate a new
underlying array with an increased capacity (usually twice as much previous capacity) and then
we need to copy the elements to the new underlying array and delete the old array. Deleting the
old array will invalidate references. Invalidating references: if you have a reference to an element
of a vector and the vectors capacity changes (doubling in size), the elements are going to move
How linear search works. correct answers In linear search, we are iterating through a list (starting
at the beginning) until we find the target element.
How binary search works. When it can be used. correct answers The algorithm for binary search:
Splitting the ordered list in half, check if the middle item in the list is equal to the target. If it is:
return true.
If not:
If target < middle, repeat the process on the left-side of the list
If target > middle, repeat the process on the right-side of the list.
Repeat the above steps until you either find the target or there are no more elements to look at.
Binary search can only be used on ordered/sorted lists (it does not matter whether sorted from
smallest to largest or largest to smallest).
Big-O Formal Definition. Proving that a big-O holds. Big-O is an upper-bound. correct answers
It indicated how fast a function grows as n increases, so the rate of change of f(n) as n increases.
Classifies growth rate of a mathematical function f(n) by placing an upper bound on said growth
rate. Focuses on dominant part of the function, dismissing non-dominant terms and leading
coefficients. Can be thought of as an "upper-bound".
Definition of big-Ω (omega). What kinds of bounds big-Ω has. Proving that it holds. correct
answers Big Omega is a lower bound.
Definition of big-Θ. What kinds of bounds it is. Proving that it holds. correct answers T(n) is
Θ(f(n)) if both of the following are true:
1. T(n) is O(f(n))
2. T(n) is Ω(f(n)).
This means that T is asymptotically tight-bounded by f.
Example:
T(n) = 2n^2 +6
T(n) is Θ(n^2)
T(n) is not Θ(n^3) because it is not Ω(n^3)
T(n) is not Θ(n) because it is not O(n).
Identifying worst-case time complexity, best-case time complexity, worst-case space complexity,
and best-case space complexity (with each of big-O, big-Ω, and big-Θ of a given segment of
code (or function). correct answers na
Common categories of functions: constant, logarithmic, or polynomial time. correct answers na
Different Classes of Functions * correct answers - Logarithms (regardless of base) are upper-
bounded by polynomials.
- Polynomials are upper-bounded by exponential functions.
, - Exponential functions are upper-bounded by factorial
- n! is O(n^n)
Effect of the change-of-base formula on using logarithmic functions with asymptotic notation.
correct answers
Logarithmic Functions correct answers A common example of a logarithmic function is found in
binary search. The worst-case scenario for logarithmic functions is the case where the target is
not found, meaning we would go through every iteration. The number of iterations scales
logarithmically as n increases. Doubling the input size will increase the number of times the list
can be cut in half by 1 which then increases the number of iterations by 1. The time complexity
of a binary search: Θ(lg n) (Θ(log2n)).
Change of Base Formula correct answers To go from base of b to base of a (and vice versa):
EX: log(5)n is Θ(log(7)n)
log(7)n is Θlog(5)n
Using asymptotic notation to create a placeholder for an anonymous function, as was done on
slide #49. correct answers Abuses of notation such as T(n) = O(f(n)) permits certain
conveniences, such as making formulas containing an anonymous function that need not be
named.
EXAMPLE:
f(n) = 2n^2 + Θ(n) is Θ(n^2)
- "Θ(n)" refers to some function satisfying this order of growth.
Properties correct answers Symmetry: f(n) is Θ(g(n)) if and only if g(n) is Θ(f(n)).
Transpose symmetry: f(n) is O(g(n)) if and only if g(n) is Ω(f(n)).
Auxiliary space* correct answers Space used by the algorithm, as a function of n(the input size),
not including the input size.
Vectors:
- Why inserting at the front of a vector takes linear time.
- Implementation of std::vector. Capacity. When the capacity changes. Why changing the
capacity may invalidate
references.
- Why inserting at the back of a vector takes linear time in the worst case.
- You will not be asked about amortized analysis. correct answers When we insert an item into a
vector at full capacity, we can't assume that there will be an open slot at the end of the vector
where we'll be able to place the new item. In order to correct this, we need to allocate a new
underlying array with an increased capacity (usually twice as much previous capacity) and then
we need to copy the elements to the new underlying array and delete the old array. Deleting the
old array will invalidate references. Invalidating references: if you have a reference to an element
of a vector and the vectors capacity changes (doubling in size), the elements are going to move