Now Reading
Can a Rubik’s Dice be brute-forced?

Can a Rubik’s Dice be brute-forced?

2023-07-08 11:19:54

By Robert Smith


After I was about 13, whereas nonetheless a middle-schooler, I grew to become
fascinated with the Rubik’s Dice. I by no means received terribly good
at fixing it, perhaps finally entering into the 30 to 40 seconds
vary. Whereas I didn’t have a penchant for memorizing transfer sequences, I
was drawn into how we discover these transfer sequences.

The story about my curiosity and exploration within the Rubik’s Dice is for
one other put up. Lengthy story brief, I received occupied with “laptop
puzzling”—utilizing computer systems to control combinatorial puzzles, like
the Rubik’s Dice, both to resolve them shortly, to find patterns,
or to seek out novel transfer sequences to be used in speedcubing—and ever
since, I’ve been engaged on totally different applications for fixing Rubik-like

Purely in precept, it shouldn’t be onerous to resolve a Rubik’s Dice with
a pc, proper? Our program would have three components:

  1. A mannequin of the Rubik’s Dice, that’s, some information construction that
    represents a dice state.
  2. Some features which may simulate turns of every aspect.
  3. A fixing process which takes a scrambled dice, tries each
    doable flip sequence, and stops when solved.

Fact be identified, and particulars apart, it is a provably right methodology
for fixing a Rubik’s Dice. If you happen to go away your laptop on lengthy sufficient,
it’ll return an answer.

The issue is that it takes a very long time. Most likely longer than your

Laptop puzzling with out brute-force

“Brute-force” typically means to attempt each chance of one thing
with out a lot of any technique. Our methodology above is a brute-force
algorithm. Brute-force algorithms typically aren’t sensible, as a result of
when you’ve got $N$ of one thing to discover, a brute-force algorithm will
take $O(N)$ time. For a Rubik’s Dice, $N$ is 43 quintillion—a really
massive quantity.

It has been identified, virtually because the Rubik’s Dice’s inception,
that one thing else is required to resolve a Rubik’s Dice. Rubik’s Dice
options, clearly, consider the particular construction and
properties of the dice in order to implicitly or explicitly keep away from
senseless search. These strategies have turned out to be:

  1. Fixing strategies for people: memorize some sequences which allow you to
    transfer only some items round in isolation, and apply these
    sequences mechanically till all items are in place. The extra
    sequences you memorize, the quicker you’ll be.
  2. Heuristic tree search: do a tree search (with e.g.,
    iterative-deepening depth-first search), however aggressively
    prune off branches by the use of intelligent heuristics.
  3. Section-based solvers: a deeply mathematical means which includes
    characterizing the Rubik’s Dice as a sequence of nested
    (mathematical) subgroups so that every successive coset area small
    sufficient that it may be solved by laptop.

