*Applied Discrete Structures *by Alan Doerr & Kenneth Levasseur is licensed under a Creative Commons Attribution - Noncommercial - No Derivative Works 3.0 United States License.

Here are a few tips on how to get started using Sage to work with posets, lattices and boolean algebras. These topics are introduced in chapters 6 and 13 of *Applied Discrete Structures*.

Most of the calculations in this notebook are based on the posets package containing the definitions in posets.py and poset_examples.py. They are part of the larger combinatorics package, combinat. More complete documentation: http://www.sagemath.org/doc/reference/combinat/posets.html

Here is one of several ways you can define a poset. It is essentially a digraph, but without any cycles which would violate the antisymmetry property of a poset.

The addition of an edge from 3 to 0 creates a cycle and the error message is very clear on this.

Error in lines 1-1
Traceback (most recent call last):
File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1044, in execute
exec compile(block+'\n', '', 'single', flags=compile_flags) in namespace, locals
File "", line 1, in <module>
File "/ext/sage/sage-8.2_1604/local/lib/python2.7/site-packages/sage/combinat/posets/posets.py", line 691, in Poset
D = transitive_reduction_acyclic(D)
File "sage/graphs/generic_graph_pyx.pyx", line 1461, in sage.graphs.generic_graph_pyx.transitive_reduction_acyclic (build/cythonized/sage/graphs/generic_graph_pyx.c:20528)
raise ValueError("The graph is not directed acyclic")
ValueError: The graph is not directed acyclic

False

In addition to specifying a poset by its edge lists as above, there are several built-in posets, the PendagonPoset being among the simplest.

Finite lattice containing 5 elements

(False, (3, 1, 2))

If a poset has unique minimal and maximal elements, and each pair of elements has a greatest lower bound (the meet) and least upper bound (the join) then we can consider whether elements have complements. These conditions are met for the pentagon poset.

[True, True, True, True]

Each element of the pentagon poset does indeed have a complement. In fact, 1's complement is not unique, but only one of the compements is given here.

[[0, 1, 2, 3, 4], [4, 3, 1, 1, 0]]

A simple example where complements do not exist for SOME elements is the total ordering of set of integers 0, 1, 2, and 3. This is a predefined poset called **ChainPoset(4)**. The middle elements, 1 and 2 have no complement.

[3, None, None, 0]

Among the built-in posets is a function called RandomPoset. Here is documentation:

**File:** /tmp/sage-mac-app/local/lib/python2.6/site-packages/sage/combinat/posets/poset_examples.py

**Type:** <type â€˜instancemethodâ€™>

**Definition:** Posets.RandomPoset(n, p)

**Docstring:**

Generate a random poset on

nvertices according to a probability distributionp.EXAMPLES:

sage: Posets.RandomPoset(17,.15) Finite poset containing 17 elements

Here is a single random poset on {0, 1, 2, ... 7}.

Suppose you wanted a random poset that had maximal and minimal elements, and had greatest lower bounds and least upper bounds for each pair of elements in the set. Here is a bit of code that will give it to you. In generating posets, we set the probability of an edge being included to 0.5. Changing this would clearly have an effect on the distribution of results.

Here is the operation table for the greatest lower bound operation on the poset above.

True

Error in lines 1-1
Traceback (most recent call last):
File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1188, in execute
flags=compile_flags) in namespace, locals
File "", line 1, in <module>
File "sage/structure/category_object.pyx", line 819, in sage.structure.category_object.CategoryObject.__getattr__ (build/cythonized/sage/structure/category_object.c:6687)
return self.getattr_from_category(name)
File "sage/structure/category_object.pyx", line 834, in sage.structure.category_object.CategoryObject.getattr_from_category (build/cythonized/sage/structure/category_object.c:6850)
attr = getattr_from_other_class(self, cls, name)
File "sage/cpython/getattr.pyx", line 389, in sage.cpython.getattr.getattr_from_other_class (build/cythonized/sage/cpython/getattr.c:2534)
raise AttributeError(dummy_error_message)
AttributeError: 'FinitePoset_with_category' object has no attribute 'meet_matrix'

You might notice immediately that the entries in the matrix don't always match the poset diagram. This is because the matrix entries are indicees to the list of poset elements that you get from the list method:

[0, 1, 2, 3, 7, 4, 6, 5]

We close with some further examples of built-in posets. The first is an ordering of the partitions of a number, 4 in this case, according to whether each partition is an refinement of the other. Here, refinement means taking a list of numbers that add up to 4 and replacing a number with two numbers that add to it. So 2,2 is refined to 2,1,1 by breaking one of the 2's into 1 and 1.

Here is the ordering diagram of a boolean algebra with $2^4$ elements.

True

If you want to save a copy of this graph to your computer, you can't use **show()** because of the way it produces the image above. Use **plot()** instead and then **save( filename)**.

Why this is a diamond poset should be clear:

(False, (1, 2, 3))

For those who are unfamiliar with the weak ordering on permutations, see the discrete math wiki page. For basic information on permutations, see Section 15.3 of Applied Discrete Structures.

Finite poset containing 4 elements

Poset of Subsets

{1, 2, 3}

{{3}: [{3}, {1, 3}, {2, 3}, {1, 2, 3}], {1, 2}: [{1, 2}, {1, 2, 3}], {}: [{}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}], {2, 3}: [{2, 3}, {1, 2, 3}], {1}: [{1}, {1, 2}, {1, 3}, {1, 2, 3}], {1, 3}: [{1, 3}, {1, 2, 3}], {1, 2, 3}: [{1, 2, 3}], {2}: [{2}, {1, 2}, {2, 3}, {1, 2, 3}]}

{{3}: [{1, 3}, {2, 3}, {1, 2, 3}], {1, 2}: [{1, 2, 3}], {}: [{1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}], {2, 3}: [{1, 2, 3}], {1}: [{1, 2}, {1, 3}, {1, 2, 3}], {1, 3}: [{1, 2, 3}], {1, 2, 3}: [], {2}: [{1, 2}, {2, 3}, {1, 2, 3}]}

Finite poset containing 9 elements

True