Uploaded by Karis Woo

HW10 - Algorithm Analysis (2022)

advertisement
CS (MA) 295: Discrete Structures
Homework #10 – Algorithm Analysis
1) Consider the following statement (n)  10 + 2(n)  20(n), for every n  1. Use Θ-notation
to express that statement.
10 + 2(n) ∈ Θ(n)
2) Consider the following real-valued function, f(n) = 10 + 2 Log2(n).
2a) Find Big-O for f(n)
O(log2 n)
2b) Find Big-Omega for f(n)
Ω(log2 n)
2c) Find Big-Theta for f(n)
Θ(log2 n)
3) Prove that 5 + 10 + 15 + 20 + 25 +… + 5n  Θ(n2). Hint: you need to use the closed form formula
for the sum the first n integers, and reshape the expression in f(n) to fit the formula. Then you
will be able to justify if it is Θ(n2)
5 + 10 + 15 + 20 + 25 + ⋯ + 5n  Θ(n2 )
= 5(1 + 2 + 3 + 4 + 5 + ⋯ + n)  Θ(n2 )
𝑛(𝑛 + 1)
= 5(
)  Θ(n2 )
2
𝑛2 + 𝑛
= 5(
)  Θ(n2 )
2
𝑛2 𝑛
= 5( + )  Θ(n2 )
2 2
5𝑛2 5𝑛
=
+
 Θ(n2 )
2
2
5
5
= 𝑛2 + n Θ(n2 )
2
2
5
5
Because is 2 𝑛2 + 2 n a polynomial, the Theorem of Theta for Polynomial orders applies, and since
the highest power is n^2, it is justified that it is Θ(n2).
𝑚(𝑚+1)
𝑚2 +𝑚
Infinite series 1 + 2 + 3 + ⋯ + 𝑚 = 2
= 2
5 + 10 + 15 + 20 + 25 + ⋯ + 5n = 5(1 + 2 + 3 + 4 + 5 + ⋯ + n)
𝑎𝑚 𝑛𝑚 + 𝑎𝑚−1 𝑛𝑚−1 + ⋯ + 𝑎1 𝑛 + 𝑎0 ∈ Θ(𝑛𝑚 )
4) Using the Theorem of Theta for Polynomial orders, find the Θ of the polynomials bellow:
4a) n(n-1)(n+1) = (𝑛2 − 𝑛)(𝑛 − 1) = (𝑛3 − 𝑛2 − 𝑛2 + 𝑛) = Θ(𝑛3 )
4b) (n+1)4 = (𝑛 + 1)2 ∗ (𝑛 + 1)2 = (𝑛2 + 2𝑛 + 1) ∗ (𝑛2 + 2𝑛 + 1) = Θ(𝑛4 )
5) Consider f and g are real valued functions. Why does Θ(g(n)) show a better representation of
f(n) growth over time, instead of O(g(n))?
Loyola University Maryland Internal Use Only
CS (MA) 295: Discrete Structures
Homework #10 – Algorithm Analysis
Theta has both upper and lower bounds while O only has the upper bound. Theta has ‘tight’
bounds unlike O, meaning it gives precise measures for measuring function growths.
6) Consider the following algorithm (written in Java) that finds the maximum and minimum
value inside an array of integers and returns the result as a string.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
public static String fMaxMin(int[]arr){
int max = arr[0];
int min = arr[0];
}
for(int i=1; i<arr.length; i++){
if(arr[i] > max){
max = arr[i];
} else if(arr[i] < min){
min = arr[i];
}
}
return ("Max "+max+", Min "+min);
(You may use this side for notes)
C initialization int i
2c index comparison, increment
2c array indexing comparison
2c worst case array indexing comparison
2c return
Suppose every elementary operation (as described in the handout and the book) has a
constant cost of “c”.
6a) Find the running time function (in closed form) of the above algorithm. Show your work.
c + ∑𝑛−1
𝑛=1 6𝑐 + c
2c + ∑𝑛−1
𝑛=1 6𝑐
2c + 6𝑐 ∑𝑛−1
𝑛=1 1
6b) Find the Big-Theta of the algorithm based on the time function from question 6a.
𝑓(𝑛) 𝜖 Θ(𝑛)
Loyola University Maryland Internal Use Only
Download