{"id":2452,"date":"2018-09-29T19:18:25","date_gmt":"2018-09-30T00:18:25","guid":{"rendered":"https:\/\/laurentlessard.com\/bookproofs\/?p=2452"},"modified":"2018-09-30T11:00:21","modified_gmt":"2018-09-30T16:00:21","slug":"pool-hall-robots","status":"publish","type":"post","link":"https:\/\/laurentlessard.com\/bookproofs\/pool-hall-robots\/","title":{"rendered":"Pool hall robots"},"content":{"rendered":"<p>This <a href=\"https:\/\/fivethirtyeight.com\/features\/the-robot-invasion-has-come-for-our-pool-halls\/\">Riddler puzzle<\/a> is about arranging pool balls using a robot!<\/p>\n<blockquote><p>\nYou own a start-up, RoboRackers\u2122, that makes robots that can rack pool balls. To operate the robot, you give it a template, such as the one shown below. (The template only recognizes the differences among stripes, solids and the eight ball. None of the other numbers matters.)<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/fivethirtyeight.com\/wp-content\/uploads\/2018\/09\/RIDDLER-BilliardsDiagram.png\" alt=\"source: https:\/\/fivethirtyeight.com\/features\/the-robot-invasion-has-come-for-our-pool-halls\/\" width=300\/><\/p>\n<p>First, the robot randomly corrals all of the balls into the wooden triangle. From there, the robot can either swap the location of two balls or rotate the entire rack 120 degrees in either direction. The robot continues performing these operations until the balls\u2019 formation matches the template, and it always uses the fewest number of operations possible to do so.<\/p>\n<p>Using the template given above \u2014 a correct rack for a standard game of eight-ball \u2014 what is the maximum number of operations the robot would perform? What starting position would yield this? How about the average number of operations?<\/p>\n<p>Extra credit: What is the maximum number of operations the robot would perform using any template? Which template and starting position would yield this?\n<\/p><\/blockquote>\n<p>Here is my solution:<br \/>\n<a href=\"javascript:Solution('soln_pool_hall','toggle_pool_hall')\" id=\"toggle_pool_hall\">[Show Solution]<\/a><\/p>\n<div id=\"soln_pool_hall\", style=\"display: none\">\n<p>One way to think about this problem is as a graph. Imagine each vertex of the graph corresponds to a different arrangement of the pool balls, and we add an edge connecting two different arrangements if it&#8217;s possible to transition from one to the other in one move (either with a swap or a rotation). Then, the problem becomes: &#8220;which vertex in the graph is farthest in path-length from the target vertex?&#8221;. When two vertices are connected by an edge, we&#8217;ll call them &#8220;neighbors&#8221;. Note that the edges in this graph are bi-directional because all the moves are reversible.<\/p>\n<p>Our general approach will be to label each vertex with a whole number that corresponds to the smallest number of moves required to reach the target vertex. In effect, we are computing what is called the <a href=\"http:\/\/mathworld.wolfram.com\/GraphEccentricity.html\">graph eccentricity<\/a> of the target vertex. <\/p>\n<p>Here is a greedy approach that achieves the desired labeling.<\/p>\n<ol>\n<li> Create the graph by enumerating all possible unique arrangements of pool balls and adding an edge between each pair that are one move apart.\n<li> Label the target vertex &#8220;0&#8221;. Then set $i=0$ and:\n<li> For each vertex with label $i$, visit all of its neighbors. For each of those neighbors that is currently unlabeled, assign it the label $i+1$.\n<li> Set $i\\mapsto i+1$ and repeat from Step 3 until all vertices in the graph have been labeled.\n<\/ol>\n<p>Now that all vertices have been labeled, the vertices with maximal label correspond to those that require the most moves to be reached. In order to reconstruct the path back to the target vertex, we go in reverse order:<\/p>\n<ol>\n<li> Call the maximum label $d$. Then pick any vertex with label $d$. Call this the &#8220;current vertex&#8221;. Set $i=d$ and repeat:\n<li> Enumerate the neighbors of the current vertex and pick any of them that has label $i-1$.\n<li> Add the newly chosen vertex to the path and set it to be the new current vertex.\n<li> Set $i\\mapsto i-1$ and repeat from Step 2 until we reach $i=0$.\n<\/ol>\n<p>And it&#8217;s as simple as that! Note that we <em>must<\/em> do the second sequence in reverse. If we try to start from the target and work our way towards one of maximum-label vertices, there is no guarantee that we&#8217;ll be able to pick a valid max-length path because some paths will end in dead ends (they will not be max-length). When we work in reverse, however, all paths with decreasing labels must find their way back to the target because of how the labeling was constructed.<\/p>\n<p>The approach I described above is a variant of <href url=\"https:\/\/en.wikipedia.org\/wiki\/dynamic_programming\">, which is a popular method for solving recursively defined problems.<\/p>\n<h3>Numerical solution<\/h3>\n<p>I coded up the procedure above and here is what I found:<\/p>\n<ul>\n<li> The graph has 51480 vertices 1673106 edges (yikes!).\n<li> Each vertex has 65 neighbors. This is because from any position, there are 49 possible ways of swapping a stripe and a solid, 14 ways of swapping the eight-ball with one of the other balls, and 2 possible rotations.\n<li>Once we finish labeling all the vertices, the largest label is $6$. This means it will take at most six moves to get to the target arrangement.\n<li>Here is the distribution of the labels on the vertices<br \/>\n<table>\n<tr>\n<td width=\"20%\">distance to target<\/td>\n<td>0<\/td>\n<td>1<\/td>\n<td>2<\/td>\n<td>3<\/td>\n<td>4<\/td>\n<td>5<\/td>\n<td>6<\/td>\n<\/tr>\n<tr>\n<td>number of vertices<\/td>\n<td>1<\/td>\n<td>65<\/td>\n<td>1253<\/td>\n<td>9653<\/td>\n<td>27422<\/td>\n<td>12946<\/td>\n<td>140<\/td>\n<\/tr>\n<\/table>\n<\/ul>\n<p>So in fact, there are 140 different vertices that are max-length away from the target vertex. We can convert the frequencies listed above into probabilities by dividing by 51480, the total number of vertices. Then, this becomes a probability distribution. Computing its expected value, we obtain $\\frac{51697}{12870}\\approx 4.017$. So it takes about 4 moves on average to reach the target arrangement assuming we start from a position (vertex) that is chosen uniformly at random. Here is a plot of the distribution:<\/p>\n<p><a href=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2018\/09\/pool_hall_distr.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2018\/09\/pool_hall_distr.png\" alt=\"\" width=\"600\" height=\"400\" class=\"aligncenter size-full wp-image-2453\" srcset=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2018\/09\/pool_hall_distr.png 600w, https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2018\/09\/pool_hall_distr-300x200.png 300w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/a><\/p>\n<p>And finally, here is a plot of a possible sequence of moves that takes you to the target vertex from one of the 140 worst-case starting vertices.<\/p>\n<p><a href=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2018\/09\/pool_hall_soln1-e1538322223270.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2018\/09\/pool_hall_soln1-e1538322223270.png\" alt=\"\" width=\"690\" height=\"697\" class=\"aligncenter size-full wp-image-2465\" srcset=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2018\/09\/pool_hall_soln1-e1538322223270.png 690w, https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2018\/09\/pool_hall_soln1-e1538322223270-297x300.png 297w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/a><\/p>\n<h3>Extra credit<\/h3>\n<p>If we look for the &#8220;longest shortest path&#8221; between <em>any two vertices<\/em> in the graph, then we are computing what is known as the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Distance_(graph_theory)\">diameter of the graph<\/a>. This is much more difficult to compute than the eccentricity of a particular vertex, because it involves computing the eccentricities of all vertices! Crunching the numbers took a little over an hour, and the answer is&#8230; 8. Below I show an example of a worst-case path of length 8.<\/p>\n<p><a href=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2018\/09\/pool_hall_soln2b.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2018\/09\/pool_hall_soln2b.png\" alt=\"\" width=\"692\" height=\"695\" class=\"aligncenter size-full wp-image-2467\" srcset=\"https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2018\/09\/pool_hall_soln2b.png 692w, https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2018\/09\/pool_hall_soln2b-150x150.png 150w, https:\/\/laurentlessard.com\/bookproofs\/wp-content\/uploads\/2018\/09\/pool_hall_soln2b-300x300.png 300w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/a><\/p>\n<p>In hindsight, it&#8217;s clear that the diameter can&#8217;t be any larger than 8. To see why, it suffices to show that we can get from any position to any other position in 8 moves or fewer. Here is how you do it. First, swap the eight-ball to move it to its correct location. This takes at most one move. Now, consider the remaining seven solids and seven stripes. In the worst case, all of them are out of place. So it will take at most seven additional swaps to fix it, which makes 8 total moves.<\/p>\n<p>Note that in this argument, I didn&#8217;t make use of any rotations! This implies that we can always get from one configuration to another in 8 moves without using rotations. So if the shortest path between two configurations has length 8, then there also exists a shortest path that doesn&#8217;t use rotations. In the 8-length solution above, I chose the one that doesn&#8217;t have any rotations. Rotations are important in other cases, however. For example, in the original problem of achieving the eight-ball configuration, if we disallow rotations, the solution will have length 8 rather than 6.\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>This Riddler puzzle is about arranging pool balls using a robot! You own a start-up, RoboRackers\u2122, that makes robots that can rack pool balls. To operate the robot, you give it a template, such as the one shown below. (The template only recognizes the differences among stripes, solids and the eight ball. None of the &hellip; <a href=\"https:\/\/laurentlessard.com\/bookproofs\/pool-hall-robots\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Pool hall robots&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":2465,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[7],"tags":[16,25,2],"class_list":["post-2452","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-riddler","tag-dynamic-programming","tag-graph-theory","tag-riddler"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/posts\/2452","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/comments?post=2452"}],"version-history":[{"count":9,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/posts\/2452\/revisions"}],"predecessor-version":[{"id":2468,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/posts\/2452\/revisions\/2468"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/media\/2465"}],"wp:attachment":[{"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/media?parent=2452"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/categories?post=2452"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/laurentlessard.com\/bookproofs\/wp-json\/wp\/v2\/tags?post=2452"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}