2. Methodical guidelines / Методичні вказівки
This section is self-contained: it collects all the theory of the work’s theme — ordered pairs, the Cartesian product, and binary relations — together with practical guidance for carrying out the tasks in 4task.md. No outside material is required.
2.1 Ordered pairs and the Cartesian product
An ordered pair is two objects taken in a definite order: is the first component, the second. Unlike a set, order matters and repetition is meaningful:
Thus , whereas .
The Cartesian product of sets and is the set of all ordered pairs whose first component comes from and second from :
Its size is the product of the sizes:
For example, , which has elements. In general : the pairs and are different. If either factor is empty, the product is empty.

2.2 Binary relations
A binary relation from to is any subset . When we say “ is related to .” If we call a relation on .

Because a relation is just a set of ordered pairs, everything from set theory applies to it. Two consequences used in this work:
- Validity. A list of ordered pairs is a valid relation for and
exactly when every pair in it satisfies and —
i.e. the list is a subset of . This is what
isRelationValidchecks. - Counting. Since has elements, the number of distinct relations from to is (every subset is a relation).
2.3 Relations defined by a predicate
A relation is most often described not by listing pairs but by a predicate — a condition that each pair either satisfies or not:
To build you walk over the candidate pairs and keep those for which holds. Typical predicates are “ divides ”, “”, or “ and have the same parity.” The predicate itself decides the fine print — for instance whether the pair is allowed.
Two of this work’s tasks are exactly this pattern:
findRelationsapplies a predicate to pairs drawn from a single set (a relation on that set);filteredCartesianProductapplies a predicate to pairs of a product , keeping only those that pass.
Both are the same idea — form the candidate pairs, then filter by .
2.4 Representing pairs and products without built-in types
Represent an ordered pair as a fixed two-element structure (a 2-tuple or a length-2 array ), and a product / relation as a list of such pairs. The membership test from Laboratory Work 1 — scan a list and compare — is reused to check whether a component belongs to a set:
function contains(S, x):
for y in S:
if y == x: return true
return false
2.5 Reference algorithms
function cartesianProduct(A, B):
R ← empty list
for a in A:
for b in B:
append (a, b) to R
return R
function isRelationValid(relation, A, B):
for (a, b) in relation:
if not contains(A, a) or not contains(B, b):
return false # a pair lies outside A × B
return true
function findRelations(A, P): # relation on A defined by predicate P
R ← empty list
for a in A:
for b in A:
if P(a, b): append (a, b) to R
return R
function filteredCartesianProduct(A, B, P):
R ← empty list
for a in A:
for b in B:
if P(a, b): append (a, b) to R
return R
The predicate P(a, b) is supplied by the caller; whether it admits the pair
is part of its definition, so findRelations does not special-case it.
2.6 Size and complexity
cartesianProductproduces pairs and runs in — this is optimal, since the output itself has that size.isRelationValidcosts with a list-based membership test (each pair triggers two scans); a hash-based membership test would reduce this to .findRelationsevaluates the predicate on all ordered pairs, so it is where is the cost of one predicate call; likewisefilteredCartesianProductis .