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 , defined by , 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 to a count of operations, for example , with positive integers used here for problem sizes.
Different families of functions grow at different rates:
| Function | Family | n = 2 | n = 4 | n = 8 |
|---|---|---|---|---|
| Linear | 4 | 8 | 16 | |
| Polynomial, here quadratic | 8 | 32 | 128 | |
| Exponential | 4 | 16 | 256 | |
| Logarithmic | Approximately 0.301 | Approximately 0.602 | Approximately 0.903 |
A polynomial has fixed nonnegative integer powers such as . In an exponential function, the variable is in the exponent: is not . Doubling doubles and quadruples , whereas increasing by one doubles . Small input values alone may conceal the eventual difference.
A logarithm is the inverse of exponentiation: because . The logarithmic function has positive real inputs and real outputs, unlike the illustrative integer operation-count mapping. Repeatedly halving a problem explains 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 distinct objects, the number of permutations is
Thus , , and . Going from to objects multiplies the number by . The empty collection has one empty ordering, so . The formula assumes distinct objects: the word LEVEL has repeated letters, so blindly using 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:
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 for . 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