Skip to main content

Discrete Math with SageMath: Learn math with open-source software

Section 5.1 Introduction to Relations

A relation R from set A to set B is any subset of the Cartesian product AΓ—B, indicating that RβŠ†AΓ—B. We can ask Sage to decide if R is a relation from A to B. First, construct the Cartesian product C=AΓ—B. Then, build the set S of all subsets of C. Finally, ask if R is a subset of S.
Recall the Cartesian product consists of all possible ordered pairs (a,b), where a∈A and b∈B. Each pair combines an element from set A with an element from set B.
In this example, an element in the set A relates to an element in B if the element from A is twice the element in B.

Subsection 5.1.1 Relation Composition

If we have two relations with a set in common, such as R from A to B and S from B to C, we can compose them to create a relation between the two non repeated sets, such as S∘R from A to C, where the relation is defined as all aS∘Rc where there exists an element b∈B such that aRb and bSc exist.
We can also use Sage to compose relations.

Subsection 5.1.2 Relations On a Set

When A=B we refer to the relation as a relation on A.
Consider the set A={2,3,4,6,8}. Let’s define a relation R on A such that aRb iff a|b (a divides b). The relation R can be represented by the set of ordered pairs where the first element divides the second: