Skip to content

Instantly share code, notes, and snippets.

@axiomsofchoice
Last active May 27, 2016 16:22
Show Gist options
  • Save axiomsofchoice/21bfddcf22df1daf4296fc7f914ea3cd to your computer and use it in GitHub Desktop.
Save axiomsofchoice/21bfddcf22df1daf4296fc7f914ea3cd to your computer and use it in GitHub Desktop.
Solution to Random Walk Problem #1

Solution to Random Walk Problem #1

By Dan Hagon. CC0 License.

The problem was described by @solvemymaths:

https://twitter.com/solvemymaths/status/733780754040299520

This solution is based on the one given by Colin Beveridge:

https://twitter.com/icecolbeveridge/status/734134443338792961

Update: Turns out I've completely misinterpreted the diagram in the question. The requirement was to ensure the dot returns back to the start, whereas I've allowed it the possibility to continue on from there: https://twitter.com/icecolbeveridge/status/736194532937732096 Upon further clarification it turns out that the finishing position does not matter: https://twitter.com/solvemymaths/status/736212010363453441

The key idea to this solution is "divide and conquer" by decomposing the problem up in to smaller pieces that are easier to solve. First notice that the overall probability is given by the product of the probabilities:

A: The moves we make stay on the square. B: Given that we stay on the square, the trajectory we make actually describes the square and just 1 ,2 or 3 sides of it.

To stay on the square every move we make needs to be the right move in the sense that is keeps us on the square. At every point on the square we have 4 different directions we can choose from but only 2 of these will ensure we stay on the square. We need to do this 8 times in a row and so the probability A above is (2/4)^8 = (1/2)^8 = 1/256.

The key idea for probability B above is to note that one way to describe the shape of the square is if we return to the start position and make at least one complete loop in the process. Since our trajectory can only keep us on the square at each move we have two alternatives which means there are only 2^8 possible trajectories. However, by symmetry we can ignore half these possibilities since for every "clockwise" solution there will be a mirror image "anti-clockwise" solution. (This just means we're dividing the numerator and denominaotr in probability B by two and the probability will be just the same.)

To decompose the problem further we first consider each of the cases where the dot first returns to the start position. Clearly it can do this no quicker than 4 moves and no slower than 8 moves but since any "back-track" (I prefer the term "wiggle") move will always consume 2 available moves for zero progress we'll only ever get back to the start after an even number of moves so the final case to consider is getting back in 6 moves.

4: Here there is only one way to get back to the start but once we are there any further moves are not going to affect the outcome and there are 2^4 possibilities for them.

6: To make it in 6 moves means that we must have made a single wiggle along the way and we can do this in 4 places, including as the first thing we do along our trajectory. (Note that if we make the wiggle right at end, i.e. when we get back to the start, we will have actually got to the start again in 4 moves and would be double counting from the above case.) When we get to the start we still have 2 moves to spend and we can do this in 2^2 possible ways.

8: For 8 moves we must have made 2 wiggles along the way and there are 5 places these could have occurred, including the possibility that one or two of them were made right at the beginning of our trajectory. Hence there are 5 choose 2 possible trajectories.

However there are other ways to describe the square (Thanks @pozorvlak https://twitter.com/pozorvlak/status/736141601525923844). Instead of making a complete loop the shape could also be described by the overlap of two incomplete loops. We can distinguish 3 different cases depending on the maximum number of distinct points, passed through in a clockwise direction, of the first of these incomplete loops before the trajectory returns back past the starting point to start describing the second of the incomplete loops and up to the point where second incomplete loop only just meets back with the first incomplete loop again. To make things easier we first consider cases where there are no wiggles initially (i.e. we take the most direct route out and back) and then add them in afterwards to make up the missing possibilities. Clearly then the first incomplete loop can make excursions out to 1, 2 or 3 points before returning back; starting with the easiest case:

3: The 6 moves of the first incomplete loop must have described 3 sides of the square, out and back, and the 7th move gives us the 4th side of the square; here there is no choice which direction to take. For the 8th move we have two possibilities to choose from since our square is already complete (one of which actually creates a wiggle). There are no additional wiggles to consider since we've used up all our move.

2: The 4 moves of the first incomplete loop must have described 2 sides of the square, out and back, and the 5th and 6th moves gives us the 3rd and 4th side of the square. We are left with a balance of 2 moves to spend which we could spend either continuing forward two moves or add a wiggle at any one of the 6 edges we've already generated.

1: The 2 moves of the first incomplete loop must have described 1 sides of the square, out and back, and the 3rd, 4th and 5th moves gives us the 2nd, 3rd and 4th side of the square. We are left with a balance of 3 moves to spend which we could spend either continuing forward three moves or add a wiggle at any one of the 5 edges we've already generated plus at one position within the remaining three moves, but for each of these we have two possible ways to spend the final remaining move.

Therefore the total number of possible correct trajectories is 2^4 + (4 * (2^2)) + 5C2 + 2 + 7 + (6 * 2) = 16 + (4 * 4) + 10 + 9 + 12 = 63. Hence probability B is 63/128.

Finally then the overall probability is, by the product rule, (1/256) * (63/128) = 63/(2^15).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment