<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Brad Hodkinson on Medium]]></title>
        <description><![CDATA[Stories by Brad Hodkinson on Medium]]></description>
        <link>https://medium.com/@brad.hodkinson2?source=rss-44123ebc3bbc------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*NdXnnd6WZbASjRmk</url>
            <title>Stories by Brad Hodkinson on Medium</title>
            <link>https://medium.com/@brad.hodkinson2?source=rss-44123ebc3bbc------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 06:48:21 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@brad.hodkinson2/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Writing Code to Solve a Rubik’s Cube]]></title>
            <link>https://medium.com/@brad.hodkinson2/writing-code-to-solve-a-rubiks-cube-7bf9c08de01f?source=rss-44123ebc3bbc------2</link>
            <guid isPermaLink="false">https://medium.com/p/7bf9c08de01f</guid>
            <category><![CDATA[rubiks-cube]]></category>
            <category><![CDATA[neural-networks]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[matrix]]></category>
            <category><![CDATA[group-theory]]></category>
            <dc:creator><![CDATA[Brad Hodkinson]]></dc:creator>
            <pubDate>Wed, 29 Apr 2020 02:58:24 GMT</pubDate>
            <atom:updated>2020-04-29T02:58:24.814Z</atom:updated>
            <content:encoded><![CDATA[<p>Discussion on Techniques to Solve a Rubik’s Cube</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*lAXw3UX5MYOdf8Zn" /></figure><p>As a software developer and avid Rubik’s cube solver, I have always been fascinated by the idea of creating a solving algorithm. I decided to embark on the challenge of trying to develop an algorithm that can solve a Rubik’s cube of size N in the optimal number of moves. Here is my collection of research in preparation for designing an algorithm.</p><h3>Background</h3><p>A Rubik’s cube is a popular puzzle that became popular in the 1980s. Ernõ Rubik, a professor from Hungary, invented the Rubik’s Cube in 1974 to help his students better understand three-dimensional problems.¹ It took the inventor a month to figure out how to solve the magic cube.</p><p>Now the world record for fastest time solving a Rubik’s cube at the time of this article is being written is 3.47 seconds, set in 2018 by Yusheng Du.³ There have been hundreds of millions Rubik’s cubes sold worldwide, meaning that most people have either seen or played with one before. The puzzle offers a great challenge. This is because there are approximately forty-three quintillion possible states the cube can be in, and only one correct solution. This number is obtained by knowing that 8 corner cuboids can be rotated in three different ways, and 12 edge cuboids can be rotated in 2 different ways. Knowing this, we can multiply the number of combinations of the corners by the number of combinations of the edges and divide by the number of correct orbits. This eliminates any of the impossible cube states.</p><figure><img alt="Calculation for obtaining 43 quintillion." src="https://cdn-images-1.medium.com/max/1024/0*MzCpqLy7orEnJEcg" /></figure><h3>Notation</h3><p>To better understand the Rubik’s cube, it is important to have a good grasp on the notation. The World Cubing Associations(WCA) has approved a standard notation that can be found in Article 12 of their posted Regulations.² It is crucial to understand what a face is if you wish to understand the common notation. A face is simply the collection of Facelets on a given side. A Rubik’s cube has 6 faces. If you hold a cube flat in your hand, the face that is on top is known as the up face, the opposite of this face is the down face. The face in front of you is known as the front face, and the one opposite of this is known as the back face. The last faces are the left and the right face, and as the name suggests, they are the faces on the left and right. Figure 1 better illustrates the orientation of the faces.</p><figure><img alt="A diagram of cube faces being labeled." src="https://cdn-images-1.medium.com/max/752/0*HqTX7bPFD_wAM-bA" /><figcaption>Figure 1. Face Notation</figcaption></figure><p>The common notation for a Rubik’s cube uses capital letters to represent a face.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FVU8UHPfmnNv48tMosw7UQ.png" /></figure><p>Each notation is a capital letter corresponding to the first letter for the face that it represents. A single tick following a letter represents a counter-clockwise rotation. So the notation F’ would mean a counter-clockwise rotation of the front face. The number two following a letter represents a double turn. So the notation R2 would mean two rotations of the right face.</p><h3>Scrambling</h3><p>Scrambling, or mixing up the cube, is done by applying a series of random moves to the cube. There are plenty of scrambling software programs that exist. These programs give the option to enter the size of the cube, and the length of the scrambling algorithm that should be generated. The WCA outlines scrambling in Article 4 of its regulations.² A typical scramble is between 20 to 30 moves in duration.</p><h3>How Humans Solve the Cube</h3><p>There are multiple ways a human can learn to solve a Rubik’s cube. Many tutorials can be found on YouTube. The most common methods are the beginner’s method and CFOP. The beginner’s method consists of 7 steps: cross, corners, middle layer, top cross, fix cross, permutation of corners, and orientation of corners. In contrast, the more advanced method CFOP is only 4 steps: cross, first two layers(F2L), the orientation of the last layer(OLL), and the permutation of the last layer(PLL). Orientation refers to twisting and flipping a cuboid in its current location, while permutation refers to rearranging the cuboids to a new location.</p><p>A dedicated speedcuber will remember all 57 algorithms to solve all possible OLL cases, and all 21 algorithms to solve the PLL cases. An algorithm for one of these steps can involve anywhere from 7 to 17 moves. A novice using the beginner method can typically solve a Rubik’s cube in about 200 moves, while a speedcuber using CFOP can solve a cube in roughly 60 moves.</p><p>The optimal number of moves to solve a Rubik’s cube is 20. This was discovered in 2010 by Tomas Rokicki, Herbert Kociemba, Morley Davidson, and John Dethridge.⁴ They were able to achieve this by reducing the 43 quintillion possible combinations by a factor of 48 using symmetry and mirrored states. There are 24 ways you can orient a Rubik’s cube in space and you can multiply this by two to account for mirrored states. After this reduction, only a set of 55,882,296 cube states had to be examined.⁴ Using Google’s computers they were able to generate the numbers shown in Table 2. Calculating these numbers would take a good desktop computer 35 CPU-years.⁴</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qFth7x3XrvoefO8YRdSSsw.png" /></figure><h3>Representing the Cube in Code</h3><p>There are many different ways a Rubik’s cube can be represented in code, each comes with perks with regards to space complexity and ability to be easily manipulated by a solving algorithm. A few common ways of representing a cube are: one-dimensional array, two-dimensional array, three-dimensional array, object-oriented programming, matrix, and binary. Additionally, there are many more ways you can represent the cube such as the use of strings and other data structures, but those are not discussed in this article. Another important design choice to consider is how to represent the Facelet of the cube. Usually, a Facelet will store the color, but optionally it might also store the location depending on how the cube is being represented.</p><h4>One-dimensional array</h4><p>Using a one-dimensional array each Facelet of the cube can be labeled 0 through 53. Each of these labels maps the respective Facelet to the index in the array. A move to the cube can be executed by performing a mapping function that rearranges the Facelets accordingly.</p><h4>Two-Dimensional Array</h4><p>A cube can be represented using a two-dimensional array to store an array of faces. This approach stores six faces, where each face is an array of the nine Facelets corresponding to the given face.</p><h4>Three-Dimensional Array</h4><p>Using object-oriented programming(OOP) principles a cube can be represented using a three-dimensional array. Working with an abstract cubie class, corners, edges, and centers can all extend from this class. The three-dimensional array can consist of cubies. Each one located in its corresponding location to where its three-dimensional coordinate would be on a real cube.</p><h4>Matrix</h4><p>A matrix can be used to store the cubes state by using a 6 by 54 matrix. Each of the columns represents one Facelet on the cube and a one is placed in the column corresponding to its color row. An example of an encoded matrix can be seen below in Figure 2.</p><figure><img alt="Matrix encoded with the representation of a Rubik’s cube state." src="https://cdn-images-1.medium.com/max/1024/1*MafSQUF-M5uk5okBRCmGAA.png" /><figcaption>Figure 2. Matrix Encoding of Cube State</figcaption></figure><p><strong>Binary</strong></p><p>A Rubik’s cube can be represented using binary. This approach is similar to the two-dimensional array approach. It stores six 64 bit integers, one for each face of the cube. Each byte(8 bits) stores the color encoded in binary. Since the centers never move we can omit them and just store the eight remaining Facelets. This gives the advantage of operations such as bit shifting and masks to manipulate the cube, leading to fast execution time and efficient space complexity.</p><h3>Current Algorithms</h3><p>There are many algorithms to solve a Rubik’s cube. One of the easiest ways is to model the way humans solve the cube. This works, but is not the most efficient way to solve the cube, given it takes a human 60 to 200 moves to solve a cube depending on their method of choice. Knowing that any cube can be solved in 20 moves or less, there must be a better approach. There are advanced algorithms such as the 2-phase algorithm that works by solving small sections of the cube and expanding out.¹⁰ Additionally, some algorithms work using machine learning algorithms, such as neural networks, to train a model to solve a cube. These models tend to take a long time to train but tend to find the optimal solution to solving the cube.</p><h4>Neural Networks</h4><p>Neural networks are modeled after the human brain. They work by going through a series of training phases called epochs. Each iteration updates weights on a node in a network at a specified learning rate to make it produce the expected value. Neural networks are one of the most powerful machine learning algorithms out there. With proper training, they can produce extremely accurate results, but they take a long time to train depending on how many layers the network has, and how many iterations of training it undergoes.</p><p>Trying to solve a Rubik’s cube using a neural network tends to be a difficult challenge since there is a large state space with only one correct solution. This means the neural network will have a tough time knowing how to update the states since randomly doing moves will not likely stumble upon the solved cube state. One way to improve the performance of a neural network is by the use of boosting using weak learners. One group tested a program using this method and found “AdaBoost had worse classification accuracy than a baseline [Stochastic Gradient Descent] method that did no boosting.”⁶ Their findings concluded that neural networks are expensive to train and boosting did not work well because weak learners are better suited when the weak learner is truly a weak learner.⁶</p><p>Neural networks can be combined with other algorithms to make a powerful solver. This is exactly what powers the DeepCubeA solver. DeepCubeA “works by using approximate value iteration to train a deep neural network (DNN) to approximate a function that outputs the cost to reach the goal (also known as the cost-to-go function).”⁷ This DNN is then combined with an A* search algorithm to help approximate and compare the values of various cube states while searching for a solution.</p><p>Another group used a neural network to help improve their solving algorithm by swapping between two different algorithms. They called their method Approximate Policy Iteration(API). The API “algorithms iterate between two policies: a slow policy (tree search), and a fast policy (a neural network).”⁶ By doing this they were able to use the perks of both a search tree(Monte Carlo Search Tree) and a neural network to train and build an accurate model that could efficiently solve Rubik’s cubes.</p><p>Another option is to give the neural network easier training data in the beginning. An example of this would be to give it cubes that have only been scrambled with 3 moves instead of 1000 moves. This will allow the neural network to be trained at a much faster rate. As the neural network starts to become more proficient at solving cubes in an easier state, we can increase the difficulty by training on cubes that have more scramble moves applied. This also helps overcome the problem of there being a sparse reward in a large state space.</p><h4>A*</h4><p>A* is a popular heuristic search algorithm that seeks to find the smallest cost or shortest path to a given goal node, in this case, the solved cube. This is done by using the following equation:</p><blockquote>f(n) = g(n) + h(n)</blockquote><p>This function helps the search algorithm determine which node to advance to. This is applied to all the available nodes from the given starting node. N is the next node on the path, g(n) is the cost of the path from the start node, and h(n) is a heuristic that estimates the cost of the cheapest path from n to the goal. The algorithm chooses to advance to the node that produces the lowest f(n). Also important to note if it is not possible to extend the path, or it has reached some terminating state then it goes back to the starting node and chooses the next minimum f(n) value. This algorithm is memory intensive since it has to store all the nodes it uses in memory. Similar to the neural network, it works well if combined with another algorithm technique.</p><h4>Korfs/Iterative-Deepening-A*</h4><p>Korf’s algorithm can find an optimal solution to solving a Rubik’s cube. The algorithm uses “iterative-deepening-A* (IDA*), with a lower bound heuristic function based on large memory-based lookup tables, or pattern databases.”⁹ This method uses the A* algorithm previously discussed but it can improve the searching with the pattern databases. These pattern databases “store the exact number of moves required to solve various subgoals of the problem.”⁹ This technique allows this algorithm to solve the cube in the optimal number of moves, but this comes with the trade-off of time. The DeepCube team concluded in their comparison that “Korf’s algorithm will always find the optimal solution from any given starting state; but, since it is a heuristic tree search, it will often have to explore many different states and it will take a long time to compute a solution.”⁶ Since this algorithm directly scales with the amount of memory, as computers harness more computing power and memory increases, this algorithm will see drastic increases in time. For now, we can improve this algorithm with better indexing techniques. Additionally finding better ways to hash the cube will help minimize the amount of memory required to store all the cube states in the pattern database.</p><h4>Kociemba/2-Phase Algorithm</h4><p>As the name implies, this algorithm has two phases. The first phase puts pieces into the correct position but does not change the orientation of any of the corners or edges. This is done by restricting the moves to only a small set of moves (U, D, B2 F2, L2, R2). Once this phase finds “the shortest number of moves required to do this, it continues until it has a large enough supply of possible solutions ranging from the lowest number of moves to a much larger number.”¹⁰ Advancing on to the second phase once all the permutations are restored, the algorithm searches for an optimal solution. This is not an optimal solution of the second phase, but rather an optimal solution of combining the moves of phase 1. An example of this is given by Ruwix. They state “for example, an 8 move Phase 1 followed by a 15 move Phase 2 is less optimal than a 10 move Phase 1 followed by a 5 move Phase 2.”¹⁰ Because the two phases break the problem into smaller problems, multiple solutions can be found very quickly for each step and combined to make the most favorable solution. The team at DeepCube states that “This algorithm relies on human domain knowledge of the group theory of the Rubik’s Cube. Kociemba will always solve any cube given to it, and it runs very quickly.”⁶ They also conclude that “Kociemba solved each cube in under a second.”⁶ This algorithm is extremely fast and is one of the best algorithms used to quickly solve the cube.</p><h3>Beyond the 3x3</h3><p>Beyond the 3x3, other cube sizes such as the 2x2 and 4x4 cubes on up to NxN cube can be solved using the methods discussed. Additionally, other combinatorial puzzles or group theory problems can benefit from this research as well. After making proper adjustments, other puzzles can be solved in the same way. Puzzles such as the 15 Sliding Puzzle and Lights Out can be solved using similar approaches or in some cases even using the same model.</p><h3>Conclusion</h3><p>There are many approaches to solve a Rubik’s cube. All are correct. Depending on what design choice is valued the most will determine the method selected. If the goal is an easy algorithm to develop, then modeling the way humans solve the cube using a layer-by-layer approach or CFOP is the best solution. If having the optimal number of moves is the desired result, then implementing Korf’s Iterative-Deepening-A*algorithm will suffice. If time is a concern, then a quicker algorithm such as Kociemba’s 2-Phase algorithm will yield a much faster solver. There is plenty we can learn from developing solutions to hard combinatorial puzzles such as the Rubik’s cube. Moving forward with difficult engineering challenges or problems that are too hard to solve using an exhaustive brute force approach, we can apply the methods and techniques discussed to help overcome them.</p><h3>Work Cited</h3><p>[1]: Rubik’s About Us. Our Heritage. <a href="https://www.rubiks.com/en-us/about">https://www.rubiks.com/en-us/about</a></p><p>[2]: World Cubing Association. (January 1, 2020). WCA Regulations. <a href="https://www.worldcubeassociation.org/regulations/">https://www.worldcubeassociation.org/regulations/</a></p><p>[3]: Guinness World Records. (November 24, 2018). Fastest time to solve a Rubik’s Cube. <a href="https://www.worldcubeassociation.org/regulations/">https://www.worldcubeassociation.org/regulations/</a></p><p>[4]: Cube 20. (July 1, 2010). God’s Number is 20. <a href="https://cube20.org/">https://cube20.org/</a></p><p>[5]: Irpan, Alexander. (2016). Exploring Boosted Neural Nets for Rubik’s Cube Solving. <a href="https://www.alexirpan.com/public/research/nips_2016.pdf">https://www.alexirpan.com/public/research/nips_2016.pdf</a></p><p>[6]: McAleer, Stephen, Forest Agostinelli, Alexander Shmakov, and Pierre Baldi. (2018). Solving the Rubik’s Cube with Approximate Policy Iteration.</p><p>[7]: Agostinelli, F., McAleer, S., Shmakov, A. et al. (2019). Solving the Rubik’s cube with deep reinforcement learning and search. <a href="https://doi.org/10.1038/s42256-019-0070-z">https://doi.org/10.1038/s42256-019-0070-z</a></p><p>[8]: Ben Botto. (May 26, 2019) Sequentially Indexing Permutations: A Linear Algorithm for Computing Lexicographic Rank. <a href="https://medium.com/@benjamin.botto/sequentially-indexing-permutations-a-linear-algorithm-for-computing-lexicographic-rank-a22220ffd6e3">https://medium.com/@benjamin.botto/sequentially-indexing-permutations-a-linear-algorithm-for-computing-lexicographic-rank-a22220ffd6e3</a></p><p>[9]: Korf, Richard E. (1997). Finding optimal solutions to Rubik’s cube using pattern databases. <a href="https://www.aaai.org/Papers/AAAI/1997/AAAI97-109.pdf">https://www.aaai.org/Papers/AAAI/1997/AAAI97-109.pdf</a></p><p>[10]: Ruwix. Herbert Kociemba’s Optimal Cube Solver — Cube Explorer. <a href="https://ruwix.com/the-rubiks-cube/herbert-kociemba-optimal-cube-solver-cube-explorer/">https://ruwix.com/the-rubiks-cube/herbert-kociemba-optimal-cube-solver-cube-explorer/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7bf9c08de01f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>