Comparing Functions (14 points)
For the following pairs of functions, indicate whether the ?? could be replaced with O, Ω or Θ.
Pr. | f(n) | g(n) | f(n)∈??(g(n)) |
---|---|---|---|
a) | 48n | 2n2+1 | |
b) | 0.00001n2 | 2210(nlogn) | |
c) | 5n20+n2log2n | n! | |
d) | 4n | 4log2(2n) | |
e) | 5nlogn | 2n1.5 | |
f) | 7n1024 | 1.000001n | |
g) | 2n2logn | 3n |
Orders of Functions (14 points)
List each of the functions from the previous problem from slowest to fastest growing in a table like the table below (include both the f(n) and g(n) functions). Group the functions by complexity class. Indicate which functions are in the same complexity class (same big-Θ set).
Complexity class | Functions from previous problem. |
---|---|
Θ( ) | |
Θ( ) | |
Θ( ) | |
Θ( ) | |
Θ( ) | |
Θ( ) | |
Θ( ) |
NOTE: You may not need all of the rows in the table above.
Analyzing Java Code (12 points)
For each of the following Java methods, analyze the running time by:
- writing an expression in terms of n for the every-time case analysis T(n) (if it exists) or worst case analysis W(n) (if the every-time case analysis does not exist) for the number of arithmetic operations,
- give the appropriate big-Θ complexity class for your analysis function T(n) or W(n).
Code Listing A
public int A(int[] array) {
int result = 25;
int i = array.length;
while (i > 1) {
if (array[i-1] < 0) {
return 0;
}
for (int j = 0; j < array.length; j++) {
result += 1;
result += 3 * array[j];
}
i = i / 2;
}
return result;
}
- Give your equation for T(n) or W(n) (which did you choose and why)?
- Give the Θ complexity class:
Code Listing B
public int B(int[] array1) {
int result = 0;
for (int i = 0; i < array1.length / 2; i++) {
result += array1[i] * 2;
}
for (int j = array1.length-1; j >= 0; j--) {
result += array1[j] * 3;
}
return result;
}
- Give your equation for T(n) or W(n) (which did you choose and why)?
- Give the Θ complexity class:
Code Listing C
public int C(int[] array1) {
int result = 0;
for (int i = 0; i < array1.length; i++) {
for (int j = 0; j < array1.length; j++) {
for (int k = 1; k < array1.length; k = k * 2) {
result += j * j * array1[k] + array1[i] + array1[j];
}
}
}
return result;
}
- Give your equation for T(n) or W(n) (which did you choose and why)?
- Give the Θ complexity class:
How much faster? (6 points)
Suppose you have an algorithm that performs T(n)=8n3 arithmetic operations on an input of size n. Suppose you have two machines, machine A and machine B. Machine A performs 10 arithmetic operations per second, while machine B performs 20 arithmetic operations per second.
- What is the largest input size nA you can compute on machine A in 60 seconds?
- What is the largest input size nB you can compute on machine B in 60 seconds?
- Write an equation describing the relationship between nA and nB.
Submitting
Use an appropriate typesetting program to produce a PDF of your solutions and submit them via the turn-in link on Canvas. (We encourage you to used LaTeX, but are not requiring it for this submission.)