Sorting Solitaire

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

ANALYSIS AND MONTE-CARLO ESTIMATION OF SUCCESS IN THE GENERALIZED

GAME OF SORTING SOLITAIRE

ALEXANDER DASHEVSKY

Introduction

This paper describes an attempt to analyze the game shall see, the success of the attempt was only partial. In-
of “Sorting-Solitaire” invented by the author, and specif- deed, the results and techniques described in this section
ically the probability of victory in any given game. The are presented only as a reference and checkpoint for pos-
first section of the paper is devoted to describing the prob- sible further elaboration. The third section of the paper
lem itself, first in the original card game form, and after- is devoted to the description of the Monte-Carlo simula-
wards in a more mathematical, generalized way. In the tion techniques that were used to determine the desired
following section an attempt is described to find an ex- probabilities. The fourth and final section contains the
act, closed formula for the probability of victory; as we results obtained from the simulations, together with their
analysis and possible conclusions.

1. The problem

The game of Sorting-Solitaire was invented by the au- game every card on the table occupies its reserved posi-
thor many years ago. It is a vastly simplified, totally de- tion, regardless of whether it was put there by the player
terministic version of the classic Solitaire game, and is in the course of the game or was there from the start.
played as follows: When considering a card game, the definitions above
A complete deck of cards (52 cards) is shuffled, and four are fine; however, in order to analyze the game mathe-
cards (the hand cards) are placed aside. The remaining 48 matically, some restatements and generalizations are in
cards are dealt face-up on a table in four rows of 12 cards order.
each. The rows and columns of this 4 × 12 matrix are First of all, we shall consider the general case, with n
mentally assigned the values of card suits (in a predefined cards numbered from 1 to n in the deck and k cards in the
order, such as Spades, Diamonds, Clubs, Hearts) and card hand. Second, instead of relying on a cumbersome matrix
values (from Two to King), respectively. For example, the with easily-confused indices, we shall consider the deck as
third position from the left in the uppermost column is spread in a single row, with each card’s place indicating
assigned the value (reserved for ) “Four of Spades”. This the reserved value (e.g., the fourth place from the left is
reservation is done in complete disregard of the card that reserved for card number 4). The initially empty places of
is currently in that position (which is most likely not the the hand cards will be defined as the first k places from
Four of Spades). In addition, an empty thirteenth column the left, for the sake of convenience.
for the Aces is assumed at the far right, beyond the Kings. It is easy to see that there is a simple one-to-one corre-
With that set, the game can begin. spondence between this new form of the game in the case
The first hand card is brought forth and examined; let where n = 52 and k = 4, and the original matrix form.
us assume, for example’s sake, that it is the Ten of Hearts. The final change we shall perform is a restatement of
The card is then placed in its reserved place (in this case: the problem in a more rigorous way.
fourth row, ninth column), and the card previously occu-
pying that position is taken out. This action constitutes Definition 1. A permutation of the numbers {1, . . . , n} is
a move in the game. The next move is carried out sim- called k-winnable, if and only if it satisfies both following
ilarly: the extracted card from the previous move (e.g., conditions:
the Five of Clubs) is placed in its reserved spot (in this (1) The number of nontrivial (length ≥ 2) cycles the
case: third row, fourth column), and the card previously permutation contains is at most k.
occupying the spot is extracted. Now, let us assume that (2) Every nontrivial cycle of the permutation has at
the extracted card was the Ace of Diamonds. In this case, least one representative among the first k permu-
on the next move the card will be placed in the Aces col- tation members.
umn of the second row, and, as the column had started Problem 1. For any given n and k, determine the per-
out empty, there would be no card to extract. On such centage of permutations p(n, k) on {1, . . . , n} which are
an occasion the second hand card is brought out, and the k-winnable.
play continues as usual.
Naturally, the play terminates when the player no In this form the problem becomes much easier to ana-
longer has a card to play either from the last move or lyze mathematically, as we shall attempt in the next sec-
from the hand. Victory is achieved when at the end of the tion.
Key words and phrases. solitaire, simulation, algorithms.
1
2. Mathematical analysis

