# Random walk on the edges of a cube

An ant is placed in a corner of a cube and cannot move. A spider starts from the opposite corner, and can move along the cube's edges in any direction $(x,y,z)$ with equal probability $1/3$. On average, how many steps will the spider need to get to the ant?

(This is not homework, it was an interview question.)

• Homework? What have you tried so far? – Adrian Feb 26 '15 at 8:41
• Regarding Markov chains, here is a great intro setosa.io/blog/2014/07/26/markov-chains – D L Dahly Feb 26 '15 at 13:47
• Normally this sort of routine bookwork should be marked with the self-study tag and follow the guidelines at its tag wiki. Please edit this question and include it on future similar questions – Glen_b Feb 26 '15 at 21:38
• @GarethMcCaughan - No it was an interview question. – Elizabeth Susan Joseph Feb 27 '15 at 3:19
• Following @alesc I made a JavaScript Plunker. plnkr.co/edit/jYQVDI – abbaf33f Feb 27 '15 at 15:24

## 8 Answers

I suggest modeling the problem as a Markov chain where each state represents the distance between the spider and the ant. In this case we have 4 possible states $S_i$ as the distances $i$ can be $\{0,1,2,3\}$.

When the spider is at the opposite corner of the cube, it is at a distance of 3 steps from the ant. It is in state $S_3$.

Building the transition matrix $\mathbf{P}$.

• If we draw a cube we see that when we are at state $S_3$, every movement reduces the distance between the spider and the ant to 2 steps. So, when we are at state $S_3$ we move to state $S_2$ with probability 1.

• When we are at state $S_2$, we can go back to state $S_3$ using the edge we arrived from there or we can decrease the distance to only one step if we choose two other edges. So, when we are at state $S_2$ we can move to state $S_1$ with probability 2/3 and to state $S_3$ with probability 1/3.

• When we are at state $S_1$, we can go to state $S_0$ using one of the three possible edges. If we use the other two, we go back to state $S_2$. So, when we are at state $S_1$ we can move to state $S_0$ with probability 1/3 and to state $S_2$ with probability 2/3.

• When we get to state $S_0$, we stay there since it is our goal. $S_0$ is an absorbing state.

$$\mathbf{P} = \left[\begin{array}{cccc} P_{S_3 \to S_3} & P_{S_3 \to S_2}& P_{S_3 \to S_1} & P_{S_3 \to S_0} \\ P_{S_2 \to S_3} & P_{S_2 \to S_2}& P_{S_2 \to S_1} & P_{S_2 \to S_0} \\ P_{S_1 \to S_3} & P_{S_1 \to S_2}& P_{S_1 \to S_1} & P_{S_1 \to S_0} \\ P_{S_0 \to S_3} & P_{S_0 \to S_2} & P_{S_0 \to S_1}& P_{S_0 \to S_0} \\ \end{array}\right] = \left[\begin{array}{cccc} 0 & 1 & 0 & 0 \\ 1/3 & 0 & 2/3 & 0 \\ 0 & 2/3 & 0 & 1/3 \\ 0 & 0 & 0 & 1 \\ \end{array}\right]$$

This is an absorbing Markov chain with three transient states ($S_3$, $S_2$, $S_1$) and one absorbing state ($S_0$).

According to the theory, the transition matrix of a Markov chain with $t$ transient states and $r$ absorbing states can be rewritten as: $$\mathbf{P} = \left[\begin{array}{cc} \mathbf{Q}_t &\mathbf{R} \\ \mathbf{0}_{r \times t} & \mathbf{I}_r \\ \end{array}\right]$$

where $\mathbf{Q}_t$ is a $t \times t$ matrix that shows the probability of transitioning from some transient state to another transient state, while $\mathbf{R}$ is a $t \times r$ matrix with the probabilities of transitioning from one of the $t$ transient states to one of the $r$ absorbing states. The identity matrix $\mathbf{I}_r$ shows us that when any of the $r$ absorbing state is reached, there is no transition away from that state. The all zeros matrix $\mathbf{0}_{r \times t}$ can be interpreted as that there is no transition from any of the $r$ absorbing states to any of the $t$ transient states.

