Data structures and algorithms

Exercises: Complexity, parts B–C

Note: The exercises are optional, but highly recommended. If you are uncertain about any problem or want to discuss, write in the course discussion channel, or ask a teacher.

The complexity exercises are divided into two parts, one early in the course and a second part later on. These B+C complexity exercises consist of both core and bonus exercises. We recommend that you try all the core exercises. Try some of the bonus exercises if you want to learn more.

Note: First solve all the early A complexity exercises before you try the ones here.

Core exercises (part B)

B1. Simplify the following orders of growth.

B2. Suppose we want to be able to delete elements from a dynamic array of size n.

B3. Give proofs of the following rules for O-notation from the lecture:

B4. What are the asymptotic (time) complexities of the following recursive functions?

First write down the mathematical function T(n) that the procedure computes (i.e. how many times the statement sum+=1 is executed). Since these are recursive functions, you should write a recurrence relation for T(n) which may use O-notation. You can then write down the order of growth of each function.

Bonus exercises (part C)

C1 (not really a problem). Read through some of the stories at Accidentally Quadratic and think about why the asymptotic complexity is quadratic.

C2. Try some of the problems from chapter 1.4 in S&W, such as these ones. They can all be found (here, but you have to scroll down a bit).

C3. Prove or disprove the following statements:

C4. For f, g : ℕ → ℝ>0, we consider f less or equal than g if f(n) ∈ O(g(n)). Convince yourself that this defines a preorder on functions from natural numbers to positive reals. This means the following:

Conclude that f(n) ∈ O(g(n)) is equivalent to O(f(n)) ⊆ O(g(n)), so the preorder is equivalently that of subset inclusion.

Show that the above preorder is not a total order (so orders of growth, and also complexity classes, are not ordered linearly!). That is, there are f, g : ℕ → ℝ>0 such that neither f(n) ∈ O(g(n)) nor g(n) ∈ O(f(n)). Show that this holds even when restricted to monotone (increasing) functions.

C5. Let f : ℕ → ℝ>0 be monotone (increasing). We say that f has polynomial order of growth if there is k ∈ ℕ such that f(n) ∈ O(nk). Show that f has polynomial order of growth if f(2n) ∈ O(f(n)).