4.4.4.1–4.4.4.3 - Functions, growth and algorithm complexity (C#)

4.4.4.1–4.4.4.3 - Functions, growth and algorithm complexity (C#)

Use mathematical functions to compare growth, count permutations and derive the time and space demands of C# algorithms. This route uses written C# tasks and is incomplete: runnable support pending.

Functions and growth

A mathematical function maps each input in its domain to exactly one output in its declared co-domain.

Domain and co-domain

The domain is the set of permitted inputs. The co-domain is the declared set from which outputs are drawn. The range is the subset of the co-domain actually produced.

For f:{1,2,3}→{1,2,3,4,5,6}f:\{1,2,3\}\rightarrow\{1,2,3,4,5,6\}, defined by f(x)=2xf(x)=2x, the outputs are 2, 4 and 6. The co-domain contains six values; the range contains three. A function may map different inputs to the same output, but it cannot assign two different outputs to one input. A complexity function can map a problem size nn to a count of operations, for example T:N→NT:\mathbb{N}\rightarrow\mathbb{N}, with positive integers used here for problem sizes.

Different families of functions grow at different rates:

FunctionFamilyn = 2n = 4n = 8
2n2nLinear4816
2n22n^2Polynomial, here quadratic832128
2n2^nExponential416256
log⁡10n\log_{10}nLogarithmicApproximately 0.301Approximately 0.602Approximately 0.903

A polynomial has fixed nonnegative integer powers such as 3n3+2n+13n^3+2n+1. In an exponential function, the variable is in the exponent: 2n2^n is not n2n^2. Doubling nn doubles 2n2n and quadruples 2n22n^2, whereas increasing nn by one doubles 2n2^n. Small input values alone may conceal the eventual difference.

A logarithm is the inverse of exponentiation: log⁡101000=3\log_{10}1000=3 because 103=100010^3=1000. The logarithmic function has positive real inputs and real outputs, unlike the illustrative integer operation-count mapping. Repeatedly halving a problem explains log⁡2n\log_2n growth: reducing 16 candidates to 1 takes four halvings. Logarithms with different fixed bases differ by a constant factor, which does not change their Big-O class.

Permutations and factorials

A permutation is an ordering of a set of objects, and distinct objects can be ordered in factorially many ways.

For the three distinct letters A, B and C, there are three choices for the first position, two remaining choices for the second and one for the third. The six orders are ABC, ACB, BAC, BCA, CAB and CBA.

For nn distinct objects, the number of permutations is

n!=n(n−1)(n−2)⋯2⋅1.n!=n(n-1)(n-2)\cdots2\cdot1.

Thus 4!=244!=24, 5!=1205!=120, 6!=7206!=720 and 8!=403208!=40320. Going from nn to n+1n+1 objects multiplies the number by n+1n+1. The empty collection has one empty ordering, so 0!=10!=1. The formula assumes distinct objects: the word LEVEL has repeated letters, so blindly using 5!5! would count some identical letter strings multiple times.

An exhaustive route planner that considers every ordering of six different delivery stops examines 720 candidates. Checking each candidate's distance requires further work; the candidate count alone is not necessarily the algorithm's complete operation count. Pruning impossible prefixes may improve practical performance, but it does not automatically make the worst case small.

The following C# calculation uses a bounded input so its result fits in long:

C#
static long Factorial(int n) // Precondition: 0 <= n <= 20.
{
    long result = 1;
    for (int factor = 2; factor <= n; factor++)
        result *= factor;
    return result;
}

For n = 4, result becomes 2, 6 and 24. For n = 0 or 1 the loop body is skipped and the method returns 1. The value calculated grows factorially, but the number of loop iterations is n−1n-1 for n≥2n\ge2. Under the ordinary fixed-width arithmetic model, this calculation has linear time. Do not confuse the growth of an output value with the cost of computing it. Unbounded factorials would require larger-number arithmetic and a different model.

Time and space complexity

Complexity compares resource demands as a function of problem size, rather than treating one machine's running time as a universal result.

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account