Sub topic 13
Patience Sorting, Piles, and Dilworth's Theorem
This chapter assumes you know the O(n log n) tails-array algorithm from earlier chapters and that you are comfortable reading short proofs. The goal is to see that algorithm from a different angle. It turns out to be a card game, and that view comes with a clean proof of why it works.
The Card Game
Patience sorting is a way of dealing cards into piles. Take the cards one at a time and follow these rules:
- Put the card on the leftmost pile whose top card is greater than or equal to it.
- If no pile qualifies, start a new pile on the right.
When the deck runs out, the number of piles equals the length of the longest strictly increasing subsequence.
This is the tails algorithm with extra bookkeeping. The pile tops, read left to right, are exactly the tails array. "Leftmost top that is ≥ x" is what bisect_left finds.
from bisect import bisect_left
def patience_piles(a):
piles = [] # each pile is a list; its last element is the top
tops = [] # tops[k] == piles[k][-1], kept sorted ascending
for x in a:
k = bisect_left(tops, x) # leftmost pile whose top >= x
if k == len(piles):
piles.append([x])
tops.append(x)
else:
piles[k].append(x)
tops[k] = x
return piles
piles = patience_piles([10, 9, 2, 5, 3, 7, 101, 18])
print(piles) # [[10, 9, 2], [5, 3], [7], [101, 18]]
print(len(piles)) # 4Why the Pile Count Is the LIS Length
The proof has two halves.
Upper bound. Inside a pile, every card is ≤ the card beneath it, so each pile is a non-increasing subsequence of the input. A strictly increasing subsequence cannot use two cards from the same pile. So it uses at most one card per pile, which means LIS ≤ number of piles.
Lower bound. Whenever a card lands on pile k > 0, the top of pile k−1 at that moment came earlier in the input and is strictly smaller. If it weren't, the card would have gone on pile k−1. Link the card to that top. Following these links back from any card on the last pile gives a strictly increasing subsequence with one card from each pile. So LIS ≥ number of piles.
Together the two halves give equality. The links are the parent pointers from the O(n log n) reconstruction chapter.
The Partial-Order View: Mirsky and Dilworth
Order the indices by i ≺ j when i < j and a[i] < a[j]. In this partial order:
| Order-theory term | Meaning for the sequence |
|---|---|
| Chain (all pairs comparable) | Strictly increasing subsequence |
| Antichain (no pair comparable) | Non-increasing subsequence |
There are two dual theorems about partial orders:
- Mirsky's theorem: the longest chain equals the minimum number of antichains needed to cover all elements. For sequences, this says the LIS length equals the minimum number of non-increasing subsequences that partition the input. Patience sorting builds such a partition: the piles.
- Dilworth's theorem: the largest antichain equals the minimum number of chains needed to cover all elements. For sequences, this says the longest non-increasing subsequence equals the minimum number of strictly increasing subsequences that partition the input.
The Dilworth form shows up in interview problems: "What is the fewest increasing runs (or rising missile interceptors, or non-crossing tracks) needed to cover the sequence?" To solve it, compute a longest non-increasing subsequence. One way is to negate the values and take the longest non-decreasing subsequence with bisect_right.
A Counting Consequence
The pile picture also proves the Erdős–Szekeres theorem. Take any sequence of more than r·s distinct numbers. It contains an increasing subsequence of length r+1 or a decreasing subsequence of length s+1. Here is why: if there are at most r piles, then by pigeonhole some pile holds more than s cards. With distinct values, that pile is strictly decreasing.
Exercise
Deal [3, 1, 4, 1, 5, 9, 2, 6] using the strict (bisect_left) rules. List the final piles and give the LIS length along with one LIS.
Show answer
Piles are [3, 1, 1], [4, 2], [5], [9, 6]. There are 4 piles, so the LIS length is 4. One LIS is 1, 4, 5, 9.
Exercise
Use piles to prove that any sequence of 10 distinct numbers has an increasing or a decreasing subsequence of length 4.
Show answer
Suppose the LIS has length at most 3. Then there are at most 3 piles. Ten cards in at most 3 piles means some pile has at least 4 cards. Piles are non-increasing, and the values are distinct, so that pile is a strictly decreasing subsequence of length at least 4.
Exercise
What is the minimum number of strictly increasing subsequences needed to partition [5, 3, 4, 2, 1]?
Show answer
By Dilworth's theorem, the answer equals the longest non-increasing subsequence, which is 5, 3, 2, 1 (or 5, 4, 2, 1). So the answer is 4.