Exercises Project and Analysis of Algorithms
Exercises Project and Analysis of Algorithms
EXERCISE LIST E1
1. In each item indicate and explain if f=O(g), f = (g), f=o(g), f= (g) or f= (g)
f g
a. n -100 n-200
b. n1/2 n2/3
c. log n log2n
d. 10 log n log n2
e. n2 n log n
1/2
f. n 5log2n
n
g. 2 2n plus one
h. n! 2n
4. For each function f(n) and each time t in the table below, determine the largest size.
in a problem that can be solved in time t, assuming that the algorithm
to solve the problem takes f(n) microseconds.
f(n) t 1 second 1 minute 1 hour 1 day
lg n
n
n2
n3
2n
n!
5. The table below shows the size of the largest instance of a problem that a
the current computer can solve in 1 hour, given the complexity function of
algorithm. Fill in the table showing the largest instance size that is
can solve in one hour on a computer 100 times and 1000 times more
faster than the current one
6. You have two algorithms A1 and A2 to solve the same problem. The function
the complexity of the same is respectively. Which
Which algorithm would you choose? Discuss all the possibilities.
12. Provide the algorithm that informs the median of a vector in O(n).
n 2logn (n n)
T(n) n2
64Tn/8 (n2)
Whenever f=O(h) and g=O(h), f=O(g)
If f if g e f=O(g) then g=O(f)
The functions n.log(n) and n.log(n.n) have the same order of complexity.
log(ncθ(log(n)) for any constant c>0
2100is O(1)
2n-1 = O(2n)
2n= (2n-1)
c) A nested loop where the number of times the innermost loop runs depends on the value
of the index in the outermost loop:
for (i = 0; i < N; i++) {
for (j = i; j < N; j++) {
sequence of commands
}
}
18. Find the best-case and worst-case complexity of the following excerpt from
program:
if (x == y) {
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
sequence of commands;
}
}
for (i = 0; i < N; i++) {
sequence of commands
}
}
else {
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
for (k = 0; k < N; k++) {
sequence of commands;
}
}
}
}