You are on page 1of 3

Exercises - Algorithmics

SOLUTIONS

Question 1
Consider the following pairs of functions. Prove or disprove that f ∈ O(g).
(a) f (x) = x and g(x) = 2x.
(b) f (x) = 2x and g(x) = x.
(c) f (x) = x2 and g(x) = x log x.

(d) f (x) = x x and g(x) = x2 .
(e) f (x) = 2x3 − x2 + 10 and g(x) = x3 − x.
(f) f (x) = 3x and g(x) = 2x .
Solution
Recall that the statement f ∈ O(g) means that there exist numbers a and c such that ∀x > a,
we have f (x) < c · g(x). To prove that f ∈ O(g), we need to simply find such a and c. To
disprove it, we need to show that for any choice of a and c, there exists an x > a such that
f (x) ≥ c · g(x).
(a) Yes, for example, take a = 1 and c = 1.
(b) Yes, for example, take a = 1 and c = 3.
(c) No, for example, take x > c2 .
(d) Yes, for example, take a = 1 and c = 1.
(e) Yes, for example, take a = 10 and c = 3.
(f) No, for example, take x > log3/2 c.
Question 2
Can an algorithm have:
(a) a best-case running time in Ω(n) and a worst-case running time in O(n2 )?
(b) a best-case running time in O(n) and a worst-case running time in Ω(n2 )?
(c) a best-case running time in Θ(n) and a worst-case running time in Θ(n2 )?
(d) a best-case running time in Ω(n2 ) and a worst-case running time in O(n)?
(e) a best-case running time in O(n2 ) and a worst-case running time in Ω(n)?
(f) a best-case running time in Θ(n2 ) and a worst-case running time in Θ(n)?
Solution

(a) Yes, this is possible; it means that on every possible input the algorithm takes between
linear and quadratic time.
(b) Yes, this is possible; this means the algorithm is strictly faster on some inputs than on
others.
(c) Yes, this is (a) and (b) at the same time.
(d) No, the best case must be faster than the worst case, so it cannot have a higher upper
bound than the worst case has a lower bound.
(e) Yes, this is possible, but the bounds could not be tight.
(f) No, because Θ(n) is strictly slower than Θ(n2 ).
Question 3
Give pseudocode for an algorithm to find the largest element in an array. How efficient is your
algorithm?

1
Solution
Data: A: an array of numbers
x = −∞;
i = 1;
while A has at least i elements do
if A[i] > x then
x = A[i];
end
i = i + 1;
end
return x;
This algorithm loops over the array once, which takes O(|A|) time.
Question 4
Give pseudocode for an algorithm to check if a graph has any triangles (cycles of length 3).
How efficient is your algorithm?
Solution
Data: G = (V, E): a graph
b = false;
for u ∈ V do
for v ∈ V do
for w ∈ V do
if (u, v) ∈ E ∧ (v, w) ∈ E ∧ (u, w) ∈ E then
b = true;
end
end
end
end
return b;
This algorithm checks all triples of vertices, which is not particularly efficient. Assuming
we stored the graph in an adjacency matrix, it runs in O(|V |3 ) time.
Question 5
Give pseudocode for an algorithm to check if a polygon is convex. How efficient is your
algorithm?
Solution
Data: P : a sequence of points
b+ = true;
b− = true;
for pi ∈ P do
~v = P [i + 1] − pi ; // indices loop around;
~ = P [i + 2] − pi ; // indices loop around;
w
c = ~v .x · w.y
~ − ~v .y · w.x;
~
if c < 0 then
b+ = false;
end
if c > 0 then
b− = false;
end
end
return b+ ∨ b− ;

2
This algorithm checks all consecutive triples of points, of which there are linearly many.
So, it runs in O(|P |) time.
Question 6
Give pseudocode for an algorithm to check if a graph is connected. How efficient is your
algorithm?
Solution
Data: G = (V, E): a graph
pick any v ∈ V ;
marked(v) = true;
R = {v};
while R 6= V do
pick any v ∈ R;
for e ∈ E connected to v do
w = the other vertex of e;
if not marked(w) then
marked(w) = true;
R = R ∪ {w};
end
end
end
b = true for v ∈ V do
if not marked(w) then
b = false;
end
end
return b;
This algorithm visits every edge of the graph at most once, so it takes O(|E|) time, provided
that we can efficiently access the neighbours of each vertex (that is, if the graph is stored as
an adjacency list rather than an adjacency matrix).

You might also like