2. Methodical guidelines / Методичні вказівки
This section is self-contained: it develops the theory of the work’s theme — combinatorial counting and descriptive statistics — together with algorithms for the tasks in 4task.md. No outside material is required.
2.1 Permutations, arrangements, combinations
Let be the number of available elements and the size of a selection ().
- A permutation is an ordering of all elements. Their number is
- An arrangement (written here; the same count is in Lecture 13 and
Practical 6) is an ordered selection of elements (order matters):
- A combination is an unordered selection of elements (order does not
matter):
The single distinction is order: arrangements count and separately; combinations treat them as the same selection. For : , , and .
2.2 Computing the counts
Compute a factorial with a simple loop. For and , prefer the multiplicative form over dividing two large factorials — it keeps the numbers small and exact:
function factorial(n):
r ← 1
for i from 2 to n: r ← r * i
return r
function arrangements(n, k): # A(n,k) = n·(n-1)···(n-k+1)
r ← 1
for i from 0 to k-1: r ← r * (n - i)
return r
function combinations(n, k): # C(n,k) = A(n,k) / k!
return arrangements(n, k) / factorial(k)
2.3 Descriptive statistics
For a dataset :
- the mean (mathematical expectation) is the average value
- the variance measures the average squared spread about the mean
- the standard deviation is its square root, in the same units as the data, .
(The formula above is the population variance, dividing by ; a sample variance divides by .) For the dataset : , the squared deviations sum to , so and .
![Expectation as the weighted-average balance point of a distribution: E[X] = 3.5 for a fair die](img/expectation.png)
2.4 Generating combinatorial objects
Counting says how many; generating produces the objects themselves. Permutations are generated by recursion — fix each element in turn as the first, and permute the rest:
function permutations(elements):
if length(elements) ≤ 1: return [ elements ]
result ← empty list
for i from 0 to length(elements) - 1:
first ← elements[i]
rest ← elements without index i
for p in permutations(rest):
append [first] + p to result
return result
If elements is sorted, this emits them in lexicographic order:
— six in total ().
Combinations of size are generated similarly: for each element, either include
it (and choose from the rest) or skip it.
2.5 Complexity
Computing , , or takes (or ) multiplications. The statistics take one or two passes over the data, . Generating all permutations is inherently — the output alone has items — so explicit generation is practical only for small .