Salta al contenuto
Note per Studenti Computational Complexity Report

Computational Complexity

In questa pagina 9

An algorithma finite and unambiguous sequence of instructions that, starting from a set of input data, solves a problem in a finite number of steps usually has many alternatives for the same problem: how can we tell that one is "better" than another? For many centuries the study of algorithms focused on correctness. With electronic computers it became evident that correctness is not enough: a correct but too slow algorithm, or one too expensive in memory, can be unusable in practice. Hence the need to analyse algorithms quantitatively. The analysis of algorithms was systematised in the 1960s by Donald Knuth; the theory of computational complexity (the time and memory intrinsically required to solve a problem, whatever the algorithm) dates from 1965. The symbols OO, Ω\Omega, Θ\Theta come from number theory (Bachmann 1894, Landau) and were adopted for algorithms by Knuth. Which problems can be solved efficiently (for instance the open question "P versus NP") is still a subject of research.

1. Why analyse algorithms

Analysis allows us to

  • make performance predictions on inputs never processed before, without having to run them;
  • compare different algorithmic solutions of the same problem and consciously choose the most suitable one for the case at hand.

Two complementary types of analysis exist:

  1. experimental: implement the algorithm and measure its behaviour;
  2. theoretical: reason about the algorithm "on paper", without executing it.

Resources that can be studied:

resource name
execution time time complexity
amount of memory occupied space complexity

We focus mainly on execution time, the most critical resource.

2. Experimental analysis

To evaluate the time efficiency of an implementation we measure the execution time on datasets of different sizes. This measures the scalability of the implementation with respect to the input size.

Running example: reverse returns a reversed copy of a list (last element first, and so on). This is an ineffective implementation, as we shall see.

python
def reverse(my_list):
    # Prepare space for the output list,
    # and treat it as a partially filled list
    n = len(my_list)
    output = [0] * n
    # the number of elements inserted in `output` so far
    output_size = 0

    # For each element of the input, insert it
    # _at the beginning_ of the "output" list, taking
    # care to shift all previously inserted elements
    # by one position, to make room.
    for i in range(n):
        # make room by shifting all elements
        for j in range(output_size, 0, -1):
            output[j] = output[j - 1]
        # write the element at the beginning of the output list
        output[0] = my_list[i]
        # increase the logical size
        output_size += 1

    return output

At each iteration of the outer loop all elements already in output are shifted one position to the right (this is the inner loop for j in range(output_size, 0, -1)), then my_list[i] is written at position 0.

Measuring time

time.time() from the time module returns the number of seconds elapsed since the UNIX epoch (midnight of January 1st, 1970). It returns an instant (a timestamp): a duration is the difference between two timestamps.

python
import time

# run the algorithm and measure execution time
start = time.time()
reverse(my_list)
end = time.time()
elapsed = end - start

For a scalability experiment inputs of increasing length are used; to cover a wide range of sizes with few experiments, the size doubles at each experiment. Results are written as CSVComma Separated Values: one record per line, fields separated by a comma, easy to import in other software such as Excel: input size, comma, time.

python
import time

# Try different sizes, in increasing order
for i in range(1, 11):
    # The size of the list
    n = 2 ** i * 1000
    # The list to reverse, the content doesn't matter
    my_list = [0] * n

    # run the algorithm and measure execution time
    start = time.time()
    reverse(my_list)
    end = time.time()

    # Write to output the problem size
    # and the execution time (in seconds)
    with open("times.csv", "a") as file:
        print(str(n) + "," + str(end - start), file=file)

Measured times of the quadratic reverse (size, seconds):

size time (s)
2000 0.0526
4000 0.2352
8000 0.9751
16000 4.0043
32000 16.4047

Estimating the exponent

When the size doubles, the time tends to quadruple. If the relation were linear the time would simply double, so the relation is non-linear, governed by a polynomial of higher degree. To make it quantitative, suppose the time on an instance of size nn is

T(n)=a⋅nbT(n) = a\cdot n^{b}

with aa and bb constants to determine (bb is the exponent we want). In the ratio of two consecutive measurements (nn and 2n2n) the constant aa cancels:

T(2n)T(n)=(2n)bnb=2b⟹b=log⁡2 ⁣(T(2n)T(n))\frac{T(2n)}{T(n)} = \frac{(2n)^b}{n^b} = 2^b \quad\Longrightarrow\quad b=\log_2\!\left(\frac{T(2n)}{T(n)}\right)

With the data: b=log⁡2 ⁣(T(32000)T(16000))≈log⁡2 ⁣(16.40474.004)≈log⁡24=2b=\log_2\!\left(\dfrac{T(32000)}{T(16000)}\right)\approx\log_2\!\left(\dfrac{16.4047}{4.004}\right)\approx\log_2 4 = 2, so the trend is approximated by T(n)=a n2T(n)=a\,n^2.

Why doubling the size is convenient: it covers a very wide range of values with few measurements, and the ratio T(2n)/T(n)T(2n)/T(n) between consecutive trials directly reveals the exponent.

Limits of the experimental analysis

The measured time is influenced by many factors:

  • the content of the input (integers, strings, lists, ...);
  • the characteristics of the hardware;
  • the programming language used;
  • the interpreter or compiler used;
  • the quality of the code.

So we measure the properties of an implementation of the algorithm, not of the algorithm itself. To reason about the algorithm we need the tools of theoretical analysis.

3. Theoretical analysis: the cost model

Theoretical analysis is qualitative and based on counting the fundamental operations an algorithm performs. It lets us predict performance before implementing, and compare algorithms for the same problem by grouping them into complexity classes.

Key assumption: the running time is proportional to the number of machine instructions executed. Counting exact machine instructions from the code is impractical, so we adopt simplifying assumptions: the cost model.

We assume the execution time depends on: the number of primitive operations (basic steps), the size of the data, the value of the data.

Basic steps are operations with constant execution time, independent of the number, type and value of the data:

operation example
assigning a value to a variable a = 5
arithmetic operations b = a + 5
logical and comparison operations isX = b1 and b2, b1 = a > 5
read or write access to a cell of a list value = my_list[k]

Warnings:

  • a function call is not a primitive operation, since the function may not take constant time. len(my_list) on an already known list has constant cost, but reverse takes a time that grows with the input size: in the count the call must be replaced by the actual cost of the function's execution;
  • the steps considered basic depend on the context. For sorting algorithms only the read and write list accesses are counted as basic steps and all other operations (arithmetic, ...) are given zero cost, because the number of list accesses is a good approximation of the overall work and simplifies the analysis.

4. Asymptotic notations

In theoretical analysis we care about the qualitative trend of performance, in particular for large inputs. We characterise an algorithm with functions f,g:N→R≥0f,g:\mathbb{N}\to\mathbb{R}_{\ge 0} (domain: the natural numbers) and define three sets of functions.

  • Big OO (upper bound). f(n)∈O(g(n))f(n)\in O(g(n)) if there exist constants c>0c>0 and n0∈Nn_0\in\mathbb{N} such that for every n≥n0n\ge n_0: f(n)≤c⋅g(n)f(n)\le c\cdot g(n).
  • Big Ω\Omega (lower bound). f(n)∈Ω(g(n))f(n)\in \Omega(g(n)) if there exist c>0c>0 and n0∈Nn_0\in\mathbb{N} such that for every n≥n0n\ge n_0: f(n)≥c⋅g(n)f(n)\ge c\cdot g(n).
  • Big Θ\Theta (lower and upper bound). f(n)∈Θ(g(n))f(n)\in \Theta(g(n)) if there exist c1>0c_1>0, c2>0c_2>0 and n0∈Nn_0\in\mathbb{N} such that for every n≥n0n\ge n_0: c1 g(n)≤f(n)≤c2 g(n)c_1\, g(n)\le f(n)\le c_2\, g(n).

Intuitively: f∈O(g)f\in O(g) means ff grows at most as fast as gg; f∈Ω(g)f\in\Omega(g) means at least as fast; f∈Θ(g)f\in\Theta(g) means exactly as fast (neither faster nor slower), up to a multiplicative constant.

  • The constants cc, c1c_1, c2c_2 let us ignore multiplicative factors.
  • The threshold n0n_0 lets us neglect the behaviour for small inputs, looking only at the trend for large ones.

In the graphs of the course: for OO, f(n)f(n) stays below c g(n)c\,g(n) for n≥n0n\ge n_0 (even if above it for smaller nn); for Ω\Omega it stays above c g(n)c\,g(n); for Θ\Theta it stays between c1g(n)c_1 g(n) and c2g(n)c_2 g(n).