The $(i,j)$ entry of $\mathbf{Q}_t$ represents probability of transitioning from a state $i$ to a state $j$ in exactly one step. To get the probability for $k$ steps we need the $(i,j)$ entry of $\mathbf{Q}_t^k$. Summing for all $k$, we get a matrix that contains in its $(i,j)$ entry the expected number of visits to transient state $j$ after starting from transient state $i$.

$$\sum_{k=0}^{\infty} \mathbf{Q}_t^k = (\mathbf{I}_t - \mathbf{Q}_t)^{-1}$$

To get the number of steps until being absorbed, just sum the values of each row of $(\mathbf{I}_t - \mathbf{Q}_t)^{-1}$. This can be represented by

$$\mathbf{t} = (\mathbf{I}_t - \mathbf{Q}_t)^{-1} \mathbf{1}$$

where $\mathbf{1}$ is a column vector with all components equal to 1.

Let us apply this to our case:

As stated above, in our case we have $t$=3 transient states and $r$=1 absorbing state, therefore: $$\mathbf{Q}_t = \left[\begin{array}{ccc} 0 & 1 & 0 \\ 1/3 & 0 & 2/3\\ 0 & 2/3 & 0 \\ \end{array}\right] \quad \quad \mathbf{R} = \left[\begin{array}{c} 0 \\ 0\\ 1/3 \\ \end{array}\right]$$

The matrix with the expected number of visits is $$(\mathbf{I}_t - \mathbf{Q}_t)^{-1} = \left[\begin{array}{ccc} 2.5 & 4.5 & 3 \\ 1.5 & 4.5 & 3\\ 1 & 3 & 3 \\ \end{array}\right]$$

This matrix can be interpreted as follows. Starting from state $S_3$ and before getting absorbed at $S_0$ we visit, on average, $S_3$ 2.5 times, $S_2$ 4.5 times, and $S_1$ 3 times.

The expected number of steps from state $S_3$ to state $S_0$ is given by the first component of the following vector:

$$\mathbf{t} = \left[\begin{array}{ccc} 2.5 & 4.5 & 3 \\ 1.5 & 4.5 & 3\\ 1 & 3 & 3 \\ \end{array}\right] \left[\begin{array}{c} 1 \\ 1\\ 1\\ \end{array}\right] = \left[\begin{array}{c} 10 \\ 9\\ 7\\ \end{array}\right].$$

The second and third components of $\mathbf{t}$ are the expected number of steps to $S_0$ if we start from $S_2$ and $S_1$ respectively.

• I have no idea what mcmc is . I have to read it and then check your solution. Is there any good mcmc explanation that compliments your solution? – Elizabeth Susan Joseph Feb 26 '15 at 13:30
• @ElizabethSusanJoseph Note that Markov chains and MCMC (Markov chain Monte Carlo) are two distinct concepts (although MCMC is based on Markov chains). This answer does not use MCMC for anything. So you are probably searching for a good explanation about Markov chains, not about MCMC. – Juho Kokkala Feb 26 '15 at 14:05
• tiagotvv your explanation would be improved by defining and explaining the use of the transition matrix P, the meaning of the quantity r, and the height of the column vector. Bonus points for meaning of subsequent elements of the vector t. :) – Alexis Feb 26 '15 at 18:25
• @JuhoKokkala - thanks I will then look at the markov chain explanations. – Elizabeth Susan Joseph Feb 27 '15 at 3:20
• @Alexis I added some explanation regarding the matrices and vectors. – tiagotvv Feb 27 '15 at 14:05

Let $$x^*$$ be the number of expected steps. Let $$x_1$$ be the number of expected steps from any corner adjacent to the origin of the spider and $$x_0$$ ditto for the ant.

Then $$x^* = 1 + x_1$$ and $$x_0 = 1 + \frac{2}{3}x_1$$. Since $$x_1 = 1 + \frac{2}{3}x_0 + \frac{1}{3}x^*= 1 + \frac{2}{3}x_0 + \frac{1}{3} + \frac{1}{3}x_1$$

we get that $$x_1 = x_0 + 2$$. So $$x_0 = 1 + \frac{2}{3}x_0 + \frac{4}{3}$$ implying that $$x_0=7$$ and $$x_1=9$$.