Naturally, the first (and by far, the best) way of solving Let us now concentrate on a single decomposition of
such a problem as the one presented is to find an exact for- n into k potential cycles - essentially, it is a list of cycle
mula, which, given any n and k, would produce the exact lengths {l1 , l2 , . . . , lk }, which sum to some 2k ≤ M ≤ n.
number of k-winnable partitions on {1, . . . , n}. Presented
Stage 2. Given such a list of k cycle lengths, how many
here is a partially successful attempt at such a formula:
ways are there of distributing the n members of a permuta-
the enumeration of k-winnable permutations with exactly
tion between them, so that every cycle has a representative
k nontrivial cycles.
among the first k places?
We shall progress in stages.
Observation 1. Because there are exactly k cycles, each
Stage 1. Given n and k, k ≤ n, how many ways there cycle must have exactly one representative in the first k
are of breaking up n into exactly k sets of length ≥ 2 (and positions.
any additional number of singletons)? Let M be the to-
tal length of the k sets; clearly, M can range from 2k to Let us assume that the first member of the permutation
n. Let us denote by comps(M,k) the list of all composi- belongs to cycle number one, the second - to cycle number
tions of M into k parts. Then, the total number of such 2 and so on. Now, each cycle needs only l − 1 more mem-
decompositions is bers (where l is the cycle’s length). Let us propose the
n function choices(L), which gets as input a list L of k cycle
lengths, and produces the number of ways to populate the
X
|comps(M, k)|
M =2k cycles:

     
n−k n − k − (l1 − 1) n − k − (l1 − 1) − . . . − (lk−1 − 1)
choices(L) = · · ... ·
l1 − 1 l2 − 1 lk − 1

Stage 3. Now that we have k cycles of known lengths of k-winnable partitions on {1, . . . , n} is determined
which are populated by known members of {1, . . . , n}, thus:
how many ways are there of forming cycles in them? It accum ← 0
is known that the unsigned Stirling numbers of the first for M = 2k to n do
kind c(n, k) count the number of permutations of n ele- comps ← comps(M, k)
ments with k disjoint cycles. Therefore, if we have a set for all L ∈ comps do
of length l, there are exactly c(l, 1) ways for it to form accum ← accum + choices(L) · stirlings(L)
a single cycle. Let us again define a function stirlings(L) end for
which takes as input a list of cycle lengths and produces end for
the number of cycles possible: As evident from the previous stages, at the end accum
Yk holds the precise number of k-winnable permutations with
stirlings({l1 , . . . , lk }) = c(li , 1) exactly k nontrivial cycles.
i=1 Unfortunately, all attempts so far to extend this tech-
At this point we can assemble everything together. nique to enumerate all k-winnable partitions had failed.
Final stage. The complete computation is better Therefore, we shall try and estimate the probability of a
presented as an algorithm, rather than as a for- randomly chosen permutation to be k-winnable by way of
mula. Given n and k ≤ n, the number Monte Carlo simulation, as described in the next section.

Simulation methods and accuracy

Simulation setup. To perform Monte-Carlo simulation permutations is divided by iter to give the estimate of the
of Sorting Solitaire, two programs were used. The first needed probability.
one, written in Mathematica, models a game of Sorting
Solitaire directly, and is mostly intended as a benchmark
for correctness. The main simulations were performed Accuracy of the simulation. While the process of sim-
with a heavily optimized [1] program written in C. The ulation is straightforward, a question arises: how accu-
source codes for both programs are attached. rately does such an estimate match the actual probability
of a permutation being k-winnable? To answer this ques-
tion, we will use the Hoeffding-Chernoff inequality:
Simulation process. Each one of the simulation pro- Theorem 1. Let X1 , X2 , . . . , Xn be independent random
grams requests from the user the parameters n and k, and variables in [0, 1]. Then, for all ε ≥ 0,
also the desired number of simulations iter. Then, for iter
iterations, it chooses a random permutation of {1, . . . , n}
n !
1 X 2
and determines whether it is k-winnable, and if so, in- P Xi − EXI ≥ ε ≤ 2e−2nε

