{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is an ipython (jupyter) worksheet with code from the lecture notes for the course\n",
"**Permutation Puzzles: A Mathematical Perspective**, by Jamie Mulholland\n",
"\n",
"Coures webpage: http://www.sfu.ca/~jtmulhol/math302
\n",
"Course notes booklet: http://www.sfu.ca/~jtmulhol/math302/notes/302notes.pdf"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Lecture 3: Permutations"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Section 3.1 Permutation: Preliminary Definition\n",
"\n",
"Curly braces { } denote *sets*, i.e. the order that elements are listed doesn't matter. Square braces [ ] denote lists, i.e. the order that elements appear does matter. So as sets {1,2,3} = {2,1,3} but as lists [1,2,3] $\\neq$ [2,1,3]."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Set([1,2,3])==Set([2,1,3])"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[1,2,3]==[2,1,3]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Example 3.2**: Find the number of ways to arrange 7 books on a shelf. "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"5040"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"factorial(7)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can use SageMath to generate permutations of a list, for example [1,2,3]."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Permutations of the set [1, 2, 3]\n"
]
}
],
"source": [
"terms=[1,2,3]\n",
"p = Permutations(terms)\n",
"print p"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"p.list()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"p.cardinality()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"factorial(3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Example 3.5:** Two permutations of the multi-set [a,a,b,b,b] are [b,a,b,a,b] and [b,b,a,a,b]. There are $\\dfrac{5!}{2!\\cdot3!}=10$ permutations in total."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Permutations of the multi-set [a, a, b, b, b]\n"
]
}
],
"source": [
"var('a,b');\n",
"terms=[a,a,b,b,b];\n",
"p = Permutations(terms)\n",
"print p"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[[a, a, b, b, b],\n",
" [a, b, a, b, b],\n",
" [a, b, b, a, b],\n",
" [a, b, b, b, a],\n",
" [b, a, a, b, b],\n",
" [b, a, b, a, b],\n",
" [b, a, b, b, a],\n",
" [b, b, a, a, b],\n",
" [b, b, a, b, a],\n",
" [b, b, b, a, a]]"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"p.list()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"p.cardinality()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"factorial(5)/(factorial(2)*factorial(3))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Section 3.2 Permutation: Mathematical Definition\n",
"\n",
"### Section 3.2.2 Permutations\n",
"\n",
"A **permutation** of a set A is a function $\\alpha : A \\rightarrow A$ that is bijective (i.e. both one-to-one and onto).\n",
"\n",
"For example, we can define a permutation $\\alpha$ of the set $\\{1,2,3\\}$ by stating:\n",
"$$\\alpha(1)=2, \\quad \\alpha(2)=1, \\quad \\alpha(3)=3.$$ \n",
"\n",
"In SageMath we can use the `Permutation()` command to construct a permutation. Here we define the permutation by the list of images $[\\alpha(1), \\alpha(2), \\ldots]$."
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[2, 1, 3]"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a=Permutation([2,1,3]); a # permutation which maps 1->2, 2->1, 3->3"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a(1)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a(2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here is an example of how to use matrices in SageMath to display a permutation in array form. We can use the `matrix()` command, where the syntax is the following.
\n",
"`matrix( [ , ] )`"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[1 2 3]\n",
"[2 1 3]"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a=Permutation([2,1,3])\n",
"matrix([[1,2,3],[a(i) for i in [1,2,3]]])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Section 3.3 Composing Permutations\n",
"\n",
"Let $\\alpha$ and $\\beta$ be two permutations of [1,...,n]. We wish to define a new permutation $\\alpha\\circ \\beta$, called the *permutation composition*. In order to define a function we just need to specify how it maps the elements. For $k\\in [n]$ we'll define $(\\alpha\\circ \\beta)(k)$ to be the result of first applying $\\alpha$, then applying $\\beta$ to the result. In other words, \n",
"\n",
"$$(\\alpha \\circ \\beta) (k) = \\beta(\\alpha(k)), \\text{ for $k \\in [n]$.}$$ \n",
"\n",
"(Warning: Notice that the composition is from left-to-right, which is opposite to the way functions were combined in calculus.)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a = [5, 3, 1, 4, 2]\n",
"b = [5, 3, 2, 1, 4]\n"
]
}
],
"source": [
"a=Permutation([5,3,1,4,2]); print \"a =\", a\n",
"b=Permutation([5,3,2,1,4]); print \"b =\", b"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[4, 2, 5, 1, 3]"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a*b"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Permutation composition is not commutative in general. That is, we typically have $\\alpha\\beta \\ne\\beta\\alpha$."
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[2, 1, 3, 5, 4]"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b*a"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Section 3.5 Inverses of Permutation\n",
"\n",
"SageMath has a built in `inverse` command."
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[2, 3, 1, 5, 4]"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a=Permutation([3,1,2,5,4])\n",
"a.inverse()"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[3, 4, 1, 2]"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b=Permutation([3,4,1,2])\n",
"b.inverse()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"\n",
"**Note**: This lecture provided a very basic introduction to permutations in SageMath. We will be interested in doing algebra with permutations so we will use a different way to work with permuations in SageMath. This will be introduced in Lecture 4, after we introduce the *Symmetric Group*.\n",
"\n",
"
"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "SageMath 7.3",
"language": "",
"name": "sagemath"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}