Equivalently, f(n)∈Θ(g(n))f(n)\in\Theta(g(n)) if and only if f(n)∈O(g(n))f(n)\in O(g(n)) and f(n)∈Ω(g(n))f(n)\in\Omega(g(n)): the "exact" characterisation of Θ\Theta is the combination of an upper and a lower bound. In practice one often finds f(n)=O(g(n))f(n)=O(g(n)) written instead of f(n)∈O(g(n))f(n)\in O(g(n)): a well-established abuse of notation, since O(g(n))O(g(n)) is a set of functions and the "equal" sign must be understood as membership.

Growth rates

Typical classes compared in the course, for the number of operations as the input size grows (O(1)O(1), O(log⁡n)O(\log n), O(n)O(n), O(n2)O(n^2), O(2n)O(2^n)):

Grafico interattivo

Examples

3n2+10000n+101000∈Θ(n2),6∈Θ(1),n2+3log⁡n+5∈Θ(n2),3n^2+10000n+10^{1000}\in\Theta(n^2),\qquad 6\in\Theta(1),\qquad n^2+3\log n+5\in\Theta(n^2), 3n3+10n2+5n∈Θ(n3),3⋅2n+5n15+100n∈Θ(2n)3n^3+10n^2+5n\in\Theta(n^3),\qquad 3\cdot 2^n+5n^{15}+100n\in\Theta(2^n)

Only the fastest-growing term decides the class; constant factors and lower-order terms are absorbed.

The upper bound of OO is not necessarily tight: n∈O(n3)n\in O(n^3). Likewise the lower bound of Ω\Omega: n2∈Ω(n)n^2\in\Omega(n). The Θ\Theta characterisation is always tight: n∉Θ(n2)n\notin\Theta(n^2).

Relation with limits

Let L=lim⁡n→∞f(n)/g(n)L=\lim_{n\to\infty} f(n)/g(n).

  1. If 0<L<∞0<L<\infty then f∈Θ(g)f\in\Theta(g). Proof: by the definition of limit, for every ε>0\varepsilon>0 there is n0n_0 such that for all n≥n0n\ge n_0: L−ε<f(n)/g(n)<L+εL-\varepsilon<f(n)/g(n)<L+\varepsilon. Choose ε=L/2\varepsilon=L/2 (valid because L>0L>0). Then for all n≥n0n\ge n_0: L/2<f(n)/g(n)<3L/2L/2<f(n)/g(n)<3L/2. Since g(n)>0g(n)>0, multiplying by g(n)g(n): (L/2) g(n)<f(n)<(3L/2) g(n)(L/2)\,g(n)<f(n)<(3L/2)\,g(n). This is exactly the definition of Θ\Theta with c1=L/2c_1=L/2 and c2=3L/2c_2=3L/2.
  2. If L=0L=0 then f∈O(g)f\in O(g) (sketch): take ε=1\varepsilon=1; for n≥Nn\ge N, f(n)/g(n)<1f(n)/g(n)<1, so f(n)<g(n)f(n)<g(n) and c=1c=1 works. (Actually f∈o(g)f\in o(g), which is stronger: it holds for every c>0c>0.)
  3. If L=∞L=\infty then f∈Ω(g)f\in\Omega(g) (sketch): by the definition of infinite limit, for M=1M=1 there is NN such that f(n)/g(n)>1f(n)/g(n)>1 for n≥Nn\ge N, so f(n)>g(n)f(n)>g(n) and c=1c=1 works.

5. Deriving the complexity from code

Rules to derive the complexity from pseudocode or code:

  • blocks without loops or decisions: count the basic operations;
  • two blocks executed alternatively (the two branches of an if): take the number of operations of the branch that executes more when deriving an upper bound, the number of the branch that executes fewer when deriving a lower bound;
  • loops: sum the number of operations performed at each iteration.

(Sequential blocks: the operations of the components are summed.)

Example: the quadratic reverse

Block before the loops

python
n = len(my_list)
output = [0] * n
output_size = 0
  • 2 operations for the first line (call to len and writing of n);
  • nn operations to initialise the list output;
  • 1 operation to initialise output_size;

total n+3n+3.

Body of the innermost loop: output[j] = output[j - 1]: one arithmetic operation (j−1j-1), one read access, one write access: 3 operations, constant (it does not depend on the input size).

How many times does the inner loop run? Throughout the function output_size == i holds, so in iteration ii of the outer loop the inner body is executed ii times: the inner loop costs 3i3i.