n
creases a counter. At the end, the number of k-winnable

i=1
2
Here, X1 , . . . , Xiter are the iter independent simula- close as we wish to it with any desired probability. In this
tions, each of which can either succeed or fail. The sought- case we have set iter to be 1,000,000,000 , which gives us
after “real” p(n, k) is therefore E, and as we can see, by an error ε of less than 0.0001 with probability ≤ 0.01.
controlling the number of simulations iter, we can get as

Results and analysis

The simulation program was launched for all n from 1


to 100 and for all k from 1 to 30. Below are graphs of
p(n, k) for selected values of k:

1.0 1.0

0.8 0.8

0.6 0.6

0.4 0.4

0.2 0.2

0 20 40 60 80 100 0 20 40 60 80 100

Figure 1. k=1 Figure 4. k=10

1.0 1.0

0.8 0.8

0.6 0.6

0.4 0.4

0.2 0.2

0 20 40 60 80 100 0 20 40 60 80 100

Figure 2. k=4 Figure 5. k=15

1.0 1.0

0.8 0.8

0.6 0.6

0.4 0.4

0.2 0.2

0 20 40 60 80 100 0 20 40 60 80 100

Figure 3. k=7 Figure 6. k=30

3
0.20

As common sense might dictate, for any given k, the


probability for victory in a game declines as n grows. A
natural question arises: can the graphs of the decline be 0.15

matched with a well-known function? To answer this, ob-


serve the data for k = 1 and n from 1 to 12:
n p(n, k) 0.10

1 1
2 1
3 0.833298 0.05

4 0.666885
5 0.541528
6 0.453031 20 40 60 80 100

7 0.388477
8 0.339882 Figure 7. k=4
9 0.302049
10 0.271899
11 0.247164 0.8

12 0.226816
The p(n, k) enry for n = 10 is suspiciously similar to
e
10 . A quick calculation reveals a similarity between p(n, 1)
0.6

and ne , which seems to close


to be a coincidence. Observe
the graph of p(n, 1) − ne for all possible n: 0.4

0.00030

0.00025 0.2

0.00020

20 40 60 80 100

0.00015

0.00010 Figure 8. k=10

0.00005

20 40 60 80 100

As evident from the graph, the difference between 1.5

p(n, 1) and ne diminishes while n grows, and even for small


n the difference never exceeds 0.0003. Having discovered
this pattern to p(n, 1), can we generalize it to any value of 1.0

k? It turns out that we can. Observe the folowing graph


of p(n, 2) − 2e
n for all n:

0.035 0.5

0.030

0.025

20 40 60 80 100

0.020

0.015
Figure 9. k=20

0.010

The graphs show that, although p(n, k) in all cases


0.005
approaches k·e
n , the difference gets bigger as k increases.
20 40 60 80 100
Therefore, we shall amend the conjecture thus:
As before, the difference starts out small to begin with, Conjecture 2.
and, as n grows, quickly diminishes. This allows us to put
forward a conjecture: k·e
lim p(n, k) =
Conjecture 1. (n−k)→∞ n
k·e This conjecture seems to be much better supported by
lim p(n, k) =
n→∞ n the graphs. The next step would be trying to prove the
To test ke
the conjecture, we shall present more graphs of conjecture mathematically, after oberving it empirically;
p(n, k) − , for various values of k: but this falls out of this paper’s scope.
n

4
References
[1] The XKCD forum: What can be done to speed it up?, http://echochamber.me/viewtopic.php?f=11&t=56752.
E-mail address, A. Dashevsky: atanvarnoalda@gmail.com

You might also like