Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Lecture 6: Combinatorics
References:
Summary:
We start by recalling the Python datatype of sets and showing a nice trick for finding extreme values of functions on a finite set. Then we discuss useful functions in combinatorics, including cartesian products, how to enumerate subsets, partitions and various types of integer vectors. We also show how to use the online encyclopedia of integer sequences to identify interesting sequences of numbers.
Python warmup: sets
One of the basic data types in Python are sets, which are unordered collections of objects.
If we list an element multiple times, it is still only recorded once in the set:
We can form unions, intersections and relative complements of sets:
An important way to create sets is to obtain them by converting a list (or tuple) into a set:
Exercise
Create the set of all even numbers .
Create the set of all primes between and .
Check the Goldbach conjecture ("Every even number greater than is the sum of two primes") for .
Hint: If only there was a method to check whether a set is a subset of another set.
Solution (uncomment to see)
One final word of warning: elements of a set must be hashable. Here a hash is an integer assigned to a Python object, only depending on its value, so that (hopefully) two objects are equal only if their hashes are equal. An example of an unhashable type are lists, which is why the following does not work:
This could be repaired by using a tuple, i.e. a list that cannot be changed:
Python warmup: finding extreme values
In one exercise below (but also more generally) we encounter the following situation: we have list (or set) L
and a function f
from L
to the real numbers, and want to find an element l
such that f(l)
is minimal (or maximal). The following is a nice trick to find such an element:
This uses that when ordering tuples (such as (f(p,q), p, q)
) and finding the minimum, Python will first sort by the first entry, and only for equal first entries start sorting by the second entry etc.
Exercise
For which integer is the value maximal? What is the value for this (approximately)?
Solution (uncomment to see)
Combinatorics
Below we will see different ways of enumerating certain objects (such as partitions, integer vectors with given sums, permutations etc.). One very useful tool when studying combinatorics is the On-line Encyclopedia of Integer Sequences. This is a database of meaningful sequences of integers. The idea is: if you come across some sequence of numbers and wonder whether there is a pattern explaining them, you can look up this sequence in the OEIS database and check whether it appears somewhere.
Exercise
What is the next element of the sequence
Solution (uncomment to see)
Products
A basic operation in enumerative combinatorics is the cartesian product of sets:
Instead of an exercise, let's just show a fun application:
Question
Can you insert operations +, *, -
in the equation below such that it becomes true?
Subsets
A second operation is forming the power set of , i.e. the set of subsets of .
We can also restrict to subsets of a given size:
Exercise
Consider the alternating group and let be a subset of two different elements of , chosen uniformly at random. Compute the probability Remark: Considering more generally the group , this probability tends to as , as proven by Dixon. Replacing by , the probability tends to . Can you see why it can't be greater than ?
Solution (uncomment to see)
Permutations
Given we can list all permutations of the set :
We can also look at permutations of any other set/list:
Exercise
Consider the following two vectors:
For which permutation is the sum minimal?
Optional: Can you make a guess, even before computing the answer?
Solution (uncomment to see)
Partitions and integer vectors
We have already seen that a partition of is a way to write as a sum of positive integers, regardless of orientation. We can enumerate these as follows:
There are also several optional arguments that can restrict the shape of the partition (see the documentation of Partitions
for more details):
We can also look at the related problem where the order of the summands matters. In other words, of enumerating vectors of integers summing to a given number. Here, since counts as an integer, we have to either restrict the length of the vector, or give some minimal part in order to obtain a finite set (otherwise we have as solutions):
Exercise
A frog sits on the origin of the real axis and wants to hop to the number . It always jumps in positive direction, and its jumps can either have length or length .
Compute the number of ways that the frog can jump from to for .
What is the number in general? Can you see a proof?
Solution (uncomment to see)
Set partitions
Finally, we can enumerate all ways of writing a given set as a disjoint union of subsets:
Exercise
Compute the numbers of set partitions of for .
Find out what the numbers are called.
The numbers have a nice exponential generating series: Use this formula to compute .
Solution (uncomment to see)
Exercise
List all partitions into disjoint subsets such that all have the same sum (e.g. in the smaller example would be a solution, since ).
Solution (uncomment to see)
Appendix: sorting in Python
For some computations it can be useful to sort elements in a list. This comes in two variants: firstly, we can create a new list, which is the sorted version of the old one:
This doesn't change the list itself:
We can also change the original list by sorting it:
We can also sort in the opposite direction:
And finally, we can specify a key
function ourselves. This is a function which sends the elements of our list to numbers, and the list is then sorted in increasing order of these numbers:
Here we used the a lambda
-function, which is a short way to define a function. For example, the following two constructions define the same functions:
If this was a very serious lecture on computer algebra, I could discuss some sorting algorithms and how they allow to sort a list with elements using operations. But it's not, and so we won't.
Assignments
Exercise
Compute the number of conjugacy classes of for .
Do you know (or can you find out) what this sequence of integers is?
Solution (uncomment to see)
Exercise
Recall the following question we saw above:
Can you insert operations
+, *, -
in the equation below such that it becomes true?
Write a function taking a list L
(like [13,9,4,6,21]
) and an integer s
(like 208
) which prints out all possibilities of combining the elements of L
using +,*,-
and obtaining the number s
.
Hint: Some useful functions:
Solution (uncomment to see)