Laptop puzzling principally offers with the latter two approaches, often
in some mixture. Each approaches result in terribly
high-performing solvers. For instance:

  • Korf’s algorithm (strategy #2) finds optimum options—options
    of shortest size—however can take hours to seek out one.
  • Thistlethwaite’s algorithm (strategy #3) solves a dice in 4
    phases nearly instantaneously. The options are assured to be no
    longer than triple the optimum size.

The story might as effectively finish right here. We have now gradual however optimum methods of
fixing the Rubik’s Dice, and quick however sub-optimal methods. Choose your
poison (sub-optimal or gradual), relying on what you’re attempting to

Taking a step again: puzzles as permutations

Evidently any Rubik’s Dice solver has to know one thing about
the construction of the dice. It is likely to be price asking how little
construction we are able to get away with, in order to make no matter fixing
algorithm we write generic over a broad class of puzzles.

For a brute-force algorithm with tree search, we would want one thing
like the next:

interface GenericPuzzle:
  sort State
  sort Transfer

  perform isSolved(State) -> Boolean
  perform allMoves() -> Record(Transfer)
  perform performMove(State, Transfer) -> State

With this, we may write the next solver based mostly off of
iterative-deepening depth-first search, which is completely generic on
the above interface.

perform clear up(State) -> Record(Transfer)
perform clear up(p):
  if isSolved(p):
    return []

  for maxDepth from 1 to infinity:
    solved?, resolution = dfs(0, maxDepth, p)
    if solved?:
      return resolution

perform dfs(Integer, Integer, State, Record(Transfer)) -> (Boolean, Record(Transfer))
perform dfs(depth, maxDepth, p, s):
  if isSolved(p):
    return (True, s)

  if depth == maxDepth:
    return (False, [])

  for m in allMoves():
    p' = performMove(p, m)
    (solved?, resolution) = dfs(depth+1, maxDepth, p', append(s, [m])

    if solved?:
      return (solved?, resolution)

As mentioned earlier than, whereas this technique is efficient for issues
with small search areas, it’s no assist when the area is
massive. Sadly, the GenericPuzzle interface doesn’t give us
a lot room for enchancment. Can we nonetheless stay generic, whereas giving
us a minimum of a bit of extra room for exploring different algorithms?

The reply is sure, if we limit ourselves to permutation
. Roughly talking, a permutation puzzle is one the place items
shift round based on a hard and fast and at all times out there set of shifting
strikes. The Rubik’s Dice is an outstanding and non-trivial instance: We
can label every cellular sticker with a #1 to 48, and
these stickers can at all times be shifted round with a twist of any of the
six sides. Since we are able to twist any of the six sides at any time, the
puzzle is a permutation puzzle. (Not all comparable puzzles are
permutation puzzles. There are some puzzles that are “bandaged”, that
is, items of the puzzle are fused collectively, proscribing some
out there strikes relying on the configuration.)

On this view, we checked out a solved configuration as a listing of
numbers. For instance, the solved Rubik’s Dice as a permutation would

(1, 2, ldots, 47, 48).

After we flip a aspect, these numbers get permuted. As an example,
assuming a selected labeling of stickers with numbers, turning the
prime face of a Rubik’s Dice would possibly permute the primary sticker within the listing
to the third, the second sticker to the fifth, the third sticker to
the eighth, and many others. We will use the identical notation

(3, 5, 8, 2, 7, 1, ldots)

This notation has two interpretations:

  1. The literal place of numbered stickers on a bodily dice (with
    an agreed upon labeling).
  2. An instruction for methods to relabel the stickers of a given dice.

If we take a look at the notation underneath the second interpretation, a
permutation really represents a perform that’s utilized to
particular person stickers. As an example, if

F := (3, 5, 8, 2, 7, 1, ldots)

then $F(1) = 3$, $F(2) = 5$, and many others. The entire clockwise face
turns—Entrance, Proper, Up, Again, Left, Down—of a Rubik’s Dice will be
described like so:

F &:= (1, 2, 3, 4, 5, 25, ldots)
R &:= (1, 2, 38, 4, 36, 6, ldots)
U &:= (3, 5, 8, 2, 7, 1, ldots)
B &:= (14, 12, 9, 4, 5, 6, ldots)
L &:= (17, 2, 3, 20, 5, 22, ldots)
D &:= (1, 2, 3, 4, 5, 6, ldots, 48, 42, 47, 41, 44, 46)

We wrote a few of the final parts of $D$ as a result of a “down” transfer doesn’t
change the primary six stickers on this labeling scheme.

This provides is a complete new interpretation of what it means to “clear up” a
dice. Given a scrambled dice, we first write down the permutation that
describes how the stickers moved from a solved state to the scrambled
state. Let’s name it $s$. That is straightforward, as a result of we are able to simply learn the
labeled stickers off of a dice one-by-one, so as. For instance, $s$
is likely to be:

s := (27, 42, 30, 15, 39, 6, ldots).

It is a description of a perform! The worth of $s(1)$ describes
how the primary sticker of a dice will likely be shifted to its scrambled
place, on this case $27$. Subsequent, fixing a dice is discovering a
sequence of $okay$ strikes $m_1, m_2, ldots, m_k$ such that, for all $1leq
ileq 48$,

i = m_k(m_{k-1}(cdots(m_2(m_1(s(i)))))).

Said one other means in perform composition notation, the perform

m_k circ m_{k-1} circ cdots circ m_2 circ m_1circ s

have to be the id perform—a permutation that doesn’t transfer

Within the permutation puzzle mind-set, we are able to nonetheless implement our
GenericPuzzle interface:

  • State could be a permutation;
  • Transfer would even be a permutation;
  • isSolved would verify if a permutation is $(1, 2, 3, ldots)$;
  • allMoves could be a hard-coded listing of the doable strikes, like
    $F$, $R$, $U$, $B$, $L$, and $D$ for the Rubik’s dice; and
  • performMove would take the enter transfer permutation, and apply it as
    a perform to every component of the state permutation.

This would possibly even be extra environment friendly than one other selection of
illustration, since permutations will be represented very effectively
on a pc as packed arrays of bytes!

However we didn’t do all this mathematical groundwork simply to goof round;
there’s one thing wonderful lurking in these permutations.

Brute-force, nonetheless ignorant, however kinda sensible?

Within the late Eighties, Adi Shamir and his college students made a
sensible sequence of observations that got here collectively to make for a
lovely consequence. Sadly, to my data, solely two writings
exist on the subject.

  1. Shamir and his colleagues wrote a paper about it [1], form of in
    the fashion of a short convention continuing, nevertheless it’s very mild on
    particulars and skips implementation concerns. It’s the form of
    paper the place you comply with it, however you need to fill in an excellent quantity
    of blanks to make something from it work.

  2. Shamir gave a chat someday within the 80’s about his consequence, and
    any individual (none aside from Alan Bawden) wrote a short e-mail [2] to a
    mailing listing about his recollection of it.

A tremendous consequence, buried in historical past, with none good exposition that
I may discover.

What’s the consequence? The essence of the result’s this. Harking back to a
“meet within the center” algorithm, if we wish to brute-force an issue
that ordinarily requires visiting $N$ states to seek out a solution, we are able to
as a substitute cleverly cut up the work into two searches that requires visits
to round $sqrt{N}$ states. For a Rubik’s Dice, that cuts work
related to 43 quintillion states, right down to work related to 6
billion states. The perfect half is, that is nonetheless brute-force;
just about no data of the construction of the issue is required to
make it work.

Let’s stroll by means of the requisite steps and construct as much as the
consequence. I’ll try to write down in a basic framework (because it’s a
basic algorithm), however make frequent appeals to the Rubik’s Dice

Statement #1: decomposition as intersection

Suppose the next:

  • We have now a mysterious permutation $s$, say, a scrambled puzzle;
  • We have now two units of permutations $X$ and $Y$; and
  • We assume there’s an $hat xin X$ and $hat yin Y$ such that $s =
    hat ycirc hat x$.

The purpose is to seek out exactly $hat x$ and $hat y$ are. The only
means to do that is to verify each mixture of parts in $X$ and

for x in X:
  for y in Y:
    when s = compose(y, x):
      return (x, y)

This can take time proportional to the product of the set sizes:
$O(vert Xvertcdotvert Yvert)$. Shamir seen the next: If
$s=hat ycirchat x$, then $hat y^{-1}circ s = hat x$. With this, we
preprocess our $Y$ set to be as a substitute

Y’ := {y^{-1}circ s : yin Y}.

By doing this, there have to be a component in widespread between $X$ and
$Y’$, since $hat xin X$ and $hat y^{-1}circ sin Y’$ and people are
equal. So we’ve decreased the issue to figuring out what the
intersection between $X$ and $Y’$ is.

As soon as we discover our $z$ which is in widespread with $X$ and $Y’$, then our
recovered permutation will likely be $hat x = z$ and $hat y = (zcirc

We’ve simply established that the issue of decomposing a component like
$s$ is equivalent to the issue of calculating a set
intersection. Nonetheless, if we wish to do the intersection, our instinct
tells us we nonetheless want a quadratic algorithm, which brings us to the
second commentary.

Statement #2: sorting actually helps!

Permutations have a pure ordering, referred to as lexicographic
. In case you have two permutations, and also you learn their parts
left-to-right, you’ll be able to evaluate them like abnormal numbers. Simply
as $123 < 213$, we are able to say that

(1,2,3) < (2,1,3).

A pleasant property of that is that the id permutation $(1, 2, 3,
ldots)$ is the smallest permutation of a given dimension.

How does this assist us? Nicely, suppose we type our units $X$ and $Y’$
into lists $L_X$ and $L_{Y’}$, so the permutations are so as. If
$L_X$ and $L_{Y’}$ have a component in widespread, we are able to discover it in linear
time: $O(min{vert Xvert, vert Y’vert})$. How? One thing like
the next:

perform findCommon(Lx, Ly):
  x = pop(Lx)
  y = pop(Ly)
    if x == y:
      return x
    if empty(Lx) or empty(Ly):
      error("No widespread parts discovered.")

    if x < y:
      x = pop(Lx)
    else if x > y:
      y = pop(Ly)

This works as a result of we’re basically taking a look at all the parts
of $L_X$ and $L_{Y’}$ collectively in sorted order. It’s like a merge
type, with out the merge half.

Earlier than persevering with, we should always take a bit of scenic tour on a extra
formal that means of “strikes” and “transfer sequences”, since in the end any
permutation puzzle fixing algorithm should produce them as output.

What’s a transfer?

A fast bit about notation. If we now have a permutation $f$, then its
inverse is written $f^{-1}$, and it’s $okay$-fold repetition $fcirc
fcirccdotscirc f$ is written $f^okay$. If we now have a set of
permutations $S := {f_1, f_2, ldots}$, then we write the
following shorthands:

S^{-1} &:= {f^{-1} : f in S}
S^{occasions okay} &:= {f^okay : f in S}.

If $g$ is a few permutation, we additionally write these shorthands:

gcirc S &:= {gcirc f : f in S}
Scirc g &:= {fcirc g : f in S}.

Equally, if $T := {g_1, g_2, ldots}$, then we are able to write

Scirc T &:= {fcirc g : fin S, gin T}
&= {f_1circ g_1, f_2circ g_1, ldots, f_1circ g_2, ldots}.

With that out of the best way, let’s speak in regards to the idea of a single
“transfer”. What counts as a “transfer” in a permutation puzzle?

Actually, we are able to select any set of strikes we please, as long as each
state of the puzzle is reachable by means of some mixture of the
strikes. For instance, let

C := {F, R, U, B, L, D},

the essential and effectively understood ninety-degree clockwise strikes of the
Rubik’s Dice. Certainly, $C$ itself is a wonderful definition of accessible
strikes. The entire following are additionally legitimate definitions of strikes:

Ccup C^{-1},quad Ccup C^{occasions 2},quad C^{-1},quad Ccup C^{occasions 2}cup C^{-1},

and so forth. Maybe surprisingly, we are able to take any component of $C$ and
take away it, and it could nonetheless be a legitimate set of strikes for the Rubik’s

Which set of strikes we choose often has little relevance
mathematically (they’re all expressible as each other), however has
nice relevance after we are synthesizing environment friendly transfer sequences, or when
we wish to discuss “optimality”. As an example, think about a
counterclockwise transfer: $F^{-1}$. It’s pure to think about this a
single transfer, but when we think about our set to be $C$, then we’d should
rely it as three strikes, since $F^{-1} = Fcirc Fcirc F = F^3$. What
about $F^2$? Is that one transfer or two? Speedcubers typically think about
$F^2$ to be one movement, so counting that as one transfer is pure, however
many laptop puzzlers just like the simplicity of $Ccup C^{-1}$, i.e.,
solely ninety-degree turns.

For the remainder of this word, we’ll be within the former camp, the place half-turns rely as one, and we’ll denote this set of strikes as:

bar C := C cup C^{-1} cup C^{occasions 2}.

What’s a phrase?

After we agree on what we think about a transfer, we will be extra particular as
to what we imply about transfer sequences. A transfer sequence is a presumably
empty listing of strikes. A transfer sequence will be composed to type the
permutation it represents. This composition operator known as
$kappa$, and is definitely outlined. Let $M$ be a transfer set, and let $s =
[s_1, s_2, ldots, s_n]$ be a sequence of $n$ strikes with every
$s_{bullet}$ a transfer from $M$. The size is $s$ is of course $n$,
and its composition is outlined as:

kappa([,]) &:= (1, 2, 3, ldots)
kappa([s_1, s_2, ldots, s_{n-1}, s_n]) &:= kappa([s_1, s_2, ldots, s_{n-1}])circ s_n.

If $M$ is a transfer set, then the set of all transfer sequences (together with
the empty sequence) is denoted $M^{*}$, a notation kindly borrowed
from formal language idea.

If we determine the weather of $M$ with symbols, then a transfer sequence
known as a phrase. We’ll at all times sort symbols in $texttt{typewriter}$
font. The strikes ${F, R, U, B, L, D}$ have the symbols
${texttt{F}, texttt{R}, texttt{U}, texttt{B}, texttt{L},
texttt{D}}$, an inverse $F^{-1}$ has the image $texttt{F’}$, and
a sq. $F^2$ has the image $texttt{F2}$. And we sort phrases as
symbols joined collectively in reverse order, so $[R^{-1},
U^2, L]$ will be represented by the phrase $texttt{L U2 R’}$.

The excellence is delicate however necessary. In a pc program, a transfer
sequence is a listing of permutations, whereas a phrase is a listing of
symbols. A Rubik’s Dice fixing program ought to take as enter a
permutation, and output a phrase which when composed as a transfer sequence,
brings that permutation to id.

When doing math, we frequently combine up all of those ideas since they’ve
little bearing on the correctness of an argument. Whether or not it’s the
permutation $Fcirc R^{-1}$ or the transfer sequence $[F, R^{-1}]$ or the
phrase $texttt{R’ F}$ or in any other case, all of them symbolize roughly
the identical factor, however computer systems have to be express about which
illustration is being manipulated.

So, in abstract:

  • A transfer set is a set of permutations that “rely” as one transfer.
  • A transfer sequence is a listing of strikes from a transfer set.
  • The composition of a transfer sequence is the permutation that transfer sequence represents.
  • A image is a designator for a transfer in a transfer set.
  • A phrase is a sequence of symbols.

Again to this brute-force factor&mldr;

Statement #3: sorting as fixing

As foolish as the instance is, let’s suppose we all know, for a truth, {that a}
Rubik’s Dice was combined up utilizing solely six strikes from $bar C$. Since
$bar C$ has 18 parts, with none optimization, we’d should
attempt $18^6$ transfer sequences to discover a resolution.

As a substitute of brute-forcing in that means, we are able to do one other trick. Let s
be our scrambled permutation.

  1. Write out each mixture of three strikes right into a desk. The important thing would
    be the permutation, and the worth could be the phrase related to
    that permutation. Name this desk A.

  2. Type A in ascending lexicographic order on the permutation.

  3. Make a replica of A, name it B. For all (perm, phrase) in B,
    reassign perm := compose(invert(perm), s). We do that due to
    Statement #1.

  4. Type B.

  5. Name x := findCommon(A, B). We do that through Statement #2.

  6. Reconstruct a phrase equal to s by A[x].phrase ++ reverse(B[x].phrase). We do that to get better a ultimate consequence through Statement

Since we now have a phrase that brings us from solved to s, we are able to
invert the phrase to convey us from s to solved.

By this methodology, we prevented visiting all $16^6$ transfer sequences by
as a substitute pre-calculating two teams of $16^3$ sequences and exploring
them for an intersection. We have now lower the quantity of labor right down to its
sq. root.

If we generalize to size $n+m$ (for some splitting of $n$ and $m$),
then we are able to substitute the work of visiting $16^{n+m}$ states with
$16^m + 16^n$ states, which is a lot better.

So we’re executed? We now know that the Rubik’s Dice requires not more than
20 strikes, so if we make two tables enumerating 10 strikes, we needs to be

Nicely, err, $16^{10} = 1,099,511,627,776$. Until we now have trillions of
sources to area, be it time or area, it’s nonetheless not going to work.

Extra splitting?

An enterprising laptop science pupil, at this level, would possibly odor
recursion. If we cut up as soon as, can we cut up once more? If we all know a Rubik’s
Dice will be solved in 20 strikes, can we cut up it into two 10 transfer
issues, and every of these into two 5 transfer issues?

The issue with that is that on the prime layer of recursion, it’s
clear what we’re fixing. At decrease layers, it’s not clear. What
really is the recursive construction at play? And if we may do that
trick, couldn’t we decimate any brute-force drawback of exponential
complexity (e.g., in variety of strikes) into one in all linear?

That isn’t going to work, however we will be impressed by it. Let $L := bar
C^5$ be the set of 5-move combos from $bar C$. The scale of $L$
goes to be $621,649$ if we don’t retailer redundant
permutations. That is positively doable to compute. Then our purpose is
to discover a decomposition of $s$ when it comes to a component in $Lcirc
Lcirc Lcirc L$. Utilizing the identical trick from Statement #1, suppose
there’s a decomposition $$s = l_4circ l_3circ l_2circ l_1.$$ Then
$$l_3^{-1}circ l_4^{-1} circ s = l_2circ l_1.$$ So we create 4

  • $L_1 = L$,
  • $L_2 = L_1$,
  • $L_4 = L_1^{-1}$, and
  • $L_3 = L_4circ s$.

No, the $4$ earlier than $3$ isn’t a typo! We put this to be able to save on
computation and keep away from redundant work. Now our purpose is to seek out an
component in widespread between the 2 units

X &= L_2 circ L_1
Y &= L_4 circ L_3.

By some means, we should do that with out really calculating all parts of
$L_icirc L_j$. And, so as to add insult to harm, for findCommon to
work, we want to have the ability to undergo the set in sorted order.

Iterating by means of merchandise with Schroeppel–Shamir

Suppose we now have two lists of constructive numbers $A$ and $B$. How can we
print the weather of ${a+b : ain A, bin B}$ in numerical order
with out explicitly setting up and sorting this set? Shamir and his
collaborator Schroeppel did so with the next algorithm.

  1. Type $A$ in ascending order. Pop off the primary (and subsequently
    smallest) component $a_1$.

  2. Create a precedence queue $Q$ and initialize it with $(a,b)$ with
    precedence $a_1 + b$ for all $bin B$.

  3. Repeat the next till $Q$ is empty:

    1. Pop $(a,b)$ off $Q$. This can type the following smallest sum, so print $a+b$.
    2. Discover $a’$ which instantly succeeds $a$ in our sorted listing $A$.
    3. Push $(a’,b)$ with precedence $a+b$ onto $Q$.

This algorithm will terminate, having printed every sum successively
with at most $O(vert Avert + vert Bvert)$ area and nearly linear
time. (The sorting and precedence queue upkeep require some
logarithmic components.)

With a bit of work, one can see why this works. In a way it’s a
two-dimensional sorting drawback, that will depend on one essential truth: If
$x le y$ then $x+z le y+z$. (That is to say that addition is
monotonic.) Given how the precedence queue is constructed, it’ll
at all times include the smallest sum.

Might we do that with permutations? If we now have two lists of
permutations $A$ and $B$, and $a_1$ is the “smallest” (i.e.,
lexicographically least) permutation of $A$, and $b_1$ is the
“smallest” permutation of $B$, then it’s patently not true that
$a_1circ b_1$ is the smallest component of $Acirc B$. In symbols,

(min A) circ (min B) neq min (Acirc B).

Equally, if two permutations fulfill $a < b$, then it’s patently
not true

acirc z < bcirc z

for a permutation $z$.

The monotonicity of addition is what permits us to do steps 3.2 and three.3
so simply. If we did the identical with permutations, we’d not
have the assure that the minimal composition exists throughout the

See Also

This was the following hurdle Shamir cleared. Fixed within the dimension of $A$
or $B$, Shamir discovered a option to clear up the next drawback: Given a
permutation $ain A$ and $bin B$, discover the component $b’in B$ such
that $acirc b’$ instantly succeeds $acirc b$. In different phrases, we
can generate, one-by-one, a sequence of $b$’s wanted for step 3.2 and
3.3. With this algorithm (which we’ll describe within the subsequent part),
our Shamir–Schroeppel algorithm for permutations turns into the

Algorithm (Stroll Merchandise):

  1. Initialize an empty precedence queue $Q$ whose parts are pairs of
    permutations with precedence decided by one other permutation in
    lexicographic ordering.
  2. For every permutation $bin B$:
    1. With Shamir’s trick, discover the $ain B$ such that $acirc b = min (Acirc b)$.
    2. Push $(a, b)$ onto $Q$ with precedence $acirc b$.
  • (Invariant: At this level, we will definitely have $min (Acirc B)$ within the queue.)
  1. Repeat the next till $Q$ is empty:
    1. Pop $(a,b)$ off $Q$. This can type the following smallest $acirc b$, so print it.
    2. With Shamir’s trick, discover $a’$ such that $a’circ b$ instantly succeeds $acirc b$.
    3. Push $(a’,b)$ with precedence $a’circ b$ onto $Q$.

This algorithm will produce the weather of $Acirc B$, one-by-one in
lexicographic order.

What’s Shamir’s trick? We’d like an information construction and a intelligent commentary.

Permutation tries

With a purpose to deal with units of ordered permutations higher, Shamir created
an information construction. I name it a permutation trie. A permutation trie
of size-$okay$ permutations is a $okay$-deep, $okay$-ary tree, such {that a}
path from root-to-leaf follows the weather of a permutation. The leaf
comprises information which we wish to affiliate with the permutation.

For instance, think about permutations of dimension $5$. Suppose we wished to
affiliate the image $texttt{p6}$ with the permutation
$(2,4,1,3,5)$. Then we’d have a $5$-layer tree with a root node
$R$, such that $R[2][4][1][3][5] = texttt{p6}$.

Extra typically, let’s affiliate the next symbols with the
following permutations in a permutation trie $R$:

texttt{p1} &leftarrow (1,2,3,4,5) & texttt{p2} &leftarrow (1,2,3,5,4) & texttt{p3} &leftarrow (1,2,4,3,5)
texttt{p4} &leftarrow (1,2,5,3,4) & texttt{p5} &leftarrow (1,3,4,5,2) & texttt{p6} &leftarrow (2,4,1,3,5)
texttt{p7} &leftarrow (4,1,3,2,5) & texttt{p8} &leftarrow (4,1,3,5,2) & texttt{p9} &leftarrow (5,1,2,3,4)

The trie could be an information construction that appears like this:

An example permutatioen trie.

Regardless that we don’t present them, conceptually, every node within the trie
has a full length-$5$ array, with some parts empty (i.e., there are
no youngsters).

What’s good about this information construction? At the start, pre-order
traversal will go to the permutations in lexicographic order. We will
use this information construction to retailer two issues on the leaves (i.e.,

  1. The precise permutation information construction representing that path, and
  2. The phrase we used to assemble that permutation.

That is the info construction, and now we get to Shamir’s
perception. Suppose we now have a permutation $s$ and a permutation trie $R$
(which represents a set of permutations), and we wish to traverse
$scirc R$ in lexicographic order. The naive means is to assemble a brand new
trie, however we want to keep away from that. To elucidate the concept, we’ll select a
concrete instance.

Let’s use $R$ from above. Let $s := (3,1,4,2,5)$. (Word that $snotin
R$, however that’s not necessary.) We want to discover an $r’in R$ such that
$scirc r’ = min (scirc R)$. Nicely, the smallest permutation could be
one such that $r'(1) = 2$, as a result of then $s(r'(1)) = s(2) = 1$. Wanting
at our trie $R$, we are able to see the one candidate is that related to
$texttt{p6}$: $(2,4,1,3,5)$, which is the minimal.

What in regards to the subsequent smallest $scirc r”$? For ease, let’s name this
product $m$. We might need a permutatation such that $r”(1) = 4$,
as a result of $m(1) = s(r”(1)) = s(1) = 2$. This time, there are two

(4,1,3,2,5)qquad (4,1,3,5,2)

So a minimum of we all know $m = (2, ldots)$. To disambiguate, we have to
take a look at $r”(2)$. These are the identical, likewise $r”(3)$, so we now have no
diploma of freedom at $2$ or $3$ to reduce the product. Thus $m = (2,
3, 4, ldots)$. We have now a selection at $r”(4)$, nevertheless. Your best option
is $r”(4) = 2$, as a result of $m(4) = s(r”(4)) = s(2) = 1$, the smallest
doable selection. This disambiguates our selection of $r”$ to be
$(4,1,3,2,5)$ in order that $m = (2,3,4,1,5)$.

We may repeat the process to seek out the following smallest product
$scirc r”’$. What precisely is the process right here? Nicely, we walked
down the tree $R$, however as a substitute of strolling down it straight, we as a substitute
did so in a permuted order based mostly on $s$—particularly
$s^{-1}$. Take into account our regular algorithm for strolling the tree in
lexicographic order:

perform walkLex(R):
  if notTree(R):
    print R
    for i from 1 to size(R):
      if R[i] exists:

We will as a substitute stroll in permuted order, in order that we produce a sequence
$[r, r”, r”’, ldots]$ such that

scirc r < s circ r’ < s circ r”’ < cdots,

we modify our strolling algorithm as so:

perform walkProductLex(R, s):
  stroll'(R, inverse(s))

perform stroll'(R, s):
  if notTree(R):
    print R
    for i from 1 to size(R):
      j = s(i)
      if R[j] exists:
        stroll'(R[j], s)

Word that $s$ was inverted earlier than the recursion to make fast permuting of every node.

With this, we now have the outstanding capacity to iterate by means of merchandise
in lexicographic order, with out having to enumerate all of them and kind
them. This was the final and demanding ingredient.

The 4-Record Algorithm and fixing the Rubik’s Dice

Now we wish to put this all collectively to create the 4-Record
. Let’s restate the issue in clear phrases.

Drawback (4-Record): Let $s$ be a permutation. Let $L_1$, $L_2$,
$L_3$, and $L_4$ be units of permutations such that we all know $sin
L_4circ L_3circ L_2circ L_1$. Discover $l_1in L_1$, $l_2in L_2$,
$l_3in L_3$, and $l_4in L_4$ such that $s = l_4circ l_3circ
l_2circ l_1$.

Piecing collectively the weather above, we arrive on the 4-Record Algorithm.

Algorithm (4-Record):

  1. Assemble $L’_3 := L_3^{-1}circ s$ and $L’_4 := L_4^{-1}$.
  2. Create two mills: $X_1$ that walks $L_2circ L_1$ in
    lexicographic order, and $X_2$ that walks $L’_3circ L’_4$ in
    lexicographic order. Do that through the use of the Stroll Merchandise
    algorithm, which itself is applied by setting up permutation
    tries and utilizing walkProductLex.
  3. Name findCommon on $X_2$ and $X_1$. That is assured to discover a
    resolution $(l_3^{-1},l_4^{-1}circ s,l_2,l_1)$. Course of the answer
    to return $(l_4, l_3, l_2, l_1)$.

The principle problem of this algorithm, apart from implementing every
subroutine accurately, is plumbing the suitable information round.

Now, we are able to use this to resolve a scrambled Rubik’s Dice $s$.

Algorithm (Clear up Dice):

  1. Let $L = bar C^5$, conserving a report of the phrases used to assemble
    every component of $L$. (We advocate instantly making a permutation
    trie, the place the leaves retailer the phrases.)
  2. Apply the 4-Record Algorithm to the issue $s in Lcirc Lcirc
    Lcirc L$ to supply $(l_4, l_3, l_2, l_1)$.
  3. Return phrases $(w_4, w_3, w_2, w_1)$ related to the
    permutations $(l_4, l_3, l_2, l_1)$.

Amazingly, this algorithm actually works, and solutions our weblog put up
query within the affirmative: sure, the Rubik’s Dice will be

Instance and supply code

This algorithm is applied in Frequent Lisp, in my computational
group idea bundle
already has inbuilt help for Rubik’s Cubes as permutation
teams. Beginning a brand new Frequent Lisp session, we now have the next:

> (ql:quickload '(:cl-permutation :cl-permutation-examples))
> (in-package :cl-permutation)
> (group-order (perm-examples:make-rubik-3x3))
> (format t "~R" *)
forty-three quintillion 2 hundred fifty-two quadrillion three trillion
2 hundred seventy-four billion 4 hundred eighty-nine million
eight hundred fifty-six thousand

The built-in Rubik’s Dice mannequin solely makes use of ${F, R, U, B, L, D}$, so
we make new mills equivalent to $bar C$.

> (defvar *c (loop :with dice := (perm-examples:make-rubik-3x3)
                   :for g :in (perm-group.mills dice)
                   :acquire (perm-expt g 1)
                   :acquire (perm-expt g 2)
                   :acquire (perm-expt g 3)))
> (size *c)

Now we assemble $bar C^5$.

> (defvar *c5 (generate-words-of-bounded-length *c 5))
> (perm-tree-num-elements *c5)

Word that this constructs a perm-tree object, which routinely
shops the phrases related to every permutation generated.

Now let’s generate a random component of the dice group.

> (defvar *s (random-group-element (perm-examples:make-rubik-3x3)))
> *s
#<PERM 43 44 41 20 47 11 28 9 24 13 17 42 36 40 37 25 6 21 1 29 7 19 10 3 35 39 22 18 34 33 31 48 16 15 30 2 23 32 26 46 8 4 27 12 45 14 5 38>

Lastly, we run the 4-list algorithm and wait.

> (decompose-by-4-list *s *c5 *c5 *c5 *c5 :verbose t)
10,000,000: 52 sec @ 192,553 perms/sec; .0013% full, eta 1114 hours 58 minutes
20,000,000: 48 sec @ 206,858 perms/sec; .0026% full, eta 1037 hours 51 minutes
Analysis took:
  145.094 seconds of actual time
  145.097120 seconds of whole run time (144.961382 person, 0.135738 system)
  [ Run times consist of 2.405 seconds GC time, and 142.693 seconds non-GC time. ]
  100.00% CPU
  421,375,385,955 processor cycles
  11,681,934,352 bytes consed

((8 11 14 2 4)
 (1 16 9 15 1)
 (7 6 18 8 15)
 (9 13 16 15 8))

We’re fairly fortunate this one resulted in a mere 2 minutes 25 seconds! It
often isn’t so immediate with a solution.

The outcomes are printed as 4 phrases: our $l_4$, $l_3$, $l_2$, and
$l_1$. Every integer $n$ represents the 1-indexed $n$th permutation of
$bar C$ (ordered by the way it was constructed). We will create a extra
conventional notation:

> (defvar *resolution (scale back #'append *))
> (defun notation (ws)
    (dolist (w (reverse ws))
      (multiple-value-bind (transfer order)
          (ground (1- w) 3)
        (format t "~C~[~;2~;'~] "
                (aref "FRUBLD" transfer)
> (notation *resolution)
U2 L' D L U' L' U2 D' R' U F L' U' D F R F2 L2 B2 U2

How do we all know if that is right? We have to verify that the
composition of this phrase equals our random component, which we do by
composing the phrase (utilizing one thing CL-PERMUTATION calls a “free-group
homomorphism”), inverting the permutation, and composing it with our
scramble to see that it brings us to an id permutation.

> (defvar *hom (free-group->perm-group-homomorphism
                (make-free-group 18)
                (generate-perm-group *c)))
> (perm-compose (perm-inverse (funcall *hom *resolution)) *s)
#<PERM 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48>

Certainly, we discovered a reconstruction of our dice.

Ideas for optimizing the 4-Record Algorithm

Probably the most troubling facets of implementing this algorithm is
making it quick sufficient. My preliminary implementation labored at a whopping
200 permutations per second. That’s extremely gradual, and meant that it
would take effectively over a century (within the worst case) for my program to
end. Now, it really works at about 190,000 permutations per second, with
an estimated worst-case search time of two months. (I haven’t
encountered a scrambled dice place which has taken greater than 10

Listed below are some methods I sped issues up.

  1. Be economical with reminiscence. When doing exploratory programming, it’s
    fascinating to tag and retailer every little thing, however every of these storages
    and accesses take time.
  2. Don’t use precise arrays within the permutation trie. After I did that,
    I ran out of reminiscence. I as a substitute opted for a sparse illustration
    utilizing an “a-list” (that’s, a linked listing of (index, worth)
  3. Make the permutation dealing with quick, like composition, equality
    testing, and lexicographic ordering. I used to be initially utilizing generic
    arithmetic and 64-bits to symbolize every permutation component, and
    it degraded velocity.
  4. Use a great precedence queue implementation. You’ll be pushing and
    popping tons of of tens of millions of parts.
  5. Do some evaluation and compress the permutation trie
    illustration. Most nodes of the trie will solely include one
    worth. If that’s the case, simply retailer as a substitute the permutation (and
    no matter worth related to it) on the shallowest depth. This
    will save loads of time by avoiding loads of useless (permuted)

In case you have different ideas for dashing up the algorithm, please e-mail me!

Pattern benchmarks

Within the following, we solely think about the issue of fixing the Rubik’s
Dice utilizing the 4-list algorithm, assuming an answer size of 20

My laptop is a ThinkPad twenty fifth Anniversary Version. It has an Intel
Core i7-7500U processor at 2.70 GHz, however boosting to three.50 GHz. It has
32 GiB of RAM, however comfortably runs the solver with round 3–4 GiB.

The algorithm as applied is ready to verify round 190,000 parts
per second.

Producing the transfer lists and pre-processing is a comparatively fastened
value. The lists will be generated as soon as, however the preprocessing (i.e.,
composing the scramble with one of many lists) must occur every
clear up. In my implementation, the initialization value is constantly 9

After initialization, the search is carried out. The run time varies
wildly, anyplace from seconds to hours.

  • 64 s, 188 billion CPU cycles, 4 GiB of allocation
  • 165 s, 480 billion CPU cycles, 12 GiB of allocation
  • 2210 s, 6 trillion CPU cycles, 162 GiB of allocation
  • 4613 s, 13 trillion CPU cycles, 356 GiB of allocation
  • 24010 s, 70 trillion CPU cycles, 2 TiB of allocation

These are randomly sampled Rubik’s Dice scrambles, sorted by time.

In precept, with the present degree of optimization, the algorithm
can take as a lot as 2 months to complete. I’m assured that my
implementation will be introduced down an element of two, much less assured it
will be simply introduced down an element of fifty—nevertheless it wouldn’t shock
me both means.

One attention-grabbing factor about this algorithm is that it appears to return
very, in a short time if the answer is 10 or fewer strikes. Why? I
haven’t executed a cautious evaluation, however I imagine it’s basically
as a result of the answer will likely be in $L_2circ L_1$. The permutations $l_3$
and $l_4$ will likely be id, which reduces to the issue of simply
discovering $sin L_2circ L_1$.


“Meet within the center” algorithms are outdated and effectively understood. After we
can’t brute-force a whole area, we are able to attempt splitting it in two and
attempt to mix them. That’s in fact the spirit of the 4-Record
Algorithm, however the satan is at all times within the particulars, and I hope this
weblog put up confirmed loads of disparate information wanted to return collectively to
notice the algorithm.

I feel the algorithm communicated by Shamir and his colleagues has
been outstanding however forgotten. Whereas higher algorithms exist for the
particular job of fixing the Rubik’s Dice, the generality of the
4-Record Algorithm ought not be understated.


  1. A. Fiat, S. Moses, A. Shamir, I. Shimshoni and G. Tardos, “Planning and studying in permutation teams,” thirtieth Annual Symposium on Foundations of Laptop Science, Analysis Triangle Park, NC, USA, 1989, pp. 274–279, doi: 10.1109/SFCS.1989.63490. (Link)
  2. A. Bawden. “Shamir’s speak actually was about methods to clear up the dice!”. Alan Bawden. From the Dice Lovers mailing listing. 27 Could 1987. (Link)

Source Link

What's Your Reaction?
In Love
Not Sure
View Comments (0)

Leave a Reply

Your email address will not be published.

2022 Blinking Robots.
WordPress by Doejo

Scroll To Top