We get our answer as $$x^*=10$$.

### Edit:

If we draw the cube with coordinates $$(x, y, z)$$ then $$111$$ is the starting position of the spider and $$000$$ the position of the ant.

The spider can move to either $$011$$, $$101$$ or $$110$$.

By the symmetry of the cube these must have the same number of expected steps to the ant, denoted by $$x_1$$. From $$x_1$$, we can either return to the origin (with probability $$1/3$$) or (with probability $$2/3$$) we can go to one of the points $$001$$, $$100$$, $$010$$ depending on which state we are in.

Again, by symmetry, these points will have the same number of expected steps which we call $$x_0$$. From these positions we can reach the goal in one step with probability $$1/3$$ or go back to one of the $$x_1$$-positions with probability $$2/3$$. This means that $$x_0 = \frac{1}{3}1 + \frac{2}{3}(1 + x_1) = 1 + \frac{2}{3}x_1$$.

• Could you further elaborate your answer? Please explain in layman terms :) – Elizabeth Susan Joseph Feb 27 '15 at 12:49

One nice abstraction to think of it is this:

Think of the Position of the Ant as $(0,0,0)$ and Spider $(1,1,1)$, now each move the spider can make will essentially switch exactly one of the three components from $1\to0$ or $0\to1$. So the question becomes:

If I randomly switch bits in (1,1,1) after how many steps in average do I get 0,0,0


We see the shortest way is 3 switches. Since it doesn't matter with which bit I start the probability of that happening is 1 * 2/3 * 1/3 = 2/9. If we make 1 mistake (switch one bit back to 1) we will need 5 steps. And the chances of making a mistake are 7/9 - if we want to make only one mistake, we have to get from there back and do everything right again - so the chance of making exactly 1 mistake resulting in 5 steps is 7/9 * 2/9 and the chance of making 2 mistakes aka 7 steps is (7/9)² * 2/9 and so on.

So the formula for the expected average number of steps is:

$$\mathbb E(\mathrm{steps}) = \sum_{n=0}^{\infty} (3 + 2n) \cdot \frac{2}{9} \cdot \left ( \frac{7}{9} \right ) ^{n} = 10$$

• Your solution is some what confusing. What is this formula? what is n here? – Elizabeth Susan Joseph Feb 27 '15 at 12:50
• It is actually the shortest and cleanest solution. The solution is in the form of an infinite sum of numbers from zero to infinity and n is the current integer in that infinite sum. – alesc Feb 27 '15 at 14:11
• This is really nice! My answer is similar, but breaks up the sequence of switches into pairs - which lets me expectate a geometric variable (or alternatively, sum a geometric series) rather than sum an arithmetico-geometric series. That's the only substantive difference: it doesn't matter much whether one takes "first three switches, then subsequent pairs" (as you did) or "first switch, then subsequent pairs" (as I did), since unless the fly is caught in 3 switches, then either way you're dealing with one odd and two even parities. – Silverfish Feb 28 '15 at 0:07

Just to compliment tiagotvv's answer:

I don't naturally think of these kinds of problems as matrices (even though they are). I have to draw it out, which I've done below. You can see that there are 3 places to move from S, all of which are As. From any A, you can either return to the S, or move to one of two Bs. From any B, you can move to the E, or to one of two As. This all translates to the transition matrix given by tiagotvv, which can also be drawn in graph form.

Because I am terrible at math, I would just try to simulate your problem. You can do this with the markovchain package in R.

  library(markovchain)
library(ggplot2)

# Create a markovchain object, given the states and their transition matrix

mcCube <- new("markovchain",
states = c("S", "A", "B", "E"),
transitionMatrix = matrix(data = c(0,   1,   0,   0,
1/3, 0,   2/3, 0,
0,   2/3, 0,   1/3,
0,   0,   0,   1),
byrow = T, nrow = 4),
name = "cube")

# The following code calcuates the probability of landing on E after taking
# between 1 and 100 steps from the start, given the above set of transition
# probabilities.

start <- c(1, 0, 0, 0)

list <- list()

for (i in 1:100){

list[[i]] <- (start * mcCube^i)[4]

}

a <- do.call(rbind, list)

data <- data.frame(propE = a,
steps = c(1:100))

ggplot(data, aes(x = steps, y = propE)) +
geom_line(size = 1) +
ylab("Probability you reached the spider") +
xlab("Number of steps taken") +
theme_bw() +
theme(panel.grid.minor = element_blank())


  # This code simulates 1000 different applications of the markov chain where you
# take 1000 steps, and records the step at which you landed on E

list <- list()
for (i in 1:1000) {

b <- rmarkovchain(n = 1000, object = mcCube, t0 = "S", include.t0 = T)

list[[i]] <- 1001 - length(b[b == "E"])

}

data <- as.data.frame(do.call(rbind, list))

ggplot(data, aes(x = V1)) +
geom_density(fill = "grey50", alpha = 0.5) +
geom_vline(aes(xintercept = mean(V1))) +
ylab("Density") +
xlab("Number of steps to reach E") +
theme_bw() +
theme(panel.grid.minor = element_blank())

mean(data$V1) # ~10 is the average number of steps to reach E in this set of # simulations  tiagotvv's answer can be calcuated in R as: q = matrix(c(0, 1, 0, 1/3, 0, 2/3, 0, 2/3, 0), byrow = T, nrow = 3) (solve(diag(3) - q) %*% c(1, 1, 1))[1] # = 10  Parity considerations give a very clean solution, using surprisingly simple machinery: no Markov chains, no iterated expectations, and only high school level summations. The basic idea is that if the spider has moved an even number of times in the$x$direction, it has returned to its original$x$coordinate so can't be at the ant's position. If it has moved an odd number of times in the$x$direction, then its$x$coordinate matches the ant's. Only if it has moved an odd number of times in all three directions will it match the$x$,$y$and$z$coordinates of the ant. Initially the spider has made zero moves in any of the three directions, so the parity for each direction is even. All three parities need to be flipped to reach the ant. After the spider's first move (let's label that direction$x$), exactly one direction has odd parity and the other two ($y$and$z$) are even. To catch the ant, only those two parities need to be reversed. Since that can't be achieved in an odd number of subsequent moves, from now on we consider pairs of moves. There are nine possible combinations for the first paired move: $$(x,x), \,(x,y), \,(x,z), \,(y,x), \,(y,y), \,(y,z), \,(z,x), \,(z,y), \text{or} \,(z,z)$$ We need to move in the$y$and$z$directions to reach the ant after one paired move, and two out of nine combinations will achieve this:$(y,z)$and$(z,y)$would ensure all three parities are odd. The other seven combinations leave one odd and two even parities. The three repeated moves,$(x,x)$,$(y,y)$or$(z,z)$, leave all parities unchanged so we still require one$y$and one$z$movement to reach the ant. The other pairs contain two distinct moves, including one in the$x$direction. This switches the parity of$x$and one of the other parities (either$y$or$z$) so we are still left with one odd and two even parities. For instance the pair$(x,z)$leaves us needing one more$x$and one more$y$to reach the ant: an equivalent situation (after relabelling of axes) to where we were before. We can then analyse the next paired move in the same way. In general paired moves start with one odd and two even parities, and will either end with three odd parities (with probability$\frac{2}{9}$) and the immediate capture of the ant, or with one odd and two even parities (with probability$\frac{7}{9}$) which returns us to the same situation. Let$M$be the number of paired moves required to reach the ant. Clearly$M$follows the geometric distribution on the support$\{1, 2, 3, \dots\}$with probability of success$p = \frac{2}{9}$so has mean$\mathbb{E}(M) = p^{-1} = \frac{9}{2} = 4.5$. Let$N$be the total number of moves required, including the initial move and the$M$subsequent paired moves. Then$N = 2M + 1$so, applying linearity of expectations,$\mathbb{E}(N) = 2\mathbb{E}(M) + 1 = 2 \times 4.5 + 1 = 10$. Alternatively you might note$P(M \geq m) = (\frac{7}{9})^{m-1}$and apply the well-known formula for the mean of a discrete distribution taking only non-negative integer values,$\mathbb{E}(M)=\sum_{m=1}^\infty P(M\geq m)$. This gives$\mathbb{E}(M)=\sum_{m=1}^\infty (\frac{7}{9})^{m-1}$which is a geometric series with first term$a=1$and common ratio$r=\frac{7}{9}$so has sum$\frac{a}{1-r} = \frac{1}{1-7/9}=\frac{1}{2/9}=\frac{9}{2}$. We can then take$\mathbb{E}(N)$as before. Comparison to Markov chain solutions How might I have spotted this from the Markov chain transition matrix? Using @DLDahly's notation, the states in the transition matrix correspond to my description of the number of the number of directions with odd parity. The one-step transition matrix is $$\mathbf{P} = \left[\begin{array}{cccc} P_{S \to S} & P_{S \to A}& P_{S \to B} & P_{S \to E} \\ P_{A \to S} & P_{A \to A}& P_{A \to B} & P_{A \to E} \\ P_{B \to S} & P_{B \to A}& P_{B \to B} & P_{B \to E} \\ P_{E \to S} & P_{E \to A} & P_{E \to B}& P_{E \to E} \\ \end{array}\right] = \left[\begin{array}{cccc} 0 & 1 & 0 & 0 \\ 1/3 & 0 & 2/3 & 0 \\ 0 & 2/3 & 0 & 1/3 \\ 0 & 0 & 0 & 1 \\ \end{array}\right]$$ The first row show us that after one movement, the spider is guaranteed to be in state A (one odd and two even parities). The two-step transition matrix is: $$\mathbf{P}^{(2)} = \mathbf{P}^{2} = \left[\begin{array}{cccc} 1/3 & 0 & 2/3 & 0 \\ 0 & 7/9 & 0 & 2/9 \\ 2/9 & 0 & 4/9 & 1/3 \\ 0 & 0 & 0 & 1 \\ \end{array}\right]$$ The second row shows us that once the spider has entered state A, in two moves time it has either returned to state A with probability$7/9$or has reached state E (all odd parities) and captured the ant, with probabilty$2/9$. So having reached state A, we see from the two-step transition matrix that the number of two-step moves required can be analysed using the geometric distribution as above. This isn't how I found my solution, but it is sometimes worth calculating the first few powers of the transition matrix to see if a useful pattern like this can be exploited. I have occasionally found this to give simpler solutions than having to invert a matrix or perform an eigendecomposition by hand - admittedly something that is only really relevant in an exam or interview situation. I have written a short Java program to answer your question numerically. The traversing of the spider is truly random, meaning that it can also traverse in cycles before getting to the ant. However, you did not defined the term "opposite corner", so I have two different scenarios. Opposite as in across the same plane or as across the cube. In the first scenario, the shortest path is 2 steps, and 3 steps in the second scenario. I hava used 100 million repeats and the results are the following: -- First scenario -- Steps sum: 900019866 Repeats: 100000000 Avg. step count: 9.00019866 -- Second scenario -- Steps sum: 1000000836 Repeats: 100000000 Avg. step count: 10.00000836  Source code: import java.util.Random; import java.util.concurrent.atomic.AtomicLong; import java.util.stream.IntStream; public class ProbabilityQuizSpider { // Edges of the cube private static final int[][] EDGES = new int[][] { {1, 3, 7}, // corner 0 {0, 2, 4}, // corner 1 {1, 3, 5}, // corner 2 {0, 2, 6}, // corner 3 {1, 5, 7}, // corner 4 {2, 4, 6}, // corner 5 {3, 5, 7}, // corner 6 {0, 4, 6} // corner 7 }; private static final int START = 0; // Spider private static final int FINISH = 5; // Ant private static final int REPEATS = (int) Math.pow(10, 8); public static void main(String[] args) { final Random r = new Random(); final AtomicLong stepsSum = new AtomicLong(); IntStream.range(0, REPEATS).parallel().forEach(i -> { int currentPoint = START; int steps = 0; do { // Randomly traverse to next point currentPoint = EDGES[currentPoint][r.nextInt(3)]; // Increase number of steps steps++; } while(currentPoint != FINISH); stepsSum.addAndGet(steps); }); // Results System.out.println("Steps sum: " + stepsSum.get()); System.out.println("Repeats: " + REPEATS); System.out.println("Avg. step count: " + (((double) stepsSum.get()) / ((double) REPEATS))); } }  EDIT: fixed a typo in the script (and also updated the results) • I think your edges are wrong. Corner 3 has 7 in its list, but corner 7 doesn't have 3 in its list. (I suggest that the Right Way to map the vertices to the numbers 0..7 is to say that each bit position corresponds to one axis, so that traversing an edge equals XOR with 1, 2, or 4.) – Gareth McCaughan Feb 26 '15 at 14:58 • Thank you for comment. I have made a typo when defining corner #3, it should be {0, 2, 6}. I have re-run the program and got the following result: 10.00000836 steps for traversing from corner #0 to corner #5 (body diagonal of the cube). This is also consistent with @Hunaphu. – alesc Feb 26 '15 at 15:06 • Yup, much better. – Gareth McCaughan Feb 26 '15 at 15:11 I solved your conundrum via Monte Carlo simulations ($n = 10^4$) and obtained$\mathtt{mean(steps)} \approx 10$. Here is the R code I used: ant = c(0,0,0) # ant's coordinates sim = 1e4 # number of MC simulations steps = numeric() # initialize array of steps for (i in 1:sim) { spider = c(1,1,1) # spider's coordinates count = 0 # initialize step counter # while ant's coordinates == spider's coordinates while (!isTRUE(all.equal(ant, spider))) { # random walk in one of three dimensions xyz = trunc(runif(1,1,4)) # let the spider move if (spider[xyz] == 1) { spider[xyz] = 0 } else if (spider[xyz] == 0) { spider[xyz] = 1 } # add one step count = count + 1 } # add the number of step occurred in the ith iteration steps = c(steps, count) # print i and number of steps occurred cat("\n", i, " ::: ", count) } # print the mean of steps (mean(steps))  • The code is nice and clear--but it's asking rather much of your users to watch a million lines printed out over the course of a half hour! And how do you know the correct answer isn't, say,$10.000001\$? :-) FWIW, you can exploit some native R functions to speed this up to under a second: n.sim <- 1e6; x <- matrix(runif(n.sim*3), ncol=3); moves <- x >= pmax(x[, 1], x[, 2], x[, 3]); positions <- apply(moves, 2, cumsum) %% 2; types <- rowSums(positions); vertices <- types[types==0 | types==3]; transitions <- cumsum(diff(vertices) != 0); n.sim / transitions[length(transitions)] – whuber Feb 27 '15 at 17:25