Body of the outer loop (per iteration ii): 3i3i for the inner loop, plus a read access to my_list and a write access to output (for output[0] = my_list[i]), plus the increment output_size += 1. The course counts this as 3i+33i+3 operations. The outer loop runs nn times, so, including the initial block, the total is

∑i=1n(3i+3)+n+3=3∑i=1ni+4n+3=3 n(n+1)2+4n+3=32n2+112n+3∈Θ(n2)\sum_{i=1}^{n}(3i+3)+n+3 = 3\sum_{i=1}^{n} i + 4n + 3 = 3\,\frac{n(n+1)}{2}+4n+3 = \frac{3}{2}n^2+\frac{11}{2}n+3\in\Theta(n^2)

using the Gauss sum formula

∑i=1ni=n(n+1)2\sum_{i=1}^{n} i=\frac{n(n+1)}{2}

(the course material prints 6n6n for the linear term; the arithmetic gives 112n\frac{11}{2}n, which does not change the class). The algorithm is therefore Θ(n2)\Theta(n^2). By the properties of asymptotic notation the precise count of constant operations is irrelevant: it is enough to identify the terms that grow fastest as nn increases, the only ones that determine the complexity class.

Proof of the Gauss formula.

  • Arithmetic proof: write the sum twice, the second time in reverse order, and add column by column: n+1n+1 for each of the nn columns (pairs nn and 11, n−1n-1 and 22, ..., 11 and nn). So ∑i+∑i=2 [ n+(n−1)+⋯+2+1 ]=n(n+1)\sum i + \sum i = 2\,[\,n+(n-1)+\dots+2+1\,]=n(n+1), hence ∑i=1ni=n(n+1)/2\sum_{i=1}^{n} i=n(n+1)/2.
  • Geometric proof: two staircases of n+(n−1)+⋯+1n+(n-1)+\dots+1 squares fit together into a rectangle n×(n+1)n\times(n+1), which has n(n+1)n(n+1) squares.

Agreement with the experiment. Experimentally, doubling the input made the time quadruple (b=2b=2); the derived Θ(n2)\Theta(n^2) explains it exactly: doubling nn multiplies the time by 22=42^2=4. The two analyses, from different premises, reach the same conclusion and reinforce each other.

6. A linear algorithm for reverse

The inefficiency of the first version comes from inserting each new element at the beginning of output, which forces shifting all elements already present: this "hidden work" in the inner loop makes it quadratic. Idea: visit the input list from the end and write the elements into output starting from the first position, so each element goes directly into its final position, without moving anything.

python
def reverse(my_list):
    # Prepare space for the output list
    n = len(my_list)
    output = [0] * n

    # Visit the input list starting from the last
    # element (index n - 1) down to the first (index 0),
    # copying each element into `output` starting from
    # the first position onward.
    for i in range(n):
        output[i] = my_list[n - 1 - i]

    return output

For i = 0 it writes the last input element into output[0], for i = 1 the second to last into output[1], and so on until output[n - 1] receives the first input element.

Theoretical analysis

  • block before the loop: 2 operations for the first line plus nn for the initialisation of output: n+2n+2;
  • loop body output[i] = my_list[n - 1 - i]: a constant number of arithmetic operations for the index (two subtractions), one read access, one write access: a constant cc (here c=4c=4);
  • there is no inner loop: the body has no operations that depend on nn. The loop runs nn times, so

c⋅n+(n+2)=(c+1) n+2∈Θ(n)c\cdot n+(n+2)=(c+1)\,n+2\in\Theta(n)

The algorithm is linear.

Experimental check (same method, sizes doubling): the times now tend to double, not quadruple.

size time (s)
2000 0.000091
4000 0.000151
8000 0.000318
16000 0.000621
32000 0.001281

b=log⁡2 ⁣(T(32000)T(16000))≈log⁡2 ⁣(0.0012810.000621)≈log⁡22=1b=\log_2\!\left(\dfrac{T(32000)}{T(16000)}\right)\approx\log_2\!\left(\dfrac{0.001281}{0.000621}\right)\approx\log_2 2=1, in agreement with Θ(n)\Theta(n).

7. Comparing the two algorithms

They solve exactly the same problem with the same result, but with radically different performance:

size Θ(n2)\Theta(n^2) (s) Θ(n)\Theta(n) (s) ratio
2000 0.053 0.000091 about 580
4000 0.235 0.000151 about 1560
8000 0.975 0.000318 about 3070
16000 4.004 0.000621 about 6440
32000 16.405 0.001281 about 12800

Two observations:

  1. the linear algorithm is faster: already for n=32000n=32000 it is more than ten thousand times faster;
  2. more important, the gap grows with the input size, from about 580 to about 12800. This is what we expect from the classes: since n2/n=nn^2/n=n, the ratio between the two times grows linearly with the input size.

The choice of the algorithm (and thus of its complexity class) has an impact on performance that no improvement in hardware or programming language could ever compensate for, for sufficiently large inputs. The real difference lies in the asymptotic behaviour, not in the speed on a single input. A faster computer only reduces times by a constant factor, it does not change the order of growth.

8. Worked checks (review questions of the course)

  • Exponent from two times. T(n)=3T(n)=3 s and T(2n)=24T(2n)=24 s: b=log⁡2(24/3)=log⁡28=3b=\log_2(24/3)=\log_2 8=3, so the algorithm is presumably Θ(n3)\Theta(n^3) (cubic).
  • Nested loops.
python
total = 0
for i in range(n):
    for j in range(n):
        total = total + 1

The innermost instruction runs n⋅n=n2n\cdot n=n^2 times with constant cost: Θ(n2)\Theta(n^2).

python
total = 0
for i in range(n):
    for j in range(i):
        total = total + 1

At iteration ii (i=0,…,n−1i=0,\dots,n-1) the inner loop runs ii times, so in total ∑i=0n−1i=n(n−1)2\sum_{i=0}^{n-1} i=\frac{n(n-1)}{2} times. The inner loop is shorter on average than before, but the complexity is still Θ(n2)\Theta(n^2).

  • True or false?
    • 5n2+100n∈Θ(n2)5n^2+100n\in\Theta(n^2): true (the dominant term is n2n^2, the lower-order term is absorbed).
    • n∈O(n2)n\in O(n^2): true (n≤n2n\le n^2 for n≥1n\ge 1; the upper bound need not be tight).
    • n2∈Θ(n)n^2\in\Theta(n): false (n2∈Ω(n)n^2\in\Omega(n) but not O(n)O(n): it grows strictly faster).
    • 2n+n10∈Θ(2n)2^n+n^{10}\in\Theta(2^n): true (the exponential dominates any polynomial term).
    • n∈Ω(n2)n\in\Omega(n^2): false (it would need n≥c n2n\ge c\,n^2 eventually, i.e. 1≥c n1\ge c\,n, impossible for large nn).
    • n3∈O(n3)n^3\in O(n^3): true (take c=1c=1; in fact n3∈Θ(n3)n^3\in\Theta(n^3)).
  • Why constants do not matter: asymptotic notation absorbs constant multiplicative factors and lower-order terms; only the fastest-growing term determines the class, and a constant contribution does not change the order of growth.

See RecursionA function is recursive when it calls itself; it is correct only if it has at least one base case that needs no recursive call and if every recursive call works on a simpler problem that gets closer to a base case (otherwise Python stops with RecursionError, the limit being 1000 nested calls). Each pending call is a record on the call stack. Types: linear (one call, e.g. factorial), binary (two calls, e.g. sum of a list by halves), tail (the call is the last action, solved with an accumulator and easy to turn into a loop), multiple (fib_bad recomputes the same values and explodes). Typical examples: factorial, Fibonacci, reversing a string, Towers of Hanoi (2^n - 1 moves). Recursion is elegant for naturally recursive problems but usually less efficient than a loop.Recursion → for algorithms whose cost is not a simple loop count (for example the doubly recursive Fibonacci).

Typical mistakes

  • Treating a function call as one basic step: its real cost must be counted (len on a known list is constant, reverse is not).
  • Reading n0n_0 and cc as part of the algorithm: they only serve to ignore small inputs and multiplicative constants.
  • Confusing OO with Θ\Theta: n∈O(n3)n\in O(n^3) is true but not tight, while n∉Θ(n2)n\notin\Theta(n^2).
  • Concluding from one measurement: times depend on hardware, language, interpreter and input, so experiments measure an implementation; the exponent is estimated from the ratio T(2n)/T(n)T(2n)/T(n) over increasing sizes.
  • For an if, using the wrong branch: the longer branch for an upper bound, the shorter one for a lower bound.

Versione ripasso

