---
format:
  html:
    html-math-method: katex
---


A Game of Permutations, Part 1: Basics
======================================

In the time since [my last post]() discussing graphs, I have been spurred on to continue playing with them, with a slight focus on abstract algebra. This post will primarily focus on some fundamental concepts before launching into some constructions which will make the journey down this road more manageable. However, I will still assume you are already familiar with what both a [group](https://mathworld.wolfram.com/Group.html) and a [graph](https://mathworld.wolfram.com/Graph.html) are.


The Symmetric Group
-------------------

Some of the most important finite groups are the symmetric group of degree *n* ($S_n$). They are the groups formed by permutations of lists containing *n* elements. There is a group element for each permutation, so the order of the group is the same as the number of permutations, $n!$.

Note: Since both lists and groups consist of "elements", I will do my best to refer to objects in a group as "group elements" and the objects in a list as "items".

There are many ways to denote elements of the symmetric group. I will take the liberty of explaining some common notations, each of which are useful in different ways. More information about them can be found [elsewhere](https://mathworld.wolfram.com/Permutation.html) [online](https://en.wikipedia.org/wiki/Permutation#Notations) as well as any adequate group theory text.


Naive List Notation
-------------------

Arguably the simplest way to do things is just apply the permutation to the list $[1, 2, 3]$. Take the element of $S_3$ which can be described as the action "assign the first item to the second index, the second to the third, and the third to the first". When applied to our list, it results in $[3, 1, 2]$, since "1" is in position 2, and similarly for the other items.

The problem is that this choice is too results-oriented. We can't compose group elements in a meaningful way since all of the information about the permutation is in the position of items in the list, rather than the items of the list themselves.


True List Notations (One- and Two-line Notation)
------------------------------------------------

Therefore, we do the opposite. We instead denote an element of the group by a list of (1-based) indices. The *n*th item in the list describes the position in which *n* can be found after the element acts on a list. Using the same example as previous, we obtain the list $[\![2, 3, 1]\!]$, using double brackets to express that it is a permutation. The first item "2" describes the destination of 1, and similarly for the other items.

This is known as "one-line notation". By using it, it is straightforward to encode symmetric group elements on a computer. After all, we only have to read the items of a list by the indices in another. Here's a compact definition in Haskell:

<!--
TODO: This seems wrong.
--->
```{haskell}
-- convenient wrapper type for below
newtype Permutation = P { unP :: [Int] }
apply :: [a] -> Permutation -> [a]
apply xs =
  map (        -- for each item of the permutation, map it to...
     (xs !!) . -- the nth item of the first list
     (+(-1))   -- (indexed starting with 1)
   ) . unP         -- (after undoing the wrapping)
-- written in a non-point free form
apply' xs (P ys) = map ( \n -> xs !! (n-1) ) ys

print $ [1,2,3] `apply` P [2,3,1]
```

One-line notation can be made made more explicit by writing the list $[1, 2, 3, ..., n]$ above it, resulting in "two-line notation". A distinct benefit over one-line notation is that we can easily find the inverse of an element. All we have to do is sort the columns of the permutation by the value in the second row, then swap the two rows.

$$
[\![2, 3, 1]\!]^{-1} =
\begin{bmatrix}
1 & 2 & 3 \\
2 & 3 & 1
\end{bmatrix}^{-1} =
\begin{bmatrix}
3 & 1 & 2 \\
1 & 2 & 3
\end{bmatrix}^{-1} =
\begin{bmatrix}
1 & 2 & 3 \\
3 & 1 & 2
\end{bmatrix} =
[\![3, 1, 2]\!]
$$

This also makes clear what the naive notation describes: the inverse of the (true) one-line notation.


While lists are great for being explicit and easy to describe for a computer, humans can easily misinterpret them, and mistakenly switch back and forth between naive and true list notation. There is also a lot of redundancy: $[\![2, 1]\!]$ and $[\![2, 3, 1]\!]$ both describe a group element which swaps the first two items of a list. Worst of all, the notation is so explicit that it is group-theoretically unintuitive.


Cycle Notation
--------------

*Cycle notation* addresses all of these issues at the expense of a more complex group operation. Let's try phrasing our earlier element differently. We start at position 1 and follow it to "2". Then, we see that in position 2, we have "3", and in position 3 we have "1". We have just described a *cycle*, since continuing in this manner would go on forever. We denote this cycle as $(1 ~ 2 ~ 3)$.

Cycle notation is much more delicate than list notation, since the notation is nonunique:

- Naturally, the elements of a cycle may be cycled to produce an equivalent expression.

    - $(1 ~ 2 ~ 3) = (3 ~ 1 ~ 2) = (2 ~ 3 ~ 1)$

- Cycles which have no common elements (i.e., are disjoint) commute, since they act on separate parts of the list.

    - $(1 ~ 2 ~ 3)(4 ~ 5) = (4 ~ 5)(1 ~ 2 ~ 3)$

The true benefit of cycles is that they are easy to manipulate algebraically. For some reason, [Wikipedia](https://en.wikipedia.org/wiki/Permutation#Cycle_notation) does not elaborate on the composition rules for cycles, and the text which I read as an introduction to group theory simply listed it as an exercise. While playing around with them and deriving these rules oneself *is* a good idea, I will list the most important here:

- Cycles can be inverted by reversing the order of the indices, and are unchanged by cycling them.

    - $(1 ~ 2 ~ 3)^{-1} = (3 ~ 2 ~ 1) = (1 ~ 3 ~ 2)$

- Cycles may be composed if the last element in the first is the first index on the right. Inversely, cycles may also be decomposed by partitioning it on an index and duplicating.

    - $(1 ~ 2 ~ 3) = (1 ~ 2 )(2 ~ 3)$

- If an index in a cycle is repeated twice, it may be omitted from the cycle.

    - $(1 ~ 2 ~ 3)(1 ~ 3) = (1 ~ 2 ~ 3)(3 ~ 1) = (1 ~ 2 ~ 3 ~ 1) = (1 ~ 1 ~ 2 ~ 3) = (2 ~ 3)$

Going back to $(1 ~ 2 ~ 3)$, if we apply this permutation to the list $[1, 2, 3]$:

$$
(1 ~ 2 ~ 3) \left( \vphantom{0\over1} [1, 2, 3] \right)
= (1 ~ 2)(2 ~ 3) \left( \vphantom{0\over1} [1, 2, 3] \right)
= (1 ~ 2) \left( \vphantom{0\over1} [1, 3, 2] \right)
= [3, 1, 2]
$$

Which is exactly what we expected with our naive notation.


Generators, Permutation Groups, and Sorting
-------------------------------------------

Remember that if we have a group G, then we can select a set of elements $\langle g_1, g_2, g_3, ... \rangle$ as *generators*. If we form all possible products -- not only the pairwise ones $g_1 g_2$, but also $g_1 g_2 g_3$ and all powers of any $g_n$ -- then the products form a subgroup of G. Naturally, the set is called the *generating set*.

Symmetric groups are of primary interest because of their subgroups, also known as permutation groups. A fundamental result of group theory, known as [Cayley's theorem](https://en.wikipedia.org/wiki/Cayley%27s_theorem), states that all finite groups are isomorphic to one of these subgroups. This means that we can code effectively any group by using elements of the symmetric group.

For example, consider the generating set $\langle (1 ~ 2) \rangle$, which contains a single element that swaps the first two items of a list. Its square is the identity, meaning that its inverse is itself. The identity and $(1 ~ 2)$ are the only two elements of the generated group, which is isomorphic to $C_2$, the cyclic group of order 2. Similarly, the 3-cycle in the generating set $\langle (1 ~ 2 ~ 3) \rangle$ generates $C_3$, cyclic group of order 3.

However, the generating set $\langle (1 ~ 2), (1 ~ 2 ~ 3)\rangle$, which contains both of these permutations, generates the entirety of $S_3$. In fact, [every symmetric group can be generated by two elements](https://groupprops.subwiki.org/wiki/Symmetric_group_on_a_finite_set_is_2-generated): a permutation which cycles all elements once, and the permutation which swaps the first two elements, i.e., $\langle (1 ~ 2), (1 ~ 2 ~ 3 ~ 4 ~ ... ~ n) \rangle$.


### Sorting and 2-Cycles

The proof linked to above, that every symmetric group can be generated by two elements, uses the (somewhat obvious) result that one can produce any permutation of a list by picking two items, swapping them, and repeating until the list is in the desired order.

This is reminiscent of how sorting algorithms are able to sort a list by only comparing and swapping items. As mentioned earlier when finding inverses using in two-line notation, sorting is almost the inverse of permuting.


### Bubble Sort

However, not all 2-cycles are necessary to generate the whole symmetric group. Consider [bubble sort](https://en.wikipedia.org/wiki/Bubble_sort), where two elements are swapped when, of two adjacent elements, the latter is less than the former. Until the list is sorted, the algorithm finds all such adjacent inversions. In the worst case, it will swap every pair of adjacent elements, some possibly multiple times, corresponding to the generating set $\langle (1 ~ 2), (2 ~ 3), (3 ~ 4), (4 ~ 5), …, (n-1 ~\ ~ n) \rangle$.

::: {}
![]()
Bubble sort ordering a reverse-sorted list
:::


### Selection Sort

Another method, [selection sort](https://en.wikipedia.org/wiki/Selection_sort), searches through the list for the smallest item and swaps it to the beginning. If this is the final item of the list, this results in the permutation $(1 ~ n)$. Supposing that process is continued with the second element and we also want it to swap with the final element, then the next swap corresponds to $(2 ~ n)$. Continuing until the last element, this gives the generating set $\langle (1 ~ n), (2 ~ n), (3 ~ n), (4 ~ n), …, (n-1 ~\ ~ n) \rangle$.

::: {}
![]()
Selection sort ordering a particular list, using only swaps with the final element
:::

Note that this behavior for selection sort is uncommon, and this animation omits the selection of a swap candidate. The animation below shows a more destructive selection sort, in which the least element candidate is placed at the end of the list (position 5). Once the algorithm hits the end of the list, the candidate is swapped to the least unsorted position, and the algorithm continues on the rest of the list.

::: {}
![]()
"Destructive" selection sort. The actual list being sorted consists of the initial four items, with the final item as temporary storage.
:::


Swap Diagrams
-------------

Given a set of 2-cycles, it would be nice to know at a glance if the entire group is generated. At its simplest, a 2-cycle expressed in cycle notation is simply an unordered pair of natural numbers which swap items of an n-list. Similarly, the edges of an undirected graph on n vertices (labelled from 1 to n) may be interpreted as an unordered pair of the vertices it connects.

If we treat the two objects as the same, then we can convert between graphs and sets of 2-cycles. Going from the latter to the former, we start on an empty graph on n vertices (labelled from 1 to n). Then, we connect two vertices with an edge when the set includes the permutation swapping the indices labelled by the vertices.

Returning to the generating sets we identified with sorting algorithms we identify with each a graph family.

- Bubble sort: $\langle (1 ~ 2), (2 ~ 3), (3 ~ 4), (4 ~ 5), …, (n-1 ~~ n) \rangle$

    - The path graphs ($P_n$), which are precisely as they sound: an unbranching path formed by n vertices.

- "Selection" sort: $\langle (1 ~ n), (2 ~ n), (3 ~ n), (4 ~ n), …, (n-1 ~~ n) \rangle$

    - The star graphs ($\bigstar_n$, since $S_n$ means the symmetric group), one vertex connected to all others.

- Every 2-cycle in $S_n$

    - The complete graphs ($K_n$), which connect every vertex to every other vertex.

::: {}
![]()
:::

This interpretation of these objects doesn't have proper name, but I think the name "swap diagram" fits. They allow us to answer at least one question about the generating set from a graph theory perspective.


### Connected Graphs

A graph is connected if a path exists between all pairs of vertices. The simplest possible path is simply a single edge, which we already know to be an available 2-cycle. The next simplest case is a path consisting of two edges. Some cycle algebra shows that we can produce a third cycle which corresponds to an edge connecting the two distant vertices.

:::: {layout-ncol = "2"}
::: {}
![]()
:::

::: {}
$$
\begin{align*}
&(m ~ n) (n ~ o) (m ~ n) \\
&= (m ~ n ~ o) (m ~ n) \\
&= (n ~ o ~ m) (m ~ n) \\
&= (n ~ o ~ m ~ n) \\
&= (o ~ m) = (m ~ o)
\end{align*}
$$
Note that this is just the conjugation of $(n ~ o)$ by $(m ~ n)$
:::
::::

In other words, if we have have two adjacent edges, the new edge corresponds to the product of elements from the generating set. Graph theory has a name for this operation: when we produce *all* new edges by linking vertices that were separated by a distance of 2, the result is called the *square of that graph*. In fact, [higher graph](https://en.wikipedia.org/wiki/Graph_power) powers will reflect connections induced by more conjugations of adjacent edges.

::: {}
![]()
As you might be able to guess, this implies that $(1 ~~ 4) = (3 ~~ 4)(2 ~~ 3)(1 ~~ 2)(2 ~~ 3)(3 ~~ 4)$

Also, $\bigstar_n^2 = K_n$
:::

If our graph is connected, then repeating this operation will tend toward a complete graph. Complete graphs contain every possible edge, and correspond to all possible 2-cycles, which in turn generate the symmetric group. Conversely, if a graph has *n* vertices, then for it to be connected, it must have at least $n-1$ edges. Thus, a generating set of 2-cycles must have at least $n-1$ items to generate the symmetric group.

Picking a different vertex labelling will correspond to a different generating set. For example, in the image of $P_4$ above, if the edge connecting vertices 1 and 2 is replaced with an edge connecting 1 and 4, then the resulting graph is still $P_4$, even though it describes a different generating set. We can ignore these extra cases entirely -- either way, the graph power argument shows that a connected graph corresponds to a generating set of the whole symmetric group.


### Disconnected Graphs

A disconnected graph is the [disjoint union](https://en.wikipedia.org/wiki/Disjoint_union_of_graphs) of connected graphs. Under graph powers, we know that each connected graph tends toward a complete graph, meaning a disconnected graph as a whole tends toward a disjoint union of complete graphs, or [cluster graph](https://en.wikipedia.org/wiki/Cluster_graph). But what groups do cluster graphs correspond to?

The simplest case to consider is what happens when the graph is $P_2 \oplus P_2$. One of the generating sets it corresponds to is $\langle (1 ~ 2), (3 ~ 4) \rangle$, a pair of disjoint cycles. The group they generate is

$$
\{id, (1 ~ 2), (3 ~ 4), (1 ~ 2)(3 ~ 4) \} \cong S_2 \times S_2 \cong \mathbb{Z}_2 \times \mathbb{Z}_2
$$

One way to look at this is by considering paths on each component: we can either cross an edge on the first component (corresponding to $(1 ~ 2)$), the second component (corresponding to $(3 ~ 4)$), or both at the same time. This independence means that one group's structure is duplicated over the other's, or more directly, gives the direct product. In general, if we denote $\gamma$ as the map which "runs" the swap diagram and produces the group, then

$$
\gamma( A \oplus B ) = S_{|A|} \times S_{|B|}, ~ A, B \text{ connected}
$$

where $|A|$ is the number of vertices in A. Or, if we express a disconnected graph *G* as the disjoint union of its connected components $H_i$

$$
\begin{gather*}
G = \bigsqcup_i H_i \\
\gamma( G ) = \gamma( ~ \bigsqcup_i H_i ~ ) = \prod_i S_{|H_i|}
\end{gather*}
$$

Which takes care of every simple graph. While we're rather limited by what kinds of groups can be expressed by a swap diagram, it is rather satisfying that $\gamma$ maps a sum-like object onto a product-like object.


Closing
-------

This concludes the dry introduction to this series of posts discussing some investigations of mine into symmetric groups. I probably could have omitted the sections about permutation notation and generators, but I wanted to be thorough and tie it to concepts which were useful to my understanding. The notion of a graph encoding a generating set in particular will be fairly important going forward.

Originally, this post was half of a single, sprawling article which meandered this way and that. I hope I've improved the organization by keeping the digression about sorting algorithms to this initial article. The next post will cover some interesting structures which can fill Euclidean space and incredibly large graphs.

Sorting and graph diagrams made with GeoGebra.