I believe alesc is on the right track when mentioning "However, you did not defined the term "opposite corner" Unless I am missing something in the question, there is no correct answer, just answers based on assumptions. The cube size is not defined I.E. 10 cubic ft, 1000 cubic ft etc. Ant size is not defined I.E. small garden, carpenter, giant red etc, Spider type is not defined (to determine step size) I.E small garden, Tarantula etc. IF you combine all "not defined" variables. the answer could be 0 steps or an undetermined/infinite number of steps.

• This answer would not get one to the next level of interviewing unless it were perhaps for a gardening position. – whuber Feb 28 '15 at 0:41
• In this case it's clear enough that 'step' means 'a move from one node (corner) to an adjacent node', and it's quite clear what "opposite corner" of a cube means -- take a unit cube for example -- if the ant is at corner (x,y,z) on a unit cube, the spider is at (1-x,1-y,1-z) (so if the ant's at the origin, the spider's at (1,1,1)). As such, none of your concerns seem to substantively relate to the question being asked. [Note to voters: While I don't think this is a good answer without a substantive edit, I don't think this should be the subject of a deletion vote -- up and down votes suffice] – Glen_b Feb 28 '15 at 1:49
• @Glen_b Since it seems to be seeking clarity on the particulars of the question, I thought this was probably intended as a comment rather than a substantive answer. – Silverfish Feb 28 '15 at 3:03
• @Silverfish You may be correct, but then it would close as 'not an answer'. I read it instead as an attempt to say "this question is not answerable", which I'd normally regard as an answer when supported with reasoning, but I think the reasons are simply based on misunderstanding the question. – Glen_b Feb 28 '15 at 5:23