Applied Discrete Structures by Alan Doerr & Kenneth Levasseur is licensed under a Creative Commons Attribution - Noncommercial - ShareAlike 3.0 United States License.
This document describes the basic ways in which a relation can be represented with Sage. These topics are introduced in chapter 6 of Applied Discrete Structures.
Example 1. Consider the "random relation" the set of digits, 0 through 9, which each pair of digits is related with probablity 0.25. The list of digits, which we call A, is created using range. In Python, the default first number in range is 0 and the list specification is "open on the right" so that the upper limit is not included. Therefore range(10) produces the digits 0 through 9.
Here is the complete list of pairs from which we draw approximately 25% for our relation.
The result of the next expression is random. The output represents a fundamental way to represent any relation as a set of ordered pairs.
There are several wasy to create a directed graph (DiGraph) in Sage. The most basic as a dictionary. The following code creates a dictionary corresponding to the relation.
To recover the information defined within a (small) dictionary, use the
We can plot the graph. For larger relations, the information you get may not be very easy to read, but here it is clear.
Here is the adjacency matrix of the graph. Row i column j is 1 if the edge [i,j] is part of the relation. Recall that in Python, indexing of matrices starts at 0 just as with lists; so the first row is row 0, corresponding to the digit 0. This is why we used the digits as our vertex set.
The square of this matrix gives us information on the number of paths of length 2 between any two vertices.
The cube gives us information on paths of length 3.
The matrices above are technically not adjacencly matrices since they are not 0-1 matrices. See Example 3, below, for how to remedy this situation.
We define on the divisors of 60 by is a prime.
This short example illustrates the transitive closure of a relation. We start by defining a relation directly as a simple dictionary. The graph is a "chain" of length 4.
The transitive closure of a relation is the smallest transitive relation containing that relation.
Example 3. This next example is of a relation that is defined in terms of a boolean function. It is based on mod 17 arithmetic, where is defined to be the remainder when 17 is divided into . For example since
The set of remainders when you divide by 17 is but since 17 is prime and we start with the positive remainders, products will never equal 0 mod 17.
We define the relation on by if or . In Sage, this translates to the following function.
Changing the number 3 and 5 in the definition above to 3 and 6 creates an interesting alternative relation. Also changing the modulus can be interesting, but if you use a non-prime modulus, you have to include 0 as a possible remainder.
Again, we build a dictionary based on the boolean function.
The square of relation m can be determined by squaring the adjacency matrix of m. Thanks to Jason Grout, who pointed out how to turn all nonzero entries in the product to a 1. By doing that you lose the information about the number of paths of length two between any pair of vertices, but the plot of the graph is much nicer.
Here is the graph of the square of m, but with multiple edges.