Uploaded by Douglas Adu

Assg2

advertisement
1. For each function f(n) and time t in the following table, determine the largest size n of
a problem that can be solved in time t assuming that the algorithm to solve the
problem takes f(n) microseconds. (1 second = 1,000,000 microseconds). Recall that
logn denotes the logarithm in base 2 of n. Your input sizes do not have to be very
precise. Close approximations are good enough. (27 Points)
1 Second
1 Minute
1 Hour
logn
2^1000000
2^60000000
2^3600000000
sqrt(n)
1000000^2
60000000^2
3600000000^2
n
1000000
60000000
3600000000
nlogn
62746
2.801
1.3337
n2
1000
7745.966
60000
n3
99.999
391
1532
2n
19
25
31
n!
9
11
12
Page #1
2. For each of the following program fragments, give a Θ-analysis of the running time.
(26 Points)
int sum = 0;
for (int i = 0; i < n; i++)
sum++;
int sum = 0;
for (int i = 0; i < n; i += 2)
sum++;
int sum = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
sum++;
int sum = 0;
for (int i = 0; i < n; i++)
sum++;
for (int j = 0; j < n; j++)
sum++;
int sum = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n2; j++)
sum++;
int sum = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < 2*i; j++)
sum++;
int sum = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n2; j++)
for (int k = 0; k < j; k++)
sum++;
Page #2
int sum = 0;
for (int i = 1; i <= n; i = i * 2)
sum++;
3. Order the following functions by the increasing order of growth rate: N, sqrt(N),
N1.5, N2, NlogN, NloglogN, Nlog2N, Nlog(N2), 2/N, 2N, 2N/2, 37, N3 and
N2logN (14 Points)
4. Prove that if g(n) is O(f(n)) then f(n) is Ω(g(n)) (7 Points)
Solution:
If f(n) is O(g(n)) then
there exist a constant c > 0, and a constant n0 such that for all n ≥ n0: f(n) ≤ c*g(n)
hence:
there exist a constant c > 0, and a constant n0 such that for all n ≥ n0: g(n) ≥ (1/c)*f(n)
note that since c > 0, then the constant (1/c) > 0
hence:
there exist a constant k > 0, namely k = (1/c), and a constant n0 such that for all n ≥ n0:
g(n) ≥ k*f(n)
which is the definition of g(n) = Ω (f(n)).
5. Prove that f(n) is Θ(f(n))
(6 Points)
Soultion:
T(n) = O(n):
Take constant c = (k0 + k1) &ge 0, and take n0 = 2. Then for all n &ge n0:
T(n) = k0 + (k1*n) &le (k0*n) + (k1*n) = (k0 + k1)*n = c*n
Hence T(n) = O(n).
6. Show that n2/2 – 3n is Θ(n2)
(10 Points)
Page #3
Download