4.4.4.1–4.4.4.3 - Functions, growth and algorithm complexity (Java)
Use functions to express how an algorithm's work and storage change with problem size. Compare growth rates, count permutations and derive Big-O from Java source. This Java route is incomplete: runnable support pending; code questions are written practice.
Functions and growth rates
A mathematical function maps each permitted input value to exactly one output value, and can describe an operation count for each input size.
In , is the domain, the set of permitted inputs, and is the co-domain, the specified set from which outputs are drawn. For , , taking , every positive integer input maps to an even positive integer. The actual output set (the range) is the even positive integers, although the co-domain also contains odd numbers. A co-domain need not equal the range.
These functions illustrate different growth:
| Input x | Linear | Polynomial | Exponential |
|---|---|---|---|
| 1 | 2 | 2 | 2 |
| 2 | 4 | 8 | 4 |
| 4 | 8 | 32 | 16 |
| 8 | 16 | 128 | 256 |
Doubling x doubles , multiplies by four and changes much more sharply for large x. A polynomial has fixed powers of the input; in an exponential, the input appears in the exponent. Linear functions are also a special case of polynomials, but algorithm comparisons usually name the linear class separately.
A logarithm is the inverse of exponentiation. because ; it is defined for positive inputs. counts how many repeated halvings reduce a power-of-two input size to one. The base changes values by a constant factor and therefore does not change a logarithmic Big-O class.
Permutations and factorials
A permutation is an arrangement of distinct objects in an order, and the number of complete arrangements grows factorially.
For three distinct letters A, B and C, choose the first in three ways, the second from two remaining letters, and the last in one way. This gives ABC, ACB, BAC, BCA, CAB and CBA: six arrangements.
For distinct objects, the number of permutations is
For example, . The convention represents the one empty arrangement.
An exhaustive route planner trying every order of n distinct stops has candidate orders before counting the work used to score each one. Increasing from five to six stops changes the candidates from 120 to 720, a sixfold increase. This is very different from linear growth. The distinct-object condition matters: repeated identical letters do not produce different visible words; this lesson's counting tasks specify distinct objects.
public class Main {
static long permutations(int n) {
long result = 1;
for (int factor = 2; factor <= n; factor++) {
result *= factor;
}
return result;
}
}
For non-negative n up to 20, this calculation fits Java's signed long. permutations(4) multiplies 1 by 2, 3 and 4 to return 24. The loop computes the number of permutations with linear work; it does not enumerate all 24 arrangements. The complexity of counting a set is not necessarily the complexity of visiting every member.
Time and space complexity
Complexity compares resource requirements as a function of input size, so the size of the problem is the key variable rather than a single observed runtime.
For a list, n is normally its element count; for a graph, vertices and edges may both matter. Define the size measure before comparing algorithms. Time complexity describes growth in work, such as comparisons. Space complexity describes growth in memory. State whether storage includes the input or only additional working storage.
Big-O notation
An asymptotic upper-bound description of growth, ignoring constant factors and lower-order terms. In usual algorithm comparisons, report the tightest useful class and state the case being considered.
Continue this lesson.
You’re previewing NovaLearn without an account. Create a free account to read the rest.
Create free account