Mathematics Behind Patience Sorting David Barra Capstone Advisor: Dr. King
What is Patience Sorting? Simple one person card game that is a practical method of sorting a real deck of cards Take a deck of cards labeled 1, 2, 3, …, n. The deck is shuffled and cards are turned up one at a time and dealt into piles on the table. At each stage of the game, we see the top card of each pile. A low card may be placed on a higher card, or may be put into a new pile to the right of the existing pile. The object of the game is to finish with as few piles as possible.
Example of Patience Sorting Permutation { }
Patience Sorting Mathematics Random set of n cards can be represented by a permutation of n elements An example for n = 10 is { } The optimal way to play patience sorting is to play using the Greedy Algorithm The greedy algorithm says to always take the best move at that given time. It involves no look ahead strategy. The greedy strategy is to always place a card on the leftmost possible pile, thus taking advantage of the first opportunity to place a card on top of another.
Patience Sorting Mathematics When patience sorting is played using the Greedy strategy, the game will finish with ℓ(π) piles where ℓ(π) is the length of the longest increasing scattered subsequence of a permutation π. The longest increasing scattered subsequence is the longest subsequence of increasing terms, where intervening, non- increasing terms may be dropped. Example π = { }. Longest increasing scattered subsequence is { } ℓ(π) = 4
Lemma 1 With deck π, patience sorting played using the greedy strategy ends with ℓ(π) piles. Furthermore, the game played with any legal strategy will end with at least ℓ(π) piles. Therefore, the greedy strategy is the optimal way to patience sort.
Proof: First we show that the number of piles is at least ℓ(π). If a subsequence of cards a 1 < a 2 < a 3 < … < a n appears in increasing order in a permutation of cards π, then under any legal strategy, each a i must be placed in some pile to the right of a i-1, because the top card of each pile can only decrease. In other words, no two cards in any increasing subsequence may end up in the same pile. Therefore, the final number of piles is at least the length of any increasing subsequence of π, and therefore, at least ℓ(π ).
Proof (cont.): Next we show that the number of piles is at most ℓ(π). We can use patience sorting to find an instance of an increasing subsequence thereby showing that we have at most ℓ(π) piles. Every time we place a card c in a pile i that is not the first pile, draw an arrow from c to the top card d of the previous pile. We know d<c and each arrow goes from a later card to an earlier card. The path of the arrows represents an increasing sequence in π. Therefore, the number of piles is at most ℓ(π). Since the number of piles is also at least ℓ(π), the number of piles is ℓ(π).
Playing with a 52 card deck To play with a deck of 52 cards, we need to linearly order the fifty cards. The problem with 52 cards is that you may find yourself in the situation where, for example, a 3 is the top card of a pile and you are holding a 3. You need to know whether or not you can place that 3 on top of the other one. There can’t be any ties in patience sorting. To solve the problem, order the suits. This orders the 52 cards such that you could order them 1, 2, 3,…, 52 in increasing order ….
Objective Compute the probability distribution of ℓ(π) for permutations of a given length n. In other words, what is the probability of getting ℓ(π) = 1, 2, 3,…, n for a permutation of length n? Three methods we investigate are: Patience Sorting with every possible permutation of length n Formula formed from the Schensted Correspondence Mapping between a permutation and the combinatorical structures called standard Young tableaux Maple Programming
Compute P(L n = ℓ) by Patience Sorting Example for permutation of length 3 π# Piles {1, 2, 3}3 {1, 3, 2}2 {2, 1, 3}2 {2, 3, 1}2 {3, 1, 2}2 {3, 2, 1}1 Probability Distribution is: P(L 3 = 1) = 1 / 6 P(L 3 = 2) = 4 / 6 P(L 3 = 3) = 1 / 6
Standard Young Tableaux Along each row and down each column, we place the numbers 1…n in the cells in increasing order. This gives rise to a partition of a number n putting it in a standard Young tableau of shape λ, size n. The size n of a standard Young tableau is the number of cells it contains. Partition λ is the shape of the standard Young tableau of size n. A partition λ = (λ 1, λ 2,…, λ j ) of an integer n ≥ 1 is a sequence with λ 1 ≤ λ 2 ≤ … ≤ λ j, where λ i is the number of cells in row i. d λ is the number of standard Young tableaux of a given shape
Standard Young Tableaux The idea is to construct a pair of standard Young tableau from a permutation π by using the Schensted Column Insertion Algorithm. This forms a bijection between permutations and pairs of standard Young tableaux (P,Q). This is the Schensted Correspondence. For π = { } P Tableau Q Tableau
How to construct P tableau from permutation π π = { } 1.Place card into top row of tableau by patience sorting 2.If a card is covered, place it into the next row by patience sorting (thus bumping it)
P tableau for π = { }
Correspondence between Permutations of π cards and pairs of Standard Young Tableaux The length of the top row in the Young tableau associated with a permutation π is the length of the longest increasing scattered subsequence of π. Furthermore, the top row is the same as the top card of each pile in patience sorting. This is another way to calculate ℓ(π) P Tableau for π = { }. Longest increasing subsequence is { }
How does everything relate to each other? π P tableauQ tableau Length of top row of tableaux Patient Sort # Piles ℓ(π) {1, 2, 3} 333 {1, 3, 2} 222 {2, 1, 3} 222 {2, 3, 1} 222 {3, 1, 2} 222 {3, 2, 1} 111
Correspondence between permutation of π cards and pairs of standard Young tableaux This gives a formula for the probability P(L n = ℓ) that the longest increasing scattered subsequence of a random permutation of {1, 2, …, n) has length ℓ.
Applying the Formula for P(L 3 = ℓ) P(L 3 = 1) = (1 / 3!) ∙ 1 2 = 1 / 6 P(L 3 = 2) = (1 / 3!) ∙ 2 2 = 4 / 6 P(L 3 = 3) = (1 / 3!) ∙ 1 2 = 1 / 6 d (3) = 1 d (2,1) = 2 d (1,1,1) = Remember: d λ is the number of standard Young tableaux of a given shape
Programming Maple to Patience Sort By David Barra and Dr. Wyels Program Maple to Patience Sort using patience sorting as the algorithm for computing ℓ(π) Program Maple to collect the data for a large number of trials and find the P(L n = ℓ) for a given n
Acknowledgements Dr. King Dr. Fogel Dr. Wyels David Parker Lucas Lembrick
Sources Diaconis, Perci. “Longest Increasing Subsequences: From Patience Sorting to the Baik-Deift-Johansson Theorem.” Stanford University, D. Stanton and D. White. Constructive Combinatorics. Undergraduate Textbooks in Mathematics. Springer and Verlag, Eric. W Weisstein. “Young Tableau.” From Mathworld—A Wolfram Web Resource.
The Hook Formula (another way to calculate d λ ) The hook length of a cell h c is the number of cells in the set consisting of the cells to the right of c in its row, the cells below c in its column, and c itself. The number of standard Young tableau d λ of a given shape λ, is given by the Hook Formula. d λ = n! / (Π c h c )