Why. Correctness is not enough: time and memory decide whether an algorithm is usable. Analysis predicts performance on unseen inputs and compares solutions. Focus: time complexity (also space).

Experimental analysis.

  • Time with time.time(): the difference of two timestamps, in seconds.
  • Double the input size at each run (n = 2 ** i * 1000) and write size,time lines to a CSV file.
  • If T(n)=a nbT(n) = a\,n^{b}, then b=log⁡2(T(2n)/T(n))b = \log_2(T(2n)/T(n)). Quadratic reverse: time about x4 when the size doubles, so b≈2b \approx 2.
  • It measures an implementation (hardware, language, interpreter, input content), not the algorithm.

Theoretical analysis. Count basic steps under a cost model: assignment, arithmetic, logical and comparison operations, read or write of a list cell (constant time). A function call is not a basic step: count its real cost. For sorting, only list accesses are counted.

Asymptotic notation (for f,gf, g on the naturals, constants cc, n0n_0):

  • O(g)O(g), upper bound: f(n)≤c g(n)f(n) \le c\,g(n) for n≥n0n \ge n_0.
  • Ω(g)\Omega(g), lower bound: f(n)≥c g(n)f(n) \ge c\,g(n) for n≥n0n \ge n_0.
  • Θ(g)\Theta(g), both: c1g(n)≤f(n)≤c2g(n)c_1 g(n) \le f(n) \le c_2 g(n); equivalent to OO and Ω\Omega together.
  • Constants and lower-order terms are absorbed: 3n2+10000n+101000∈Θ(n2)3n^2+10000n+10^{1000} \in \Theta(n^2).
  • OO need not be tight: n∈O(n3)n \in O(n^3), but n∉Θ(n2)n \notin \Theta(n^2).
  • Growth order to remember: log⁡n\log n, nn, n2n^2, 2n2^n.

Limits. With L=lim⁡n→∞f(n)/g(n)L = \lim_{n\to\infty} f(n)/g(n): if 0<L<∞0<L<\infty then f∈Θ(g)f \in \Theta(g) (with c1=L/2c_1 = L/2, c2=3L/2c_2 = 3L/2); if L=0L=0 then f∈O(g)f \in O(g) (in fact oo); if L=∞L=\infty then f∈Ω(g)f \in \Omega(g).

Deriving complexity from code.

  • Basic blocks: count the operations; sequential blocks: sum them.
  • if: the longer branch for an upper bound, the shorter for a lower bound.
  • Loops: sum the operations of each iteration. Nested for i in range(n) / for j in range(n) gives Θ(n2)\Theta(n^2); with range(i) the inner count is ∑i=n(n−1)/2\sum i = n(n-1)/2, still Θ(n2)\Theta(n^2).

Worked example: quadratic reverse (each element inserted at the front, shifting the others). At iteration ii the inner loop runs ii times, cost 3i3i; the outer body costs 3i+33i+3. Total:

∑i=1n(3i+3)+n+3=32n2+112n+3∈Θ(n2)\sum_{i=1}^{n}(3i+3)+n+3 = \tfrac{3}{2}n^2+\tfrac{11}{2}n+3 \in \Theta(n^2)

using ∑i=1ni=n(n+1)/2\sum_{i=1}^{n} i = n(n+1)/2 (the course prints 6n6n for the linear term; the class is unchanged).

Linear version: write each element directly into its final place, output[i] = my_list[n - 1 - i]; no inner loop, cost (c+1)n+2∈Θ(n)(c+1)n+2 \in \Theta(n). Experimentally the times double.

Comparison. At n=32000n = 32000: 16.405 s against 0.001281 s, a ratio of about 12800; the gap grows linearly with nn. No faster hardware compensates a worse class for large inputs.

Checks. T(n)=3T(n)=3 s and T(2n)=24T(2n)=24 s give b=3b=3, so Θ(n3)\Theta(n^3). 5n2+100n∈Θ(n2)5n^2+100n \in \Theta(n^2) is true; n2∈Θ(n)n^2 \in \Theta(n) is false; 2n+n10∈Θ(2n)2^n+n^{10} \in \Theta(2^n) is true; n∈Ω(n2)n \in \Omega(n^2) is false.

Common mistakes: counting a function call as one step; reading n0n_0 and cc as part of the algorithm; confusing OO with Θ\Theta; concluding from one measurement; taking the wrong branch of an if.

Teoria collegata